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 #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
26 enum exynos_crtc_mode {
27 CRTC_MODE_NORMAL, /* normal mode */
28 CRTC_MODE_BLANK, /* The private plane of crtc is blank */
32 * Exynos specific crtc structure.
34 * @drm_crtc: crtc object.
35 * @drm_plane: pointer of private plane object for this crtc
36 * @manager: the manager associated with this crtc
37 * @pipe: a crtc index created at load() with a new crtc object creation
38 * and the crtc object would be set to private->crtc array
39 * to get a crtc object corresponding to this pipe from private->crtc
40 * array when irq interrupt occurred. the reason of using this pipe is that
41 * drm framework doesn't support multiple irq yet.
42 * we can refer to the crtc to current hardware interrupt occurred through
44 * @dpms: store the crtc dpms value
45 * @mode: store the crtc mode value
47 struct exynos_drm_crtc {
48 struct drm_crtc drm_crtc;
49 struct drm_plane *plane;
50 struct exynos_drm_manager *manager;
53 enum exynos_crtc_mode mode;
54 wait_queue_head_t pending_flip_queue;
55 atomic_t pending_flip;
58 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
60 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
61 struct exynos_drm_manager *manager = exynos_crtc->manager;
63 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
65 if (exynos_crtc->dpms == mode) {
66 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
70 if (mode > DRM_MODE_DPMS_ON) {
71 /* wait for the completion of page flip. */
72 wait_event(exynos_crtc->pending_flip_queue,
73 atomic_read(&exynos_crtc->pending_flip) == 0);
74 drm_vblank_off(crtc->dev, exynos_crtc->pipe);
77 if (manager->ops->dpms)
78 manager->ops->dpms(manager, mode);
80 exynos_crtc->dpms = mode;
83 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
85 /* drm framework doesn't check NULL. */
88 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
90 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
91 struct exynos_drm_manager *manager = exynos_crtc->manager;
93 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
95 exynos_plane_commit(exynos_crtc->plane);
97 if (manager->ops->commit)
98 manager->ops->commit(manager);
100 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
104 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
105 const struct drm_display_mode *mode,
106 struct drm_display_mode *adjusted_mode)
108 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
109 struct exynos_drm_manager *manager = exynos_crtc->manager;
111 if (manager->ops->mode_fixup)
112 return manager->ops->mode_fixup(manager, mode, adjusted_mode);
118 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
119 struct drm_display_mode *adjusted_mode, int x, int y,
120 struct drm_framebuffer *old_fb)
122 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
123 struct exynos_drm_manager *manager = exynos_crtc->manager;
124 struct drm_plane *plane = exynos_crtc->plane;
130 * copy the mode data adjusted by mode_fixup() into crtc->mode
131 * so that hardware can be seet to proper mode.
133 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
135 crtc_w = crtc->primary->fb->width - x;
136 crtc_h = crtc->primary->fb->height - y;
138 if (manager->ops->mode_set)
139 manager->ops->mode_set(manager, &crtc->mode);
141 ret = exynos_plane_mode_set(plane, crtc, crtc->primary->fb, 0, 0, crtc_w, crtc_h,
142 x, y, crtc_w, crtc_h);
147 plane->fb = crtc->primary->fb;
152 static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
153 struct drm_framebuffer *old_fb)
155 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
156 struct drm_plane *plane = exynos_crtc->plane;
161 /* when framebuffer changing is requested, crtc's dpms should be on */
162 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
163 DRM_ERROR("failed framebuffer changing request.\n");
167 crtc_w = crtc->primary->fb->width - x;
168 crtc_h = crtc->primary->fb->height - y;
170 ret = exynos_plane_mode_set(plane, crtc, crtc->primary->fb, 0, 0, crtc_w, crtc_h,
171 x, y, crtc_w, crtc_h);
175 exynos_drm_crtc_commit(crtc);
180 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
181 struct drm_framebuffer *old_fb)
183 return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
186 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
188 struct drm_plane *plane;
191 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
193 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
194 if (plane->crtc != crtc)
197 ret = plane->funcs->disable_plane(plane);
199 DRM_ERROR("Failed to disable plane %d\n", ret);
203 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
204 .dpms = exynos_drm_crtc_dpms,
205 .prepare = exynos_drm_crtc_prepare,
206 .commit = exynos_drm_crtc_commit,
207 .mode_fixup = exynos_drm_crtc_mode_fixup,
208 .mode_set = exynos_drm_crtc_mode_set,
209 .mode_set_base = exynos_drm_crtc_mode_set_base,
210 .disable = exynos_drm_crtc_disable,
213 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
214 struct drm_framebuffer *fb,
215 struct drm_pending_vblank_event *event,
216 uint32_t page_flip_flags)
218 struct drm_device *dev = crtc->dev;
219 struct exynos_drm_private *dev_priv = dev->dev_private;
220 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
221 struct drm_framebuffer *old_fb = crtc->primary->fb;
224 /* when the page flip is requested, crtc's dpms should be on */
225 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
226 DRM_ERROR("failed page flip request.\n");
230 mutex_lock(&dev->struct_mutex);
234 * the pipe from user always is 0 so we can set pipe number
235 * of current owner to event.
237 event->pipe = exynos_crtc->pipe;
239 ret = drm_vblank_get(dev, exynos_crtc->pipe);
241 DRM_DEBUG("failed to acquire vblank counter\n");
246 spin_lock_irq(&dev->event_lock);
247 list_add_tail(&event->base.link,
248 &dev_priv->pageflip_event_list);
249 atomic_set(&exynos_crtc->pending_flip, 1);
250 spin_unlock_irq(&dev->event_lock);
252 crtc->primary->fb = fb;
253 ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
256 crtc->primary->fb = old_fb;
258 spin_lock_irq(&dev->event_lock);
259 drm_vblank_put(dev, exynos_crtc->pipe);
260 list_del(&event->base.link);
261 spin_unlock_irq(&dev->event_lock);
267 mutex_unlock(&dev->struct_mutex);
271 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
273 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
274 struct exynos_drm_private *private = crtc->dev->dev_private;
276 private->crtc[exynos_crtc->pipe] = NULL;
278 drm_crtc_cleanup(crtc);
282 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
283 struct drm_property *property,
286 struct drm_device *dev = crtc->dev;
287 struct exynos_drm_private *dev_priv = dev->dev_private;
288 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
290 if (property == dev_priv->crtc_mode_property) {
291 enum exynos_crtc_mode mode = val;
293 if (mode == exynos_crtc->mode)
296 exynos_crtc->mode = mode;
299 case CRTC_MODE_NORMAL:
300 exynos_drm_crtc_commit(crtc);
302 case CRTC_MODE_BLANK:
303 exynos_plane_dpms(exynos_crtc->plane,
316 static struct drm_crtc_funcs exynos_crtc_funcs = {
317 .set_config = drm_crtc_helper_set_config,
318 .page_flip = exynos_drm_crtc_page_flip,
319 .destroy = exynos_drm_crtc_destroy,
320 .set_property = exynos_drm_crtc_set_property,
323 static const struct drm_prop_enum_list mode_names[] = {
324 { CRTC_MODE_NORMAL, "normal" },
325 { CRTC_MODE_BLANK, "blank" },
328 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
330 struct drm_device *dev = crtc->dev;
331 struct exynos_drm_private *dev_priv = dev->dev_private;
332 struct drm_property *prop;
334 prop = dev_priv->crtc_mode_property;
336 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
337 ARRAY_SIZE(mode_names));
341 dev_priv->crtc_mode_property = prop;
344 drm_object_attach_property(&crtc->base, prop, 0);
347 int exynos_drm_crtc_create(struct exynos_drm_manager *manager)
349 struct exynos_drm_crtc *exynos_crtc;
350 struct exynos_drm_private *private = manager->drm_dev->dev_private;
351 struct drm_crtc *crtc;
353 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
357 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
358 atomic_set(&exynos_crtc->pending_flip, 0);
360 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
361 exynos_crtc->manager = manager;
362 exynos_crtc->pipe = manager->pipe;
363 exynos_crtc->plane = exynos_plane_init(manager->drm_dev,
364 1 << manager->pipe, true);
365 if (!exynos_crtc->plane) {
370 crtc = &exynos_crtc->drm_crtc;
372 private->crtc[manager->pipe] = crtc;
374 drm_crtc_init(manager->drm_dev, crtc, &exynos_crtc_funcs);
375 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
377 exynos_drm_crtc_attach_mode_property(crtc);
382 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
384 struct exynos_drm_private *private = dev->dev_private;
385 struct exynos_drm_crtc *exynos_crtc =
386 to_exynos_crtc(private->crtc[pipe]);
387 struct exynos_drm_manager *manager = exynos_crtc->manager;
389 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
392 if (manager->ops->enable_vblank)
393 manager->ops->enable_vblank(manager);
398 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
400 struct exynos_drm_private *private = dev->dev_private;
401 struct exynos_drm_crtc *exynos_crtc =
402 to_exynos_crtc(private->crtc[pipe]);
403 struct exynos_drm_manager *manager = exynos_crtc->manager;
405 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
408 if (manager->ops->disable_vblank)
409 manager->ops->disable_vblank(manager);
412 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
414 struct exynos_drm_private *dev_priv = dev->dev_private;
415 struct drm_pending_vblank_event *e, *t;
416 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
417 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
420 spin_lock_irqsave(&dev->event_lock, flags);
422 list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
424 /* if event's pipe isn't same as crtc then ignore it. */
428 list_del(&e->base.link);
429 drm_send_vblank_event(dev, -1, e);
430 drm_vblank_put(dev, pipe);
431 atomic_set(&exynos_crtc->pending_flip, 0);
432 wake_up(&exynos_crtc->pending_flip_queue);
435 spin_unlock_irqrestore(&dev->event_lock, flags);
438 void exynos_drm_crtc_plane_mode_set(struct drm_crtc *crtc,
439 struct exynos_drm_overlay *overlay)
441 struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
443 if (manager->ops->win_mode_set)
444 manager->ops->win_mode_set(manager, overlay);
447 void exynos_drm_crtc_plane_commit(struct drm_crtc *crtc, int zpos)
449 struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
451 if (manager->ops->win_commit)
452 manager->ops->win_commit(manager, zpos);
455 void exynos_drm_crtc_plane_enable(struct drm_crtc *crtc, int zpos)
457 struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
459 if (manager->ops->win_enable)
460 manager->ops->win_enable(manager, zpos);
463 void exynos_drm_crtc_plane_disable(struct drm_crtc *crtc, int zpos)
465 struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
467 if (manager->ops->win_disable)
468 manager->ops->win_disable(manager, zpos);
471 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
473 struct exynos_drm_manager *manager;
474 struct drm_device *dev = fb->dev;
475 struct drm_crtc *crtc;
478 * make sure that overlay data are updated to real hardware
481 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
482 manager = to_exynos_crtc(crtc)->manager;
485 * wait for vblank interrupt
486 * - this makes sure that overlay data are updated to
489 if (manager->ops->wait_for_vblank)
490 manager->ops->wait_for_vblank(manager);