11c248b7dSInki Dae /* exynos_drm_crtc.c
21c248b7dSInki Dae  *
31c248b7dSInki Dae  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
41c248b7dSInki Dae  * Authors:
51c248b7dSInki Dae  *	Inki Dae <inki.dae@samsung.com>
61c248b7dSInki Dae  *	Joonyoung Shim <jy0922.shim@samsung.com>
71c248b7dSInki Dae  *	Seung-Woo Kim <sw0312.kim@samsung.com>
81c248b7dSInki Dae  *
9d81aecb5SInki Dae  * This program is free software; you can redistribute  it and/or modify it
10d81aecb5SInki Dae  * under  the terms of  the GNU General  Public License as published by the
11d81aecb5SInki Dae  * Free Software Foundation;  either version 2 of the  License, or (at your
12d81aecb5SInki Dae  * option) any later version.
131c248b7dSInki Dae  */
141c248b7dSInki Dae 
15760285e7SDavid Howells #include <drm/drmP.h>
16760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
171c248b7dSInki Dae 
181c248b7dSInki Dae #include "exynos_drm_drv.h"
191c248b7dSInki Dae #include "exynos_drm_encoder.h"
20b5d2eb3bSJoonyoung Shim #include "exynos_drm_plane.h"
211c248b7dSInki Dae 
221c248b7dSInki Dae #define to_exynos_crtc(x)	container_of(x, struct exynos_drm_crtc,\
231c248b7dSInki Dae 				drm_crtc)
241c248b7dSInki Dae 
253b8d1cf8SJoonyoung Shim enum exynos_crtc_mode {
263b8d1cf8SJoonyoung Shim 	CRTC_MODE_NORMAL,	/* normal mode */
273b8d1cf8SJoonyoung Shim 	CRTC_MODE_BLANK,	/* The private plane of crtc is blank */
283b8d1cf8SJoonyoung Shim };
293b8d1cf8SJoonyoung Shim 
301c248b7dSInki Dae /*
311c248b7dSInki Dae  * Exynos specific crtc structure.
321c248b7dSInki Dae  *
331c248b7dSInki Dae  * @drm_crtc: crtc object.
34b5d2eb3bSJoonyoung Shim  * @drm_plane: pointer of private plane object for this crtc
351c248b7dSInki Dae  * @pipe: a crtc index created at load() with a new crtc object creation
361c248b7dSInki Dae  *	and the crtc object would be set to private->crtc array
371c248b7dSInki Dae  *	to get a crtc object corresponding to this pipe from private->crtc
381c248b7dSInki Dae  *	array when irq interrupt occured. the reason of using this pipe is that
391c248b7dSInki Dae  *	drm framework doesn't support multiple irq yet.
401c248b7dSInki Dae  *	we can refer to the crtc to current hardware interrupt occured through
411c248b7dSInki Dae  *	this pipe value.
42ec05da95SInki Dae  * @dpms: store the crtc dpms value
433b8d1cf8SJoonyoung Shim  * @mode: store the crtc mode value
441c248b7dSInki Dae  */
451c248b7dSInki Dae struct exynos_drm_crtc {
461c248b7dSInki Dae 	struct drm_crtc			drm_crtc;
47b5d2eb3bSJoonyoung Shim 	struct drm_plane		*plane;
481c248b7dSInki Dae 	unsigned int			pipe;
49ec05da95SInki Dae 	unsigned int			dpms;
503b8d1cf8SJoonyoung Shim 	enum exynos_crtc_mode		mode;
5120cd2640SInki Dae 	wait_queue_head_t		pending_flip_queue;
5220cd2640SInki Dae 	atomic_t			pending_flip;
531c248b7dSInki Dae };
541c248b7dSInki Dae 
551c248b7dSInki Dae static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
561c248b7dSInki Dae {
57d2716c89SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
581c248b7dSInki Dae 
59d2716c89SJoonyoung Shim 	DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
60d2716c89SJoonyoung Shim 
61ec05da95SInki Dae 	if (exynos_crtc->dpms == mode) {
62ec05da95SInki Dae 		DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
63ec05da95SInki Dae 		return;
64ec05da95SInki Dae 	}
65ec05da95SInki Dae 
6620cd2640SInki Dae 	if (mode > DRM_MODE_DPMS_ON) {
6720cd2640SInki Dae 		/* wait for the completion of page flip. */
6820cd2640SInki Dae 		wait_event(exynos_crtc->pending_flip_queue,
6920cd2640SInki Dae 				atomic_read(&exynos_crtc->pending_flip) == 0);
7020cd2640SInki Dae 		drm_vblank_off(crtc->dev, exynos_crtc->pipe);
7120cd2640SInki Dae 	}
7220cd2640SInki Dae 
73cf5188acSJoonyoung Shim 	exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
74ec05da95SInki Dae 	exynos_crtc->dpms = mode;
751c248b7dSInki Dae }
761c248b7dSInki Dae 
771c248b7dSInki Dae static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
781c248b7dSInki Dae {
791c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
801c248b7dSInki Dae 
811c248b7dSInki Dae 	/* drm framework doesn't check NULL. */
821c248b7dSInki Dae }
831c248b7dSInki Dae 
841c248b7dSInki Dae static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
851c248b7dSInki Dae {
86d2716c89SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
87d2716c89SJoonyoung Shim 
881c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
891c248b7dSInki Dae 
9050caf25cSInki Dae 	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
914070d212SJoonyoung Shim 	exynos_plane_commit(exynos_crtc->plane);
92cf5188acSJoonyoung Shim 	exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
931c248b7dSInki Dae }
941c248b7dSInki Dae 
951c248b7dSInki Dae static bool
961c248b7dSInki Dae exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
97e811f5aeSLaurent Pinchart 			    const struct drm_display_mode *mode,
981c248b7dSInki Dae 			    struct drm_display_mode *adjusted_mode)
991c248b7dSInki Dae {
1001c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1011c248b7dSInki Dae 
1021c248b7dSInki Dae 	/* drm framework doesn't check NULL */
1031c248b7dSInki Dae 	return true;
1041c248b7dSInki Dae }
1051c248b7dSInki Dae 
1061c248b7dSInki Dae static int
1071c248b7dSInki Dae exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
1081c248b7dSInki Dae 			  struct drm_display_mode *adjusted_mode, int x, int y,
1091c248b7dSInki Dae 			  struct drm_framebuffer *old_fb)
1101c248b7dSInki Dae {
111aeb29224SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
1124070d212SJoonyoung Shim 	struct drm_plane *plane = exynos_crtc->plane;
1134070d212SJoonyoung Shim 	unsigned int crtc_w;
1144070d212SJoonyoung Shim 	unsigned int crtc_h;
115d249ce02SJoonyoung Shim 	int pipe = exynos_crtc->pipe;
116aeb29224SJoonyoung Shim 	int ret;
117aeb29224SJoonyoung Shim 
1181c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1191c248b7dSInki Dae 
1201de425b0SInki Dae 	/*
1211de425b0SInki Dae 	 * copy the mode data adjusted by mode_fixup() into crtc->mode
1221de425b0SInki Dae 	 * so that hardware can be seet to proper mode.
1231de425b0SInki Dae 	 */
1241de425b0SInki Dae 	memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
1251c248b7dSInki Dae 
1264070d212SJoonyoung Shim 	crtc_w = crtc->fb->width - x;
1274070d212SJoonyoung Shim 	crtc_h = crtc->fb->height - y;
1284070d212SJoonyoung Shim 
1294070d212SJoonyoung Shim 	ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
1304070d212SJoonyoung Shim 				    x, y, crtc_w, crtc_h);
131aeb29224SJoonyoung Shim 	if (ret)
132aeb29224SJoonyoung Shim 		return ret;
133aeb29224SJoonyoung Shim 
1344070d212SJoonyoung Shim 	plane->crtc = crtc;
1354070d212SJoonyoung Shim 	plane->fb = crtc->fb;
1364070d212SJoonyoung Shim 
137d249ce02SJoonyoung Shim 	exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
138aeb29224SJoonyoung Shim 
139aeb29224SJoonyoung Shim 	return 0;
1401c248b7dSInki Dae }
1411c248b7dSInki Dae 
1427fd65df1SInki Dae static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
1431c248b7dSInki Dae 					  struct drm_framebuffer *old_fb)
1441c248b7dSInki Dae {
1454070d212SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
1464070d212SJoonyoung Shim 	struct drm_plane *plane = exynos_crtc->plane;
1474070d212SJoonyoung Shim 	unsigned int crtc_w;
1484070d212SJoonyoung Shim 	unsigned int crtc_h;
1491c248b7dSInki Dae 	int ret;
1501c248b7dSInki Dae 
1511c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1521c248b7dSInki Dae 
15332aeab17SInki Dae 	/* when framebuffer changing is requested, crtc's dpms should be on */
15432aeab17SInki Dae 	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
15532aeab17SInki Dae 		DRM_ERROR("failed framebuffer changing request.\n");
15632aeab17SInki Dae 		return -EPERM;
15732aeab17SInki Dae 	}
15832aeab17SInki Dae 
1594070d212SJoonyoung Shim 	crtc_w = crtc->fb->width - x;
1604070d212SJoonyoung Shim 	crtc_h = crtc->fb->height - y;
1614070d212SJoonyoung Shim 
1624070d212SJoonyoung Shim 	ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
1634070d212SJoonyoung Shim 				    x, y, crtc_w, crtc_h);
1641c248b7dSInki Dae 	if (ret)
1651c248b7dSInki Dae 		return ret;
1661c248b7dSInki Dae 
167bebab8ffSJoonyoung Shim 	exynos_drm_crtc_commit(crtc);
1681c248b7dSInki Dae 
1694070d212SJoonyoung Shim 	return 0;
1701c248b7dSInki Dae }
1711c248b7dSInki Dae 
1727fd65df1SInki Dae static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1737fd65df1SInki Dae 					  struct drm_framebuffer *old_fb)
1747fd65df1SInki Dae {
1757fd65df1SInki Dae 	return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
1767fd65df1SInki Dae }
1777fd65df1SInki Dae 
178a365d9ebSJoonyoung Shim static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
179a365d9ebSJoonyoung Shim {
180a365d9ebSJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
181a365d9ebSJoonyoung Shim 
182a365d9ebSJoonyoung Shim 	DRM_DEBUG_KMS("%s\n", __FILE__);
183a365d9ebSJoonyoung Shim 
184a365d9ebSJoonyoung Shim 	exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
185a365d9ebSJoonyoung Shim 	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
186a365d9ebSJoonyoung Shim }
187a365d9ebSJoonyoung Shim 
1881c248b7dSInki Dae static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
1891c248b7dSInki Dae 	.dpms		= exynos_drm_crtc_dpms,
1901c248b7dSInki Dae 	.prepare	= exynos_drm_crtc_prepare,
1911c248b7dSInki Dae 	.commit		= exynos_drm_crtc_commit,
1921c248b7dSInki Dae 	.mode_fixup	= exynos_drm_crtc_mode_fixup,
1931c248b7dSInki Dae 	.mode_set	= exynos_drm_crtc_mode_set,
1941c248b7dSInki Dae 	.mode_set_base	= exynos_drm_crtc_mode_set_base,
195a365d9ebSJoonyoung Shim 	.disable	= exynos_drm_crtc_disable,
1961c248b7dSInki Dae };
1971c248b7dSInki Dae 
1981c248b7dSInki Dae static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
1991c248b7dSInki Dae 				      struct drm_framebuffer *fb,
2001c248b7dSInki Dae 				      struct drm_pending_vblank_event *event)
2011c248b7dSInki Dae {
2021c248b7dSInki Dae 	struct drm_device *dev = crtc->dev;
2031c248b7dSInki Dae 	struct exynos_drm_private *dev_priv = dev->dev_private;
2041c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
2051c248b7dSInki Dae 	struct drm_framebuffer *old_fb = crtc->fb;
2061c248b7dSInki Dae 	int ret = -EINVAL;
2071c248b7dSInki Dae 
2081c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2091c248b7dSInki Dae 
210ef6223dcSInki Dae 	/* when the page flip is requested, crtc's dpms should be on */
211ef6223dcSInki Dae 	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
212ef6223dcSInki Dae 		DRM_ERROR("failed page flip request.\n");
213ef6223dcSInki Dae 		return -EINVAL;
214ef6223dcSInki Dae 	}
215ef6223dcSInki Dae 
2161c248b7dSInki Dae 	mutex_lock(&dev->struct_mutex);
2171c248b7dSInki Dae 
218ccf4d883SInki Dae 	if (event) {
219ccf4d883SInki Dae 		/*
220ccf4d883SInki Dae 		 * the pipe from user always is 0 so we can set pipe number
221ccf4d883SInki Dae 		 * of current owner to event.
222ccf4d883SInki Dae 		 */
223ccf4d883SInki Dae 		event->pipe = exynos_crtc->pipe;
224ccf4d883SInki Dae 
2251c248b7dSInki Dae 		ret = drm_vblank_get(dev, exynos_crtc->pipe);
2261c248b7dSInki Dae 		if (ret) {
2271c248b7dSInki Dae 			DRM_DEBUG("failed to acquire vblank counter\n");
228ccf4d883SInki Dae 
2291c248b7dSInki Dae 			goto out;
2301c248b7dSInki Dae 		}
2311c248b7dSInki Dae 
23285473328SImre Deak 		spin_lock_irq(&dev->event_lock);
233c5614ae3SInki Dae 		list_add_tail(&event->base.link,
234c5614ae3SInki Dae 				&dev_priv->pageflip_event_list);
23520cd2640SInki Dae 		atomic_set(&exynos_crtc->pending_flip, 1);
23685473328SImre Deak 		spin_unlock_irq(&dev->event_lock);
237c5614ae3SInki Dae 
2381c248b7dSInki Dae 		crtc->fb = fb;
2397fd65df1SInki Dae 		ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
2404070d212SJoonyoung Shim 						    NULL);
2411c248b7dSInki Dae 		if (ret) {
2421c248b7dSInki Dae 			crtc->fb = old_fb;
24385473328SImre Deak 
24485473328SImre Deak 			spin_lock_irq(&dev->event_lock);
2451c248b7dSInki Dae 			drm_vblank_put(dev, exynos_crtc->pipe);
246ccf4d883SInki Dae 			list_del(&event->base.link);
24785473328SImre Deak 			spin_unlock_irq(&dev->event_lock);
2481c248b7dSInki Dae 
2491c248b7dSInki Dae 			goto out;
2501c248b7dSInki Dae 		}
2511c248b7dSInki Dae 	}
2521c248b7dSInki Dae out:
2531c248b7dSInki Dae 	mutex_unlock(&dev->struct_mutex);
2541c248b7dSInki Dae 	return ret;
2551c248b7dSInki Dae }
2561c248b7dSInki Dae 
2571c248b7dSInki Dae static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
2581c248b7dSInki Dae {
2591c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
2601c248b7dSInki Dae 	struct exynos_drm_private *private = crtc->dev->dev_private;
2611c248b7dSInki Dae 
2621c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2631c248b7dSInki Dae 
2641c248b7dSInki Dae 	private->crtc[exynos_crtc->pipe] = NULL;
2651c248b7dSInki Dae 
2661c248b7dSInki Dae 	drm_crtc_cleanup(crtc);
2671c248b7dSInki Dae 	kfree(exynos_crtc);
2681c248b7dSInki Dae }
2691c248b7dSInki Dae 
2703b8d1cf8SJoonyoung Shim static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
2713b8d1cf8SJoonyoung Shim 					struct drm_property *property,
2723b8d1cf8SJoonyoung Shim 					uint64_t val)
2733b8d1cf8SJoonyoung Shim {
2743b8d1cf8SJoonyoung Shim 	struct drm_device *dev = crtc->dev;
2753b8d1cf8SJoonyoung Shim 	struct exynos_drm_private *dev_priv = dev->dev_private;
2763b8d1cf8SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
2773b8d1cf8SJoonyoung Shim 
2783b8d1cf8SJoonyoung Shim 	DRM_DEBUG_KMS("%s\n", __func__);
2793b8d1cf8SJoonyoung Shim 
2803b8d1cf8SJoonyoung Shim 	if (property == dev_priv->crtc_mode_property) {
2813b8d1cf8SJoonyoung Shim 		enum exynos_crtc_mode mode = val;
2823b8d1cf8SJoonyoung Shim 
2833b8d1cf8SJoonyoung Shim 		if (mode == exynos_crtc->mode)
2843b8d1cf8SJoonyoung Shim 			return 0;
2853b8d1cf8SJoonyoung Shim 
2863b8d1cf8SJoonyoung Shim 		exynos_crtc->mode = mode;
2873b8d1cf8SJoonyoung Shim 
2883b8d1cf8SJoonyoung Shim 		switch (mode) {
2893b8d1cf8SJoonyoung Shim 		case CRTC_MODE_NORMAL:
2903b8d1cf8SJoonyoung Shim 			exynos_drm_crtc_commit(crtc);
2913b8d1cf8SJoonyoung Shim 			break;
2923b8d1cf8SJoonyoung Shim 		case CRTC_MODE_BLANK:
2933b8d1cf8SJoonyoung Shim 			exynos_plane_dpms(exynos_crtc->plane,
2943b8d1cf8SJoonyoung Shim 					  DRM_MODE_DPMS_OFF);
2953b8d1cf8SJoonyoung Shim 			break;
2963b8d1cf8SJoonyoung Shim 		default:
2973b8d1cf8SJoonyoung Shim 			break;
2983b8d1cf8SJoonyoung Shim 		}
2993b8d1cf8SJoonyoung Shim 
3003b8d1cf8SJoonyoung Shim 		return 0;
3013b8d1cf8SJoonyoung Shim 	}
3023b8d1cf8SJoonyoung Shim 
3033b8d1cf8SJoonyoung Shim 	return -EINVAL;
3043b8d1cf8SJoonyoung Shim }
3053b8d1cf8SJoonyoung Shim 
3061c248b7dSInki Dae static struct drm_crtc_funcs exynos_crtc_funcs = {
3071c248b7dSInki Dae 	.set_config	= drm_crtc_helper_set_config,
3081c248b7dSInki Dae 	.page_flip	= exynos_drm_crtc_page_flip,
3091c248b7dSInki Dae 	.destroy	= exynos_drm_crtc_destroy,
3103b8d1cf8SJoonyoung Shim 	.set_property	= exynos_drm_crtc_set_property,
3111c248b7dSInki Dae };
3121c248b7dSInki Dae 
3133b8d1cf8SJoonyoung Shim static const struct drm_prop_enum_list mode_names[] = {
3143b8d1cf8SJoonyoung Shim 	{ CRTC_MODE_NORMAL, "normal" },
3153b8d1cf8SJoonyoung Shim 	{ CRTC_MODE_BLANK, "blank" },
3163b8d1cf8SJoonyoung Shim };
3173b8d1cf8SJoonyoung Shim 
3183b8d1cf8SJoonyoung Shim static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
3193b8d1cf8SJoonyoung Shim {
3203b8d1cf8SJoonyoung Shim 	struct drm_device *dev = crtc->dev;
3213b8d1cf8SJoonyoung Shim 	struct exynos_drm_private *dev_priv = dev->dev_private;
3223b8d1cf8SJoonyoung Shim 	struct drm_property *prop;
3233b8d1cf8SJoonyoung Shim 
3243b8d1cf8SJoonyoung Shim 	DRM_DEBUG_KMS("%s\n", __func__);
3253b8d1cf8SJoonyoung Shim 
3263b8d1cf8SJoonyoung Shim 	prop = dev_priv->crtc_mode_property;
3273b8d1cf8SJoonyoung Shim 	if (!prop) {
3283b8d1cf8SJoonyoung Shim 		prop = drm_property_create_enum(dev, 0, "mode", mode_names,
3293b8d1cf8SJoonyoung Shim 						ARRAY_SIZE(mode_names));
3303b8d1cf8SJoonyoung Shim 		if (!prop)
3313b8d1cf8SJoonyoung Shim 			return;
3323b8d1cf8SJoonyoung Shim 
3333b8d1cf8SJoonyoung Shim 		dev_priv->crtc_mode_property = prop;
3343b8d1cf8SJoonyoung Shim 	}
3353b8d1cf8SJoonyoung Shim 
3363b8d1cf8SJoonyoung Shim 	drm_object_attach_property(&crtc->base, prop, 0);
3373b8d1cf8SJoonyoung Shim }
3383b8d1cf8SJoonyoung Shim 
3391c248b7dSInki Dae int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
3401c248b7dSInki Dae {
3411c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc;
3421c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
3431c248b7dSInki Dae 	struct drm_crtc *crtc;
3441c248b7dSInki Dae 
3451c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
3461c248b7dSInki Dae 
3471c248b7dSInki Dae 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
3481c248b7dSInki Dae 	if (!exynos_crtc) {
3491c248b7dSInki Dae 		DRM_ERROR("failed to allocate exynos crtc\n");
3501c248b7dSInki Dae 		return -ENOMEM;
3511c248b7dSInki Dae 	}
3521c248b7dSInki Dae 
3531c248b7dSInki Dae 	exynos_crtc->pipe = nr;
354ec05da95SInki Dae 	exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
35520cd2640SInki Dae 	init_waitqueue_head(&exynos_crtc->pending_flip_queue);
35620cd2640SInki Dae 	atomic_set(&exynos_crtc->pending_flip, 0);
357b5d2eb3bSJoonyoung Shim 	exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
358b5d2eb3bSJoonyoung Shim 	if (!exynos_crtc->plane) {
359b5d2eb3bSJoonyoung Shim 		kfree(exynos_crtc);
360b5d2eb3bSJoonyoung Shim 		return -ENOMEM;
361b5d2eb3bSJoonyoung Shim 	}
362b5d2eb3bSJoonyoung Shim 
3631c248b7dSInki Dae 	crtc = &exynos_crtc->drm_crtc;
3641c248b7dSInki Dae 
3651c248b7dSInki Dae 	private->crtc[nr] = crtc;
3661c248b7dSInki Dae 
3671c248b7dSInki Dae 	drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
3681c248b7dSInki Dae 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
3691c248b7dSInki Dae 
3703b8d1cf8SJoonyoung Shim 	exynos_drm_crtc_attach_mode_property(crtc);
3713b8d1cf8SJoonyoung Shim 
3721c248b7dSInki Dae 	return 0;
3731c248b7dSInki Dae }
3741c248b7dSInki Dae 
3751c248b7dSInki Dae int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
3761c248b7dSInki Dae {
3771c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
378ec05da95SInki Dae 	struct exynos_drm_crtc *exynos_crtc =
379ec05da95SInki Dae 		to_exynos_crtc(private->crtc[crtc]);
3801c248b7dSInki Dae 
3811c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
3821c248b7dSInki Dae 
383ec05da95SInki Dae 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
384ec05da95SInki Dae 		return -EPERM;
385ec05da95SInki Dae 
3861c248b7dSInki Dae 	exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
3871c248b7dSInki Dae 			exynos_drm_enable_vblank);
3881c248b7dSInki Dae 
3891c248b7dSInki Dae 	return 0;
3901c248b7dSInki Dae }
3911c248b7dSInki Dae 
3921c248b7dSInki Dae void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
3931c248b7dSInki Dae {
3941c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
395ec05da95SInki Dae 	struct exynos_drm_crtc *exynos_crtc =
396ec05da95SInki Dae 		to_exynos_crtc(private->crtc[crtc]);
3971c248b7dSInki Dae 
3981c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
3991c248b7dSInki Dae 
400ec05da95SInki Dae 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
401ec05da95SInki Dae 		return;
402ec05da95SInki Dae 
4031c248b7dSInki Dae 	exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
4041c248b7dSInki Dae 			exynos_drm_disable_vblank);
4051c248b7dSInki Dae }
406663d8766SRahul Sharma 
407663d8766SRahul Sharma void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc)
408663d8766SRahul Sharma {
409663d8766SRahul Sharma 	struct exynos_drm_private *dev_priv = dev->dev_private;
410663d8766SRahul Sharma 	struct drm_pending_vblank_event *e, *t;
41120cd2640SInki Dae 	struct drm_crtc *drm_crtc = dev_priv->crtc[crtc];
41220cd2640SInki Dae 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
413663d8766SRahul Sharma 	unsigned long flags;
414663d8766SRahul Sharma 
415663d8766SRahul Sharma 	DRM_DEBUG_KMS("%s\n", __FILE__);
416663d8766SRahul Sharma 
417663d8766SRahul Sharma 	spin_lock_irqsave(&dev->event_lock, flags);
418663d8766SRahul Sharma 
419663d8766SRahul Sharma 	list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
420663d8766SRahul Sharma 			base.link) {
421663d8766SRahul Sharma 		/* if event's pipe isn't same as crtc then ignore it. */
422663d8766SRahul Sharma 		if (crtc != e->pipe)
423663d8766SRahul Sharma 			continue;
424663d8766SRahul Sharma 
425c5cca97fSRob Clark 		list_del(&e->base.link);
426c5cca97fSRob Clark 		drm_send_vblank_event(dev, -1, e);
427663d8766SRahul Sharma 		drm_vblank_put(dev, crtc);
42820cd2640SInki Dae 		atomic_set(&exynos_crtc->pending_flip, 0);
42920cd2640SInki Dae 		wake_up(&exynos_crtc->pending_flip_queue);
430663d8766SRahul Sharma 	}
431663d8766SRahul Sharma 
432663d8766SRahul Sharma 	spin_unlock_irqrestore(&dev->event_lock, flags);
433663d8766SRahul Sharma }
434