xref: /linux/drivers/gpu/drm/exynos/exynos7_drm_decon.c (revision 1e1d13932283419ddfdb60c73319cc0e753ded5b)
196976c3dSAjay Kumar /* drivers/gpu/drm/exynos/exynos7_drm_decon.c
296976c3dSAjay Kumar  *
396976c3dSAjay Kumar  * Copyright (C) 2014 Samsung Electronics Co.Ltd
496976c3dSAjay Kumar  * Authors:
596976c3dSAjay Kumar  *	Akshu Agarwal <akshua@gmail.com>
696976c3dSAjay Kumar  *	Ajay Kumar <ajaykumar.rs@samsung.com>
796976c3dSAjay Kumar  *
896976c3dSAjay Kumar  * This program is free software; you can redistribute  it and/or modify it
996976c3dSAjay Kumar  * under  the terms of  the GNU General  Public License as published by the
1096976c3dSAjay Kumar  * Free Software Foundation;  either version 2 of the  License, or (at your
1196976c3dSAjay Kumar  * option) any later version.
1296976c3dSAjay Kumar  *
1396976c3dSAjay Kumar  */
1496976c3dSAjay Kumar #include <drm/drmP.h>
1596976c3dSAjay Kumar #include <drm/exynos_drm.h>
1696976c3dSAjay Kumar 
1796976c3dSAjay Kumar #include <linux/clk.h>
1896976c3dSAjay Kumar #include <linux/component.h>
1996976c3dSAjay Kumar #include <linux/kernel.h>
2096976c3dSAjay Kumar #include <linux/of.h>
2196976c3dSAjay Kumar #include <linux/of_address.h>
2296976c3dSAjay Kumar #include <linux/of_device.h>
2396976c3dSAjay Kumar #include <linux/platform_device.h>
2496976c3dSAjay Kumar #include <linux/pm_runtime.h>
2596976c3dSAjay Kumar 
2696976c3dSAjay Kumar #include <video/of_display_timing.h>
2796976c3dSAjay Kumar #include <video/of_videomode.h>
2896976c3dSAjay Kumar #include <video/exynos7_decon.h>
2996976c3dSAjay Kumar 
3096976c3dSAjay Kumar #include "exynos_drm_crtc.h"
317ee14cdcSGustavo Padovan #include "exynos_drm_plane.h"
3296976c3dSAjay Kumar #include "exynos_drm_drv.h"
3396976c3dSAjay Kumar #include "exynos_drm_fbdev.h"
3496976c3dSAjay Kumar #include "exynos_drm_iommu.h"
3596976c3dSAjay Kumar 
3696976c3dSAjay Kumar /*
3796976c3dSAjay Kumar  * DECON stands for Display and Enhancement controller.
3896976c3dSAjay Kumar  */
3996976c3dSAjay Kumar 
4096976c3dSAjay Kumar #define DECON_DEFAULT_FRAMERATE 60
4196976c3dSAjay Kumar #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
4296976c3dSAjay Kumar 
4396976c3dSAjay Kumar #define WINDOWS_NR	2
4496976c3dSAjay Kumar 
4596976c3dSAjay Kumar struct decon_context {
4696976c3dSAjay Kumar 	struct device			*dev;
4796976c3dSAjay Kumar 	struct drm_device		*drm_dev;
4896976c3dSAjay Kumar 	struct exynos_drm_crtc		*crtc;
497ee14cdcSGustavo Padovan 	struct exynos_drm_plane		planes[WINDOWS_NR];
5096976c3dSAjay Kumar 	struct clk			*pclk;
5196976c3dSAjay Kumar 	struct clk			*aclk;
5296976c3dSAjay Kumar 	struct clk			*eclk;
5396976c3dSAjay Kumar 	struct clk			*vclk;
5496976c3dSAjay Kumar 	void __iomem			*regs;
5596976c3dSAjay Kumar 	unsigned int			default_win;
5696976c3dSAjay Kumar 	unsigned long			irq_flags;
5796976c3dSAjay Kumar 	bool				i80_if;
5896976c3dSAjay Kumar 	bool				suspended;
5996976c3dSAjay Kumar 	int				pipe;
6096976c3dSAjay Kumar 	wait_queue_head_t		wait_vsync_queue;
6196976c3dSAjay Kumar 	atomic_t			wait_vsync_event;
6296976c3dSAjay Kumar 
6396976c3dSAjay Kumar 	struct exynos_drm_panel_info panel;
6496976c3dSAjay Kumar 	struct exynos_drm_display *display;
6596976c3dSAjay Kumar };
6696976c3dSAjay Kumar 
6796976c3dSAjay Kumar static const struct of_device_id decon_driver_dt_match[] = {
6896976c3dSAjay Kumar 	{.compatible = "samsung,exynos7-decon"},
6996976c3dSAjay Kumar 	{},
7096976c3dSAjay Kumar };
7196976c3dSAjay Kumar MODULE_DEVICE_TABLE(of, decon_driver_dt_match);
7296976c3dSAjay Kumar 
7396976c3dSAjay Kumar static void decon_wait_for_vblank(struct exynos_drm_crtc *crtc)
7496976c3dSAjay Kumar {
7596976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
7696976c3dSAjay Kumar 
7796976c3dSAjay Kumar 	if (ctx->suspended)
7896976c3dSAjay Kumar 		return;
7996976c3dSAjay Kumar 
8096976c3dSAjay Kumar 	atomic_set(&ctx->wait_vsync_event, 1);
8196976c3dSAjay Kumar 
8296976c3dSAjay Kumar 	/*
8396976c3dSAjay Kumar 	 * wait for DECON to signal VSYNC interrupt or return after
8496976c3dSAjay Kumar 	 * timeout which is set to 50ms (refresh rate of 20).
8596976c3dSAjay Kumar 	 */
8696976c3dSAjay Kumar 	if (!wait_event_timeout(ctx->wait_vsync_queue,
8796976c3dSAjay Kumar 				!atomic_read(&ctx->wait_vsync_event),
8896976c3dSAjay Kumar 				HZ/20))
8996976c3dSAjay Kumar 		DRM_DEBUG_KMS("vblank wait timed out.\n");
9096976c3dSAjay Kumar }
9196976c3dSAjay Kumar 
92fc2e013fSHyungwon Hwang static void decon_clear_channels(struct exynos_drm_crtc *crtc)
9396976c3dSAjay Kumar {
94fc2e013fSHyungwon Hwang 	struct decon_context *ctx = crtc->ctx;
955b1d5bc6STobias Jakobi 	unsigned int win, ch_enabled = 0;
9696976c3dSAjay Kumar 
9796976c3dSAjay Kumar 	DRM_DEBUG_KMS("%s\n", __FILE__);
9896976c3dSAjay Kumar 
9996976c3dSAjay Kumar 	/* Check if any channel is enabled. */
10096976c3dSAjay Kumar 	for (win = 0; win < WINDOWS_NR; win++) {
10196976c3dSAjay Kumar 		u32 val = readl(ctx->regs + WINCON(win));
10296976c3dSAjay Kumar 
10396976c3dSAjay Kumar 		if (val & WINCONx_ENWIN) {
10496976c3dSAjay Kumar 			val &= ~WINCONx_ENWIN;
10596976c3dSAjay Kumar 			writel(val, ctx->regs + WINCON(win));
10696976c3dSAjay Kumar 			ch_enabled = 1;
10796976c3dSAjay Kumar 		}
10896976c3dSAjay Kumar 	}
10996976c3dSAjay Kumar 
11096976c3dSAjay Kumar 	/* Wait for vsync, as disable channel takes effect at next vsync */
11196976c3dSAjay Kumar 	if (ch_enabled) {
11296976c3dSAjay Kumar 		unsigned int state = ctx->suspended;
11396976c3dSAjay Kumar 
11496976c3dSAjay Kumar 		ctx->suspended = 0;
11596976c3dSAjay Kumar 		decon_wait_for_vblank(ctx->crtc);
11696976c3dSAjay Kumar 		ctx->suspended = state;
11796976c3dSAjay Kumar 	}
11896976c3dSAjay Kumar }
11996976c3dSAjay Kumar 
12096976c3dSAjay Kumar static int decon_ctx_initialize(struct decon_context *ctx,
12196976c3dSAjay Kumar 			struct drm_device *drm_dev)
12296976c3dSAjay Kumar {
12396976c3dSAjay Kumar 	struct exynos_drm_private *priv = drm_dev->dev_private;
124fc2e013fSHyungwon Hwang 	int ret;
12596976c3dSAjay Kumar 
12696976c3dSAjay Kumar 	ctx->drm_dev = drm_dev;
12796976c3dSAjay Kumar 	ctx->pipe = priv->pipe++;
12896976c3dSAjay Kumar 
129eb7a3fc7SJoonyoung Shim 	decon_clear_channels(ctx->crtc);
130eb7a3fc7SJoonyoung Shim 
131eb7a3fc7SJoonyoung Shim 	ret = drm_iommu_attach_device(drm_dev, ctx->dev);
132fc2e013fSHyungwon Hwang 	if (ret)
133fc2e013fSHyungwon Hwang 		priv->pipe--;
13496976c3dSAjay Kumar 
13596976c3dSAjay Kumar 	return ret;
13696976c3dSAjay Kumar }
13796976c3dSAjay Kumar 
13896976c3dSAjay Kumar static void decon_ctx_remove(struct decon_context *ctx)
13996976c3dSAjay Kumar {
14096976c3dSAjay Kumar 	/* detach this sub driver from iommu mapping if supported. */
14196976c3dSAjay Kumar 	drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
14296976c3dSAjay Kumar }
14396976c3dSAjay Kumar 
14496976c3dSAjay Kumar static u32 decon_calc_clkdiv(struct decon_context *ctx,
14596976c3dSAjay Kumar 		const struct drm_display_mode *mode)
14696976c3dSAjay Kumar {
14796976c3dSAjay Kumar 	unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
14896976c3dSAjay Kumar 	u32 clkdiv;
14996976c3dSAjay Kumar 
15096976c3dSAjay Kumar 	/* Find the clock divider value that gets us closest to ideal_clk */
15196976c3dSAjay Kumar 	clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->vclk), ideal_clk);
15296976c3dSAjay Kumar 
15396976c3dSAjay Kumar 	return (clkdiv < 0x100) ? clkdiv : 0xff;
15496976c3dSAjay Kumar }
15596976c3dSAjay Kumar 
15696976c3dSAjay Kumar static bool decon_mode_fixup(struct exynos_drm_crtc *crtc,
15796976c3dSAjay Kumar 		const struct drm_display_mode *mode,
15896976c3dSAjay Kumar 		struct drm_display_mode *adjusted_mode)
15996976c3dSAjay Kumar {
16096976c3dSAjay Kumar 	if (adjusted_mode->vrefresh == 0)
16196976c3dSAjay Kumar 		adjusted_mode->vrefresh = DECON_DEFAULT_FRAMERATE;
16296976c3dSAjay Kumar 
16396976c3dSAjay Kumar 	return true;
16496976c3dSAjay Kumar }
16596976c3dSAjay Kumar 
16696976c3dSAjay Kumar static void decon_commit(struct exynos_drm_crtc *crtc)
16796976c3dSAjay Kumar {
16896976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
169020e79deSJoonyoung Shim 	struct drm_display_mode *mode = &crtc->base.state->adjusted_mode;
17096976c3dSAjay Kumar 	u32 val, clkdiv;
17196976c3dSAjay Kumar 
17296976c3dSAjay Kumar 	if (ctx->suspended)
17396976c3dSAjay Kumar 		return;
17496976c3dSAjay Kumar 
17596976c3dSAjay Kumar 	/* nothing to do if we haven't set the mode yet */
17696976c3dSAjay Kumar 	if (mode->htotal == 0 || mode->vtotal == 0)
17796976c3dSAjay Kumar 		return;
17896976c3dSAjay Kumar 
17996976c3dSAjay Kumar 	if (!ctx->i80_if) {
18096976c3dSAjay Kumar 		int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
18196976c3dSAjay Kumar 	      /* setup vertical timing values. */
18296976c3dSAjay Kumar 		vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
18396976c3dSAjay Kumar 		vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
18496976c3dSAjay Kumar 		vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
18596976c3dSAjay Kumar 
18696976c3dSAjay Kumar 		val = VIDTCON0_VBPD(vbpd - 1) | VIDTCON0_VFPD(vfpd - 1);
18796976c3dSAjay Kumar 		writel(val, ctx->regs + VIDTCON0);
18896976c3dSAjay Kumar 
18996976c3dSAjay Kumar 		val = VIDTCON1_VSPW(vsync_len - 1);
19096976c3dSAjay Kumar 		writel(val, ctx->regs + VIDTCON1);
19196976c3dSAjay Kumar 
19296976c3dSAjay Kumar 		/* setup horizontal timing values.  */
19396976c3dSAjay Kumar 		hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
19496976c3dSAjay Kumar 		hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
19596976c3dSAjay Kumar 		hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
19696976c3dSAjay Kumar 
19796976c3dSAjay Kumar 		/* setup horizontal timing values.  */
19896976c3dSAjay Kumar 		val = VIDTCON2_HBPD(hbpd - 1) | VIDTCON2_HFPD(hfpd - 1);
19996976c3dSAjay Kumar 		writel(val, ctx->regs + VIDTCON2);
20096976c3dSAjay Kumar 
20196976c3dSAjay Kumar 		val = VIDTCON3_HSPW(hsync_len - 1);
20296976c3dSAjay Kumar 		writel(val, ctx->regs + VIDTCON3);
20396976c3dSAjay Kumar 	}
20496976c3dSAjay Kumar 
20596976c3dSAjay Kumar 	/* setup horizontal and vertical display size. */
20696976c3dSAjay Kumar 	val = VIDTCON4_LINEVAL(mode->vdisplay - 1) |
20796976c3dSAjay Kumar 	       VIDTCON4_HOZVAL(mode->hdisplay - 1);
20896976c3dSAjay Kumar 	writel(val, ctx->regs + VIDTCON4);
20996976c3dSAjay Kumar 
21096976c3dSAjay Kumar 	writel(mode->vdisplay - 1, ctx->regs + LINECNT_OP_THRESHOLD);
21196976c3dSAjay Kumar 
21296976c3dSAjay Kumar 	/*
21396976c3dSAjay Kumar 	 * fields of register with prefix '_F' would be updated
21496976c3dSAjay Kumar 	 * at vsync(same as dma start)
21596976c3dSAjay Kumar 	 */
21696976c3dSAjay Kumar 	val = VIDCON0_ENVID | VIDCON0_ENVID_F;
21796976c3dSAjay Kumar 	writel(val, ctx->regs + VIDCON0);
21896976c3dSAjay Kumar 
21996976c3dSAjay Kumar 	clkdiv = decon_calc_clkdiv(ctx, mode);
22096976c3dSAjay Kumar 	if (clkdiv > 1) {
22196976c3dSAjay Kumar 		val = VCLKCON1_CLKVAL_NUM_VCLK(clkdiv - 1);
22296976c3dSAjay Kumar 		writel(val, ctx->regs + VCLKCON1);
22396976c3dSAjay Kumar 		writel(val, ctx->regs + VCLKCON2);
22496976c3dSAjay Kumar 	}
22596976c3dSAjay Kumar 
22696976c3dSAjay Kumar 	val = readl(ctx->regs + DECON_UPDATE);
22796976c3dSAjay Kumar 	val |= DECON_UPDATE_STANDALONE_F;
22896976c3dSAjay Kumar 	writel(val, ctx->regs + DECON_UPDATE);
22996976c3dSAjay Kumar }
23096976c3dSAjay Kumar 
23196976c3dSAjay Kumar static int decon_enable_vblank(struct exynos_drm_crtc *crtc)
23296976c3dSAjay Kumar {
23396976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
23496976c3dSAjay Kumar 	u32 val;
23596976c3dSAjay Kumar 
23696976c3dSAjay Kumar 	if (ctx->suspended)
23796976c3dSAjay Kumar 		return -EPERM;
23896976c3dSAjay Kumar 
23996976c3dSAjay Kumar 	if (!test_and_set_bit(0, &ctx->irq_flags)) {
24096976c3dSAjay Kumar 		val = readl(ctx->regs + VIDINTCON0);
24196976c3dSAjay Kumar 
24296976c3dSAjay Kumar 		val |= VIDINTCON0_INT_ENABLE;
24396976c3dSAjay Kumar 
24496976c3dSAjay Kumar 		if (!ctx->i80_if) {
24596976c3dSAjay Kumar 			val |= VIDINTCON0_INT_FRAME;
24696976c3dSAjay Kumar 			val &= ~VIDINTCON0_FRAMESEL0_MASK;
24796976c3dSAjay Kumar 			val |= VIDINTCON0_FRAMESEL0_VSYNC;
24896976c3dSAjay Kumar 		}
24996976c3dSAjay Kumar 
25096976c3dSAjay Kumar 		writel(val, ctx->regs + VIDINTCON0);
25196976c3dSAjay Kumar 	}
25296976c3dSAjay Kumar 
25396976c3dSAjay Kumar 	return 0;
25496976c3dSAjay Kumar }
25596976c3dSAjay Kumar 
25696976c3dSAjay Kumar static void decon_disable_vblank(struct exynos_drm_crtc *crtc)
25796976c3dSAjay Kumar {
25896976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
25996976c3dSAjay Kumar 	u32 val;
26096976c3dSAjay Kumar 
26196976c3dSAjay Kumar 	if (ctx->suspended)
26296976c3dSAjay Kumar 		return;
26396976c3dSAjay Kumar 
26496976c3dSAjay Kumar 	if (test_and_clear_bit(0, &ctx->irq_flags)) {
26596976c3dSAjay Kumar 		val = readl(ctx->regs + VIDINTCON0);
26696976c3dSAjay Kumar 
26796976c3dSAjay Kumar 		val &= ~VIDINTCON0_INT_ENABLE;
26896976c3dSAjay Kumar 		if (!ctx->i80_if)
26996976c3dSAjay Kumar 			val &= ~VIDINTCON0_INT_FRAME;
27096976c3dSAjay Kumar 
27196976c3dSAjay Kumar 		writel(val, ctx->regs + VIDINTCON0);
27296976c3dSAjay Kumar 	}
27396976c3dSAjay Kumar }
27496976c3dSAjay Kumar 
27596976c3dSAjay Kumar static void decon_win_set_pixfmt(struct decon_context *ctx, unsigned int win)
27696976c3dSAjay Kumar {
2777ee14cdcSGustavo Padovan 	struct exynos_drm_plane *plane = &ctx->planes[win];
27896976c3dSAjay Kumar 	unsigned long val;
2797ee14cdcSGustavo Padovan 	int padding;
28096976c3dSAjay Kumar 
28196976c3dSAjay Kumar 	val = readl(ctx->regs + WINCON(win));
28296976c3dSAjay Kumar 	val &= ~WINCONx_BPPMODE_MASK;
28396976c3dSAjay Kumar 
2847ee14cdcSGustavo Padovan 	switch (plane->pixel_format) {
28596976c3dSAjay Kumar 	case DRM_FORMAT_RGB565:
28696976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_16BPP_565;
28796976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
28896976c3dSAjay Kumar 		break;
28996976c3dSAjay Kumar 	case DRM_FORMAT_XRGB8888:
29096976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_24BPP_xRGB;
29196976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
29296976c3dSAjay Kumar 		break;
29396976c3dSAjay Kumar 	case DRM_FORMAT_XBGR8888:
29496976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_24BPP_xBGR;
29596976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
29696976c3dSAjay Kumar 		break;
29796976c3dSAjay Kumar 	case DRM_FORMAT_RGBX8888:
29896976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_24BPP_RGBx;
29996976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
30096976c3dSAjay Kumar 		break;
30196976c3dSAjay Kumar 	case DRM_FORMAT_BGRX8888:
30296976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_24BPP_BGRx;
30396976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
30496976c3dSAjay Kumar 		break;
30596976c3dSAjay Kumar 	case DRM_FORMAT_ARGB8888:
30696976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_32BPP_ARGB | WINCONx_BLD_PIX |
30796976c3dSAjay Kumar 			WINCONx_ALPHA_SEL;
30896976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
30996976c3dSAjay Kumar 		break;
31096976c3dSAjay Kumar 	case DRM_FORMAT_ABGR8888:
31196976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_32BPP_ABGR | WINCONx_BLD_PIX |
31296976c3dSAjay Kumar 			WINCONx_ALPHA_SEL;
31396976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
31496976c3dSAjay Kumar 		break;
31596976c3dSAjay Kumar 	case DRM_FORMAT_RGBA8888:
31696976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_32BPP_RGBA | WINCONx_BLD_PIX |
31796976c3dSAjay Kumar 			WINCONx_ALPHA_SEL;
31896976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
31996976c3dSAjay Kumar 		break;
32096976c3dSAjay Kumar 	case DRM_FORMAT_BGRA8888:
32196976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_32BPP_BGRA | WINCONx_BLD_PIX |
32296976c3dSAjay Kumar 			WINCONx_ALPHA_SEL;
32396976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
32496976c3dSAjay Kumar 		break;
32596976c3dSAjay Kumar 	default:
32696976c3dSAjay Kumar 		DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
32796976c3dSAjay Kumar 
32896976c3dSAjay Kumar 		val |= WINCONx_BPPMODE_24BPP_xRGB;
32996976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_16WORD;
33096976c3dSAjay Kumar 		break;
33196976c3dSAjay Kumar 	}
33296976c3dSAjay Kumar 
3337ee14cdcSGustavo Padovan 	DRM_DEBUG_KMS("bpp = %d\n", plane->bpp);
33496976c3dSAjay Kumar 
33596976c3dSAjay Kumar 	/*
33696976c3dSAjay Kumar 	 * In case of exynos, setting dma-burst to 16Word causes permanent
33796976c3dSAjay Kumar 	 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
33896976c3dSAjay Kumar 	 * switching which is based on plane size is not recommended as
33996976c3dSAjay Kumar 	 * plane size varies a lot towards the end of the screen and rapid
34096976c3dSAjay Kumar 	 * movement causes unstable DMA which results into iommu crash/tear.
34196976c3dSAjay Kumar 	 */
34296976c3dSAjay Kumar 
3437ee14cdcSGustavo Padovan 	padding = (plane->pitch / (plane->bpp >> 3)) - plane->fb_width;
3447ee14cdcSGustavo Padovan 	if (plane->fb_width + padding < MIN_FB_WIDTH_FOR_16WORD_BURST) {
34596976c3dSAjay Kumar 		val &= ~WINCONx_BURSTLEN_MASK;
34696976c3dSAjay Kumar 		val |= WINCONx_BURSTLEN_8WORD;
34796976c3dSAjay Kumar 	}
34896976c3dSAjay Kumar 
34996976c3dSAjay Kumar 	writel(val, ctx->regs + WINCON(win));
35096976c3dSAjay Kumar }
35196976c3dSAjay Kumar 
35296976c3dSAjay Kumar static void decon_win_set_colkey(struct decon_context *ctx, unsigned int win)
35396976c3dSAjay Kumar {
35496976c3dSAjay Kumar 	unsigned int keycon0 = 0, keycon1 = 0;
35596976c3dSAjay Kumar 
35696976c3dSAjay Kumar 	keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
35796976c3dSAjay Kumar 			WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
35896976c3dSAjay Kumar 
35996976c3dSAjay Kumar 	keycon1 = WxKEYCON1_COLVAL(0xffffffff);
36096976c3dSAjay Kumar 
36196976c3dSAjay Kumar 	writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
36296976c3dSAjay Kumar 	writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
36396976c3dSAjay Kumar }
36496976c3dSAjay Kumar 
36596976c3dSAjay Kumar /**
36696976c3dSAjay Kumar  * shadow_protect_win() - disable updating values from shadow registers at vsync
36796976c3dSAjay Kumar  *
36896976c3dSAjay Kumar  * @win: window to protect registers for
36996976c3dSAjay Kumar  * @protect: 1 to protect (disable updates)
37096976c3dSAjay Kumar  */
37196976c3dSAjay Kumar static void decon_shadow_protect_win(struct decon_context *ctx,
3726e2a3b66SGustavo Padovan 				     unsigned int win, bool protect)
37396976c3dSAjay Kumar {
37496976c3dSAjay Kumar 	u32 bits, val;
37596976c3dSAjay Kumar 
37696976c3dSAjay Kumar 	bits = SHADOWCON_WINx_PROTECT(win);
37796976c3dSAjay Kumar 
37896976c3dSAjay Kumar 	val = readl(ctx->regs + SHADOWCON);
37996976c3dSAjay Kumar 	if (protect)
38096976c3dSAjay Kumar 		val |= bits;
38196976c3dSAjay Kumar 	else
38296976c3dSAjay Kumar 		val &= ~bits;
38396976c3dSAjay Kumar 	writel(val, ctx->regs + SHADOWCON);
38496976c3dSAjay Kumar }
38596976c3dSAjay Kumar 
386*1e1d1393SGustavo Padovan static void decon_update_plane(struct exynos_drm_crtc *crtc,
387*1e1d1393SGustavo Padovan 			       struct exynos_drm_plane *plane)
38896976c3dSAjay Kumar {
38996976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
390020e79deSJoonyoung Shim 	struct drm_display_mode *mode = &crtc->base.state->adjusted_mode;
3916e2a3b66SGustavo Padovan 	int padding;
39296976c3dSAjay Kumar 	unsigned long val, alpha;
39396976c3dSAjay Kumar 	unsigned int last_x;
39496976c3dSAjay Kumar 	unsigned int last_y;
395*1e1d1393SGustavo Padovan 	unsigned int win = plane->zpos;
39696976c3dSAjay Kumar 
39796976c3dSAjay Kumar 	if (ctx->suspended)
39896976c3dSAjay Kumar 		return;
39996976c3dSAjay Kumar 
40096976c3dSAjay Kumar 	/*
40196976c3dSAjay Kumar 	 * SHADOWCON/PRTCON register is used for enabling timing.
40296976c3dSAjay Kumar 	 *
40396976c3dSAjay Kumar 	 * for example, once only width value of a register is set,
40496976c3dSAjay Kumar 	 * if the dma is started then decon hardware could malfunction so
40596976c3dSAjay Kumar 	 * with protect window setting, the register fields with prefix '_F'
40696976c3dSAjay Kumar 	 * wouldn't be updated at vsync also but updated once unprotect window
40796976c3dSAjay Kumar 	 * is set.
40896976c3dSAjay Kumar 	 */
40996976c3dSAjay Kumar 
41096976c3dSAjay Kumar 	/* protect windows */
41196976c3dSAjay Kumar 	decon_shadow_protect_win(ctx, win, true);
41296976c3dSAjay Kumar 
41396976c3dSAjay Kumar 	/* buffer start address */
4147ee14cdcSGustavo Padovan 	val = (unsigned long)plane->dma_addr[0];
41596976c3dSAjay Kumar 	writel(val, ctx->regs + VIDW_BUF_START(win));
41696976c3dSAjay Kumar 
4177ee14cdcSGustavo Padovan 	padding = (plane->pitch / (plane->bpp >> 3)) - plane->fb_width;
4187ee14cdcSGustavo Padovan 
41996976c3dSAjay Kumar 	/* buffer size */
4207ee14cdcSGustavo Padovan 	writel(plane->fb_width + padding, ctx->regs + VIDW_WHOLE_X(win));
4217ee14cdcSGustavo Padovan 	writel(plane->fb_height, ctx->regs + VIDW_WHOLE_Y(win));
42296976c3dSAjay Kumar 
42396976c3dSAjay Kumar 	/* offset from the start of the buffer to read */
424cb8a3db2SJoonyoung Shim 	writel(plane->src_x, ctx->regs + VIDW_OFFSET_X(win));
425cb8a3db2SJoonyoung Shim 	writel(plane->src_y, ctx->regs + VIDW_OFFSET_Y(win));
42696976c3dSAjay Kumar 
42796976c3dSAjay Kumar 	DRM_DEBUG_KMS("start addr = 0x%lx\n",
4287ee14cdcSGustavo Padovan 			(unsigned long)val);
42996976c3dSAjay Kumar 	DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
4307ee14cdcSGustavo Padovan 			plane->crtc_width, plane->crtc_height);
43196976c3dSAjay Kumar 
43296976c3dSAjay Kumar 	/*
43396976c3dSAjay Kumar 	 * OSD position.
43496976c3dSAjay Kumar 	 * In case the window layout goes of LCD layout, DECON fails.
43596976c3dSAjay Kumar 	 */
4367ee14cdcSGustavo Padovan 	if ((plane->crtc_x + plane->crtc_width) > mode->hdisplay)
4377ee14cdcSGustavo Padovan 		plane->crtc_x = mode->hdisplay - plane->crtc_width;
4387ee14cdcSGustavo Padovan 	if ((plane->crtc_y + plane->crtc_height) > mode->vdisplay)
4397ee14cdcSGustavo Padovan 		plane->crtc_y = mode->vdisplay - plane->crtc_height;
44096976c3dSAjay Kumar 
4417ee14cdcSGustavo Padovan 	val = VIDOSDxA_TOPLEFT_X(plane->crtc_x) |
4427ee14cdcSGustavo Padovan 		VIDOSDxA_TOPLEFT_Y(plane->crtc_y);
44396976c3dSAjay Kumar 	writel(val, ctx->regs + VIDOSD_A(win));
44496976c3dSAjay Kumar 
4457ee14cdcSGustavo Padovan 	last_x = plane->crtc_x + plane->crtc_width;
44696976c3dSAjay Kumar 	if (last_x)
44796976c3dSAjay Kumar 		last_x--;
4487ee14cdcSGustavo Padovan 	last_y = plane->crtc_y + plane->crtc_height;
44996976c3dSAjay Kumar 	if (last_y)
45096976c3dSAjay Kumar 		last_y--;
45196976c3dSAjay Kumar 
45296976c3dSAjay Kumar 	val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y);
45396976c3dSAjay Kumar 
45496976c3dSAjay Kumar 	writel(val, ctx->regs + VIDOSD_B(win));
45596976c3dSAjay Kumar 
45696976c3dSAjay Kumar 	DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
4577ee14cdcSGustavo Padovan 			plane->crtc_x, plane->crtc_y, last_x, last_y);
45896976c3dSAjay Kumar 
45996976c3dSAjay Kumar 	/* OSD alpha */
46096976c3dSAjay Kumar 	alpha = VIDOSDxC_ALPHA0_R_F(0x0) |
46196976c3dSAjay Kumar 			VIDOSDxC_ALPHA0_G_F(0x0) |
46296976c3dSAjay Kumar 			VIDOSDxC_ALPHA0_B_F(0x0);
46396976c3dSAjay Kumar 
46496976c3dSAjay Kumar 	writel(alpha, ctx->regs + VIDOSD_C(win));
46596976c3dSAjay Kumar 
46696976c3dSAjay Kumar 	alpha = VIDOSDxD_ALPHA1_R_F(0xff) |
46796976c3dSAjay Kumar 			VIDOSDxD_ALPHA1_G_F(0xff) |
46896976c3dSAjay Kumar 			VIDOSDxD_ALPHA1_B_F(0xff);
46996976c3dSAjay Kumar 
47096976c3dSAjay Kumar 	writel(alpha, ctx->regs + VIDOSD_D(win));
47196976c3dSAjay Kumar 
47296976c3dSAjay Kumar 	decon_win_set_pixfmt(ctx, win);
47396976c3dSAjay Kumar 
47496976c3dSAjay Kumar 	/* hardware window 0 doesn't support color key. */
47596976c3dSAjay Kumar 	if (win != 0)
47696976c3dSAjay Kumar 		decon_win_set_colkey(ctx, win);
47796976c3dSAjay Kumar 
47896976c3dSAjay Kumar 	/* wincon */
47996976c3dSAjay Kumar 	val = readl(ctx->regs + WINCON(win));
48096976c3dSAjay Kumar 	val |= WINCONx_TRIPLE_BUF_MODE;
48196976c3dSAjay Kumar 	val |= WINCONx_ENWIN;
48296976c3dSAjay Kumar 	writel(val, ctx->regs + WINCON(win));
48396976c3dSAjay Kumar 
48496976c3dSAjay Kumar 	/* Enable DMA channel and unprotect windows */
48596976c3dSAjay Kumar 	decon_shadow_protect_win(ctx, win, false);
48696976c3dSAjay Kumar 
48796976c3dSAjay Kumar 	val = readl(ctx->regs + DECON_UPDATE);
48896976c3dSAjay Kumar 	val |= DECON_UPDATE_STANDALONE_F;
48996976c3dSAjay Kumar 	writel(val, ctx->regs + DECON_UPDATE);
49096976c3dSAjay Kumar }
49196976c3dSAjay Kumar 
492*1e1d1393SGustavo Padovan static void decon_disable_plane(struct exynos_drm_crtc *crtc,
493*1e1d1393SGustavo Padovan 				struct exynos_drm_plane *plane)
49496976c3dSAjay Kumar {
49596976c3dSAjay Kumar 	struct decon_context *ctx = crtc->ctx;
496*1e1d1393SGustavo Padovan 	unsigned int win = plane->zpos;
49796976c3dSAjay Kumar 	u32 val;
49896976c3dSAjay Kumar 
499c329f667SJoonyoung Shim 	if (ctx->suspended)
50096976c3dSAjay Kumar 		return;
50196976c3dSAjay Kumar 
50296976c3dSAjay Kumar 	/* protect windows */
50396976c3dSAjay Kumar 	decon_shadow_protect_win(ctx, win, true);
50496976c3dSAjay Kumar 
50596976c3dSAjay Kumar 	/* wincon */
50696976c3dSAjay Kumar 	val = readl(ctx->regs + WINCON(win));
50796976c3dSAjay Kumar 	val &= ~WINCONx_ENWIN;
50896976c3dSAjay Kumar 	writel(val, ctx->regs + WINCON(win));
50996976c3dSAjay Kumar 
51096976c3dSAjay Kumar 	/* unprotect windows */
51196976c3dSAjay Kumar 	decon_shadow_protect_win(ctx, win, false);
51296976c3dSAjay Kumar 
51396976c3dSAjay Kumar 	val = readl(ctx->regs + DECON_UPDATE);
51496976c3dSAjay Kumar 	val |= DECON_UPDATE_STANDALONE_F;
51596976c3dSAjay Kumar 	writel(val, ctx->regs + DECON_UPDATE);
51696976c3dSAjay Kumar }
51796976c3dSAjay Kumar 
51896976c3dSAjay Kumar static void decon_init(struct decon_context *ctx)
51996976c3dSAjay Kumar {
52096976c3dSAjay Kumar 	u32 val;
52196976c3dSAjay Kumar 
52296976c3dSAjay Kumar 	writel(VIDCON0_SWRESET, ctx->regs + VIDCON0);
52396976c3dSAjay Kumar 
52496976c3dSAjay Kumar 	val = VIDOUTCON0_DISP_IF_0_ON;
52596976c3dSAjay Kumar 	if (!ctx->i80_if)
52696976c3dSAjay Kumar 		val |= VIDOUTCON0_RGBIF;
52796976c3dSAjay Kumar 	writel(val, ctx->regs + VIDOUTCON0);
52896976c3dSAjay Kumar 
52996976c3dSAjay Kumar 	writel(VCLKCON0_CLKVALUP | VCLKCON0_VCLKFREE, ctx->regs + VCLKCON0);
53096976c3dSAjay Kumar 
53196976c3dSAjay Kumar 	if (!ctx->i80_if)
53296976c3dSAjay Kumar 		writel(VIDCON1_VCLK_HOLD, ctx->regs + VIDCON1(0));
53396976c3dSAjay Kumar }
53496976c3dSAjay Kumar 
5353cecda03SGustavo Padovan static void decon_enable(struct exynos_drm_crtc *crtc)
53696976c3dSAjay Kumar {
5373cecda03SGustavo Padovan 	struct decon_context *ctx = crtc->ctx;
53838000dbbSGustavo Padovan 	int ret;
53996976c3dSAjay Kumar 
54096976c3dSAjay Kumar 	if (!ctx->suspended)
5413cecda03SGustavo Padovan 		return;
54296976c3dSAjay Kumar 
54396976c3dSAjay Kumar 	ctx->suspended = false;
54496976c3dSAjay Kumar 
54596976c3dSAjay Kumar 	pm_runtime_get_sync(ctx->dev);
54696976c3dSAjay Kumar 
54738000dbbSGustavo Padovan 	ret = clk_prepare_enable(ctx->pclk);
54838000dbbSGustavo Padovan 	if (ret < 0) {
54938000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the pclk [%d]\n", ret);
55038000dbbSGustavo Padovan 		return;
55138000dbbSGustavo Padovan 	}
55238000dbbSGustavo Padovan 
55338000dbbSGustavo Padovan 	ret = clk_prepare_enable(ctx->aclk);
55438000dbbSGustavo Padovan 	if (ret < 0) {
55538000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the aclk [%d]\n", ret);
55638000dbbSGustavo Padovan 		return;
55738000dbbSGustavo Padovan 	}
55838000dbbSGustavo Padovan 
55938000dbbSGustavo Padovan 	ret = clk_prepare_enable(ctx->eclk);
56038000dbbSGustavo Padovan 	if  (ret < 0) {
56138000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the eclk [%d]\n", ret);
56238000dbbSGustavo Padovan 		return;
56338000dbbSGustavo Padovan 	}
56438000dbbSGustavo Padovan 
56538000dbbSGustavo Padovan 	ret = clk_prepare_enable(ctx->vclk);
56638000dbbSGustavo Padovan 	if  (ret < 0) {
56738000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the vclk [%d]\n", ret);
56838000dbbSGustavo Padovan 		return;
56938000dbbSGustavo Padovan 	}
57096976c3dSAjay Kumar 
57196976c3dSAjay Kumar 	decon_init(ctx);
57296976c3dSAjay Kumar 
57396976c3dSAjay Kumar 	/* if vblank was enabled status, enable it again. */
5743cecda03SGustavo Padovan 	if (test_and_clear_bit(0, &ctx->irq_flags))
5753cecda03SGustavo Padovan 		decon_enable_vblank(ctx->crtc);
57696976c3dSAjay Kumar 
577c329f667SJoonyoung Shim 	decon_commit(ctx->crtc);
57896976c3dSAjay Kumar }
57996976c3dSAjay Kumar 
5803cecda03SGustavo Padovan static void decon_disable(struct exynos_drm_crtc *crtc)
58196976c3dSAjay Kumar {
5823cecda03SGustavo Padovan 	struct decon_context *ctx = crtc->ctx;
583c329f667SJoonyoung Shim 	int i;
5843cecda03SGustavo Padovan 
58596976c3dSAjay Kumar 	if (ctx->suspended)
5863cecda03SGustavo Padovan 		return;
58796976c3dSAjay Kumar 
58896976c3dSAjay Kumar 	/*
58996976c3dSAjay Kumar 	 * We need to make sure that all windows are disabled before we
59096976c3dSAjay Kumar 	 * suspend that connector. Otherwise we might try to scan from
59196976c3dSAjay Kumar 	 * a destroyed buffer later.
59296976c3dSAjay Kumar 	 */
593c329f667SJoonyoung Shim 	for (i = 0; i < WINDOWS_NR; i++)
594*1e1d1393SGustavo Padovan 		decon_disable_plane(crtc, &ctx->planes[i]);
59596976c3dSAjay Kumar 
59696976c3dSAjay Kumar 	clk_disable_unprepare(ctx->vclk);
59796976c3dSAjay Kumar 	clk_disable_unprepare(ctx->eclk);
59896976c3dSAjay Kumar 	clk_disable_unprepare(ctx->aclk);
59996976c3dSAjay Kumar 	clk_disable_unprepare(ctx->pclk);
60096976c3dSAjay Kumar 
60196976c3dSAjay Kumar 	pm_runtime_put_sync(ctx->dev);
60296976c3dSAjay Kumar 
60396976c3dSAjay Kumar 	ctx->suspended = true;
60496976c3dSAjay Kumar }
60596976c3dSAjay Kumar 
606f3aaf762SKrzysztof Kozlowski static const struct exynos_drm_crtc_ops decon_crtc_ops = {
6073cecda03SGustavo Padovan 	.enable = decon_enable,
6083cecda03SGustavo Padovan 	.disable = decon_disable,
60996976c3dSAjay Kumar 	.mode_fixup = decon_mode_fixup,
61096976c3dSAjay Kumar 	.commit = decon_commit,
61196976c3dSAjay Kumar 	.enable_vblank = decon_enable_vblank,
61296976c3dSAjay Kumar 	.disable_vblank = decon_disable_vblank,
61396976c3dSAjay Kumar 	.wait_for_vblank = decon_wait_for_vblank,
6149cc7610aSGustavo Padovan 	.update_plane = decon_update_plane,
6159cc7610aSGustavo Padovan 	.disable_plane = decon_disable_plane,
61696976c3dSAjay Kumar };
61796976c3dSAjay Kumar 
61896976c3dSAjay Kumar 
61996976c3dSAjay Kumar static irqreturn_t decon_irq_handler(int irq, void *dev_id)
62096976c3dSAjay Kumar {
62196976c3dSAjay Kumar 	struct decon_context *ctx = (struct decon_context *)dev_id;
62296976c3dSAjay Kumar 	u32 val, clear_bit;
62396976c3dSAjay Kumar 
62496976c3dSAjay Kumar 	val = readl(ctx->regs + VIDINTCON1);
62596976c3dSAjay Kumar 
62696976c3dSAjay Kumar 	clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
62796976c3dSAjay Kumar 	if (val & clear_bit)
62896976c3dSAjay Kumar 		writel(clear_bit, ctx->regs + VIDINTCON1);
62996976c3dSAjay Kumar 
63096976c3dSAjay Kumar 	/* check the crtc is detached already from encoder */
63196976c3dSAjay Kumar 	if (ctx->pipe < 0 || !ctx->drm_dev)
63296976c3dSAjay Kumar 		goto out;
63396976c3dSAjay Kumar 
63496976c3dSAjay Kumar 	if (!ctx->i80_if) {
635eafd540aSGustavo Padovan 		drm_crtc_handle_vblank(&ctx->crtc->base);
636eafd540aSGustavo Padovan 		exynos_drm_crtc_finish_pageflip(ctx->crtc);
63796976c3dSAjay Kumar 
63896976c3dSAjay Kumar 		/* set wait vsync event to zero and wake up queue. */
63996976c3dSAjay Kumar 		if (atomic_read(&ctx->wait_vsync_event)) {
64096976c3dSAjay Kumar 			atomic_set(&ctx->wait_vsync_event, 0);
64196976c3dSAjay Kumar 			wake_up(&ctx->wait_vsync_queue);
64296976c3dSAjay Kumar 		}
64396976c3dSAjay Kumar 	}
64496976c3dSAjay Kumar out:
64596976c3dSAjay Kumar 	return IRQ_HANDLED;
64696976c3dSAjay Kumar }
64796976c3dSAjay Kumar 
64896976c3dSAjay Kumar static int decon_bind(struct device *dev, struct device *master, void *data)
64996976c3dSAjay Kumar {
65096976c3dSAjay Kumar 	struct decon_context *ctx = dev_get_drvdata(dev);
65196976c3dSAjay Kumar 	struct drm_device *drm_dev = data;
6527ee14cdcSGustavo Padovan 	struct exynos_drm_plane *exynos_plane;
6537ee14cdcSGustavo Padovan 	enum drm_plane_type type;
6546e2a3b66SGustavo Padovan 	unsigned int zpos;
6556e2a3b66SGustavo Padovan 	int ret;
65696976c3dSAjay Kumar 
65796976c3dSAjay Kumar 	ret = decon_ctx_initialize(ctx, drm_dev);
65896976c3dSAjay Kumar 	if (ret) {
65996976c3dSAjay Kumar 		DRM_ERROR("decon_ctx_initialize failed.\n");
66096976c3dSAjay Kumar 		return ret;
66196976c3dSAjay Kumar 	}
66296976c3dSAjay Kumar 
6637ee14cdcSGustavo Padovan 	for (zpos = 0; zpos < WINDOWS_NR; zpos++) {
6647ee14cdcSGustavo Padovan 		type = (zpos == ctx->default_win) ? DRM_PLANE_TYPE_PRIMARY :
6657ee14cdcSGustavo Padovan 						DRM_PLANE_TYPE_OVERLAY;
6667ee14cdcSGustavo Padovan 		ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
6676e2a3b66SGustavo Padovan 					1 << ctx->pipe, type, zpos);
6687ee14cdcSGustavo Padovan 		if (ret)
6697ee14cdcSGustavo Padovan 			return ret;
6707ee14cdcSGustavo Padovan 	}
6717ee14cdcSGustavo Padovan 
6727ee14cdcSGustavo Padovan 	exynos_plane = &ctx->planes[ctx->default_win];
6737ee14cdcSGustavo Padovan 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
6747ee14cdcSGustavo Padovan 					   ctx->pipe, EXYNOS_DISPLAY_TYPE_LCD,
67596976c3dSAjay Kumar 					   &decon_crtc_ops, ctx);
67696976c3dSAjay Kumar 	if (IS_ERR(ctx->crtc)) {
67796976c3dSAjay Kumar 		decon_ctx_remove(ctx);
67896976c3dSAjay Kumar 		return PTR_ERR(ctx->crtc);
67996976c3dSAjay Kumar 	}
68096976c3dSAjay Kumar 
68196976c3dSAjay Kumar 	if (ctx->display)
68296976c3dSAjay Kumar 		exynos_drm_create_enc_conn(drm_dev, ctx->display);
68396976c3dSAjay Kumar 
68496976c3dSAjay Kumar 	return 0;
68596976c3dSAjay Kumar 
68696976c3dSAjay Kumar }
68796976c3dSAjay Kumar 
68896976c3dSAjay Kumar static void decon_unbind(struct device *dev, struct device *master,
68996976c3dSAjay Kumar 			void *data)
69096976c3dSAjay Kumar {
69196976c3dSAjay Kumar 	struct decon_context *ctx = dev_get_drvdata(dev);
69296976c3dSAjay Kumar 
6933cecda03SGustavo Padovan 	decon_disable(ctx->crtc);
69496976c3dSAjay Kumar 
69596976c3dSAjay Kumar 	if (ctx->display)
69696976c3dSAjay Kumar 		exynos_dpi_remove(ctx->display);
69796976c3dSAjay Kumar 
69896976c3dSAjay Kumar 	decon_ctx_remove(ctx);
69996976c3dSAjay Kumar }
70096976c3dSAjay Kumar 
70196976c3dSAjay Kumar static const struct component_ops decon_component_ops = {
70296976c3dSAjay Kumar 	.bind	= decon_bind,
70396976c3dSAjay Kumar 	.unbind = decon_unbind,
70496976c3dSAjay Kumar };
70596976c3dSAjay Kumar 
70696976c3dSAjay Kumar static int decon_probe(struct platform_device *pdev)
70796976c3dSAjay Kumar {
70896976c3dSAjay Kumar 	struct device *dev = &pdev->dev;
70996976c3dSAjay Kumar 	struct decon_context *ctx;
71096976c3dSAjay Kumar 	struct device_node *i80_if_timings;
71196976c3dSAjay Kumar 	struct resource *res;
71296976c3dSAjay Kumar 	int ret;
71396976c3dSAjay Kumar 
71496976c3dSAjay Kumar 	if (!dev->of_node)
71596976c3dSAjay Kumar 		return -ENODEV;
71696976c3dSAjay Kumar 
71796976c3dSAjay Kumar 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
71896976c3dSAjay Kumar 	if (!ctx)
71996976c3dSAjay Kumar 		return -ENOMEM;
72096976c3dSAjay Kumar 
72196976c3dSAjay Kumar 	ctx->dev = dev;
72296976c3dSAjay Kumar 	ctx->suspended = true;
72396976c3dSAjay Kumar 
72496976c3dSAjay Kumar 	i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
72596976c3dSAjay Kumar 	if (i80_if_timings)
72696976c3dSAjay Kumar 		ctx->i80_if = true;
72796976c3dSAjay Kumar 	of_node_put(i80_if_timings);
72896976c3dSAjay Kumar 
72996976c3dSAjay Kumar 	ctx->regs = of_iomap(dev->of_node, 0);
73086650408SAndrzej Hajda 	if (!ctx->regs)
73186650408SAndrzej Hajda 		return -ENOMEM;
73296976c3dSAjay Kumar 
73396976c3dSAjay Kumar 	ctx->pclk = devm_clk_get(dev, "pclk_decon0");
73496976c3dSAjay Kumar 	if (IS_ERR(ctx->pclk)) {
73596976c3dSAjay Kumar 		dev_err(dev, "failed to get bus clock pclk\n");
73696976c3dSAjay Kumar 		ret = PTR_ERR(ctx->pclk);
73796976c3dSAjay Kumar 		goto err_iounmap;
73896976c3dSAjay Kumar 	}
73996976c3dSAjay Kumar 
74096976c3dSAjay Kumar 	ctx->aclk = devm_clk_get(dev, "aclk_decon0");
74196976c3dSAjay Kumar 	if (IS_ERR(ctx->aclk)) {
74296976c3dSAjay Kumar 		dev_err(dev, "failed to get bus clock aclk\n");
74396976c3dSAjay Kumar 		ret = PTR_ERR(ctx->aclk);
74496976c3dSAjay Kumar 		goto err_iounmap;
74596976c3dSAjay Kumar 	}
74696976c3dSAjay Kumar 
74796976c3dSAjay Kumar 	ctx->eclk = devm_clk_get(dev, "decon0_eclk");
74896976c3dSAjay Kumar 	if (IS_ERR(ctx->eclk)) {
74996976c3dSAjay Kumar 		dev_err(dev, "failed to get eclock\n");
75096976c3dSAjay Kumar 		ret = PTR_ERR(ctx->eclk);
75196976c3dSAjay Kumar 		goto err_iounmap;
75296976c3dSAjay Kumar 	}
75396976c3dSAjay Kumar 
75496976c3dSAjay Kumar 	ctx->vclk = devm_clk_get(dev, "decon0_vclk");
75596976c3dSAjay Kumar 	if (IS_ERR(ctx->vclk)) {
75696976c3dSAjay Kumar 		dev_err(dev, "failed to get vclock\n");
75796976c3dSAjay Kumar 		ret = PTR_ERR(ctx->vclk);
75896976c3dSAjay Kumar 		goto err_iounmap;
75996976c3dSAjay Kumar 	}
76096976c3dSAjay Kumar 
76196976c3dSAjay Kumar 	res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
76296976c3dSAjay Kumar 					   ctx->i80_if ? "lcd_sys" : "vsync");
76396976c3dSAjay Kumar 	if (!res) {
76496976c3dSAjay Kumar 		dev_err(dev, "irq request failed.\n");
76596976c3dSAjay Kumar 		ret = -ENXIO;
76696976c3dSAjay Kumar 		goto err_iounmap;
76796976c3dSAjay Kumar 	}
76896976c3dSAjay Kumar 
76996976c3dSAjay Kumar 	ret = devm_request_irq(dev, res->start, decon_irq_handler,
77096976c3dSAjay Kumar 							0, "drm_decon", ctx);
77196976c3dSAjay Kumar 	if (ret) {
77296976c3dSAjay Kumar 		dev_err(dev, "irq request failed.\n");
77396976c3dSAjay Kumar 		goto err_iounmap;
77496976c3dSAjay Kumar 	}
77596976c3dSAjay Kumar 
77696976c3dSAjay Kumar 	init_waitqueue_head(&ctx->wait_vsync_queue);
77796976c3dSAjay Kumar 	atomic_set(&ctx->wait_vsync_event, 0);
77896976c3dSAjay Kumar 
77996976c3dSAjay Kumar 	platform_set_drvdata(pdev, ctx);
78096976c3dSAjay Kumar 
78196976c3dSAjay Kumar 	ctx->display = exynos_dpi_probe(dev);
78296976c3dSAjay Kumar 	if (IS_ERR(ctx->display)) {
78396976c3dSAjay Kumar 		ret = PTR_ERR(ctx->display);
78496976c3dSAjay Kumar 		goto err_iounmap;
78596976c3dSAjay Kumar 	}
78696976c3dSAjay Kumar 
78796976c3dSAjay Kumar 	pm_runtime_enable(dev);
78896976c3dSAjay Kumar 
78996976c3dSAjay Kumar 	ret = component_add(dev, &decon_component_ops);
79096976c3dSAjay Kumar 	if (ret)
79196976c3dSAjay Kumar 		goto err_disable_pm_runtime;
79296976c3dSAjay Kumar 
79396976c3dSAjay Kumar 	return ret;
79496976c3dSAjay Kumar 
79596976c3dSAjay Kumar err_disable_pm_runtime:
79696976c3dSAjay Kumar 	pm_runtime_disable(dev);
79796976c3dSAjay Kumar 
79896976c3dSAjay Kumar err_iounmap:
79996976c3dSAjay Kumar 	iounmap(ctx->regs);
80096976c3dSAjay Kumar 
80196976c3dSAjay Kumar 	return ret;
80296976c3dSAjay Kumar }
80396976c3dSAjay Kumar 
80496976c3dSAjay Kumar static int decon_remove(struct platform_device *pdev)
80596976c3dSAjay Kumar {
80696976c3dSAjay Kumar 	struct decon_context *ctx = dev_get_drvdata(&pdev->dev);
80796976c3dSAjay Kumar 
80896976c3dSAjay Kumar 	pm_runtime_disable(&pdev->dev);
80996976c3dSAjay Kumar 
81096976c3dSAjay Kumar 	iounmap(ctx->regs);
81196976c3dSAjay Kumar 
81296976c3dSAjay Kumar 	component_del(&pdev->dev, &decon_component_ops);
81396976c3dSAjay Kumar 
81496976c3dSAjay Kumar 	return 0;
81596976c3dSAjay Kumar }
81696976c3dSAjay Kumar 
81796976c3dSAjay Kumar struct platform_driver decon_driver = {
81896976c3dSAjay Kumar 	.probe		= decon_probe,
81996976c3dSAjay Kumar 	.remove		= decon_remove,
82096976c3dSAjay Kumar 	.driver		= {
82196976c3dSAjay Kumar 		.name	= "exynos-decon",
82296976c3dSAjay Kumar 		.of_match_table = decon_driver_dt_match,
82396976c3dSAjay Kumar 	},
82496976c3dSAjay Kumar };
825