3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
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.
16 #include <drm/drm_crtc_helper.h>
18 #include "exynos_drm_crtc.h"
19 #include "exynos_drm_drv.h"
20 #include "exynos_drm_encoder.h"
21 #include "exynos_drm_plane.h"
23 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
25 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
27 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
29 if (exynos_crtc->dpms == mode) {
30 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
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 (exynos_crtc->event == NULL), HZ/20))
38 exynos_crtc->event = NULL;
39 drm_crtc_vblank_off(crtc);
42 if (exynos_crtc->ops->dpms)
43 exynos_crtc->ops->dpms(exynos_crtc, mode);
45 exynos_crtc->dpms = mode;
47 if (mode == DRM_MODE_DPMS_ON)
48 drm_crtc_vblank_on(crtc);
51 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
53 /* drm framework doesn't check NULL. */
56 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
58 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
59 struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
61 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
63 if (exynos_crtc->ops->win_commit)
64 exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
66 if (exynos_crtc->ops->commit)
67 exynos_crtc->ops->commit(exynos_crtc);
71 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
72 const struct drm_display_mode *mode,
73 struct drm_display_mode *adjusted_mode)
75 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
77 if (exynos_crtc->ops->mode_fixup)
78 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
85 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
86 struct drm_display_mode *adjusted_mode, int x, int y,
87 struct drm_framebuffer *old_fb)
89 struct drm_framebuffer *fb = crtc->primary->fb;
95 * copy the mode data adjusted by mode_fixup() into crtc->mode
96 * so that hardware can be seet to proper mode.
98 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
100 ret = exynos_check_plane(crtc->primary, fb);
104 crtc_w = fb->width - x;
105 crtc_h = fb->height - y;
106 exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
107 crtc_w, crtc_h, x, y, crtc_w, crtc_h);
112 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
113 struct drm_framebuffer *old_fb)
115 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
116 struct drm_framebuffer *fb = crtc->primary->fb;
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");
127 ret = exynos_check_plane(crtc->primary, fb);
131 crtc_w = fb->width - x;
132 crtc_h = fb->height - y;
133 exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
134 crtc_w, crtc_h, x << 16, y << 16,
135 crtc_w << 16, crtc_h << 16);
140 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
142 struct drm_plane *plane;
145 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
147 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
148 if (plane->crtc != crtc)
151 ret = plane->funcs->disable_plane(plane);
153 DRM_ERROR("Failed to disable plane %d\n", ret);
157 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
158 .dpms = exynos_drm_crtc_dpms,
159 .prepare = exynos_drm_crtc_prepare,
160 .commit = exynos_drm_crtc_commit,
161 .mode_fixup = exynos_drm_crtc_mode_fixup,
162 .mode_set = exynos_drm_crtc_mode_set,
163 .mode_set_base = exynos_drm_crtc_mode_set_base,
164 .disable = exynos_drm_crtc_disable,
167 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
168 struct drm_framebuffer *fb,
169 struct drm_pending_vblank_event *event,
170 uint32_t page_flip_flags)
172 struct drm_device *dev = crtc->dev;
173 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
174 unsigned int crtc_w, crtc_h;
177 /* when the page flip is requested, crtc's dpms should be on */
178 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
179 DRM_ERROR("failed page flip request.\n");
186 spin_lock_irq(&dev->event_lock);
187 if (exynos_crtc->event) {
192 ret = exynos_check_plane(crtc->primary, fb);
196 ret = drm_vblank_get(dev, exynos_crtc->pipe);
198 DRM_DEBUG("failed to acquire vblank counter\n");
202 exynos_crtc->event = event;
203 spin_unlock_irq(&dev->event_lock);
206 * the pipe from user always is 0 so we can set pipe number
207 * of current owner to event.
209 event->pipe = exynos_crtc->pipe;
211 crtc->primary->fb = fb;
212 crtc_w = fb->width - crtc->x;
213 crtc_h = fb->height - crtc->y;
214 exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
215 crtc_w, crtc_h, crtc->x << 16, crtc->y << 16,
216 crtc_w << 16, crtc_h << 16);
221 spin_unlock_irq(&dev->event_lock);
225 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
227 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
228 struct exynos_drm_private *private = crtc->dev->dev_private;
230 private->crtc[exynos_crtc->pipe] = NULL;
232 drm_crtc_cleanup(crtc);
236 static struct drm_crtc_funcs exynos_crtc_funcs = {
237 .set_config = drm_crtc_helper_set_config,
238 .page_flip = exynos_drm_crtc_page_flip,
239 .destroy = exynos_drm_crtc_destroy,
242 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
243 struct drm_plane *plane,
245 enum exynos_drm_output_type type,
246 const struct exynos_drm_crtc_ops *ops,
249 struct exynos_drm_crtc *exynos_crtc;
250 struct exynos_drm_private *private = drm_dev->dev_private;
251 struct drm_crtc *crtc;
254 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
256 return ERR_PTR(-ENOMEM);
258 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
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;
266 crtc = &exynos_crtc->base;
268 private->crtc[pipe] = crtc;
270 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
275 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
280 plane->funcs->destroy(plane);
285 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
287 struct exynos_drm_private *private = dev->dev_private;
288 struct exynos_drm_crtc *exynos_crtc =
289 to_exynos_crtc(private->crtc[pipe]);
291 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
294 if (exynos_crtc->ops->enable_vblank)
295 exynos_crtc->ops->enable_vblank(exynos_crtc);
300 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
302 struct exynos_drm_private *private = dev->dev_private;
303 struct exynos_drm_crtc *exynos_crtc =
304 to_exynos_crtc(private->crtc[pipe]);
306 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
309 if (exynos_crtc->ops->disable_vblank)
310 exynos_crtc->ops->disable_vblank(exynos_crtc);
313 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
315 struct exynos_drm_private *dev_priv = dev->dev_private;
316 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
317 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
320 spin_lock_irqsave(&dev->event_lock, flags);
321 if (exynos_crtc->event) {
323 drm_send_vblank_event(dev, -1, exynos_crtc->event);
324 drm_vblank_put(dev, pipe);
325 wake_up(&exynos_crtc->pending_flip_queue);
329 exynos_crtc->event = NULL;
330 spin_unlock_irqrestore(&dev->event_lock, flags);
333 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
335 struct exynos_drm_crtc *exynos_crtc;
336 struct drm_device *dev = fb->dev;
337 struct drm_crtc *crtc;
340 * make sure that overlay data are updated to real hardware
343 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
344 exynos_crtc = to_exynos_crtc(crtc);
347 * wait for vblank interrupt
348 * - this makes sure that overlay data are updated to
351 if (exynos_crtc->ops->wait_for_vblank)
352 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
356 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
357 unsigned int out_type)
359 struct drm_crtc *crtc;
361 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
362 struct exynos_drm_crtc *exynos_crtc;
364 exynos_crtc = to_exynos_crtc(crtc);
365 if (exynos_crtc->type == out_type)
366 return exynos_crtc->pipe;
372 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
374 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
376 if (exynos_crtc->ops->te_handler)
377 exynos_crtc->ops->te_handler(exynos_crtc);