xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 5acd351427361131c583dfb11c7bf4c364c98a9b)
1dee8268fSThierry Reding /*
2dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
3dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
4dee8268fSThierry Reding  *
5dee8268fSThierry Reding  * This program is free software; you can redistribute it and/or modify
6dee8268fSThierry Reding  * it under the terms of the GNU General Public License version 2 as
7dee8268fSThierry Reding  * published by the Free Software Foundation.
8dee8268fSThierry Reding  */
9dee8268fSThierry Reding 
10dee8268fSThierry Reding #include <linux/clk.h>
11dee8268fSThierry Reding #include <linux/debugfs.h>
12df06b759SThierry Reding #include <linux/iommu.h>
13b9ff7aeaSThierry Reding #include <linux/of_device.h>
1433a8eb8dSThierry Reding #include <linux/pm_runtime.h>
15ca48080aSStephen Warren #include <linux/reset.h>
16dee8268fSThierry Reding 
179c012700SThierry Reding #include <soc/tegra/pmc.h>
189c012700SThierry Reding 
19dee8268fSThierry Reding #include "dc.h"
20dee8268fSThierry Reding #include "drm.h"
21dee8268fSThierry Reding #include "gem.h"
22*5acd3514SThierry Reding #include "plane.h"
23dee8268fSThierry Reding 
249d44189fSThierry Reding #include <drm/drm_atomic.h>
254aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
263cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
273cb9ae4fSDaniel Vetter 
28791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
29791ddb1eSThierry Reding {
30791ddb1eSThierry Reding 	stats->frames = 0;
31791ddb1eSThierry Reding 	stats->vblank = 0;
32791ddb1eSThierry Reding 	stats->underflow = 0;
33791ddb1eSThierry Reding 	stats->overflow = 0;
34791ddb1eSThierry Reding }
35791ddb1eSThierry Reding 
36d700ba7aSThierry Reding /*
3786df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
3886df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
3986df256fSThierry Reding  * active copy of some registers.
4086df256fSThierry Reding  */
4186df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
4286df256fSThierry Reding {
4386df256fSThierry Reding 	unsigned long flags;
4486df256fSThierry Reding 	u32 value;
4586df256fSThierry Reding 
4686df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
4786df256fSThierry Reding 
4886df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
4986df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
5086df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
5186df256fSThierry Reding 
5286df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
5386df256fSThierry Reding 	return value;
5486df256fSThierry Reding }
5586df256fSThierry Reding 
5686df256fSThierry Reding /*
57d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
58d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
59d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
60d700ba7aSThierry Reding  * on the next frame boundary otherwise.
61d700ba7aSThierry Reding  *
62d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
63d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
64d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
65d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
66d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
67d700ba7aSThierry Reding  */
6862b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
69205d48edSThierry Reding {
70205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
71205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
72205d48edSThierry Reding }
73205d48edSThierry Reding 
7410288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
7510288eeaSThierry Reding 				  unsigned int bpp)
7610288eeaSThierry Reding {
7710288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
7810288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
7910288eeaSThierry Reding 	u32 dda_inc;
8010288eeaSThierry Reding 	int max;
8110288eeaSThierry Reding 
8210288eeaSThierry Reding 	if (v)
8310288eeaSThierry Reding 		max = 15;
8410288eeaSThierry Reding 	else {
8510288eeaSThierry Reding 		switch (bpp) {
8610288eeaSThierry Reding 		case 2:
8710288eeaSThierry Reding 			max = 8;
8810288eeaSThierry Reding 			break;
8910288eeaSThierry Reding 
9010288eeaSThierry Reding 		default:
9110288eeaSThierry Reding 			WARN_ON_ONCE(1);
9210288eeaSThierry Reding 			/* fallthrough */
9310288eeaSThierry Reding 		case 4:
9410288eeaSThierry Reding 			max = 4;
9510288eeaSThierry Reding 			break;
9610288eeaSThierry Reding 		}
9710288eeaSThierry Reding 	}
9810288eeaSThierry Reding 
9910288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
10010288eeaSThierry Reding 	inf.full -= dfixed_const(1);
10110288eeaSThierry Reding 
10210288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
10310288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
10410288eeaSThierry Reding 
10510288eeaSThierry Reding 	return dda_inc;
10610288eeaSThierry Reding }
10710288eeaSThierry Reding 
10810288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
10910288eeaSThierry Reding {
11010288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
11110288eeaSThierry Reding 	return dfixed_frac(inf);
11210288eeaSThierry Reding }
11310288eeaSThierry Reding 
1144aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
11510288eeaSThierry Reding 				  const struct tegra_dc_window *window)
11610288eeaSThierry Reding {
11710288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
11893396d0fSSean Paul 	unsigned long value, flags;
11910288eeaSThierry Reding 	bool yuv, planar;
12010288eeaSThierry Reding 
12110288eeaSThierry Reding 	/*
12210288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
12310288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
12410288eeaSThierry Reding 	 */
125*5acd3514SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar);
12610288eeaSThierry Reding 	if (!yuv)
12710288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
12810288eeaSThierry Reding 	else
12910288eeaSThierry Reding 		bpp = planar ? 1 : 2;
13010288eeaSThierry Reding 
13193396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
13293396d0fSSean Paul 
13310288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
13410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
13510288eeaSThierry Reding 
13610288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
13710288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
13810288eeaSThierry Reding 
13910288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
14010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
14110288eeaSThierry Reding 
14210288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
14310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
14410288eeaSThierry Reding 
14510288eeaSThierry Reding 	h_offset = window->src.x * bpp;
14610288eeaSThierry Reding 	v_offset = window->src.y;
14710288eeaSThierry Reding 	h_size = window->src.w * bpp;
14810288eeaSThierry Reding 	v_size = window->src.h;
14910288eeaSThierry Reding 
15010288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
15110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
15210288eeaSThierry Reding 
15310288eeaSThierry Reding 	/*
15410288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
15510288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
15610288eeaSThierry Reding 	 */
15710288eeaSThierry Reding 	if (yuv && planar)
15810288eeaSThierry Reding 		bpp = 2;
15910288eeaSThierry Reding 
16010288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
16110288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
16210288eeaSThierry Reding 
16310288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
16410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
16510288eeaSThierry Reding 
16610288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
16710288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
16810288eeaSThierry Reding 
16910288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
17010288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
17110288eeaSThierry Reding 
17210288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
17310288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
17410288eeaSThierry Reding 
17510288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
17610288eeaSThierry Reding 
17710288eeaSThierry Reding 	if (yuv && planar) {
17810288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
17910288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
18010288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
18110288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
18210288eeaSThierry Reding 	} else {
18310288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
18410288eeaSThierry Reding 	}
18510288eeaSThierry Reding 
18610288eeaSThierry Reding 	if (window->bottom_up)
18710288eeaSThierry Reding 		v_offset += window->src.h - 1;
18810288eeaSThierry Reding 
18910288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
19010288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
19110288eeaSThierry Reding 
192c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
193c134f019SThierry Reding 		unsigned long height = window->tiling.value;
194c134f019SThierry Reding 
195c134f019SThierry Reding 		switch (window->tiling.mode) {
196c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
197c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
198c134f019SThierry Reding 			break;
199c134f019SThierry Reding 
200c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
201c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
202c134f019SThierry Reding 			break;
203c134f019SThierry Reding 
204c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
205c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
206c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
207c134f019SThierry Reding 			break;
208c134f019SThierry Reding 		}
209c134f019SThierry Reding 
210c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
21110288eeaSThierry Reding 	} else {
212c134f019SThierry Reding 		switch (window->tiling.mode) {
213c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
21410288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
21510288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
216c134f019SThierry Reding 			break;
217c134f019SThierry Reding 
218c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
219c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
220c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
221c134f019SThierry Reding 			break;
222c134f019SThierry Reding 
223c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
2244aa3df71SThierry Reding 			/*
2254aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
2264aa3df71SThierry Reding 			 * will already have filtered it out.
2274aa3df71SThierry Reding 			 */
2284aa3df71SThierry Reding 			break;
22910288eeaSThierry Reding 		}
23010288eeaSThierry Reding 
23110288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
232c134f019SThierry Reding 	}
23310288eeaSThierry Reding 
23410288eeaSThierry Reding 	value = WIN_ENABLE;
23510288eeaSThierry Reding 
23610288eeaSThierry Reding 	if (yuv) {
23710288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
23810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
23910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
24010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
24110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
24210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
24310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
24410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
24510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
24610288eeaSThierry Reding 
24710288eeaSThierry Reding 		value |= CSC_ENABLE;
24810288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
24910288eeaSThierry Reding 		value |= COLOR_EXPAND;
25010288eeaSThierry Reding 	}
25110288eeaSThierry Reding 
25210288eeaSThierry Reding 	if (window->bottom_up)
25310288eeaSThierry Reding 		value |= V_DIRECTION;
25410288eeaSThierry Reding 
25510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
25610288eeaSThierry Reding 
25710288eeaSThierry Reding 	/*
25810288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
25910288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
26010288eeaSThierry Reding 	 */
26110288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
26210288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
26310288eeaSThierry Reding 
26410288eeaSThierry Reding 	switch (index) {
26510288eeaSThierry Reding 	case 0:
26610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
26710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
26810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
26910288eeaSThierry Reding 		break;
27010288eeaSThierry Reding 
27110288eeaSThierry Reding 	case 1:
27210288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
27310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
27410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
27510288eeaSThierry Reding 		break;
27610288eeaSThierry Reding 
27710288eeaSThierry Reding 	case 2:
27810288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
27910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
28010288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
28110288eeaSThierry Reding 		break;
28210288eeaSThierry Reding 	}
28310288eeaSThierry Reding 
28493396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
285c7679306SThierry Reding }
286c7679306SThierry Reding 
287c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
288c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
289c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
290c7679306SThierry Reding 	DRM_FORMAT_RGB565,
291c7679306SThierry Reding };
292c7679306SThierry Reding 
2934aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
2944aa3df71SThierry Reding 				    struct drm_plane_state *state)
2954aa3df71SThierry Reding {
2968f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
2978f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
29847802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
2994aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
300c7679306SThierry Reding 	int err;
301c7679306SThierry Reding 
3024aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
3034aa3df71SThierry Reding 	if (!state->crtc)
3044aa3df71SThierry Reding 		return 0;
3054aa3df71SThierry Reding 
306*5acd3514SThierry Reding 	err = tegra_plane_format(state->fb->format->format,
307*5acd3514SThierry Reding 				 &plane_state->format,
3088f604f8cSThierry Reding 				 &plane_state->swap);
3094aa3df71SThierry Reding 	if (err < 0)
3104aa3df71SThierry Reding 		return err;
3114aa3df71SThierry Reding 
3128f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
3138f604f8cSThierry Reding 	if (err < 0)
3148f604f8cSThierry Reding 		return err;
3158f604f8cSThierry Reding 
3168f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
3174aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
3184aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
3194aa3df71SThierry Reding 		return -EINVAL;
3204aa3df71SThierry Reding 	}
3214aa3df71SThierry Reding 
3224aa3df71SThierry Reding 	/*
3234aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
3244aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
3254aa3df71SThierry Reding 	 * configuration.
3264aa3df71SThierry Reding 	 */
327bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
3284aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
3294aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
3304aa3df71SThierry Reding 			return -EINVAL;
3314aa3df71SThierry Reding 		}
3324aa3df71SThierry Reding 	}
3334aa3df71SThierry Reding 
33447802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
33547802b09SThierry Reding 	if (err < 0)
33647802b09SThierry Reding 		return err;
33747802b09SThierry Reding 
3384aa3df71SThierry Reding 	return 0;
3394aa3df71SThierry Reding }
3404aa3df71SThierry Reding 
341a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
342a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
34380d3eef1SDmitry Osipenko {
344a4bfa096SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
345a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
34680d3eef1SDmitry Osipenko 	unsigned long flags;
34780d3eef1SDmitry Osipenko 	u32 value;
34880d3eef1SDmitry Osipenko 
349a4bfa096SThierry Reding 	/* rien ne va plus */
350a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
351a4bfa096SThierry Reding 		return;
352a4bfa096SThierry Reding 
35380d3eef1SDmitry Osipenko 	spin_lock_irqsave(&dc->lock, flags);
35480d3eef1SDmitry Osipenko 
355a4bfa096SThierry Reding 	value = WINDOW_A_SELECT << p->index;
35680d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
35780d3eef1SDmitry Osipenko 
35880d3eef1SDmitry Osipenko 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
35980d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
36080d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
36180d3eef1SDmitry Osipenko 
36280d3eef1SDmitry Osipenko 	spin_unlock_irqrestore(&dc->lock, flags);
36380d3eef1SDmitry Osipenko }
36480d3eef1SDmitry Osipenko 
3654aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
3664aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
3674aa3df71SThierry Reding {
3688f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
3694aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
3704aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
3714aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
3724aa3df71SThierry Reding 	struct tegra_dc_window window;
3734aa3df71SThierry Reding 	unsigned int i;
3744aa3df71SThierry Reding 
3754aa3df71SThierry Reding 	/* rien ne va plus */
3764aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
3774aa3df71SThierry Reding 		return;
3784aa3df71SThierry Reding 
37980d3eef1SDmitry Osipenko 	if (!plane->state->visible)
380a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
38180d3eef1SDmitry Osipenko 
382c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
3837d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
3847d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
3857d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
3867d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
3877d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
3887d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
3897d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
3907d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
391272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
392c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
393c7679306SThierry Reding 
3948f604f8cSThierry Reding 	/* copy from state */
3958f604f8cSThierry Reding 	window.tiling = state->tiling;
3968f604f8cSThierry Reding 	window.format = state->format;
3978f604f8cSThierry Reding 	window.swap = state->swap;
398c7679306SThierry Reding 
399bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
4004aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
401c7679306SThierry Reding 
4024aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
40308ee0178SDmitry Osipenko 
40408ee0178SDmitry Osipenko 		/*
40508ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
40608ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
40708ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
40808ee0178SDmitry Osipenko 		 */
40908ee0178SDmitry Osipenko 		if (i < 2)
4104aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
411c7679306SThierry Reding 	}
412c7679306SThierry Reding 
4134aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
4144aa3df71SThierry Reding }
4154aa3df71SThierry Reding 
416a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
4174aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
4184aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
419a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
420c7679306SThierry Reding };
421c7679306SThierry Reding 
422c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
423c7679306SThierry Reding 						       struct tegra_dc *dc)
424c7679306SThierry Reding {
425518e6227SThierry Reding 	/*
426518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
427518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
428518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
429518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
430518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
431518e6227SThierry Reding 	 * here.
432518e6227SThierry Reding 	 *
433518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
434518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
435518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
436518e6227SThierry Reding 	 */
437518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
438c7679306SThierry Reding 	struct tegra_plane *plane;
439c7679306SThierry Reding 	unsigned int num_formats;
440c7679306SThierry Reding 	const u32 *formats;
441c7679306SThierry Reding 	int err;
442c7679306SThierry Reding 
443c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
444c7679306SThierry Reding 	if (!plane)
445c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
446c7679306SThierry Reding 
447c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
448c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
449c7679306SThierry Reding 
450518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
451c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
452e6fc3b68SBen Widawsky 				       num_formats, NULL,
453e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_PRIMARY, NULL);
454c7679306SThierry Reding 	if (err < 0) {
455c7679306SThierry Reding 		kfree(plane);
456c7679306SThierry Reding 		return ERR_PTR(err);
457c7679306SThierry Reding 	}
458c7679306SThierry Reding 
459a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
4604aa3df71SThierry Reding 
461c7679306SThierry Reding 	return &plane->base;
462c7679306SThierry Reding }
463c7679306SThierry Reding 
464c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
465c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
466c7679306SThierry Reding };
467c7679306SThierry Reding 
4684aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
4694aa3df71SThierry Reding 				     struct drm_plane_state *state)
470c7679306SThierry Reding {
47147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
47247802b09SThierry Reding 	int err;
47347802b09SThierry Reding 
4744aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
4754aa3df71SThierry Reding 	if (!state->crtc)
4764aa3df71SThierry Reding 		return 0;
477c7679306SThierry Reding 
478c7679306SThierry Reding 	/* scaling not supported for cursor */
4794aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
4804aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
481c7679306SThierry Reding 		return -EINVAL;
482c7679306SThierry Reding 
483c7679306SThierry Reding 	/* only square cursors supported */
4844aa3df71SThierry Reding 	if (state->src_w != state->src_h)
485c7679306SThierry Reding 		return -EINVAL;
486c7679306SThierry Reding 
4874aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
4884aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
4894aa3df71SThierry Reding 		return -EINVAL;
4904aa3df71SThierry Reding 
49147802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
49247802b09SThierry Reding 	if (err < 0)
49347802b09SThierry Reding 		return err;
49447802b09SThierry Reding 
4954aa3df71SThierry Reding 	return 0;
4964aa3df71SThierry Reding }
4974aa3df71SThierry Reding 
4984aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
4994aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
5004aa3df71SThierry Reding {
5014aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
5024aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5034aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
5044aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
5054aa3df71SThierry Reding 
5064aa3df71SThierry Reding 	/* rien ne va plus */
5074aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5084aa3df71SThierry Reding 		return;
5094aa3df71SThierry Reding 
5104aa3df71SThierry Reding 	switch (state->crtc_w) {
511c7679306SThierry Reding 	case 32:
512c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
513c7679306SThierry Reding 		break;
514c7679306SThierry Reding 
515c7679306SThierry Reding 	case 64:
516c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
517c7679306SThierry Reding 		break;
518c7679306SThierry Reding 
519c7679306SThierry Reding 	case 128:
520c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
521c7679306SThierry Reding 		break;
522c7679306SThierry Reding 
523c7679306SThierry Reding 	case 256:
524c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
525c7679306SThierry Reding 		break;
526c7679306SThierry Reding 
527c7679306SThierry Reding 	default:
5284aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
5294aa3df71SThierry Reding 		     state->crtc_h);
5304aa3df71SThierry Reding 		return;
531c7679306SThierry Reding 	}
532c7679306SThierry Reding 
533c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
534c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
535c7679306SThierry Reding 
536c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
537c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
538c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
539c7679306SThierry Reding #endif
540c7679306SThierry Reding 
541c7679306SThierry Reding 	/* enable cursor and set blend mode */
542c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
543c7679306SThierry Reding 	value |= CURSOR_ENABLE;
544c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
545c7679306SThierry Reding 
546c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
547c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
548c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
549c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
550c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
551c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
552c7679306SThierry Reding 	value |= CURSOR_ALPHA;
553c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
554c7679306SThierry Reding 
555c7679306SThierry Reding 	/* position the cursor */
5564aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
557c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
558c7679306SThierry Reding }
559c7679306SThierry Reding 
5604aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
5614aa3df71SThierry Reding 					struct drm_plane_state *old_state)
562c7679306SThierry Reding {
5634aa3df71SThierry Reding 	struct tegra_dc *dc;
564c7679306SThierry Reding 	u32 value;
565c7679306SThierry Reding 
5664aa3df71SThierry Reding 	/* rien ne va plus */
5674aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
5684aa3df71SThierry Reding 		return;
5694aa3df71SThierry Reding 
5704aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
571c7679306SThierry Reding 
572c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
573c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
574c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
575c7679306SThierry Reding }
576c7679306SThierry Reding 
5774aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
5784aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
5794aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
5804aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
581c7679306SThierry Reding };
582c7679306SThierry Reding 
583c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
584c7679306SThierry Reding 						      struct tegra_dc *dc)
585c7679306SThierry Reding {
586c7679306SThierry Reding 	struct tegra_plane *plane;
587c7679306SThierry Reding 	unsigned int num_formats;
588c7679306SThierry Reding 	const u32 *formats;
589c7679306SThierry Reding 	int err;
590c7679306SThierry Reding 
591c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
592c7679306SThierry Reding 	if (!plane)
593c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
594c7679306SThierry Reding 
59547802b09SThierry Reding 	/*
596a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
597a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
598a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
599a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
600a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
60147802b09SThierry Reding 	 */
60247802b09SThierry Reding 	plane->index = 6;
60347802b09SThierry Reding 
604c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
605c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
606c7679306SThierry Reding 
607c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
608c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
609e6fc3b68SBen Widawsky 				       num_formats, NULL,
610e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
611c7679306SThierry Reding 	if (err < 0) {
612c7679306SThierry Reding 		kfree(plane);
613c7679306SThierry Reding 		return ERR_PTR(err);
614c7679306SThierry Reding 	}
615c7679306SThierry Reding 
6164aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
6174aa3df71SThierry Reding 
618c7679306SThierry Reding 	return &plane->base;
619c7679306SThierry Reding }
620c7679306SThierry Reding 
621c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
622dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
623dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
624dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
625dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
626f925390eSThierry Reding 	DRM_FORMAT_YUYV,
627dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
628dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
629dee8268fSThierry Reding };
630dee8268fSThierry Reding 
631c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
632c7679306SThierry Reding 						       struct tegra_dc *dc,
633c7679306SThierry Reding 						       unsigned int index)
634dee8268fSThierry Reding {
635dee8268fSThierry Reding 	struct tegra_plane *plane;
636c7679306SThierry Reding 	unsigned int num_formats;
637c7679306SThierry Reding 	const u32 *formats;
638c7679306SThierry Reding 	int err;
639dee8268fSThierry Reding 
640f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
641dee8268fSThierry Reding 	if (!plane)
642c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
643dee8268fSThierry Reding 
644c7679306SThierry Reding 	plane->index = index;
645dee8268fSThierry Reding 
646c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
647c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
648c7679306SThierry Reding 
649c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
650301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
651e6fc3b68SBen Widawsky 				       num_formats, NULL,
652e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_OVERLAY, NULL);
653f002abc1SThierry Reding 	if (err < 0) {
654f002abc1SThierry Reding 		kfree(plane);
655c7679306SThierry Reding 		return ERR_PTR(err);
656dee8268fSThierry Reding 	}
657c7679306SThierry Reding 
658a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
6594aa3df71SThierry Reding 
660c7679306SThierry Reding 	return &plane->base;
661c7679306SThierry Reding }
662c7679306SThierry Reding 
663c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
664c7679306SThierry Reding {
665c7679306SThierry Reding 	struct drm_plane *plane;
666c7679306SThierry Reding 	unsigned int i;
667c7679306SThierry Reding 
668c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
669c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
670c7679306SThierry Reding 		if (IS_ERR(plane))
671c7679306SThierry Reding 			return PTR_ERR(plane);
672f002abc1SThierry Reding 	}
673dee8268fSThierry Reding 
674dee8268fSThierry Reding 	return 0;
675dee8268fSThierry Reding }
676dee8268fSThierry Reding 
677f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
678f002abc1SThierry Reding {
679f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
680f002abc1SThierry Reding }
681f002abc1SThierry Reding 
682ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
683ca915b10SThierry Reding {
684ca915b10SThierry Reding 	struct tegra_dc_state *state;
685ca915b10SThierry Reding 
6863b59b7acSThierry Reding 	if (crtc->state)
687ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
6883b59b7acSThierry Reding 
689ca915b10SThierry Reding 	kfree(crtc->state);
690ca915b10SThierry Reding 	crtc->state = NULL;
691ca915b10SThierry Reding 
692ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
693332bbe70SThierry Reding 	if (state) {
694ca915b10SThierry Reding 		crtc->state = &state->base;
695332bbe70SThierry Reding 		crtc->state->crtc = crtc;
696332bbe70SThierry Reding 	}
69731930d4dSThierry Reding 
69831930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
699ca915b10SThierry Reding }
700ca915b10SThierry Reding 
701ca915b10SThierry Reding static struct drm_crtc_state *
702ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
703ca915b10SThierry Reding {
704ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
705ca915b10SThierry Reding 	struct tegra_dc_state *copy;
706ca915b10SThierry Reding 
7073b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
708ca915b10SThierry Reding 	if (!copy)
709ca915b10SThierry Reding 		return NULL;
710ca915b10SThierry Reding 
7113b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
7123b59b7acSThierry Reding 	copy->clk = state->clk;
7133b59b7acSThierry Reding 	copy->pclk = state->pclk;
7143b59b7acSThierry Reding 	copy->div = state->div;
7153b59b7acSThierry Reding 	copy->planes = state->planes;
716ca915b10SThierry Reding 
717ca915b10SThierry Reding 	return &copy->base;
718ca915b10SThierry Reding }
719ca915b10SThierry Reding 
720ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
721ca915b10SThierry Reding 					    struct drm_crtc_state *state)
722ca915b10SThierry Reding {
723ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
724ca915b10SThierry Reding 	kfree(state);
725ca915b10SThierry Reding }
726ca915b10SThierry Reding 
727b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
728b95800eeSThierry Reding 
729b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
730b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
731b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
732b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
733b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
734b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
735b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
736b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
737b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
738b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
739b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
740b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
741b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
742b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
743b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
744b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
745b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
746b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
747b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
748b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
749b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
750b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
751b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
752b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
753b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
754b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
755b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
756b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
757b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
758b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
759b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
760b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
761b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
762b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
763b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
764b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
765b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
766b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
767b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
768b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
769b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
770b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
771b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
772b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
773b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
774b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
775b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
776b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
777b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
778b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
779b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
780b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
781b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
782b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
783b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
784b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
785b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
786b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
787b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
788b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
789b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
790b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
791b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
792b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
793b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
794b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
795b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
796b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
797b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
798b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
799b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
800b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
801b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
802b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
803b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
804b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
805b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
806b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
807b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
808b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
809b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
810b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
811b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
812b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
813b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
814b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
815b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
816b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
817b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
818b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
819b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
820b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
821b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
822b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
823b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
824b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
825b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
826b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
827b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
828b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
829b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
830b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
831b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
832b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
833b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
834b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
835b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
836b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
837b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
838b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
839b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
840b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
841b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
842b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
843b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
844b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
845b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
846b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
847b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
848b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
849b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
850b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
851b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
852b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
853b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
854b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
855b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
856b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
857b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
858b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
859b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
860b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
861b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
862b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
863b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
864b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
865b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
866b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
867b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
868b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
869b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
870b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
871b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
872b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
873b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
874b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
875b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
876b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
877b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
878b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
879b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
880b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
881b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
882b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
883b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
884b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
885b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
886b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
887b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
888b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
889b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
890b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
891b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
892b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
893b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
894b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
895b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
896b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
897b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
898b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
899b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
900b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
901b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
902b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
903b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
904b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
905b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
906b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
907b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
908b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
909b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
910b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
911b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
912b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
913b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
914b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
915b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
916b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
917b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
918b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
919b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
920b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
921b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
922b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
923b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
924b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
925b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
926b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
927b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
928b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
929b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
930b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
931b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
932b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
933b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
934b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
935b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
936b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
937b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
938b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
939b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
940b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
941b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
942b95800eeSThierry Reding };
943b95800eeSThierry Reding 
944b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
945b95800eeSThierry Reding {
946b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
947b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
948b95800eeSThierry Reding 	unsigned int i;
949b95800eeSThierry Reding 	int err = 0;
950b95800eeSThierry Reding 
951b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
952b95800eeSThierry Reding 
953b95800eeSThierry Reding 	if (!dc->base.state->active) {
954b95800eeSThierry Reding 		err = -EBUSY;
955b95800eeSThierry Reding 		goto unlock;
956b95800eeSThierry Reding 	}
957b95800eeSThierry Reding 
958b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
959b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
960b95800eeSThierry Reding 
961b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
962b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
963b95800eeSThierry Reding 	}
964b95800eeSThierry Reding 
965b95800eeSThierry Reding unlock:
966b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
967b95800eeSThierry Reding 	return err;
968b95800eeSThierry Reding }
969b95800eeSThierry Reding 
970b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
971b95800eeSThierry Reding {
972b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
973b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
974b95800eeSThierry Reding 	int err = 0;
975b95800eeSThierry Reding 	u32 value;
976b95800eeSThierry Reding 
977b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
978b95800eeSThierry Reding 
979b95800eeSThierry Reding 	if (!dc->base.state->active) {
980b95800eeSThierry Reding 		err = -EBUSY;
981b95800eeSThierry Reding 		goto unlock;
982b95800eeSThierry Reding 	}
983b95800eeSThierry Reding 
984b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
985b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
986b95800eeSThierry Reding 	tegra_dc_commit(dc);
987b95800eeSThierry Reding 
988b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
989b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
990b95800eeSThierry Reding 
991b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
992b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
993b95800eeSThierry Reding 
994b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
995b95800eeSThierry Reding 
996b95800eeSThierry Reding unlock:
997b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
998b95800eeSThierry Reding 	return err;
999b95800eeSThierry Reding }
1000b95800eeSThierry Reding 
1001b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1002b95800eeSThierry Reding {
1003b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1004b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1005b95800eeSThierry Reding 
1006b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1007b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1008b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1009b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1010b95800eeSThierry Reding 
1011b95800eeSThierry Reding 	return 0;
1012b95800eeSThierry Reding }
1013b95800eeSThierry Reding 
1014b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1015b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1016b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1017b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1018b95800eeSThierry Reding };
1019b95800eeSThierry Reding 
1020b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1021b95800eeSThierry Reding {
1022b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1023b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1024b95800eeSThierry Reding 	struct dentry *root = crtc->debugfs_entry;
1025b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1026b95800eeSThierry Reding 	int err;
1027b95800eeSThierry Reding 
1028b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1029b95800eeSThierry Reding 				    GFP_KERNEL);
1030b95800eeSThierry Reding 	if (!dc->debugfs_files)
1031b95800eeSThierry Reding 		return -ENOMEM;
1032b95800eeSThierry Reding 
1033b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1034b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1035b95800eeSThierry Reding 
1036b95800eeSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1037b95800eeSThierry Reding 	if (err < 0)
1038b95800eeSThierry Reding 		goto free;
1039b95800eeSThierry Reding 
1040b95800eeSThierry Reding 	return 0;
1041b95800eeSThierry Reding 
1042b95800eeSThierry Reding free:
1043b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1044b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1045b95800eeSThierry Reding 
1046b95800eeSThierry Reding 	return err;
1047b95800eeSThierry Reding }
1048b95800eeSThierry Reding 
1049b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1050b95800eeSThierry Reding {
1051b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1052b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1053b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1054b95800eeSThierry Reding 
1055b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1056b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1057b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1058b95800eeSThierry Reding }
1059b95800eeSThierry Reding 
1060c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1061c49c81e2SThierry Reding {
1062c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1063c49c81e2SThierry Reding 
1064c49c81e2SThierry Reding 	if (dc->syncpt)
1065c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1066c49c81e2SThierry Reding 
1067c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
1068c49c81e2SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
1069c49c81e2SThierry Reding }
1070c49c81e2SThierry Reding 
1071c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1072c49c81e2SThierry Reding {
1073c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1074c49c81e2SThierry Reding 	unsigned long value, flags;
1075c49c81e2SThierry Reding 
1076c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
1077c49c81e2SThierry Reding 
1078c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1079c49c81e2SThierry Reding 	value |= VBLANK_INT;
1080c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1081c49c81e2SThierry Reding 
1082c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
1083c49c81e2SThierry Reding 
1084c49c81e2SThierry Reding 	return 0;
1085c49c81e2SThierry Reding }
1086c49c81e2SThierry Reding 
1087c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1088c49c81e2SThierry Reding {
1089c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1090c49c81e2SThierry Reding 	unsigned long value, flags;
1091c49c81e2SThierry Reding 
1092c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
1093c49c81e2SThierry Reding 
1094c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1095c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1096c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1097c49c81e2SThierry Reding 
1098c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
1099c49c81e2SThierry Reding }
1100c49c81e2SThierry Reding 
1101dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
11021503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
110374f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1104f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1105ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1106ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1107ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1108b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1109b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
111010437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
111110437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
111210437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1113dee8268fSThierry Reding };
1114dee8268fSThierry Reding 
1115dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1116dee8268fSThierry Reding 				struct drm_display_mode *mode)
1117dee8268fSThierry Reding {
11180444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
11190444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1120dee8268fSThierry Reding 	unsigned long value;
1121dee8268fSThierry Reding 
1122dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1123dee8268fSThierry Reding 
1124dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1125dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1126dee8268fSThierry Reding 
1127dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1128dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1129dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1130dee8268fSThierry Reding 
1131dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1132dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1133dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1134dee8268fSThierry Reding 
1135dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1136dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1137dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1138dee8268fSThierry Reding 
1139dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1140dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1141dee8268fSThierry Reding 
1142dee8268fSThierry Reding 	return 0;
1143dee8268fSThierry Reding }
1144dee8268fSThierry Reding 
11459d910b60SThierry Reding /**
11469d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
11479d910b60SThierry Reding  *     state
11489d910b60SThierry Reding  * @dc: display controller
11499d910b60SThierry Reding  * @crtc_state: CRTC atomic state
11509d910b60SThierry Reding  * @clk: parent clock for display controller
11519d910b60SThierry Reding  * @pclk: pixel clock
11529d910b60SThierry Reding  * @div: shift clock divider
11539d910b60SThierry Reding  *
11549d910b60SThierry Reding  * Returns:
11559d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
11569d910b60SThierry Reding  */
1157ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1158ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1159ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1160ca915b10SThierry Reding 			       unsigned int div)
1161ca915b10SThierry Reding {
1162ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1163ca915b10SThierry Reding 
1164d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1165d2982748SThierry Reding 		return -EINVAL;
1166d2982748SThierry Reding 
1167ca915b10SThierry Reding 	state->clk = clk;
1168ca915b10SThierry Reding 	state->pclk = pclk;
1169ca915b10SThierry Reding 	state->div = div;
1170ca915b10SThierry Reding 
1171ca915b10SThierry Reding 	return 0;
1172ca915b10SThierry Reding }
1173ca915b10SThierry Reding 
117476d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
117576d59ed0SThierry Reding 				  struct tegra_dc_state *state)
117676d59ed0SThierry Reding {
117776d59ed0SThierry Reding 	u32 value;
117876d59ed0SThierry Reding 	int err;
117976d59ed0SThierry Reding 
118076d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
118176d59ed0SThierry Reding 	if (err < 0)
118276d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
118376d59ed0SThierry Reding 
118476d59ed0SThierry Reding 	/*
118576d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
118676d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
118776d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
118876d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
118976d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
119076d59ed0SThierry Reding 	 * should therefore be avoided.
119176d59ed0SThierry Reding 	 */
119276d59ed0SThierry Reding 	if (state->pclk > 0) {
119376d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
119476d59ed0SThierry Reding 		if (err < 0)
119576d59ed0SThierry Reding 			dev_err(dc->dev,
119676d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
119776d59ed0SThierry Reding 				state->pclk);
119876d59ed0SThierry Reding 	}
119976d59ed0SThierry Reding 
120076d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
120176d59ed0SThierry Reding 		      state->div);
120276d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
120376d59ed0SThierry Reding 
120476d59ed0SThierry Reding 	value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
120576d59ed0SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
120639e08affSThierry Reding 
120739e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
120839e08affSThierry Reding 	if (err < 0)
120939e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
121039e08affSThierry Reding 			dc->clk, state->pclk, err);
121176d59ed0SThierry Reding }
121276d59ed0SThierry Reding 
1213003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1214003fc848SThierry Reding {
1215003fc848SThierry Reding 	u32 value;
1216003fc848SThierry Reding 
1217003fc848SThierry Reding 	/* stop the display controller */
1218003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1219003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1220003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1221003fc848SThierry Reding 
1222003fc848SThierry Reding 	tegra_dc_commit(dc);
1223003fc848SThierry Reding }
1224003fc848SThierry Reding 
1225003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1226003fc848SThierry Reding {
1227003fc848SThierry Reding 	u32 value;
1228003fc848SThierry Reding 
1229003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1230003fc848SThierry Reding 
1231003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1232003fc848SThierry Reding }
1233003fc848SThierry Reding 
1234003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1235003fc848SThierry Reding {
1236003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1237003fc848SThierry Reding 
1238003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1239003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1240003fc848SThierry Reding 			return 0;
1241003fc848SThierry Reding 
1242003fc848SThierry Reding 		usleep_range(1000, 2000);
1243003fc848SThierry Reding 	}
1244003fc848SThierry Reding 
1245003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1246003fc848SThierry Reding 	return -ETIMEDOUT;
1247003fc848SThierry Reding }
1248003fc848SThierry Reding 
124964581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
125064581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1251003fc848SThierry Reding {
1252003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1253003fc848SThierry Reding 	u32 value;
1254003fc848SThierry Reding 
1255003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1256003fc848SThierry Reding 		tegra_dc_stop(dc);
1257003fc848SThierry Reding 
1258003fc848SThierry Reding 		/*
1259003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1260003fc848SThierry Reding 		 * in case this fails.
1261003fc848SThierry Reding 		 */
1262003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1263003fc848SThierry Reding 	}
1264003fc848SThierry Reding 
1265003fc848SThierry Reding 	/*
1266003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1267003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1268003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1269003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1270003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1271003fc848SThierry Reding 	 * to go idle.
1272003fc848SThierry Reding 	 *
1273003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1274003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1275003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1276003fc848SThierry Reding 	 *
1277003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1278003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1279003fc848SThierry Reding 	 * the RGB encoder?
1280003fc848SThierry Reding 	 */
1281003fc848SThierry Reding 	if (dc->rgb) {
1282003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1283003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1284003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1285003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1286003fc848SThierry Reding 	}
1287003fc848SThierry Reding 
1288003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1289003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
129033a8eb8dSThierry Reding 
12919d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
12929d99ab6eSThierry Reding 
12939d99ab6eSThierry Reding 	if (crtc->state->event) {
12949d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
12959d99ab6eSThierry Reding 		crtc->state->event = NULL;
12969d99ab6eSThierry Reding 	}
12979d99ab6eSThierry Reding 
12989d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
12999d99ab6eSThierry Reding 
130033a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1301003fc848SThierry Reding }
1302003fc848SThierry Reding 
13030b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
13040b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1305dee8268fSThierry Reding {
13064aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
130776d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1308dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1309dbb3f2f7SThierry Reding 	u32 value;
1310dee8268fSThierry Reding 
131133a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
131233a8eb8dSThierry Reding 
131333a8eb8dSThierry Reding 	/* initialize display controller */
131433a8eb8dSThierry Reding 	if (dc->syncpt) {
131533a8eb8dSThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt);
131633a8eb8dSThierry Reding 
131733a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
131833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
131933a8eb8dSThierry Reding 
132033a8eb8dSThierry Reding 		value = SYNCPT_VSYNC_ENABLE | syncpt;
132133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
132233a8eb8dSThierry Reding 	}
132333a8eb8dSThierry Reding 
132433a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
132533a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
132633a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
132733a8eb8dSThierry Reding 
132833a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
132933a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
133033a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
133133a8eb8dSThierry Reding 
133233a8eb8dSThierry Reding 	/* initialize timer */
133333a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
133433a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
133533a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
133633a8eb8dSThierry Reding 
133733a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
133833a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
133933a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
134033a8eb8dSThierry Reding 
134133a8eb8dSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
134233a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
134333a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
134433a8eb8dSThierry Reding 
134533a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
134633a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
134733a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
134833a8eb8dSThierry Reding 
13497116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
13507116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
13517116e9a8SThierry Reding 	else
135233a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
135333a8eb8dSThierry Reding 
135433a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
135576d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
135676d59ed0SThierry Reding 
1357dee8268fSThierry Reding 	/* program display mode */
1358dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1359dee8268fSThierry Reding 
13608620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
13618620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
13628620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
13638620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
13648620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
13658620fc62SThierry Reding 	}
1366666cb873SThierry Reding 
1367666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1368666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1369666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1370666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1371666cb873SThierry Reding 
1372666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1373666cb873SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1374666cb873SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1375666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1376666cb873SThierry Reding 
1377666cb873SThierry Reding 	tegra_dc_commit(dc);
1378dee8268fSThierry Reding 
13798ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1380dee8268fSThierry Reding }
1381dee8268fSThierry Reding 
13824aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
13834aa3df71SThierry Reding 				   struct drm_crtc_state *state)
13844aa3df71SThierry Reding {
13854aa3df71SThierry Reding 	return 0;
13864aa3df71SThierry Reding }
13874aa3df71SThierry Reding 
1388613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1389613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
13904aa3df71SThierry Reding {
13919d99ab6eSThierry Reding 	unsigned long flags;
13921503ca47SThierry Reding 
13931503ca47SThierry Reding 	if (crtc->state->event) {
13949d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
13951503ca47SThierry Reding 
13969d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
13979d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
13989d99ab6eSThierry Reding 		else
13999d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
14001503ca47SThierry Reding 
14019d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
14029d99ab6eSThierry Reding 
14031503ca47SThierry Reding 		crtc->state->event = NULL;
14041503ca47SThierry Reding 	}
14054aa3df71SThierry Reding }
14064aa3df71SThierry Reding 
1407613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1408613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
14094aa3df71SThierry Reding {
141047802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
141147802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
141247802b09SThierry Reding 
141347802b09SThierry Reding 	tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
141447802b09SThierry Reding 	tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
14154aa3df71SThierry Reding }
14164aa3df71SThierry Reding 
1417dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
14184aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
14194aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
14204aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
14210b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
142264581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1423dee8268fSThierry Reding };
1424dee8268fSThierry Reding 
1425dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1426dee8268fSThierry Reding {
1427dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1428dee8268fSThierry Reding 	unsigned long status;
1429dee8268fSThierry Reding 
1430dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1431dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1432dee8268fSThierry Reding 
1433dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1434dee8268fSThierry Reding 		/*
1435dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1436dee8268fSThierry Reding 		*/
1437791ddb1eSThierry Reding 		dc->stats.frames++;
1438dee8268fSThierry Reding 	}
1439dee8268fSThierry Reding 
1440dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1441dee8268fSThierry Reding 		/*
1442dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1443dee8268fSThierry Reding 		*/
1444ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1445791ddb1eSThierry Reding 		dc->stats.vblank++;
1446dee8268fSThierry Reding 	}
1447dee8268fSThierry Reding 
1448dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1449dee8268fSThierry Reding 		/*
1450dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1451dee8268fSThierry Reding 		*/
1452791ddb1eSThierry Reding 		dc->stats.underflow++;
1453791ddb1eSThierry Reding 	}
1454791ddb1eSThierry Reding 
1455791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1456791ddb1eSThierry Reding 		/*
1457791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1458791ddb1eSThierry Reding 		*/
1459791ddb1eSThierry Reding 		dc->stats.overflow++;
1460dee8268fSThierry Reding 	}
1461dee8268fSThierry Reding 
1462dee8268fSThierry Reding 	return IRQ_HANDLED;
1463dee8268fSThierry Reding }
1464dee8268fSThierry Reding 
1465dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1466dee8268fSThierry Reding {
14679910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
14682bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1469dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1470d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1471c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1472c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1473dee8268fSThierry Reding 	int err;
1474dee8268fSThierry Reding 
1475617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
14762bcdcbfaSThierry Reding 	if (!dc->syncpt)
14772bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
14782bcdcbfaSThierry Reding 
1479df06b759SThierry Reding 	if (tegra->domain) {
1480df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1481df06b759SThierry Reding 		if (err < 0) {
1482df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1483df06b759SThierry Reding 				err);
1484df06b759SThierry Reding 			return err;
1485df06b759SThierry Reding 		}
1486df06b759SThierry Reding 
1487df06b759SThierry Reding 		dc->domain = tegra->domain;
1488df06b759SThierry Reding 	}
1489df06b759SThierry Reding 
1490c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1491c7679306SThierry Reding 	if (IS_ERR(primary)) {
1492c7679306SThierry Reding 		err = PTR_ERR(primary);
1493c7679306SThierry Reding 		goto cleanup;
1494c7679306SThierry Reding 	}
1495c7679306SThierry Reding 
1496c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1497c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1498c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1499c7679306SThierry Reding 			err = PTR_ERR(cursor);
1500c7679306SThierry Reding 			goto cleanup;
1501c7679306SThierry Reding 		}
1502c7679306SThierry Reding 	}
1503c7679306SThierry Reding 
1504c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1505f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1506c7679306SThierry Reding 	if (err < 0)
1507c7679306SThierry Reding 		goto cleanup;
1508c7679306SThierry Reding 
1509dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1510dee8268fSThierry Reding 
1511d1f3e1e0SThierry Reding 	/*
1512d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1513d1f3e1e0SThierry Reding 	 * controllers.
1514d1f3e1e0SThierry Reding 	 */
1515d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1516d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1517d1f3e1e0SThierry Reding 
15189910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1519dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1520dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1521c7679306SThierry Reding 		goto cleanup;
1522dee8268fSThierry Reding 	}
1523dee8268fSThierry Reding 
15249910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1525dee8268fSThierry Reding 	if (err < 0)
1526c7679306SThierry Reding 		goto cleanup;
1527dee8268fSThierry Reding 
1528dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1529dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1530dee8268fSThierry Reding 	if (err < 0) {
1531dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1532dee8268fSThierry Reding 			err);
1533c7679306SThierry Reding 		goto cleanup;
1534dee8268fSThierry Reding 	}
1535dee8268fSThierry Reding 
1536dee8268fSThierry Reding 	return 0;
1537c7679306SThierry Reding 
1538c7679306SThierry Reding cleanup:
1539c7679306SThierry Reding 	if (cursor)
1540c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1541c7679306SThierry Reding 
1542c7679306SThierry Reding 	if (primary)
1543c7679306SThierry Reding 		drm_plane_cleanup(primary);
1544c7679306SThierry Reding 
1545c7679306SThierry Reding 	if (tegra->domain) {
1546c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1547c7679306SThierry Reding 		dc->domain = NULL;
1548c7679306SThierry Reding 	}
1549c7679306SThierry Reding 
1550c7679306SThierry Reding 	return err;
1551dee8268fSThierry Reding }
1552dee8268fSThierry Reding 
1553dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1554dee8268fSThierry Reding {
1555dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1556dee8268fSThierry Reding 	int err;
1557dee8268fSThierry Reding 
1558dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1559dee8268fSThierry Reding 
1560dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1561dee8268fSThierry Reding 	if (err) {
1562dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1563dee8268fSThierry Reding 		return err;
1564dee8268fSThierry Reding 	}
1565dee8268fSThierry Reding 
1566df06b759SThierry Reding 	if (dc->domain) {
1567df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1568df06b759SThierry Reding 		dc->domain = NULL;
1569df06b759SThierry Reding 	}
1570df06b759SThierry Reding 
15712bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
15722bcdcbfaSThierry Reding 
1573dee8268fSThierry Reding 	return 0;
1574dee8268fSThierry Reding }
1575dee8268fSThierry Reding 
1576dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1577dee8268fSThierry Reding 	.init = tegra_dc_init,
1578dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1579dee8268fSThierry Reding };
1580dee8268fSThierry Reding 
15818620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
15827116e9a8SThierry Reding 	.supports_background_color = false,
15838620fc62SThierry Reding 	.supports_interlacing = false,
1584e687651bSThierry Reding 	.supports_cursor = false,
1585c134f019SThierry Reding 	.supports_block_linear = false,
1586d1f3e1e0SThierry Reding 	.pitch_align = 8,
15879c012700SThierry Reding 	.has_powergate = false,
15886ac1571bSDmitry Osipenko 	.broken_reset = true,
15898620fc62SThierry Reding };
15908620fc62SThierry Reding 
15918620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
15927116e9a8SThierry Reding 	.supports_background_color = false,
15938620fc62SThierry Reding 	.supports_interlacing = false,
1594e687651bSThierry Reding 	.supports_cursor = false,
1595c134f019SThierry Reding 	.supports_block_linear = false,
1596d1f3e1e0SThierry Reding 	.pitch_align = 8,
15979c012700SThierry Reding 	.has_powergate = false,
15986ac1571bSDmitry Osipenko 	.broken_reset = false,
1599d1f3e1e0SThierry Reding };
1600d1f3e1e0SThierry Reding 
1601d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
16027116e9a8SThierry Reding 	.supports_background_color = false,
1603d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1604d1f3e1e0SThierry Reding 	.supports_cursor = false,
1605d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1606d1f3e1e0SThierry Reding 	.pitch_align = 64,
16079c012700SThierry Reding 	.has_powergate = true,
16086ac1571bSDmitry Osipenko 	.broken_reset = false,
16098620fc62SThierry Reding };
16108620fc62SThierry Reding 
16118620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
16127116e9a8SThierry Reding 	.supports_background_color = true,
16138620fc62SThierry Reding 	.supports_interlacing = true,
1614e687651bSThierry Reding 	.supports_cursor = true,
1615c134f019SThierry Reding 	.supports_block_linear = true,
1616d1f3e1e0SThierry Reding 	.pitch_align = 64,
16179c012700SThierry Reding 	.has_powergate = true,
16186ac1571bSDmitry Osipenko 	.broken_reset = false,
16198620fc62SThierry Reding };
16208620fc62SThierry Reding 
16215b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
16227116e9a8SThierry Reding 	.supports_background_color = true,
16235b4f516fSThierry Reding 	.supports_interlacing = true,
16245b4f516fSThierry Reding 	.supports_cursor = true,
16255b4f516fSThierry Reding 	.supports_block_linear = true,
16265b4f516fSThierry Reding 	.pitch_align = 64,
16275b4f516fSThierry Reding 	.has_powergate = true,
16286ac1571bSDmitry Osipenko 	.broken_reset = false,
16295b4f516fSThierry Reding };
16305b4f516fSThierry Reding 
16318620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
16328620fc62SThierry Reding 	{
16335b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
16345b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
16355b4f516fSThierry Reding 	}, {
16368620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
16378620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
16388620fc62SThierry Reding 	}, {
16399c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
16409c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
16419c012700SThierry Reding 	}, {
16428620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
16438620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
16448620fc62SThierry Reding 	}, {
16458620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
16468620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
16478620fc62SThierry Reding 	}, {
16488620fc62SThierry Reding 		/* sentinel */
16498620fc62SThierry Reding 	}
16508620fc62SThierry Reding };
1651ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
16528620fc62SThierry Reding 
165313411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
165413411dddSThierry Reding {
165513411dddSThierry Reding 	struct device_node *np;
165613411dddSThierry Reding 	u32 value = 0;
165713411dddSThierry Reding 	int err;
165813411dddSThierry Reding 
165913411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
166013411dddSThierry Reding 	if (err < 0) {
166113411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
166213411dddSThierry Reding 
166313411dddSThierry Reding 		/*
166413411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
166513411dddSThierry Reding 		 * correct head number by looking up the position of this
166613411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
166713411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
166813411dddSThierry Reding 		 * that the translation into a flattened device tree blob
166913411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
167013411dddSThierry Reding 		 * head number.
167113411dddSThierry Reding 		 *
167213411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
167313411dddSThierry Reding 		 * cases where only a single display controller is used.
167413411dddSThierry Reding 		 */
167513411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1676cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1677cf6b1744SJulia Lawall 				of_node_put(np);
167813411dddSThierry Reding 				break;
1679cf6b1744SJulia Lawall 			}
168013411dddSThierry Reding 
168113411dddSThierry Reding 			value++;
168213411dddSThierry Reding 		}
168313411dddSThierry Reding 	}
168413411dddSThierry Reding 
168513411dddSThierry Reding 	dc->pipe = value;
168613411dddSThierry Reding 
168713411dddSThierry Reding 	return 0;
168813411dddSThierry Reding }
168913411dddSThierry Reding 
1690dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1691dee8268fSThierry Reding {
1692dee8268fSThierry Reding 	struct resource *regs;
1693dee8268fSThierry Reding 	struct tegra_dc *dc;
1694dee8268fSThierry Reding 	int err;
1695dee8268fSThierry Reding 
1696dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1697dee8268fSThierry Reding 	if (!dc)
1698dee8268fSThierry Reding 		return -ENOMEM;
1699dee8268fSThierry Reding 
1700b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
17018620fc62SThierry Reding 
1702dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1703dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1704dee8268fSThierry Reding 	dc->dev = &pdev->dev;
1705dee8268fSThierry Reding 
170613411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
170713411dddSThierry Reding 	if (err < 0)
170813411dddSThierry Reding 		return err;
170913411dddSThierry Reding 
1710dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1711dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1712dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1713dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1714dee8268fSThierry Reding 	}
1715dee8268fSThierry Reding 
1716ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1717ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1718ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1719ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1720ca48080aSStephen Warren 	}
1721ca48080aSStephen Warren 
1722a2f2f740SThierry Reding 	/* assert reset and disable clock */
1723a2f2f740SThierry Reding 	if (!dc->soc->broken_reset) {
1724a2f2f740SThierry Reding 		err = clk_prepare_enable(dc->clk);
1725a2f2f740SThierry Reding 		if (err < 0)
1726a2f2f740SThierry Reding 			return err;
1727a2f2f740SThierry Reding 
1728a2f2f740SThierry Reding 		usleep_range(2000, 4000);
1729a2f2f740SThierry Reding 
1730a2f2f740SThierry Reding 		err = reset_control_assert(dc->rst);
1731a2f2f740SThierry Reding 		if (err < 0)
1732a2f2f740SThierry Reding 			return err;
1733a2f2f740SThierry Reding 
1734a2f2f740SThierry Reding 		usleep_range(2000, 4000);
1735a2f2f740SThierry Reding 
1736a2f2f740SThierry Reding 		clk_disable_unprepare(dc->clk);
1737a2f2f740SThierry Reding 	}
173833a8eb8dSThierry Reding 
17399c012700SThierry Reding 	if (dc->soc->has_powergate) {
17409c012700SThierry Reding 		if (dc->pipe == 0)
17419c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
17429c012700SThierry Reding 		else
17439c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
17449c012700SThierry Reding 
174533a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
17469c012700SThierry Reding 	}
1747dee8268fSThierry Reding 
1748dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1749dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1750dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1751dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1752dee8268fSThierry Reding 
1753dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1754dee8268fSThierry Reding 	if (dc->irq < 0) {
1755dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1756dee8268fSThierry Reding 		return -ENXIO;
1757dee8268fSThierry Reding 	}
1758dee8268fSThierry Reding 
1759dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1760dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1761dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1762dee8268fSThierry Reding 		return err;
1763dee8268fSThierry Reding 	}
1764dee8268fSThierry Reding 
176533a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
176633a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
176733a8eb8dSThierry Reding 
176833a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
176933a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
177033a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
177133a8eb8dSThierry Reding 
1772dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1773dee8268fSThierry Reding 	if (err < 0) {
1774dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1775dee8268fSThierry Reding 			err);
1776dee8268fSThierry Reding 		return err;
1777dee8268fSThierry Reding 	}
1778dee8268fSThierry Reding 
1779dee8268fSThierry Reding 	return 0;
1780dee8268fSThierry Reding }
1781dee8268fSThierry Reding 
1782dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1783dee8268fSThierry Reding {
1784dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1785dee8268fSThierry Reding 	int err;
1786dee8268fSThierry Reding 
1787dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1788dee8268fSThierry Reding 	if (err < 0) {
1789dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1790dee8268fSThierry Reding 			err);
1791dee8268fSThierry Reding 		return err;
1792dee8268fSThierry Reding 	}
1793dee8268fSThierry Reding 
179459d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
179559d29c0eSThierry Reding 	if (err < 0) {
179659d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
179759d29c0eSThierry Reding 		return err;
179859d29c0eSThierry Reding 	}
179959d29c0eSThierry Reding 
180033a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
180133a8eb8dSThierry Reding 
180233a8eb8dSThierry Reding 	return 0;
180333a8eb8dSThierry Reding }
180433a8eb8dSThierry Reding 
180533a8eb8dSThierry Reding #ifdef CONFIG_PM
180633a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
180733a8eb8dSThierry Reding {
180833a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
180933a8eb8dSThierry Reding 	int err;
181033a8eb8dSThierry Reding 
18116ac1571bSDmitry Osipenko 	if (!dc->soc->broken_reset) {
181233a8eb8dSThierry Reding 		err = reset_control_assert(dc->rst);
181333a8eb8dSThierry Reding 		if (err < 0) {
181433a8eb8dSThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
181533a8eb8dSThierry Reding 			return err;
181633a8eb8dSThierry Reding 		}
18176ac1571bSDmitry Osipenko 	}
18189c012700SThierry Reding 
18199c012700SThierry Reding 	if (dc->soc->has_powergate)
18209c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
18219c012700SThierry Reding 
1822dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
1823dee8268fSThierry Reding 
1824dee8268fSThierry Reding 	return 0;
1825dee8268fSThierry Reding }
1826dee8268fSThierry Reding 
182733a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
182833a8eb8dSThierry Reding {
182933a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
183033a8eb8dSThierry Reding 	int err;
183133a8eb8dSThierry Reding 
183233a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
183333a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
183433a8eb8dSThierry Reding 							dc->rst);
183533a8eb8dSThierry Reding 		if (err < 0) {
183633a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
183733a8eb8dSThierry Reding 			return err;
183833a8eb8dSThierry Reding 		}
183933a8eb8dSThierry Reding 	} else {
184033a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
184133a8eb8dSThierry Reding 		if (err < 0) {
184233a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
184333a8eb8dSThierry Reding 			return err;
184433a8eb8dSThierry Reding 		}
184533a8eb8dSThierry Reding 
18466ac1571bSDmitry Osipenko 		if (!dc->soc->broken_reset) {
184733a8eb8dSThierry Reding 			err = reset_control_deassert(dc->rst);
184833a8eb8dSThierry Reding 			if (err < 0) {
18496ac1571bSDmitry Osipenko 				dev_err(dev,
18506ac1571bSDmitry Osipenko 					"failed to deassert reset: %d\n", err);
185133a8eb8dSThierry Reding 				return err;
185233a8eb8dSThierry Reding 			}
185333a8eb8dSThierry Reding 		}
18546ac1571bSDmitry Osipenko 	}
185533a8eb8dSThierry Reding 
185633a8eb8dSThierry Reding 	return 0;
185733a8eb8dSThierry Reding }
185833a8eb8dSThierry Reding #endif
185933a8eb8dSThierry Reding 
186033a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
186133a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
186233a8eb8dSThierry Reding };
186333a8eb8dSThierry Reding 
1864dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
1865dee8268fSThierry Reding 	.driver = {
1866dee8268fSThierry Reding 		.name = "tegra-dc",
1867dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
186833a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
1869dee8268fSThierry Reding 	},
1870dee8268fSThierry Reding 	.probe = tegra_dc_probe,
1871dee8268fSThierry Reding 	.remove = tegra_dc_remove,
1872dee8268fSThierry Reding };
1873