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  *
91c248b7dSInki Dae  * Permission is hereby granted, free of charge, to any person obtaining a
101c248b7dSInki Dae  * copy of this software and associated documentation files (the "Software"),
111c248b7dSInki Dae  * to deal in the Software without restriction, including without limitation
121c248b7dSInki Dae  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
131c248b7dSInki Dae  * and/or sell copies of the Software, and to permit persons to whom the
141c248b7dSInki Dae  * Software is furnished to do so, subject to the following conditions:
151c248b7dSInki Dae  *
161c248b7dSInki Dae  * The above copyright notice and this permission notice (including the next
171c248b7dSInki Dae  * paragraph) shall be included in all copies or substantial portions of the
181c248b7dSInki Dae  * Software.
191c248b7dSInki Dae  *
201c248b7dSInki Dae  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211c248b7dSInki Dae  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
221c248b7dSInki Dae  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
231c248b7dSInki Dae  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
241c248b7dSInki Dae  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
251c248b7dSInki Dae  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
261c248b7dSInki Dae  * OTHER DEALINGS IN THE SOFTWARE.
271c248b7dSInki Dae  */
281c248b7dSInki Dae 
291c248b7dSInki Dae #include "drmP.h"
301c248b7dSInki Dae #include "drm_crtc_helper.h"
311c248b7dSInki Dae 
321c248b7dSInki Dae #include "exynos_drm_drv.h"
331c248b7dSInki Dae #include "exynos_drm_encoder.h"
34b5d2eb3bSJoonyoung Shim #include "exynos_drm_plane.h"
351c248b7dSInki Dae 
361c248b7dSInki Dae #define to_exynos_crtc(x)	container_of(x, struct exynos_drm_crtc,\
371c248b7dSInki Dae 				drm_crtc)
381c248b7dSInki Dae 
391c248b7dSInki Dae /*
401c248b7dSInki Dae  * Exynos specific crtc structure.
411c248b7dSInki Dae  *
421c248b7dSInki Dae  * @drm_crtc: crtc object.
43b5d2eb3bSJoonyoung Shim  * @drm_plane: pointer of private plane object for this crtc
441c248b7dSInki Dae  * @pipe: a crtc index created at load() with a new crtc object creation
451c248b7dSInki Dae  *	and the crtc object would be set to private->crtc array
461c248b7dSInki Dae  *	to get a crtc object corresponding to this pipe from private->crtc
471c248b7dSInki Dae  *	array when irq interrupt occured. the reason of using this pipe is that
481c248b7dSInki Dae  *	drm framework doesn't support multiple irq yet.
491c248b7dSInki Dae  *	we can refer to the crtc to current hardware interrupt occured through
501c248b7dSInki Dae  *	this pipe value.
51ec05da95SInki Dae  * @dpms: store the crtc dpms value
521c248b7dSInki Dae  */
531c248b7dSInki Dae struct exynos_drm_crtc {
541c248b7dSInki Dae 	struct drm_crtc			drm_crtc;
55b5d2eb3bSJoonyoung Shim 	struct drm_plane		*plane;
561c248b7dSInki Dae 	unsigned int			pipe;
57ec05da95SInki Dae 	unsigned int			dpms;
581c248b7dSInki Dae };
591c248b7dSInki Dae 
601c248b7dSInki Dae static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
611c248b7dSInki Dae {
62ec05da95SInki Dae 	struct drm_device *dev = crtc->dev;
63d2716c89SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
641c248b7dSInki Dae 
65d2716c89SJoonyoung Shim 	DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
66d2716c89SJoonyoung Shim 
67ec05da95SInki Dae 	if (exynos_crtc->dpms == mode) {
68ec05da95SInki Dae 		DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
69ec05da95SInki Dae 		return;
70ec05da95SInki Dae 	}
71ec05da95SInki Dae 
72ec05da95SInki Dae 	mutex_lock(&dev->struct_mutex);
73ec05da95SInki Dae 
74d2716c89SJoonyoung Shim 	switch (mode) {
75d2716c89SJoonyoung Shim 	case DRM_MODE_DPMS_ON:
76ec05da95SInki Dae 		exynos_drm_fn_encoder(crtc, &mode,
77ec05da95SInki Dae 				exynos_drm_encoder_crtc_dpms);
78ec05da95SInki Dae 		exynos_crtc->dpms = mode;
79d2716c89SJoonyoung Shim 		break;
80d2716c89SJoonyoung Shim 	case DRM_MODE_DPMS_STANDBY:
81d2716c89SJoonyoung Shim 	case DRM_MODE_DPMS_SUSPEND:
82d2716c89SJoonyoung Shim 	case DRM_MODE_DPMS_OFF:
83ec05da95SInki Dae 		exynos_drm_fn_encoder(crtc, &mode,
84ec05da95SInki Dae 				exynos_drm_encoder_crtc_dpms);
85ec05da95SInki Dae 		exynos_crtc->dpms = mode;
86d2716c89SJoonyoung Shim 		break;
87d2716c89SJoonyoung Shim 	default:
88ec05da95SInki Dae 		DRM_ERROR("unspecified mode %d\n", mode);
89d2716c89SJoonyoung Shim 		break;
90d2716c89SJoonyoung Shim 	}
91ec05da95SInki Dae 
92ec05da95SInki Dae 	mutex_unlock(&dev->struct_mutex);
931c248b7dSInki Dae }
941c248b7dSInki Dae 
951c248b7dSInki Dae static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
961c248b7dSInki Dae {
971c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
981c248b7dSInki Dae 
991c248b7dSInki Dae 	/* drm framework doesn't check NULL. */
1001c248b7dSInki Dae }
1011c248b7dSInki Dae 
1021c248b7dSInki Dae static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
1031c248b7dSInki Dae {
104d2716c89SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
105d2716c89SJoonyoung Shim 
1061c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1071c248b7dSInki Dae 
108ec05da95SInki Dae 	/*
109ec05da95SInki Dae 	 * when set_crtc is requested from user or at booting time,
110ec05da95SInki Dae 	 * crtc->commit would be called without dpms call so if dpms is
111ec05da95SInki Dae 	 * no power on then crtc->dpms should be called
112ec05da95SInki Dae 	 * with DRM_MODE_DPMS_ON for the hardware power to be on.
113ec05da95SInki Dae 	 */
114ec05da95SInki Dae 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) {
115ec05da95SInki Dae 		int mode = DRM_MODE_DPMS_ON;
116ec05da95SInki Dae 
117ec05da95SInki Dae 		/*
118ec05da95SInki Dae 		 * enable hardware(power on) to all encoders hdmi connected
119ec05da95SInki Dae 		 * to current crtc.
120ec05da95SInki Dae 		 */
121ec05da95SInki Dae 		exynos_drm_crtc_dpms(crtc, mode);
122ec05da95SInki Dae 		/*
123ec05da95SInki Dae 		 * enable dma to all encoders connected to current crtc and
124ec05da95SInki Dae 		 * lcd panel.
125ec05da95SInki Dae 		 */
126ec05da95SInki Dae 		exynos_drm_fn_encoder(crtc, &mode,
127ec05da95SInki Dae 					exynos_drm_encoder_dpms_from_crtc);
128ec05da95SInki Dae 	}
129ec05da95SInki Dae 
1304070d212SJoonyoung Shim 	exynos_plane_commit(exynos_crtc->plane);
1311c248b7dSInki Dae }
1321c248b7dSInki Dae 
1331c248b7dSInki Dae static bool
1341c248b7dSInki Dae exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
135e811f5aeSLaurent Pinchart 			    const struct drm_display_mode *mode,
1361c248b7dSInki Dae 			    struct drm_display_mode *adjusted_mode)
1371c248b7dSInki Dae {
1381c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1391c248b7dSInki Dae 
1401c248b7dSInki Dae 	/* drm framework doesn't check NULL */
1411c248b7dSInki Dae 	return true;
1421c248b7dSInki Dae }
1431c248b7dSInki Dae 
1441c248b7dSInki Dae static int
1451c248b7dSInki Dae exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
1461c248b7dSInki Dae 			  struct drm_display_mode *adjusted_mode, int x, int y,
1471c248b7dSInki Dae 			  struct drm_framebuffer *old_fb)
1481c248b7dSInki Dae {
149aeb29224SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
1504070d212SJoonyoung Shim 	struct drm_plane *plane = exynos_crtc->plane;
1514070d212SJoonyoung Shim 	unsigned int crtc_w;
1524070d212SJoonyoung Shim 	unsigned int crtc_h;
153d249ce02SJoonyoung Shim 	int pipe = exynos_crtc->pipe;
154aeb29224SJoonyoung Shim 	int ret;
155aeb29224SJoonyoung Shim 
1561c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1571c248b7dSInki Dae 
1581de425b0SInki Dae 	/*
1591de425b0SInki Dae 	 * copy the mode data adjusted by mode_fixup() into crtc->mode
1601de425b0SInki Dae 	 * so that hardware can be seet to proper mode.
1611de425b0SInki Dae 	 */
1621de425b0SInki Dae 	memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
1631c248b7dSInki Dae 
1644070d212SJoonyoung Shim 	crtc_w = crtc->fb->width - x;
1654070d212SJoonyoung Shim 	crtc_h = crtc->fb->height - y;
1664070d212SJoonyoung Shim 
1674070d212SJoonyoung Shim 	ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
1684070d212SJoonyoung Shim 				    x, y, crtc_w, crtc_h);
169aeb29224SJoonyoung Shim 	if (ret)
170aeb29224SJoonyoung Shim 		return ret;
171aeb29224SJoonyoung Shim 
1724070d212SJoonyoung Shim 	plane->crtc = crtc;
1734070d212SJoonyoung Shim 	plane->fb = crtc->fb;
1744070d212SJoonyoung Shim 
175d249ce02SJoonyoung Shim 	exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
176aeb29224SJoonyoung Shim 
177aeb29224SJoonyoung Shim 	return 0;
1781c248b7dSInki Dae }
1791c248b7dSInki Dae 
1801c248b7dSInki Dae static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1811c248b7dSInki Dae 					  struct drm_framebuffer *old_fb)
1821c248b7dSInki Dae {
1834070d212SJoonyoung Shim 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
1844070d212SJoonyoung Shim 	struct drm_plane *plane = exynos_crtc->plane;
1854070d212SJoonyoung Shim 	unsigned int crtc_w;
1864070d212SJoonyoung Shim 	unsigned int crtc_h;
1871c248b7dSInki Dae 	int ret;
1881c248b7dSInki Dae 
1891c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
1901c248b7dSInki Dae 
1914070d212SJoonyoung Shim 	crtc_w = crtc->fb->width - x;
1924070d212SJoonyoung Shim 	crtc_h = crtc->fb->height - y;
1934070d212SJoonyoung Shim 
1944070d212SJoonyoung Shim 	ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
1954070d212SJoonyoung Shim 				    x, y, crtc_w, crtc_h);
1961c248b7dSInki Dae 	if (ret)
1971c248b7dSInki Dae 		return ret;
1981c248b7dSInki Dae 
1994070d212SJoonyoung Shim 	exynos_plane_commit(exynos_crtc->plane);
2001c248b7dSInki Dae 
2014070d212SJoonyoung Shim 	return 0;
2021c248b7dSInki Dae }
2031c248b7dSInki Dae 
2041c248b7dSInki Dae static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
2051c248b7dSInki Dae {
2061c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2071c248b7dSInki Dae 	/* drm framework doesn't check NULL */
2081c248b7dSInki Dae }
2091c248b7dSInki Dae 
2101c248b7dSInki Dae static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
2111c248b7dSInki Dae 	.dpms		= exynos_drm_crtc_dpms,
2121c248b7dSInki Dae 	.prepare	= exynos_drm_crtc_prepare,
2131c248b7dSInki Dae 	.commit		= exynos_drm_crtc_commit,
2141c248b7dSInki Dae 	.mode_fixup	= exynos_drm_crtc_mode_fixup,
2151c248b7dSInki Dae 	.mode_set	= exynos_drm_crtc_mode_set,
2161c248b7dSInki Dae 	.mode_set_base	= exynos_drm_crtc_mode_set_base,
2171c248b7dSInki Dae 	.load_lut	= exynos_drm_crtc_load_lut,
2181c248b7dSInki Dae };
2191c248b7dSInki Dae 
2201c248b7dSInki Dae static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
2211c248b7dSInki Dae 				      struct drm_framebuffer *fb,
2221c248b7dSInki Dae 				      struct drm_pending_vblank_event *event)
2231c248b7dSInki Dae {
2241c248b7dSInki Dae 	struct drm_device *dev = crtc->dev;
2251c248b7dSInki Dae 	struct exynos_drm_private *dev_priv = dev->dev_private;
2261c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
2271c248b7dSInki Dae 	struct drm_framebuffer *old_fb = crtc->fb;
2281c248b7dSInki Dae 	int ret = -EINVAL;
2291c248b7dSInki Dae 
2301c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2311c248b7dSInki Dae 
2321c248b7dSInki Dae 	mutex_lock(&dev->struct_mutex);
2331c248b7dSInki Dae 
234ccf4d883SInki Dae 	if (event) {
235ccf4d883SInki Dae 		/*
236ccf4d883SInki Dae 		 * the pipe from user always is 0 so we can set pipe number
237ccf4d883SInki Dae 		 * of current owner to event.
238ccf4d883SInki Dae 		 */
239ccf4d883SInki Dae 		event->pipe = exynos_crtc->pipe;
240ccf4d883SInki Dae 
2411c248b7dSInki Dae 		ret = drm_vblank_get(dev, exynos_crtc->pipe);
2421c248b7dSInki Dae 		if (ret) {
2431c248b7dSInki Dae 			DRM_DEBUG("failed to acquire vblank counter\n");
244ccf4d883SInki Dae 			list_del(&event->base.link);
245ccf4d883SInki Dae 
2461c248b7dSInki Dae 			goto out;
2471c248b7dSInki Dae 		}
2481c248b7dSInki Dae 
249c5614ae3SInki Dae 		list_add_tail(&event->base.link,
250c5614ae3SInki Dae 				&dev_priv->pageflip_event_list);
251c5614ae3SInki Dae 
2521c248b7dSInki Dae 		crtc->fb = fb;
2534070d212SJoonyoung Shim 		ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
2544070d212SJoonyoung Shim 						    NULL);
2551c248b7dSInki Dae 		if (ret) {
2561c248b7dSInki Dae 			crtc->fb = old_fb;
2571c248b7dSInki Dae 			drm_vblank_put(dev, exynos_crtc->pipe);
258ccf4d883SInki Dae 			list_del(&event->base.link);
2591c248b7dSInki Dae 
2601c248b7dSInki Dae 			goto out;
2611c248b7dSInki Dae 		}
2621c248b7dSInki Dae 	}
2631c248b7dSInki Dae out:
2641c248b7dSInki Dae 	mutex_unlock(&dev->struct_mutex);
2651c248b7dSInki Dae 	return ret;
2661c248b7dSInki Dae }
2671c248b7dSInki Dae 
2681c248b7dSInki Dae static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
2691c248b7dSInki Dae {
2701c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
2711c248b7dSInki Dae 	struct exynos_drm_private *private = crtc->dev->dev_private;
2721c248b7dSInki Dae 
2731c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2741c248b7dSInki Dae 
2751c248b7dSInki Dae 	private->crtc[exynos_crtc->pipe] = NULL;
2761c248b7dSInki Dae 
2771c248b7dSInki Dae 	drm_crtc_cleanup(crtc);
2781c248b7dSInki Dae 	kfree(exynos_crtc);
2791c248b7dSInki Dae }
2801c248b7dSInki Dae 
2811c248b7dSInki Dae static struct drm_crtc_funcs exynos_crtc_funcs = {
2821c248b7dSInki Dae 	.set_config	= drm_crtc_helper_set_config,
2831c248b7dSInki Dae 	.page_flip	= exynos_drm_crtc_page_flip,
2841c248b7dSInki Dae 	.destroy	= exynos_drm_crtc_destroy,
2851c248b7dSInki Dae };
2861c248b7dSInki Dae 
2871c248b7dSInki Dae int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
2881c248b7dSInki Dae {
2891c248b7dSInki Dae 	struct exynos_drm_crtc *exynos_crtc;
2901c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
2911c248b7dSInki Dae 	struct drm_crtc *crtc;
2921c248b7dSInki Dae 
2931c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
2941c248b7dSInki Dae 
2951c248b7dSInki Dae 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
2961c248b7dSInki Dae 	if (!exynos_crtc) {
2971c248b7dSInki Dae 		DRM_ERROR("failed to allocate exynos crtc\n");
2981c248b7dSInki Dae 		return -ENOMEM;
2991c248b7dSInki Dae 	}
3001c248b7dSInki Dae 
3011c248b7dSInki Dae 	exynos_crtc->pipe = nr;
302ec05da95SInki Dae 	exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
303b5d2eb3bSJoonyoung Shim 	exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
304b5d2eb3bSJoonyoung Shim 	if (!exynos_crtc->plane) {
305b5d2eb3bSJoonyoung Shim 		kfree(exynos_crtc);
306b5d2eb3bSJoonyoung Shim 		return -ENOMEM;
307b5d2eb3bSJoonyoung Shim 	}
308b5d2eb3bSJoonyoung Shim 
3091c248b7dSInki Dae 	crtc = &exynos_crtc->drm_crtc;
3101c248b7dSInki Dae 
3111c248b7dSInki Dae 	private->crtc[nr] = crtc;
3121c248b7dSInki Dae 
3131c248b7dSInki Dae 	drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
3141c248b7dSInki Dae 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
3151c248b7dSInki Dae 
3161c248b7dSInki Dae 	return 0;
3171c248b7dSInki Dae }
3181c248b7dSInki Dae 
3191c248b7dSInki Dae int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
3201c248b7dSInki Dae {
3211c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
322ec05da95SInki Dae 	struct exynos_drm_crtc *exynos_crtc =
323ec05da95SInki Dae 		to_exynos_crtc(private->crtc[crtc]);
3241c248b7dSInki Dae 
3251c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
3261c248b7dSInki Dae 
327ec05da95SInki Dae 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
328ec05da95SInki Dae 		return -EPERM;
329ec05da95SInki Dae 
3301c248b7dSInki Dae 	exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
3311c248b7dSInki Dae 			exynos_drm_enable_vblank);
3321c248b7dSInki Dae 
3331c248b7dSInki Dae 	return 0;
3341c248b7dSInki Dae }
3351c248b7dSInki Dae 
3361c248b7dSInki Dae void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
3371c248b7dSInki Dae {
3381c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
339ec05da95SInki Dae 	struct exynos_drm_crtc *exynos_crtc =
340ec05da95SInki Dae 		to_exynos_crtc(private->crtc[crtc]);
3411c248b7dSInki Dae 
3421c248b7dSInki Dae 	DRM_DEBUG_KMS("%s\n", __FILE__);
3431c248b7dSInki Dae 
344ec05da95SInki Dae 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
345ec05da95SInki Dae 		return;
346ec05da95SInki Dae 
3471c248b7dSInki Dae 	exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
3481c248b7dSInki Dae 			exynos_drm_disable_vblank);
3491c248b7dSInki Dae }
350