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);
26 struct exynos_drm_manager *manager = exynos_crtc->manager;
28 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
30 if (exynos_crtc->dpms == mode) {
31 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
35 if (mode > DRM_MODE_DPMS_ON) {
36 /* wait for the completion of page flip. */
37 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
38 !atomic_read(&exynos_crtc->pending_flip),
40 atomic_set(&exynos_crtc->pending_flip, 0);
41 drm_crtc_vblank_off(crtc);
44 if (manager->ops->dpms)
45 manager->ops->dpms(manager, mode);
47 exynos_crtc->dpms = mode;
49 if (mode == DRM_MODE_DPMS_ON)
50 drm_crtc_vblank_on(crtc);
53 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
55 /* drm framework doesn't check NULL. */
58 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
60 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
61 struct exynos_drm_manager *manager = exynos_crtc->manager;
62 struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
64 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
66 if (manager->ops->win_commit)
67 manager->ops->win_commit(manager, exynos_plane->zpos);
69 if (manager->ops->commit)
70 manager->ops->commit(manager);
72 exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_ON);
76 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
77 const struct drm_display_mode *mode,
78 struct drm_display_mode *adjusted_mode)
80 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
81 struct exynos_drm_manager *manager = exynos_crtc->manager;
83 if (manager->ops->mode_fixup)
84 return manager->ops->mode_fixup(manager, mode, adjusted_mode);
90 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
91 struct drm_display_mode *adjusted_mode, int x, int y,
92 struct drm_framebuffer *old_fb)
94 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
95 struct exynos_drm_manager *manager = exynos_crtc->manager;
96 struct drm_framebuffer *fb = crtc->primary->fb;
101 * copy the mode data adjusted by mode_fixup() into crtc->mode
102 * so that hardware can be seet to proper mode.
104 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
106 crtc_w = fb->width - x;
107 crtc_h = fb->height - y;
109 if (manager->ops->mode_set)
110 manager->ops->mode_set(manager, &crtc->mode);
112 return exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
113 crtc_w, crtc_h, x, y, crtc_w, crtc_h);
116 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
117 struct drm_framebuffer *old_fb)
119 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
120 struct drm_framebuffer *fb = crtc->primary->fb;
124 /* when framebuffer changing is requested, crtc's dpms should be on */
125 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
126 DRM_ERROR("failed framebuffer changing request.\n");
130 crtc_w = fb->width - x;
131 crtc_h = fb->height - y;
133 return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
134 crtc_w, crtc_h, x, y, crtc_w, crtc_h);
137 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
139 struct drm_plane *plane;
142 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
144 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
145 if (plane->crtc != crtc)
148 ret = plane->funcs->disable_plane(plane);
150 DRM_ERROR("Failed to disable plane %d\n", ret);
154 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
155 .dpms = exynos_drm_crtc_dpms,
156 .prepare = exynos_drm_crtc_prepare,
157 .commit = exynos_drm_crtc_commit,
158 .mode_fixup = exynos_drm_crtc_mode_fixup,
159 .mode_set = exynos_drm_crtc_mode_set,
160 .mode_set_base = exynos_drm_crtc_mode_set_base,
161 .disable = exynos_drm_crtc_disable,
164 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
165 struct drm_framebuffer *fb,
166 struct drm_pending_vblank_event *event,
167 uint32_t page_flip_flags)
169 struct drm_device *dev = crtc->dev;
170 struct exynos_drm_private *dev_priv = dev->dev_private;
171 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
172 struct drm_framebuffer *old_fb = crtc->primary->fb;
173 unsigned int crtc_w, crtc_h;
176 /* when the page flip is requested, crtc's dpms should be on */
177 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
178 DRM_ERROR("failed page flip request.\n");
182 mutex_lock(&dev->struct_mutex);
186 * the pipe from user always is 0 so we can set pipe number
187 * of current owner to event.
189 event->pipe = exynos_crtc->pipe;
191 ret = drm_vblank_get(dev, exynos_crtc->pipe);
193 DRM_DEBUG("failed to acquire vblank counter\n");
198 spin_lock_irq(&dev->event_lock);
199 list_add_tail(&event->base.link,
200 &dev_priv->pageflip_event_list);
201 atomic_set(&exynos_crtc->pending_flip, 1);
202 spin_unlock_irq(&dev->event_lock);
204 crtc->primary->fb = fb;
205 crtc_w = fb->width - crtc->x;
206 crtc_h = fb->height - crtc->y;
207 ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
208 crtc_w, crtc_h, crtc->x, crtc->y,
211 crtc->primary->fb = old_fb;
213 spin_lock_irq(&dev->event_lock);
214 drm_vblank_put(dev, exynos_crtc->pipe);
215 list_del(&event->base.link);
216 atomic_set(&exynos_crtc->pending_flip, 0);
217 spin_unlock_irq(&dev->event_lock);
223 mutex_unlock(&dev->struct_mutex);
227 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
229 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
230 struct exynos_drm_private *private = crtc->dev->dev_private;
232 private->crtc[exynos_crtc->pipe] = NULL;
234 drm_crtc_cleanup(crtc);
238 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
239 struct drm_property *property,
242 struct drm_device *dev = crtc->dev;
243 struct exynos_drm_private *dev_priv = dev->dev_private;
244 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
246 if (property == dev_priv->crtc_mode_property) {
247 enum exynos_crtc_mode mode = val;
249 if (mode == exynos_crtc->mode)
252 exynos_crtc->mode = mode;
255 case CRTC_MODE_NORMAL:
256 exynos_drm_crtc_commit(crtc);
258 case CRTC_MODE_BLANK:
259 exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
271 static struct drm_crtc_funcs exynos_crtc_funcs = {
272 .set_config = drm_crtc_helper_set_config,
273 .page_flip = exynos_drm_crtc_page_flip,
274 .destroy = exynos_drm_crtc_destroy,
275 .set_property = exynos_drm_crtc_set_property,
278 static const struct drm_prop_enum_list mode_names[] = {
279 { CRTC_MODE_NORMAL, "normal" },
280 { CRTC_MODE_BLANK, "blank" },
283 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
285 struct drm_device *dev = crtc->dev;
286 struct exynos_drm_private *dev_priv = dev->dev_private;
287 struct drm_property *prop;
289 prop = dev_priv->crtc_mode_property;
291 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
292 ARRAY_SIZE(mode_names));
296 dev_priv->crtc_mode_property = prop;
299 drm_object_attach_property(&crtc->base, prop, 0);
302 int exynos_drm_crtc_create(struct exynos_drm_manager *manager,
303 struct drm_device *drm_dev, int pipe,
304 enum exynos_drm_output_type type)
306 struct exynos_drm_crtc *exynos_crtc;
307 struct drm_plane *plane;
308 struct exynos_drm_private *private = drm_dev->dev_private;
309 struct drm_crtc *crtc;
312 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
316 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
317 atomic_set(&exynos_crtc->pending_flip, 0);
319 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
320 exynos_crtc->manager = manager;
321 exynos_crtc->pipe = pipe;
322 exynos_crtc->type = type;
323 plane = exynos_plane_init(drm_dev, 1 << pipe,
324 DRM_PLANE_TYPE_PRIMARY);
326 ret = PTR_ERR(plane);
330 manager->crtc = &exynos_crtc->base;
331 crtc = &exynos_crtc->base;
333 private->crtc[pipe] = crtc;
335 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
340 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
342 exynos_drm_crtc_attach_mode_property(crtc);
347 plane->funcs->destroy(plane);
353 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
355 struct exynos_drm_private *private = dev->dev_private;
356 struct exynos_drm_crtc *exynos_crtc =
357 to_exynos_crtc(private->crtc[pipe]);
358 struct exynos_drm_manager *manager = exynos_crtc->manager;
360 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
363 if (manager->ops->enable_vblank)
364 manager->ops->enable_vblank(manager);
369 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
371 struct exynos_drm_private *private = dev->dev_private;
372 struct exynos_drm_crtc *exynos_crtc =
373 to_exynos_crtc(private->crtc[pipe]);
374 struct exynos_drm_manager *manager = exynos_crtc->manager;
376 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
379 if (manager->ops->disable_vblank)
380 manager->ops->disable_vblank(manager);
383 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
385 struct exynos_drm_private *dev_priv = dev->dev_private;
386 struct drm_pending_vblank_event *e, *t;
387 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
388 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
391 spin_lock_irqsave(&dev->event_lock, flags);
393 list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
395 /* if event's pipe isn't same as crtc then ignore it. */
399 list_del(&e->base.link);
400 drm_send_vblank_event(dev, -1, e);
401 drm_vblank_put(dev, pipe);
402 atomic_set(&exynos_crtc->pending_flip, 0);
403 wake_up(&exynos_crtc->pending_flip_queue);
406 spin_unlock_irqrestore(&dev->event_lock, flags);
409 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
411 struct exynos_drm_manager *manager;
412 struct drm_device *dev = fb->dev;
413 struct drm_crtc *crtc;
416 * make sure that overlay data are updated to real hardware
419 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
420 manager = to_exynos_crtc(crtc)->manager;
423 * wait for vblank interrupt
424 * - this makes sure that overlay data are updated to
427 if (manager->ops->wait_for_vblank)
428 manager->ops->wait_for_vblank(manager);
432 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
433 unsigned int out_type)
435 struct drm_crtc *crtc;
437 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
438 struct exynos_drm_crtc *exynos_crtc;
440 exynos_crtc = to_exynos_crtc(crtc);
441 if (exynos_crtc->type == out_type)
442 return exynos_crtc->pipe;
448 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
450 struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
452 if (manager->ops->te_handler)
453 manager->ops->te_handler(manager);