xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 473079549f27eab5ad449f2c4f079014f0fe74a5)
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*47307954SThierry Reding #include "hub.h"
235acd3514SThierry Reding #include "plane.h"
24dee8268fSThierry Reding 
259d44189fSThierry Reding #include <drm/drm_atomic.h>
264aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
273cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
283cb9ae4fSDaniel Vetter 
29791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
30791ddb1eSThierry Reding {
31791ddb1eSThierry Reding 	stats->frames = 0;
32791ddb1eSThierry Reding 	stats->vblank = 0;
33791ddb1eSThierry Reding 	stats->underflow = 0;
34791ddb1eSThierry Reding 	stats->overflow = 0;
35791ddb1eSThierry Reding }
36791ddb1eSThierry Reding 
37d700ba7aSThierry Reding /*
3886df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
3986df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
4086df256fSThierry Reding  * active copy of some registers.
4186df256fSThierry Reding  */
4286df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
4386df256fSThierry Reding {
4486df256fSThierry Reding 	unsigned long flags;
4586df256fSThierry Reding 	u32 value;
4686df256fSThierry Reding 
4786df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
4886df256fSThierry Reding 
4986df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
5086df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
5186df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
5286df256fSThierry Reding 
5386df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
5486df256fSThierry Reding 	return value;
5586df256fSThierry Reding }
5686df256fSThierry Reding 
5786df256fSThierry Reding /*
58d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
59d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
60d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
61d700ba7aSThierry Reding  * on the next frame boundary otherwise.
62d700ba7aSThierry Reding  *
63d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
64d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
65d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
66d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
67d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
68d700ba7aSThierry Reding  */
6962b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
70205d48edSThierry Reding {
71205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
72205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
73205d48edSThierry Reding }
74205d48edSThierry Reding 
7510288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
7610288eeaSThierry Reding 				  unsigned int bpp)
7710288eeaSThierry Reding {
7810288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
7910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
8010288eeaSThierry Reding 	u32 dda_inc;
8110288eeaSThierry Reding 	int max;
8210288eeaSThierry Reding 
8310288eeaSThierry Reding 	if (v)
8410288eeaSThierry Reding 		max = 15;
8510288eeaSThierry Reding 	else {
8610288eeaSThierry Reding 		switch (bpp) {
8710288eeaSThierry Reding 		case 2:
8810288eeaSThierry Reding 			max = 8;
8910288eeaSThierry Reding 			break;
9010288eeaSThierry Reding 
9110288eeaSThierry Reding 		default:
9210288eeaSThierry Reding 			WARN_ON_ONCE(1);
9310288eeaSThierry Reding 			/* fallthrough */
9410288eeaSThierry Reding 		case 4:
9510288eeaSThierry Reding 			max = 4;
9610288eeaSThierry Reding 			break;
9710288eeaSThierry Reding 		}
9810288eeaSThierry Reding 	}
9910288eeaSThierry Reding 
10010288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
10110288eeaSThierry Reding 	inf.full -= dfixed_const(1);
10210288eeaSThierry Reding 
10310288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
10410288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
10510288eeaSThierry Reding 
10610288eeaSThierry Reding 	return dda_inc;
10710288eeaSThierry Reding }
10810288eeaSThierry Reding 
10910288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
11010288eeaSThierry Reding {
11110288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
11210288eeaSThierry Reding 	return dfixed_frac(inf);
11310288eeaSThierry Reding }
11410288eeaSThierry Reding 
1154aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
11610288eeaSThierry Reding 				  const struct tegra_dc_window *window)
11710288eeaSThierry Reding {
11810288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
11993396d0fSSean Paul 	unsigned long value, flags;
12010288eeaSThierry Reding 	bool yuv, planar;
12110288eeaSThierry Reding 
12210288eeaSThierry Reding 	/*
12310288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
12410288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
12510288eeaSThierry Reding 	 */
1265acd3514SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar);
12710288eeaSThierry Reding 	if (!yuv)
12810288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
12910288eeaSThierry Reding 	else
13010288eeaSThierry Reding 		bpp = planar ? 1 : 2;
13110288eeaSThierry Reding 
13293396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
13393396d0fSSean Paul 
13410288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
13510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
13610288eeaSThierry Reding 
13710288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
13810288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
13910288eeaSThierry Reding 
14010288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
14110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
14410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
14510288eeaSThierry Reding 
14610288eeaSThierry Reding 	h_offset = window->src.x * bpp;
14710288eeaSThierry Reding 	v_offset = window->src.y;
14810288eeaSThierry Reding 	h_size = window->src.w * bpp;
14910288eeaSThierry Reding 	v_size = window->src.h;
15010288eeaSThierry Reding 
15110288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
15210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
15310288eeaSThierry Reding 
15410288eeaSThierry Reding 	/*
15510288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
15610288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
15710288eeaSThierry Reding 	 */
15810288eeaSThierry Reding 	if (yuv && planar)
15910288eeaSThierry Reding 		bpp = 2;
16010288eeaSThierry Reding 
16110288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
16210288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
16310288eeaSThierry Reding 
16410288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
16510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
16610288eeaSThierry Reding 
16710288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
16810288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
16910288eeaSThierry Reding 
17010288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
17110288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
17210288eeaSThierry Reding 
17310288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
17410288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
17510288eeaSThierry Reding 
17610288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
17710288eeaSThierry Reding 
17810288eeaSThierry Reding 	if (yuv && planar) {
17910288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
18010288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
18110288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
18210288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
18310288eeaSThierry Reding 	} else {
18410288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
18510288eeaSThierry Reding 	}
18610288eeaSThierry Reding 
18710288eeaSThierry Reding 	if (window->bottom_up)
18810288eeaSThierry Reding 		v_offset += window->src.h - 1;
18910288eeaSThierry Reding 
19010288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
19110288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
19210288eeaSThierry Reding 
193c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
194c134f019SThierry Reding 		unsigned long height = window->tiling.value;
195c134f019SThierry Reding 
196c134f019SThierry Reding 		switch (window->tiling.mode) {
197c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
198c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
199c134f019SThierry Reding 			break;
200c134f019SThierry Reding 
201c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
202c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
203c134f019SThierry Reding 			break;
204c134f019SThierry Reding 
205c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
206c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
207c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
208c134f019SThierry Reding 			break;
209c134f019SThierry Reding 		}
210c134f019SThierry Reding 
211c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
21210288eeaSThierry Reding 	} else {
213c134f019SThierry Reding 		switch (window->tiling.mode) {
214c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
21510288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
21610288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
217c134f019SThierry Reding 			break;
218c134f019SThierry Reding 
219c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
220c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
221c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
222c134f019SThierry Reding 			break;
223c134f019SThierry Reding 
224c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
2254aa3df71SThierry Reding 			/*
2264aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
2274aa3df71SThierry Reding 			 * will already have filtered it out.
2284aa3df71SThierry Reding 			 */
2294aa3df71SThierry Reding 			break;
23010288eeaSThierry Reding 		}
23110288eeaSThierry Reding 
23210288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
233c134f019SThierry Reding 	}
23410288eeaSThierry Reding 
23510288eeaSThierry Reding 	value = WIN_ENABLE;
23610288eeaSThierry Reding 
23710288eeaSThierry Reding 	if (yuv) {
23810288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
23910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
24010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
24110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
24210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
24310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
24410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
24510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
24610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
24710288eeaSThierry Reding 
24810288eeaSThierry Reding 		value |= CSC_ENABLE;
24910288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
25010288eeaSThierry Reding 		value |= COLOR_EXPAND;
25110288eeaSThierry Reding 	}
25210288eeaSThierry Reding 
25310288eeaSThierry Reding 	if (window->bottom_up)
25410288eeaSThierry Reding 		value |= V_DIRECTION;
25510288eeaSThierry Reding 
25610288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
25710288eeaSThierry Reding 
25810288eeaSThierry Reding 	/*
25910288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
26010288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
26110288eeaSThierry Reding 	 */
26210288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
26310288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
26410288eeaSThierry Reding 
26510288eeaSThierry Reding 	switch (index) {
26610288eeaSThierry Reding 	case 0:
26710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
26810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
26910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
27010288eeaSThierry Reding 		break;
27110288eeaSThierry Reding 
27210288eeaSThierry Reding 	case 1:
27310288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
27410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
27510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
27610288eeaSThierry Reding 		break;
27710288eeaSThierry Reding 
27810288eeaSThierry Reding 	case 2:
27910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
28010288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
28110288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
28210288eeaSThierry Reding 		break;
28310288eeaSThierry Reding 	}
28410288eeaSThierry Reding 
28593396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
286c7679306SThierry Reding }
287c7679306SThierry Reding 
288c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
289c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
290c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
291c7679306SThierry Reding 	DRM_FORMAT_RGB565,
292c7679306SThierry Reding };
293c7679306SThierry Reding 
2944aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
2954aa3df71SThierry Reding 				    struct drm_plane_state *state)
2964aa3df71SThierry Reding {
2978f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
2988f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
29947802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
3004aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
301c7679306SThierry Reding 	int err;
302c7679306SThierry Reding 
3034aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
3044aa3df71SThierry Reding 	if (!state->crtc)
3054aa3df71SThierry Reding 		return 0;
3064aa3df71SThierry Reding 
3075acd3514SThierry Reding 	err = tegra_plane_format(state->fb->format->format,
3085acd3514SThierry Reding 				 &plane_state->format,
3098f604f8cSThierry Reding 				 &plane_state->swap);
3104aa3df71SThierry Reding 	if (err < 0)
3114aa3df71SThierry Reding 		return err;
3124aa3df71SThierry Reding 
3138f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
3148f604f8cSThierry Reding 	if (err < 0)
3158f604f8cSThierry Reding 		return err;
3168f604f8cSThierry Reding 
3178f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
3184aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
3194aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
3204aa3df71SThierry Reding 		return -EINVAL;
3214aa3df71SThierry Reding 	}
3224aa3df71SThierry Reding 
3234aa3df71SThierry Reding 	/*
3244aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
3254aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
3264aa3df71SThierry Reding 	 * configuration.
3274aa3df71SThierry Reding 	 */
328bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
3294aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
3304aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
3314aa3df71SThierry Reding 			return -EINVAL;
3324aa3df71SThierry Reding 		}
3334aa3df71SThierry Reding 	}
3344aa3df71SThierry Reding 
33547802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
33647802b09SThierry Reding 	if (err < 0)
33747802b09SThierry Reding 		return err;
33847802b09SThierry Reding 
3394aa3df71SThierry Reding 	return 0;
3404aa3df71SThierry Reding }
3414aa3df71SThierry Reding 
342a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
343a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
34480d3eef1SDmitry Osipenko {
345a4bfa096SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
346a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
34780d3eef1SDmitry Osipenko 	unsigned long flags;
34880d3eef1SDmitry Osipenko 	u32 value;
34980d3eef1SDmitry Osipenko 
350a4bfa096SThierry Reding 	/* rien ne va plus */
351a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
352a4bfa096SThierry Reding 		return;
353a4bfa096SThierry Reding 
35480d3eef1SDmitry Osipenko 	spin_lock_irqsave(&dc->lock, flags);
35580d3eef1SDmitry Osipenko 
356a4bfa096SThierry Reding 	value = WINDOW_A_SELECT << p->index;
35780d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
35880d3eef1SDmitry Osipenko 
35980d3eef1SDmitry Osipenko 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
36080d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
36180d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
36280d3eef1SDmitry Osipenko 
36380d3eef1SDmitry Osipenko 	spin_unlock_irqrestore(&dc->lock, flags);
36480d3eef1SDmitry Osipenko }
36580d3eef1SDmitry Osipenko 
3664aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
3674aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
3684aa3df71SThierry Reding {
3698f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
3704aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
3714aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
3724aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
3734aa3df71SThierry Reding 	struct tegra_dc_window window;
3744aa3df71SThierry Reding 	unsigned int i;
3754aa3df71SThierry Reding 
3764aa3df71SThierry Reding 	/* rien ne va plus */
3774aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
3784aa3df71SThierry Reding 		return;
3794aa3df71SThierry Reding 
38080d3eef1SDmitry Osipenko 	if (!plane->state->visible)
381a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
38280d3eef1SDmitry Osipenko 
383c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
3847d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
3857d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
3867d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
3877d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
3887d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
3897d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
3907d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
3917d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
392272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
393c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
394c7679306SThierry Reding 
3958f604f8cSThierry Reding 	/* copy from state */
3968f604f8cSThierry Reding 	window.tiling = state->tiling;
3978f604f8cSThierry Reding 	window.format = state->format;
3988f604f8cSThierry Reding 	window.swap = state->swap;
399c7679306SThierry Reding 
400bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
4014aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
402c7679306SThierry Reding 
4034aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
40408ee0178SDmitry Osipenko 
40508ee0178SDmitry Osipenko 		/*
40608ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
40708ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
40808ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
40908ee0178SDmitry Osipenko 		 */
41008ee0178SDmitry Osipenko 		if (i < 2)
4114aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
412c7679306SThierry Reding 	}
413c7679306SThierry Reding 
4144aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
4154aa3df71SThierry Reding }
4164aa3df71SThierry Reding 
417a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
4184aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
4194aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
420a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
421c7679306SThierry Reding };
422c7679306SThierry Reding 
423*47307954SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
424c7679306SThierry Reding 						    struct tegra_dc *dc)
425c7679306SThierry Reding {
426518e6227SThierry Reding 	/*
427518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
428518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
429518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
430518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
431518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
432518e6227SThierry Reding 	 * here.
433518e6227SThierry Reding 	 *
434518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
435518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
436518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
437518e6227SThierry Reding 	 */
438518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
439*47307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
440c7679306SThierry Reding 	struct tegra_plane *plane;
441c7679306SThierry Reding 	unsigned int num_formats;
442c7679306SThierry Reding 	const u32 *formats;
443c7679306SThierry Reding 	int err;
444c7679306SThierry Reding 
445c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
446c7679306SThierry Reding 	if (!plane)
447c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
448c7679306SThierry Reding 
449c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
450c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
451c7679306SThierry Reding 
452c4755fb9SThierry Reding 	/*
453c4755fb9SThierry Reding 	 * XXX compute offset so that we can directly access windows.
454c4755fb9SThierry Reding 	 *
455c4755fb9SThierry Reding 	 * Always use window A as primary window.
456c4755fb9SThierry Reding 	 */
457c4755fb9SThierry Reding 	plane->offset = 0;
458c4755fb9SThierry Reding 	plane->index = 0;
459c4755fb9SThierry Reding 	plane->depth = 255;
460c4755fb9SThierry Reding 
461518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
462c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
463*47307954SThierry Reding 				       num_formats, NULL, type, NULL);
464c7679306SThierry Reding 	if (err < 0) {
465c7679306SThierry Reding 		kfree(plane);
466c7679306SThierry Reding 		return ERR_PTR(err);
467c7679306SThierry Reding 	}
468c7679306SThierry Reding 
469a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
4704aa3df71SThierry Reding 
471c7679306SThierry Reding 	return &plane->base;
472c7679306SThierry Reding }
473c7679306SThierry Reding 
474c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
475c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
476c7679306SThierry Reding };
477c7679306SThierry Reding 
4784aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
4794aa3df71SThierry Reding 				     struct drm_plane_state *state)
480c7679306SThierry Reding {
48147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
48247802b09SThierry Reding 	int err;
48347802b09SThierry Reding 
4844aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
4854aa3df71SThierry Reding 	if (!state->crtc)
4864aa3df71SThierry Reding 		return 0;
487c7679306SThierry Reding 
488c7679306SThierry Reding 	/* scaling not supported for cursor */
4894aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
4904aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
491c7679306SThierry Reding 		return -EINVAL;
492c7679306SThierry Reding 
493c7679306SThierry Reding 	/* only square cursors supported */
4944aa3df71SThierry Reding 	if (state->src_w != state->src_h)
495c7679306SThierry Reding 		return -EINVAL;
496c7679306SThierry Reding 
4974aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
4984aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
4994aa3df71SThierry Reding 		return -EINVAL;
5004aa3df71SThierry Reding 
50147802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
50247802b09SThierry Reding 	if (err < 0)
50347802b09SThierry Reding 		return err;
50447802b09SThierry Reding 
5054aa3df71SThierry Reding 	return 0;
5064aa3df71SThierry Reding }
5074aa3df71SThierry Reding 
5084aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
5094aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
5104aa3df71SThierry Reding {
5114aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
5124aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5134aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
5144aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
5154aa3df71SThierry Reding 
5164aa3df71SThierry Reding 	/* rien ne va plus */
5174aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5184aa3df71SThierry Reding 		return;
5194aa3df71SThierry Reding 
5204aa3df71SThierry Reding 	switch (state->crtc_w) {
521c7679306SThierry Reding 	case 32:
522c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
523c7679306SThierry Reding 		break;
524c7679306SThierry Reding 
525c7679306SThierry Reding 	case 64:
526c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
527c7679306SThierry Reding 		break;
528c7679306SThierry Reding 
529c7679306SThierry Reding 	case 128:
530c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
531c7679306SThierry Reding 		break;
532c7679306SThierry Reding 
533c7679306SThierry Reding 	case 256:
534c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
535c7679306SThierry Reding 		break;
536c7679306SThierry Reding 
537c7679306SThierry Reding 	default:
5384aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
5394aa3df71SThierry Reding 		     state->crtc_h);
5404aa3df71SThierry Reding 		return;
541c7679306SThierry Reding 	}
542c7679306SThierry Reding 
543c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
544c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
545c7679306SThierry Reding 
546c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
547c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
548c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
549c7679306SThierry Reding #endif
550c7679306SThierry Reding 
551c7679306SThierry Reding 	/* enable cursor and set blend mode */
552c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
553c7679306SThierry Reding 	value |= CURSOR_ENABLE;
554c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
555c7679306SThierry Reding 
556c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
557c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
558c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
559c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
560c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
561c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
562c7679306SThierry Reding 	value |= CURSOR_ALPHA;
563c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
564c7679306SThierry Reding 
565c7679306SThierry Reding 	/* position the cursor */
5664aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
567c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
568c7679306SThierry Reding }
569c7679306SThierry Reding 
5704aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
5714aa3df71SThierry Reding 					struct drm_plane_state *old_state)
572c7679306SThierry Reding {
5734aa3df71SThierry Reding 	struct tegra_dc *dc;
574c7679306SThierry Reding 	u32 value;
575c7679306SThierry Reding 
5764aa3df71SThierry Reding 	/* rien ne va plus */
5774aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
5784aa3df71SThierry Reding 		return;
5794aa3df71SThierry Reding 
5804aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
581c7679306SThierry Reding 
582c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
583c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
584c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
585c7679306SThierry Reding }
586c7679306SThierry Reding 
5874aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
5884aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
5894aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
5904aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
591c7679306SThierry Reding };
592c7679306SThierry Reding 
593c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
594c7679306SThierry Reding 						      struct tegra_dc *dc)
595c7679306SThierry Reding {
596c7679306SThierry Reding 	struct tegra_plane *plane;
597c7679306SThierry Reding 	unsigned int num_formats;
598c7679306SThierry Reding 	const u32 *formats;
599c7679306SThierry Reding 	int err;
600c7679306SThierry Reding 
601c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
602c7679306SThierry Reding 	if (!plane)
603c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
604c7679306SThierry Reding 
60547802b09SThierry Reding 	/*
606a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
607a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
608a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
609a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
610a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
61147802b09SThierry Reding 	 */
61247802b09SThierry Reding 	plane->index = 6;
61347802b09SThierry Reding 
614c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
615c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
616c7679306SThierry Reding 
617c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
618c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
619e6fc3b68SBen Widawsky 				       num_formats, NULL,
620e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
621c7679306SThierry Reding 	if (err < 0) {
622c7679306SThierry Reding 		kfree(plane);
623c7679306SThierry Reding 		return ERR_PTR(err);
624c7679306SThierry Reding 	}
625c7679306SThierry Reding 
6264aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
6274aa3df71SThierry Reding 
628c7679306SThierry Reding 	return &plane->base;
629c7679306SThierry Reding }
630c7679306SThierry Reding 
631c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
632dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
633dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
634dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
635dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
636f925390eSThierry Reding 	DRM_FORMAT_YUYV,
637dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
638dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
639dee8268fSThierry Reding };
640dee8268fSThierry Reding 
641c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
642c7679306SThierry Reding 						       struct tegra_dc *dc,
643c7679306SThierry Reding 						       unsigned int index)
644dee8268fSThierry Reding {
645dee8268fSThierry Reding 	struct tegra_plane *plane;
646c7679306SThierry Reding 	unsigned int num_formats;
647c7679306SThierry Reding 	const u32 *formats;
648c7679306SThierry Reding 	int err;
649dee8268fSThierry Reding 
650f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
651dee8268fSThierry Reding 	if (!plane)
652c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
653dee8268fSThierry Reding 
654c4755fb9SThierry Reding 	/* XXX compute offset so that we can directly access windows */
655c4755fb9SThierry Reding 	plane->offset = 0;
656c7679306SThierry Reding 	plane->index = index;
657c4755fb9SThierry Reding 	plane->depth = 0;
658dee8268fSThierry Reding 
659c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
660c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
661c7679306SThierry Reding 
662c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
663301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
664e6fc3b68SBen Widawsky 				       num_formats, NULL,
665e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_OVERLAY, NULL);
666f002abc1SThierry Reding 	if (err < 0) {
667f002abc1SThierry Reding 		kfree(plane);
668c7679306SThierry Reding 		return ERR_PTR(err);
669dee8268fSThierry Reding 	}
670c7679306SThierry Reding 
671a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
6724aa3df71SThierry Reding 
673c7679306SThierry Reding 	return &plane->base;
674c7679306SThierry Reding }
675c7679306SThierry Reding 
676*47307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
677*47307954SThierry Reding 						    struct tegra_dc *dc)
678c7679306SThierry Reding {
679*47307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
680*47307954SThierry Reding 	unsigned int i, j;
681*47307954SThierry Reding 
682*47307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
683*47307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
684*47307954SThierry Reding 
685*47307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
686*47307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
687*47307954SThierry Reding 				unsigned int index = wgrp->windows[j];
688*47307954SThierry Reding 
689*47307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
690*47307954SThierry Reding 								  wgrp->index,
691*47307954SThierry Reding 								  index);
692*47307954SThierry Reding 				if (IS_ERR(plane))
693*47307954SThierry Reding 					return plane;
694*47307954SThierry Reding 
695*47307954SThierry Reding 				/*
696*47307954SThierry Reding 				 * Choose the first shared plane owned by this
697*47307954SThierry Reding 				 * head as the primary plane.
698*47307954SThierry Reding 				 */
699*47307954SThierry Reding 				if (!primary) {
700*47307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
701*47307954SThierry Reding 					primary = plane;
702*47307954SThierry Reding 				}
703*47307954SThierry Reding 			}
704*47307954SThierry Reding 		}
705*47307954SThierry Reding 	}
706*47307954SThierry Reding 
707*47307954SThierry Reding 	return primary;
708*47307954SThierry Reding }
709*47307954SThierry Reding 
710*47307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
711*47307954SThierry Reding 					     struct tegra_dc *dc)
712*47307954SThierry Reding {
713*47307954SThierry Reding 	struct drm_plane *plane, *primary;
714c7679306SThierry Reding 	unsigned int i;
715c7679306SThierry Reding 
716*47307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
717*47307954SThierry Reding 	if (IS_ERR(primary))
718*47307954SThierry Reding 		return primary;
719*47307954SThierry Reding 
720c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
721c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
722*47307954SThierry Reding 		if (IS_ERR(plane)) {
723*47307954SThierry Reding 			/* XXX tegra_plane_destroy() */
724*47307954SThierry Reding 			drm_plane_cleanup(primary);
725*47307954SThierry Reding 			kfree(primary);
726*47307954SThierry Reding 			return plane;
727*47307954SThierry Reding 		}
728f002abc1SThierry Reding 	}
729dee8268fSThierry Reding 
730*47307954SThierry Reding 	return primary;
731dee8268fSThierry Reding }
732dee8268fSThierry Reding 
733f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
734f002abc1SThierry Reding {
735f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
736f002abc1SThierry Reding }
737f002abc1SThierry Reding 
738ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
739ca915b10SThierry Reding {
740ca915b10SThierry Reding 	struct tegra_dc_state *state;
741ca915b10SThierry Reding 
7423b59b7acSThierry Reding 	if (crtc->state)
743ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
7443b59b7acSThierry Reding 
745ca915b10SThierry Reding 	kfree(crtc->state);
746ca915b10SThierry Reding 	crtc->state = NULL;
747ca915b10SThierry Reding 
748ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
749332bbe70SThierry Reding 	if (state) {
750ca915b10SThierry Reding 		crtc->state = &state->base;
751332bbe70SThierry Reding 		crtc->state->crtc = crtc;
752332bbe70SThierry Reding 	}
75331930d4dSThierry Reding 
75431930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
755ca915b10SThierry Reding }
756ca915b10SThierry Reding 
757ca915b10SThierry Reding static struct drm_crtc_state *
758ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
759ca915b10SThierry Reding {
760ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
761ca915b10SThierry Reding 	struct tegra_dc_state *copy;
762ca915b10SThierry Reding 
7633b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
764ca915b10SThierry Reding 	if (!copy)
765ca915b10SThierry Reding 		return NULL;
766ca915b10SThierry Reding 
7673b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
7683b59b7acSThierry Reding 	copy->clk = state->clk;
7693b59b7acSThierry Reding 	copy->pclk = state->pclk;
7703b59b7acSThierry Reding 	copy->div = state->div;
7713b59b7acSThierry Reding 	copy->planes = state->planes;
772ca915b10SThierry Reding 
773ca915b10SThierry Reding 	return &copy->base;
774ca915b10SThierry Reding }
775ca915b10SThierry Reding 
776ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
777ca915b10SThierry Reding 					    struct drm_crtc_state *state)
778ca915b10SThierry Reding {
779ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
780ca915b10SThierry Reding 	kfree(state);
781ca915b10SThierry Reding }
782ca915b10SThierry Reding 
783b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
784b95800eeSThierry Reding 
785b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
786b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
787b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
788b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
789b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
790b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
791b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
792b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
793b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
794b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
795b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
796b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
797b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
798b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
799b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
800b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
801b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
802b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
803b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
804b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
805b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
806b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
807b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
808b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
809b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
810b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
811b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
812b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
813b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
814b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
815b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
816b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
817b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
818b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
819b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
820b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
821b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
822b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
823b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
824b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
825b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
826b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
827b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
828b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
829b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
830b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
831b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
832b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
833b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
834b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
835b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
836b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
837b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
838b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
839b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
840b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
841b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
842b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
843b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
844b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
845b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
846b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
847b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
848b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
849b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
850b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
851b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
852b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
853b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
854b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
855b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
856b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
857b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
858b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
859b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
860b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
861b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
862b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
863b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
864b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
865b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
866b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
867b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
868b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
869b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
870b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
871b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
872b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
873b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
874b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
875b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
876b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
877b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
878b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
879b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
880b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
881b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
882b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
883b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
884b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
885b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
886b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
887b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
888b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
889b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
890b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
891b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
892b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
893b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
894b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
895b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
896b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
897b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
898b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
899b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
900b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
901b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
902b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
903b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
904b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
905b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
906b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
907b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
908b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
909b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
910b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
911b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
912b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
913b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
914b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
915b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
916b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
917b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
918b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
919b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
920b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
921b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
922b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
923b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
924b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
925b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
926b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
927b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
928b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
929b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
930b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
931b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
932b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
933b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
934b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
935b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
936b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
937b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
938b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
939b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
940b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
941b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
942b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
943b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
944b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
945b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
946b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
947b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
948b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
949b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
950b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
951b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
952b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
953b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
954b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
955b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
956b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
957b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
958b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
959b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
960b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
961b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
962b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
963b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
964b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
965b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
966b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
967b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
968b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
969b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
970b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
971b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
972b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
973b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
974b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
975b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
976b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
977b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
978b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
979b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
980b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
981b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
982b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
983b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
984b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
985b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
986b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
987b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
988b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
989b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
990b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
991b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
992b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
993b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
994b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
995b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
996b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
997b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
998b95800eeSThierry Reding };
999b95800eeSThierry Reding 
1000b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1001b95800eeSThierry Reding {
1002b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1003b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1004b95800eeSThierry Reding 	unsigned int i;
1005b95800eeSThierry Reding 	int err = 0;
1006b95800eeSThierry Reding 
1007b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1008b95800eeSThierry Reding 
1009b95800eeSThierry Reding 	if (!dc->base.state->active) {
1010b95800eeSThierry Reding 		err = -EBUSY;
1011b95800eeSThierry Reding 		goto unlock;
1012b95800eeSThierry Reding 	}
1013b95800eeSThierry Reding 
1014b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1015b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1016b95800eeSThierry Reding 
1017b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1018b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1019b95800eeSThierry Reding 	}
1020b95800eeSThierry Reding 
1021b95800eeSThierry Reding unlock:
1022b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1023b95800eeSThierry Reding 	return err;
1024b95800eeSThierry Reding }
1025b95800eeSThierry Reding 
1026b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1027b95800eeSThierry Reding {
1028b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1029b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1030b95800eeSThierry Reding 	int err = 0;
1031b95800eeSThierry Reding 	u32 value;
1032b95800eeSThierry Reding 
1033b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1034b95800eeSThierry Reding 
1035b95800eeSThierry Reding 	if (!dc->base.state->active) {
1036b95800eeSThierry Reding 		err = -EBUSY;
1037b95800eeSThierry Reding 		goto unlock;
1038b95800eeSThierry Reding 	}
1039b95800eeSThierry Reding 
1040b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1041b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1042b95800eeSThierry Reding 	tegra_dc_commit(dc);
1043b95800eeSThierry Reding 
1044b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1045b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1046b95800eeSThierry Reding 
1047b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1048b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1049b95800eeSThierry Reding 
1050b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1051b95800eeSThierry Reding 
1052b95800eeSThierry Reding unlock:
1053b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1054b95800eeSThierry Reding 	return err;
1055b95800eeSThierry Reding }
1056b95800eeSThierry Reding 
1057b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1058b95800eeSThierry Reding {
1059b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1060b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1061b95800eeSThierry Reding 
1062b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1063b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1064b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1065b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1066b95800eeSThierry Reding 
1067b95800eeSThierry Reding 	return 0;
1068b95800eeSThierry Reding }
1069b95800eeSThierry Reding 
1070b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1071b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1072b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1073b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1074b95800eeSThierry Reding };
1075b95800eeSThierry Reding 
1076b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1077b95800eeSThierry Reding {
1078b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1079b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1080b95800eeSThierry Reding 	struct dentry *root = crtc->debugfs_entry;
1081b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1082b95800eeSThierry Reding 	int err;
1083b95800eeSThierry Reding 
1084b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1085b95800eeSThierry Reding 				    GFP_KERNEL);
1086b95800eeSThierry Reding 	if (!dc->debugfs_files)
1087b95800eeSThierry Reding 		return -ENOMEM;
1088b95800eeSThierry Reding 
1089b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1090b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1091b95800eeSThierry Reding 
1092b95800eeSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1093b95800eeSThierry Reding 	if (err < 0)
1094b95800eeSThierry Reding 		goto free;
1095b95800eeSThierry Reding 
1096b95800eeSThierry Reding 	return 0;
1097b95800eeSThierry Reding 
1098b95800eeSThierry Reding free:
1099b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1100b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1101b95800eeSThierry Reding 
1102b95800eeSThierry Reding 	return err;
1103b95800eeSThierry Reding }
1104b95800eeSThierry Reding 
1105b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1106b95800eeSThierry Reding {
1107b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1108b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1109b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1110b95800eeSThierry Reding 
1111b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1112b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1113b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1114b95800eeSThierry Reding }
1115b95800eeSThierry Reding 
1116c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1117c49c81e2SThierry Reding {
1118c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1119c49c81e2SThierry Reding 
1120*47307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
1121*47307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1122c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1123c49c81e2SThierry Reding 
1124c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
1125c49c81e2SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
1126c49c81e2SThierry Reding }
1127c49c81e2SThierry Reding 
1128c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1129c49c81e2SThierry Reding {
1130c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1131c49c81e2SThierry Reding 	unsigned long value, flags;
1132c49c81e2SThierry Reding 
1133c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
1134c49c81e2SThierry Reding 
1135c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1136c49c81e2SThierry Reding 	value |= VBLANK_INT;
1137c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1138c49c81e2SThierry Reding 
1139c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
1140c49c81e2SThierry Reding 
1141c49c81e2SThierry Reding 	return 0;
1142c49c81e2SThierry Reding }
1143c49c81e2SThierry Reding 
1144c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1145c49c81e2SThierry Reding {
1146c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1147c49c81e2SThierry Reding 	unsigned long value, flags;
1148c49c81e2SThierry Reding 
1149c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
1150c49c81e2SThierry Reding 
1151c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1152c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1153c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1154c49c81e2SThierry Reding 
1155c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
1156c49c81e2SThierry Reding }
1157c49c81e2SThierry Reding 
1158dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
11591503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
116074f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1161f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1162ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1163ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1164ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1165b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1166b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
116710437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
116810437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
116910437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1170dee8268fSThierry Reding };
1171dee8268fSThierry Reding 
1172dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1173dee8268fSThierry Reding 				struct drm_display_mode *mode)
1174dee8268fSThierry Reding {
11750444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
11760444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1177dee8268fSThierry Reding 	unsigned long value;
1178dee8268fSThierry Reding 
1179*47307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1180dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1181dee8268fSThierry Reding 
1182dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1183dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1184*47307954SThierry Reding 	}
1185dee8268fSThierry Reding 
1186dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1187dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1188dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1189dee8268fSThierry Reding 
1190dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1191dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1192dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1193dee8268fSThierry Reding 
1194dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1195dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1196dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1197dee8268fSThierry Reding 
1198dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1199dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1200dee8268fSThierry Reding 
1201dee8268fSThierry Reding 	return 0;
1202dee8268fSThierry Reding }
1203dee8268fSThierry Reding 
12049d910b60SThierry Reding /**
12059d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
12069d910b60SThierry Reding  *     state
12079d910b60SThierry Reding  * @dc: display controller
12089d910b60SThierry Reding  * @crtc_state: CRTC atomic state
12099d910b60SThierry Reding  * @clk: parent clock for display controller
12109d910b60SThierry Reding  * @pclk: pixel clock
12119d910b60SThierry Reding  * @div: shift clock divider
12129d910b60SThierry Reding  *
12139d910b60SThierry Reding  * Returns:
12149d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
12159d910b60SThierry Reding  */
1216ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1217ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1218ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1219ca915b10SThierry Reding 			       unsigned int div)
1220ca915b10SThierry Reding {
1221ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1222ca915b10SThierry Reding 
1223d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1224d2982748SThierry Reding 		return -EINVAL;
1225d2982748SThierry Reding 
1226ca915b10SThierry Reding 	state->clk = clk;
1227ca915b10SThierry Reding 	state->pclk = pclk;
1228ca915b10SThierry Reding 	state->div = div;
1229ca915b10SThierry Reding 
1230ca915b10SThierry Reding 	return 0;
1231ca915b10SThierry Reding }
1232ca915b10SThierry Reding 
123376d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
123476d59ed0SThierry Reding 				  struct tegra_dc_state *state)
123576d59ed0SThierry Reding {
123676d59ed0SThierry Reding 	u32 value;
123776d59ed0SThierry Reding 	int err;
123876d59ed0SThierry Reding 
123976d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
124076d59ed0SThierry Reding 	if (err < 0)
124176d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
124276d59ed0SThierry Reding 
124376d59ed0SThierry Reding 	/*
124476d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
124576d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
124676d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
124776d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
124876d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
124976d59ed0SThierry Reding 	 * should therefore be avoided.
125076d59ed0SThierry Reding 	 */
125176d59ed0SThierry Reding 	if (state->pclk > 0) {
125276d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
125376d59ed0SThierry Reding 		if (err < 0)
125476d59ed0SThierry Reding 			dev_err(dc->dev,
125576d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
125676d59ed0SThierry Reding 				state->pclk);
125776d59ed0SThierry Reding 	}
125876d59ed0SThierry Reding 
125976d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
126076d59ed0SThierry Reding 		      state->div);
126176d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
126276d59ed0SThierry Reding 
1263*47307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
126476d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
126576d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1266*47307954SThierry Reding 	}
126739e08affSThierry Reding 
126839e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
126939e08affSThierry Reding 	if (err < 0)
127039e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
127139e08affSThierry Reding 			dc->clk, state->pclk, err);
127276d59ed0SThierry Reding }
127376d59ed0SThierry Reding 
1274003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1275003fc848SThierry Reding {
1276003fc848SThierry Reding 	u32 value;
1277003fc848SThierry Reding 
1278003fc848SThierry Reding 	/* stop the display controller */
1279003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1280003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1281003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1282003fc848SThierry Reding 
1283003fc848SThierry Reding 	tegra_dc_commit(dc);
1284003fc848SThierry Reding }
1285003fc848SThierry Reding 
1286003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1287003fc848SThierry Reding {
1288003fc848SThierry Reding 	u32 value;
1289003fc848SThierry Reding 
1290003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1291003fc848SThierry Reding 
1292003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1293003fc848SThierry Reding }
1294003fc848SThierry Reding 
1295003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1296003fc848SThierry Reding {
1297003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1298003fc848SThierry Reding 
1299003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1300003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1301003fc848SThierry Reding 			return 0;
1302003fc848SThierry Reding 
1303003fc848SThierry Reding 		usleep_range(1000, 2000);
1304003fc848SThierry Reding 	}
1305003fc848SThierry Reding 
1306003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1307003fc848SThierry Reding 	return -ETIMEDOUT;
1308003fc848SThierry Reding }
1309003fc848SThierry Reding 
131064581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
131164581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1312003fc848SThierry Reding {
1313003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1314003fc848SThierry Reding 	u32 value;
1315003fc848SThierry Reding 
1316003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1317003fc848SThierry Reding 		tegra_dc_stop(dc);
1318003fc848SThierry Reding 
1319003fc848SThierry Reding 		/*
1320003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1321003fc848SThierry Reding 		 * in case this fails.
1322003fc848SThierry Reding 		 */
1323003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1324003fc848SThierry Reding 	}
1325003fc848SThierry Reding 
1326003fc848SThierry Reding 	/*
1327003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1328003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1329003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1330003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1331003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1332003fc848SThierry Reding 	 * to go idle.
1333003fc848SThierry Reding 	 *
1334003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1335003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1336003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1337003fc848SThierry Reding 	 *
1338003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1339003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1340003fc848SThierry Reding 	 * the RGB encoder?
1341003fc848SThierry Reding 	 */
1342003fc848SThierry Reding 	if (dc->rgb) {
1343003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1344003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1345003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1346003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1347003fc848SThierry Reding 	}
1348003fc848SThierry Reding 
1349003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1350003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
135133a8eb8dSThierry Reding 
13529d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
13539d99ab6eSThierry Reding 
13549d99ab6eSThierry Reding 	if (crtc->state->event) {
13559d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
13569d99ab6eSThierry Reding 		crtc->state->event = NULL;
13579d99ab6eSThierry Reding 	}
13589d99ab6eSThierry Reding 
13599d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
13609d99ab6eSThierry Reding 
136133a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1362003fc848SThierry Reding }
1363003fc848SThierry Reding 
13640b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
13650b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1366dee8268fSThierry Reding {
13674aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
136876d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1369dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1370dbb3f2f7SThierry Reding 	u32 value;
1371dee8268fSThierry Reding 
137233a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
137333a8eb8dSThierry Reding 
137433a8eb8dSThierry Reding 	/* initialize display controller */
137533a8eb8dSThierry Reding 	if (dc->syncpt) {
1376*47307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
1377*47307954SThierry Reding 
1378*47307954SThierry Reding 		if (dc->soc->has_nvdisplay)
1379*47307954SThierry Reding 			enable = 1 << 31;
1380*47307954SThierry Reding 		else
1381*47307954SThierry Reding 			enable = 1 << 8;
138233a8eb8dSThierry Reding 
138333a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
138433a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
138533a8eb8dSThierry Reding 
1386*47307954SThierry Reding 		value = enable | syncpt;
138733a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
138833a8eb8dSThierry Reding 	}
138933a8eb8dSThierry Reding 
1390*47307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
1391*47307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
1392*47307954SThierry Reding 			DSC_OBUF_UF_INT;
1393*47307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1394*47307954SThierry Reding 
1395*47307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
1396*47307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
1397*47307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
1398*47307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
1399*47307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
1400*47307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1401*47307954SThierry Reding 
1402*47307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
1403*47307954SThierry Reding 			FRAME_END_INT;
1404*47307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1405*47307954SThierry Reding 
1406*47307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
1407*47307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1408*47307954SThierry Reding 
1409*47307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
1410*47307954SThierry Reding 	} else {
141133a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
141233a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
141333a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
141433a8eb8dSThierry Reding 
141533a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
141633a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
141733a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
141833a8eb8dSThierry Reding 
141933a8eb8dSThierry Reding 		/* initialize timer */
142033a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
142133a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
142233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
142333a8eb8dSThierry Reding 
142433a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
142533a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
142633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
142733a8eb8dSThierry Reding 
142833a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
142933a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
143033a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
143133a8eb8dSThierry Reding 
143233a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
143333a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
143433a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1435*47307954SThierry Reding 	}
143633a8eb8dSThierry Reding 
14377116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
14387116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
14397116e9a8SThierry Reding 	else
144033a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
144133a8eb8dSThierry Reding 
144233a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
144376d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
144476d59ed0SThierry Reding 
1445dee8268fSThierry Reding 	/* program display mode */
1446dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1447dee8268fSThierry Reding 
14488620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
14498620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
14508620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
14518620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
14528620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
14538620fc62SThierry Reding 	}
1454666cb873SThierry Reding 
1455666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1456666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1457666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1458666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1459666cb873SThierry Reding 
1460*47307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1461666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1462666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1463666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1464666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1465*47307954SThierry Reding 	}
1466*47307954SThierry Reding 
1467*47307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
1468*47307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
1469*47307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
1470*47307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
1471*47307954SThierry Reding 	}
1472666cb873SThierry Reding 
1473666cb873SThierry Reding 	tegra_dc_commit(dc);
1474dee8268fSThierry Reding 
14758ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1476dee8268fSThierry Reding }
1477dee8268fSThierry Reding 
14784aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
14794aa3df71SThierry Reding 				   struct drm_crtc_state *state)
14804aa3df71SThierry Reding {
1481c4755fb9SThierry Reding 	struct tegra_atomic_state *s = to_tegra_atomic_state(state->state);
1482c4755fb9SThierry Reding 	struct tegra_dc_state *tegra = to_dc_state(state);
1483c4755fb9SThierry Reding 
1484c4755fb9SThierry Reding 	/*
1485c4755fb9SThierry Reding 	 * The display hub display clock needs to be fed by the display clock
1486c4755fb9SThierry Reding 	 * with the highest frequency to ensure proper functioning of all the
1487c4755fb9SThierry Reding 	 * displays.
1488c4755fb9SThierry Reding 	 *
1489c4755fb9SThierry Reding 	 * Note that this isn't used before Tegra186, but it doesn't hurt and
1490c4755fb9SThierry Reding 	 * conditionalizing it would make the code less clean.
1491c4755fb9SThierry Reding 	 */
1492c4755fb9SThierry Reding 	if (state->active) {
1493c4755fb9SThierry Reding 		if (!s->clk_disp || tegra->pclk > s->rate) {
1494c4755fb9SThierry Reding 			s->dc = to_tegra_dc(crtc);
1495c4755fb9SThierry Reding 			s->clk_disp = s->dc->clk;
1496c4755fb9SThierry Reding 			s->rate = tegra->pclk;
1497c4755fb9SThierry Reding 		}
1498c4755fb9SThierry Reding 	}
1499c4755fb9SThierry Reding 
15004aa3df71SThierry Reding 	return 0;
15014aa3df71SThierry Reding }
15024aa3df71SThierry Reding 
1503613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1504613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
15054aa3df71SThierry Reding {
15069d99ab6eSThierry Reding 	unsigned long flags;
15071503ca47SThierry Reding 
15081503ca47SThierry Reding 	if (crtc->state->event) {
15099d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
15101503ca47SThierry Reding 
15119d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
15129d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
15139d99ab6eSThierry Reding 		else
15149d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
15151503ca47SThierry Reding 
15169d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
15179d99ab6eSThierry Reding 
15181503ca47SThierry Reding 		crtc->state->event = NULL;
15191503ca47SThierry Reding 	}
15204aa3df71SThierry Reding }
15214aa3df71SThierry Reding 
1522613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1523613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
15244aa3df71SThierry Reding {
152547802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
152647802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1527*47307954SThierry Reding 	u32 value;
152847802b09SThierry Reding 
1529*47307954SThierry Reding 	value = state->planes << 8 | GENERAL_UPDATE;
1530*47307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
1531*47307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
1532*47307954SThierry Reding 
1533*47307954SThierry Reding 	value = state->planes | GENERAL_ACT_REQ;
1534*47307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
1535*47307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
15364aa3df71SThierry Reding }
15374aa3df71SThierry Reding 
1538dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
15394aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
15404aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
15414aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
15420b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
154364581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1544dee8268fSThierry Reding };
1545dee8268fSThierry Reding 
1546dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1547dee8268fSThierry Reding {
1548dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1549dee8268fSThierry Reding 	unsigned long status;
1550dee8268fSThierry Reding 
1551dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1552dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1553dee8268fSThierry Reding 
1554dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1555dee8268fSThierry Reding 		/*
1556dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1557dee8268fSThierry Reding 		*/
1558791ddb1eSThierry Reding 		dc->stats.frames++;
1559dee8268fSThierry Reding 	}
1560dee8268fSThierry Reding 
1561dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1562dee8268fSThierry Reding 		/*
1563dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1564dee8268fSThierry Reding 		*/
1565ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1566791ddb1eSThierry Reding 		dc->stats.vblank++;
1567dee8268fSThierry Reding 	}
1568dee8268fSThierry Reding 
1569dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1570dee8268fSThierry Reding 		/*
1571dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1572dee8268fSThierry Reding 		*/
1573791ddb1eSThierry Reding 		dc->stats.underflow++;
1574791ddb1eSThierry Reding 	}
1575791ddb1eSThierry Reding 
1576791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1577791ddb1eSThierry Reding 		/*
1578791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1579791ddb1eSThierry Reding 		*/
1580791ddb1eSThierry Reding 		dc->stats.overflow++;
1581dee8268fSThierry Reding 	}
1582dee8268fSThierry Reding 
1583*47307954SThierry Reding 	if (status & HEAD_UF_INT) {
1584*47307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
1585*47307954SThierry Reding 		dc->stats.underflow++;
1586*47307954SThierry Reding 	}
1587*47307954SThierry Reding 
1588dee8268fSThierry Reding 	return IRQ_HANDLED;
1589dee8268fSThierry Reding }
1590dee8268fSThierry Reding 
1591dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1592dee8268fSThierry Reding {
15939910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
15942bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1595dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1596d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1597c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1598c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1599dee8268fSThierry Reding 	int err;
1600dee8268fSThierry Reding 
1601617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
16022bcdcbfaSThierry Reding 	if (!dc->syncpt)
16032bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
16042bcdcbfaSThierry Reding 
1605df06b759SThierry Reding 	if (tegra->domain) {
1606df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1607df06b759SThierry Reding 		if (err < 0) {
1608df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1609df06b759SThierry Reding 				err);
1610df06b759SThierry Reding 			return err;
1611df06b759SThierry Reding 		}
1612df06b759SThierry Reding 
1613df06b759SThierry Reding 		dc->domain = tegra->domain;
1614df06b759SThierry Reding 	}
1615df06b759SThierry Reding 
1616*47307954SThierry Reding 	if (dc->soc->wgrps)
1617*47307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
1618*47307954SThierry Reding 	else
1619*47307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
1620*47307954SThierry Reding 
1621c7679306SThierry Reding 	if (IS_ERR(primary)) {
1622c7679306SThierry Reding 		err = PTR_ERR(primary);
1623c7679306SThierry Reding 		goto cleanup;
1624c7679306SThierry Reding 	}
1625c7679306SThierry Reding 
1626c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1627c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1628c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1629c7679306SThierry Reding 			err = PTR_ERR(cursor);
1630c7679306SThierry Reding 			goto cleanup;
1631c7679306SThierry Reding 		}
1632c7679306SThierry Reding 	}
1633c7679306SThierry Reding 
1634c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1635f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1636c7679306SThierry Reding 	if (err < 0)
1637c7679306SThierry Reding 		goto cleanup;
1638c7679306SThierry Reding 
1639dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1640dee8268fSThierry Reding 
1641d1f3e1e0SThierry Reding 	/*
1642d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1643d1f3e1e0SThierry Reding 	 * controllers.
1644d1f3e1e0SThierry Reding 	 */
1645d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1646d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1647d1f3e1e0SThierry Reding 
16489910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1649dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1650dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1651c7679306SThierry Reding 		goto cleanup;
1652dee8268fSThierry Reding 	}
1653dee8268fSThierry Reding 
1654dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1655dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1656dee8268fSThierry Reding 	if (err < 0) {
1657dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1658dee8268fSThierry Reding 			err);
1659c7679306SThierry Reding 		goto cleanup;
1660dee8268fSThierry Reding 	}
1661dee8268fSThierry Reding 
1662dee8268fSThierry Reding 	return 0;
1663c7679306SThierry Reding 
1664c7679306SThierry Reding cleanup:
1665*47307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
1666c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1667c7679306SThierry Reding 
1668*47307954SThierry Reding 	if (!IS_ERR(primary))
1669c7679306SThierry Reding 		drm_plane_cleanup(primary);
1670c7679306SThierry Reding 
1671c7679306SThierry Reding 	if (tegra->domain) {
1672c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1673c7679306SThierry Reding 		dc->domain = NULL;
1674c7679306SThierry Reding 	}
1675c7679306SThierry Reding 
1676c7679306SThierry Reding 	return err;
1677dee8268fSThierry Reding }
1678dee8268fSThierry Reding 
1679dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1680dee8268fSThierry Reding {
1681dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1682dee8268fSThierry Reding 	int err;
1683dee8268fSThierry Reding 
1684dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1685dee8268fSThierry Reding 
1686dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1687dee8268fSThierry Reding 	if (err) {
1688dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1689dee8268fSThierry Reding 		return err;
1690dee8268fSThierry Reding 	}
1691dee8268fSThierry Reding 
1692df06b759SThierry Reding 	if (dc->domain) {
1693df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1694df06b759SThierry Reding 		dc->domain = NULL;
1695df06b759SThierry Reding 	}
1696df06b759SThierry Reding 
16972bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
16982bcdcbfaSThierry Reding 
1699dee8268fSThierry Reding 	return 0;
1700dee8268fSThierry Reding }
1701dee8268fSThierry Reding 
1702dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1703dee8268fSThierry Reding 	.init = tegra_dc_init,
1704dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1705dee8268fSThierry Reding };
1706dee8268fSThierry Reding 
17078620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
17087116e9a8SThierry Reding 	.supports_background_color = false,
17098620fc62SThierry Reding 	.supports_interlacing = false,
1710e687651bSThierry Reding 	.supports_cursor = false,
1711c134f019SThierry Reding 	.supports_block_linear = false,
1712d1f3e1e0SThierry Reding 	.pitch_align = 8,
17139c012700SThierry Reding 	.has_powergate = false,
17146ac1571bSDmitry Osipenko 	.broken_reset = true,
1715*47307954SThierry Reding 	.has_nvdisplay = false,
17168620fc62SThierry Reding };
17178620fc62SThierry Reding 
17188620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
17197116e9a8SThierry Reding 	.supports_background_color = false,
17208620fc62SThierry Reding 	.supports_interlacing = false,
1721e687651bSThierry Reding 	.supports_cursor = false,
1722c134f019SThierry Reding 	.supports_block_linear = false,
1723d1f3e1e0SThierry Reding 	.pitch_align = 8,
17249c012700SThierry Reding 	.has_powergate = false,
17256ac1571bSDmitry Osipenko 	.broken_reset = false,
1726*47307954SThierry Reding 	.has_nvdisplay = false,
1727d1f3e1e0SThierry Reding };
1728d1f3e1e0SThierry Reding 
1729d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
17307116e9a8SThierry Reding 	.supports_background_color = false,
1731d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1732d1f3e1e0SThierry Reding 	.supports_cursor = false,
1733d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1734d1f3e1e0SThierry Reding 	.pitch_align = 64,
17359c012700SThierry Reding 	.has_powergate = true,
17366ac1571bSDmitry Osipenko 	.broken_reset = false,
1737*47307954SThierry Reding 	.has_nvdisplay = false,
17388620fc62SThierry Reding };
17398620fc62SThierry Reding 
17408620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
17417116e9a8SThierry Reding 	.supports_background_color = true,
17428620fc62SThierry Reding 	.supports_interlacing = true,
1743e687651bSThierry Reding 	.supports_cursor = true,
1744c134f019SThierry Reding 	.supports_block_linear = true,
1745d1f3e1e0SThierry Reding 	.pitch_align = 64,
17469c012700SThierry Reding 	.has_powergate = true,
17476ac1571bSDmitry Osipenko 	.broken_reset = false,
1748*47307954SThierry Reding 	.has_nvdisplay = false,
17498620fc62SThierry Reding };
17508620fc62SThierry Reding 
17515b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
17527116e9a8SThierry Reding 	.supports_background_color = true,
17535b4f516fSThierry Reding 	.supports_interlacing = true,
17545b4f516fSThierry Reding 	.supports_cursor = true,
17555b4f516fSThierry Reding 	.supports_block_linear = true,
17565b4f516fSThierry Reding 	.pitch_align = 64,
17575b4f516fSThierry Reding 	.has_powergate = true,
17586ac1571bSDmitry Osipenko 	.broken_reset = false,
1759*47307954SThierry Reding 	.has_nvdisplay = false,
1760*47307954SThierry Reding };
1761*47307954SThierry Reding 
1762*47307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
1763*47307954SThierry Reding 	{
1764*47307954SThierry Reding 		.index = 0,
1765*47307954SThierry Reding 		.dc = 0,
1766*47307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
1767*47307954SThierry Reding 		.num_windows = 1,
1768*47307954SThierry Reding 	}, {
1769*47307954SThierry Reding 		.index = 1,
1770*47307954SThierry Reding 		.dc = 1,
1771*47307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
1772*47307954SThierry Reding 		.num_windows = 1,
1773*47307954SThierry Reding 	}, {
1774*47307954SThierry Reding 		.index = 2,
1775*47307954SThierry Reding 		.dc = 1,
1776*47307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
1777*47307954SThierry Reding 		.num_windows = 1,
1778*47307954SThierry Reding 	}, {
1779*47307954SThierry Reding 		.index = 3,
1780*47307954SThierry Reding 		.dc = 2,
1781*47307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
1782*47307954SThierry Reding 		.num_windows = 1,
1783*47307954SThierry Reding 	}, {
1784*47307954SThierry Reding 		.index = 4,
1785*47307954SThierry Reding 		.dc = 2,
1786*47307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
1787*47307954SThierry Reding 		.num_windows = 1,
1788*47307954SThierry Reding 	}, {
1789*47307954SThierry Reding 		.index = 5,
1790*47307954SThierry Reding 		.dc = 2,
1791*47307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
1792*47307954SThierry Reding 		.num_windows = 1,
1793*47307954SThierry Reding 	},
1794*47307954SThierry Reding };
1795*47307954SThierry Reding 
1796*47307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
1797*47307954SThierry Reding 	.supports_background_color = true,
1798*47307954SThierry Reding 	.supports_interlacing = true,
1799*47307954SThierry Reding 	.supports_cursor = true,
1800*47307954SThierry Reding 	.supports_block_linear = true,
1801*47307954SThierry Reding 	.pitch_align = 64,
1802*47307954SThierry Reding 	.has_powergate = false,
1803*47307954SThierry Reding 	.broken_reset = false,
1804*47307954SThierry Reding 	.has_nvdisplay = true,
1805*47307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
1806*47307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
18075b4f516fSThierry Reding };
18085b4f516fSThierry Reding 
18098620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
18108620fc62SThierry Reding 	{
1811*47307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
1812*47307954SThierry Reding 		.data = &tegra186_dc_soc_info,
1813*47307954SThierry Reding 	}, {
18145b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
18155b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
18165b4f516fSThierry Reding 	}, {
18178620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
18188620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
18198620fc62SThierry Reding 	}, {
18209c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
18219c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
18229c012700SThierry Reding 	}, {
18238620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
18248620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
18258620fc62SThierry Reding 	}, {
18268620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
18278620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
18288620fc62SThierry Reding 	}, {
18298620fc62SThierry Reding 		/* sentinel */
18308620fc62SThierry Reding 	}
18318620fc62SThierry Reding };
1832ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
18338620fc62SThierry Reding 
183413411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
183513411dddSThierry Reding {
183613411dddSThierry Reding 	struct device_node *np;
183713411dddSThierry Reding 	u32 value = 0;
183813411dddSThierry Reding 	int err;
183913411dddSThierry Reding 
184013411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
184113411dddSThierry Reding 	if (err < 0) {
184213411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
184313411dddSThierry Reding 
184413411dddSThierry Reding 		/*
184513411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
184613411dddSThierry Reding 		 * correct head number by looking up the position of this
184713411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
184813411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
184913411dddSThierry Reding 		 * that the translation into a flattened device tree blob
185013411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
185113411dddSThierry Reding 		 * head number.
185213411dddSThierry Reding 		 *
185313411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
185413411dddSThierry Reding 		 * cases where only a single display controller is used.
185513411dddSThierry Reding 		 */
185613411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1857cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1858cf6b1744SJulia Lawall 				of_node_put(np);
185913411dddSThierry Reding 				break;
1860cf6b1744SJulia Lawall 			}
186113411dddSThierry Reding 
186213411dddSThierry Reding 			value++;
186313411dddSThierry Reding 		}
186413411dddSThierry Reding 	}
186513411dddSThierry Reding 
186613411dddSThierry Reding 	dc->pipe = value;
186713411dddSThierry Reding 
186813411dddSThierry Reding 	return 0;
186913411dddSThierry Reding }
187013411dddSThierry Reding 
1871dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1872dee8268fSThierry Reding {
1873dee8268fSThierry Reding 	struct resource *regs;
1874dee8268fSThierry Reding 	struct tegra_dc *dc;
1875dee8268fSThierry Reding 	int err;
1876dee8268fSThierry Reding 
1877dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1878dee8268fSThierry Reding 	if (!dc)
1879dee8268fSThierry Reding 		return -ENOMEM;
1880dee8268fSThierry Reding 
1881b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
18828620fc62SThierry Reding 
1883dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1884dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1885dee8268fSThierry Reding 	dc->dev = &pdev->dev;
1886dee8268fSThierry Reding 
188713411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
188813411dddSThierry Reding 	if (err < 0)
188913411dddSThierry Reding 		return err;
189013411dddSThierry Reding 
1891dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1892dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1893dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1894dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1895dee8268fSThierry Reding 	}
1896dee8268fSThierry Reding 
1897ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1898ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1899ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1900ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1901ca48080aSStephen Warren 	}
1902ca48080aSStephen Warren 
1903a2f2f740SThierry Reding 	/* assert reset and disable clock */
1904a2f2f740SThierry Reding 	if (!dc->soc->broken_reset) {
1905a2f2f740SThierry Reding 		err = clk_prepare_enable(dc->clk);
1906a2f2f740SThierry Reding 		if (err < 0)
1907a2f2f740SThierry Reding 			return err;
1908a2f2f740SThierry Reding 
1909a2f2f740SThierry Reding 		usleep_range(2000, 4000);
1910a2f2f740SThierry Reding 
1911a2f2f740SThierry Reding 		err = reset_control_assert(dc->rst);
1912a2f2f740SThierry Reding 		if (err < 0)
1913a2f2f740SThierry Reding 			return err;
1914a2f2f740SThierry Reding 
1915a2f2f740SThierry Reding 		usleep_range(2000, 4000);
1916a2f2f740SThierry Reding 
1917a2f2f740SThierry Reding 		clk_disable_unprepare(dc->clk);
1918a2f2f740SThierry Reding 	}
191933a8eb8dSThierry Reding 
19209c012700SThierry Reding 	if (dc->soc->has_powergate) {
19219c012700SThierry Reding 		if (dc->pipe == 0)
19229c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
19239c012700SThierry Reding 		else
19249c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
19259c012700SThierry Reding 
192633a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
19279c012700SThierry Reding 	}
1928dee8268fSThierry Reding 
1929dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1930dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1931dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1932dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1933dee8268fSThierry Reding 
1934dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1935dee8268fSThierry Reding 	if (dc->irq < 0) {
1936dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1937dee8268fSThierry Reding 		return -ENXIO;
1938dee8268fSThierry Reding 	}
1939dee8268fSThierry Reding 
1940dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1941dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1942dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1943dee8268fSThierry Reding 		return err;
1944dee8268fSThierry Reding 	}
1945dee8268fSThierry Reding 
194633a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
194733a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
194833a8eb8dSThierry Reding 
194933a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
195033a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
195133a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
195233a8eb8dSThierry Reding 
1953dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1954dee8268fSThierry Reding 	if (err < 0) {
1955dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1956dee8268fSThierry Reding 			err);
1957dee8268fSThierry Reding 		return err;
1958dee8268fSThierry Reding 	}
1959dee8268fSThierry Reding 
1960dee8268fSThierry Reding 	return 0;
1961dee8268fSThierry Reding }
1962dee8268fSThierry Reding 
1963dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1964dee8268fSThierry Reding {
1965dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1966dee8268fSThierry Reding 	int err;
1967dee8268fSThierry Reding 
1968dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1969dee8268fSThierry Reding 	if (err < 0) {
1970dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1971dee8268fSThierry Reding 			err);
1972dee8268fSThierry Reding 		return err;
1973dee8268fSThierry Reding 	}
1974dee8268fSThierry Reding 
197559d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
197659d29c0eSThierry Reding 	if (err < 0) {
197759d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
197859d29c0eSThierry Reding 		return err;
197959d29c0eSThierry Reding 	}
198059d29c0eSThierry Reding 
198133a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
198233a8eb8dSThierry Reding 
198333a8eb8dSThierry Reding 	return 0;
198433a8eb8dSThierry Reding }
198533a8eb8dSThierry Reding 
198633a8eb8dSThierry Reding #ifdef CONFIG_PM
198733a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
198833a8eb8dSThierry Reding {
198933a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
199033a8eb8dSThierry Reding 	int err;
199133a8eb8dSThierry Reding 
19926ac1571bSDmitry Osipenko 	if (!dc->soc->broken_reset) {
199333a8eb8dSThierry Reding 		err = reset_control_assert(dc->rst);
199433a8eb8dSThierry Reding 		if (err < 0) {
199533a8eb8dSThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
199633a8eb8dSThierry Reding 			return err;
199733a8eb8dSThierry Reding 		}
19986ac1571bSDmitry Osipenko 	}
19999c012700SThierry Reding 
20009c012700SThierry Reding 	if (dc->soc->has_powergate)
20019c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
20029c012700SThierry Reding 
2003dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2004dee8268fSThierry Reding 
2005dee8268fSThierry Reding 	return 0;
2006dee8268fSThierry Reding }
2007dee8268fSThierry Reding 
200833a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
200933a8eb8dSThierry Reding {
201033a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
201133a8eb8dSThierry Reding 	int err;
201233a8eb8dSThierry Reding 
201333a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
201433a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
201533a8eb8dSThierry Reding 							dc->rst);
201633a8eb8dSThierry Reding 		if (err < 0) {
201733a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
201833a8eb8dSThierry Reding 			return err;
201933a8eb8dSThierry Reding 		}
202033a8eb8dSThierry Reding 	} else {
202133a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
202233a8eb8dSThierry Reding 		if (err < 0) {
202333a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
202433a8eb8dSThierry Reding 			return err;
202533a8eb8dSThierry Reding 		}
202633a8eb8dSThierry Reding 
20276ac1571bSDmitry Osipenko 		if (!dc->soc->broken_reset) {
202833a8eb8dSThierry Reding 			err = reset_control_deassert(dc->rst);
202933a8eb8dSThierry Reding 			if (err < 0) {
20306ac1571bSDmitry Osipenko 				dev_err(dev,
20316ac1571bSDmitry Osipenko 					"failed to deassert reset: %d\n", err);
203233a8eb8dSThierry Reding 				return err;
203333a8eb8dSThierry Reding 			}
203433a8eb8dSThierry Reding 		}
20356ac1571bSDmitry Osipenko 	}
203633a8eb8dSThierry Reding 
203733a8eb8dSThierry Reding 	return 0;
203833a8eb8dSThierry Reding }
203933a8eb8dSThierry Reding #endif
204033a8eb8dSThierry Reding 
204133a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
204233a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
204333a8eb8dSThierry Reding };
204433a8eb8dSThierry Reding 
2045dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2046dee8268fSThierry Reding 	.driver = {
2047dee8268fSThierry Reding 		.name = "tegra-dc",
2048dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
204933a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2050dee8268fSThierry Reding 	},
2051dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2052dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2053dee8268fSThierry Reding };
2054