xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision fd5ec0dc34dafa6c5bb46770ca283ae90a4db3c7)
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"
2247307954SThierry 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 
371087fac1SThierry Reding /* Reads the active copy of a register. */
3886df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
3986df256fSThierry Reding {
4086df256fSThierry Reding 	u32 value;
4186df256fSThierry Reding 
4286df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
4386df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
4486df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
4586df256fSThierry Reding 
4686df256fSThierry Reding 	return value;
4786df256fSThierry Reding }
4886df256fSThierry Reding 
491087fac1SThierry Reding static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
501087fac1SThierry Reding 					      unsigned int offset)
511087fac1SThierry Reding {
521087fac1SThierry Reding 	if (offset >= 0x500 && offset <= 0x638) {
531087fac1SThierry Reding 		offset = 0x000 + (offset - 0x500);
541087fac1SThierry Reding 		return plane->offset + offset;
551087fac1SThierry Reding 	}
561087fac1SThierry Reding 
571087fac1SThierry Reding 	if (offset >= 0x700 && offset <= 0x719) {
581087fac1SThierry Reding 		offset = 0x180 + (offset - 0x700);
591087fac1SThierry Reding 		return plane->offset + offset;
601087fac1SThierry Reding 	}
611087fac1SThierry Reding 
621087fac1SThierry Reding 	if (offset >= 0x800 && offset <= 0x839) {
631087fac1SThierry Reding 		offset = 0x1c0 + (offset - 0x800);
641087fac1SThierry Reding 		return plane->offset + offset;
651087fac1SThierry Reding 	}
661087fac1SThierry Reding 
671087fac1SThierry Reding 	dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
681087fac1SThierry Reding 
691087fac1SThierry Reding 	return plane->offset + offset;
701087fac1SThierry Reding }
711087fac1SThierry Reding 
721087fac1SThierry Reding static inline u32 tegra_plane_readl(struct tegra_plane *plane,
731087fac1SThierry Reding 				    unsigned int offset)
741087fac1SThierry Reding {
751087fac1SThierry Reding 	return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
761087fac1SThierry Reding }
771087fac1SThierry Reding 
781087fac1SThierry Reding static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
791087fac1SThierry Reding 				      unsigned int offset)
801087fac1SThierry Reding {
811087fac1SThierry Reding 	tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
821087fac1SThierry Reding }
831087fac1SThierry Reding 
84c57997bcSThierry Reding bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
85c57997bcSThierry Reding {
86c57997bcSThierry Reding 	struct device_node *np = dc->dev->of_node;
87c57997bcSThierry Reding 	struct of_phandle_iterator it;
88c57997bcSThierry Reding 	int err;
89c57997bcSThierry Reding 
90c57997bcSThierry Reding 	of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
91c57997bcSThierry Reding 		if (it.node == dev->of_node)
92c57997bcSThierry Reding 			return true;
93c57997bcSThierry Reding 
94c57997bcSThierry Reding 	return false;
95c57997bcSThierry Reding }
96c57997bcSThierry Reding 
9786df256fSThierry Reding /*
98d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
99d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
100d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
101d700ba7aSThierry Reding  * on the next frame boundary otherwise.
102d700ba7aSThierry Reding  *
103d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
104d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
105d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
106d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
107d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
108d700ba7aSThierry Reding  */
10962b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
110205d48edSThierry Reding {
111205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
112205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
113205d48edSThierry Reding }
114205d48edSThierry Reding 
11510288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
11610288eeaSThierry Reding 				  unsigned int bpp)
11710288eeaSThierry Reding {
11810288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
11910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
12010288eeaSThierry Reding 	u32 dda_inc;
12110288eeaSThierry Reding 	int max;
12210288eeaSThierry Reding 
12310288eeaSThierry Reding 	if (v)
12410288eeaSThierry Reding 		max = 15;
12510288eeaSThierry Reding 	else {
12610288eeaSThierry Reding 		switch (bpp) {
12710288eeaSThierry Reding 		case 2:
12810288eeaSThierry Reding 			max = 8;
12910288eeaSThierry Reding 			break;
13010288eeaSThierry Reding 
13110288eeaSThierry Reding 		default:
13210288eeaSThierry Reding 			WARN_ON_ONCE(1);
13310288eeaSThierry Reding 			/* fallthrough */
13410288eeaSThierry Reding 		case 4:
13510288eeaSThierry Reding 			max = 4;
13610288eeaSThierry Reding 			break;
13710288eeaSThierry Reding 		}
13810288eeaSThierry Reding 	}
13910288eeaSThierry Reding 
14010288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
14110288eeaSThierry Reding 	inf.full -= dfixed_const(1);
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
14410288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
14510288eeaSThierry Reding 
14610288eeaSThierry Reding 	return dda_inc;
14710288eeaSThierry Reding }
14810288eeaSThierry Reding 
14910288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
15010288eeaSThierry Reding {
15110288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
15210288eeaSThierry Reding 	return dfixed_frac(inf);
15310288eeaSThierry Reding }
15410288eeaSThierry Reding 
155ab7d3f58SThierry Reding static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
156ab7d3f58SThierry Reding {
157ebae8d07SThierry Reding 	u32 background[3] = {
158ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
159ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
160ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
161ebae8d07SThierry Reding 	};
162ebae8d07SThierry Reding 	u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
163ebae8d07SThierry Reding 			 BLEND_COLOR_KEY_NONE;
164ebae8d07SThierry Reding 	u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
165ebae8d07SThierry Reding 	struct tegra_plane_state *state;
166ebae8d07SThierry Reding 	unsigned int i;
167ebae8d07SThierry Reding 
168ebae8d07SThierry Reding 	state = to_tegra_plane_state(plane->base.state);
169ebae8d07SThierry Reding 
170ebae8d07SThierry Reding 	/* alpha contribution is 1 minus sum of overlapping windows */
171ebae8d07SThierry Reding 	for (i = 0; i < 3; i++) {
172ebae8d07SThierry Reding 		if (state->dependent[i])
173ebae8d07SThierry Reding 			background[i] |= BLEND_CONTROL_DEPENDENT;
174ebae8d07SThierry Reding 	}
175ebae8d07SThierry Reding 
176ebae8d07SThierry Reding 	/* enable alpha blending if pixel format has an alpha component */
177ebae8d07SThierry Reding 	if (!state->opaque)
178ebae8d07SThierry Reding 		foreground |= BLEND_CONTROL_ALPHA;
179ebae8d07SThierry Reding 
180ab7d3f58SThierry Reding 	/*
181ab7d3f58SThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
182ab7d3f58SThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
183ab7d3f58SThierry Reding 	 */
184ebae8d07SThierry Reding 	tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
185ebae8d07SThierry Reding 	tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
186ab7d3f58SThierry Reding 
187ab7d3f58SThierry Reding 	switch (plane->index) {
188ab7d3f58SThierry Reding 	case 0:
189ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
190ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
191ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
192ab7d3f58SThierry Reding 		break;
193ab7d3f58SThierry Reding 
194ab7d3f58SThierry Reding 	case 1:
195ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
196ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
197ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
198ab7d3f58SThierry Reding 		break;
199ab7d3f58SThierry Reding 
200ab7d3f58SThierry Reding 	case 2:
201ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
202ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
203ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
204ab7d3f58SThierry Reding 		break;
205ab7d3f58SThierry Reding 	}
206ab7d3f58SThierry Reding }
207ab7d3f58SThierry Reding 
208ab7d3f58SThierry Reding static void tegra_plane_setup_blending(struct tegra_plane *plane,
209ab7d3f58SThierry Reding 				       const struct tegra_dc_window *window)
210ab7d3f58SThierry Reding {
211ab7d3f58SThierry Reding 	u32 value;
212ab7d3f58SThierry Reding 
213ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
214ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
215ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
216ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
217ab7d3f58SThierry Reding 
218ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
219ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
220ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
221ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
222ab7d3f58SThierry Reding 
223ab7d3f58SThierry Reding 	value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
224ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
225ab7d3f58SThierry Reding }
226ab7d3f58SThierry Reding 
2271087fac1SThierry Reding static void tegra_dc_setup_window(struct tegra_plane *plane,
22810288eeaSThierry Reding 				  const struct tegra_dc_window *window)
22910288eeaSThierry Reding {
23010288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
2311087fac1SThierry Reding 	struct tegra_dc *dc = plane->dc;
23210288eeaSThierry Reding 	bool yuv, planar;
2331087fac1SThierry Reding 	u32 value;
23410288eeaSThierry Reding 
23510288eeaSThierry Reding 	/*
23610288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
23710288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
23810288eeaSThierry Reding 	 */
2395acd3514SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar);
24010288eeaSThierry Reding 	if (!yuv)
24110288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
24210288eeaSThierry Reding 	else
24310288eeaSThierry Reding 		bpp = planar ? 1 : 2;
24410288eeaSThierry Reding 
2451087fac1SThierry Reding 	tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
2461087fac1SThierry Reding 	tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
24710288eeaSThierry Reding 
24810288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
2491087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_POSITION);
25010288eeaSThierry Reding 
25110288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
2521087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_SIZE);
25310288eeaSThierry Reding 
25410288eeaSThierry Reding 	h_offset = window->src.x * bpp;
25510288eeaSThierry Reding 	v_offset = window->src.y;
25610288eeaSThierry Reding 	h_size = window->src.w * bpp;
25710288eeaSThierry Reding 	v_size = window->src.h;
25810288eeaSThierry Reding 
25910288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
2601087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
26110288eeaSThierry Reding 
26210288eeaSThierry Reding 	/*
26310288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
26410288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
26510288eeaSThierry Reding 	 */
26610288eeaSThierry Reding 	if (yuv && planar)
26710288eeaSThierry Reding 		bpp = 2;
26810288eeaSThierry Reding 
26910288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
27010288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
27110288eeaSThierry Reding 
27210288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
2731087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
27410288eeaSThierry Reding 
27510288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
27610288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
27710288eeaSThierry Reding 
2781087fac1SThierry Reding 	tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
2791087fac1SThierry Reding 	tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
28010288eeaSThierry Reding 
2811087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
2821087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
28310288eeaSThierry Reding 
2841087fac1SThierry Reding 	tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
28510288eeaSThierry Reding 
28610288eeaSThierry Reding 	if (yuv && planar) {
2871087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
2881087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
28910288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
2901087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
29110288eeaSThierry Reding 	} else {
2921087fac1SThierry Reding 		tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
29310288eeaSThierry Reding 	}
29410288eeaSThierry Reding 
29510288eeaSThierry Reding 	if (window->bottom_up)
29610288eeaSThierry Reding 		v_offset += window->src.h - 1;
29710288eeaSThierry Reding 
2981087fac1SThierry Reding 	tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
2991087fac1SThierry Reding 	tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
30010288eeaSThierry Reding 
301c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
302c134f019SThierry Reding 		unsigned long height = window->tiling.value;
303c134f019SThierry Reding 
304c134f019SThierry Reding 		switch (window->tiling.mode) {
305c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
306c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
307c134f019SThierry Reding 			break;
308c134f019SThierry Reding 
309c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
310c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
311c134f019SThierry Reding 			break;
312c134f019SThierry Reding 
313c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
314c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
315c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
316c134f019SThierry Reding 			break;
317c134f019SThierry Reding 		}
318c134f019SThierry Reding 
3191087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
32010288eeaSThierry Reding 	} else {
321c134f019SThierry Reding 		switch (window->tiling.mode) {
322c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
32310288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
32410288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
325c134f019SThierry Reding 			break;
326c134f019SThierry Reding 
327c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
328c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
329c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
330c134f019SThierry Reding 			break;
331c134f019SThierry Reding 
332c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3334aa3df71SThierry Reding 			/*
3344aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3354aa3df71SThierry Reding 			 * will already have filtered it out.
3364aa3df71SThierry Reding 			 */
3374aa3df71SThierry Reding 			break;
33810288eeaSThierry Reding 		}
33910288eeaSThierry Reding 
3401087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
341c134f019SThierry Reding 	}
34210288eeaSThierry Reding 
34310288eeaSThierry Reding 	value = WIN_ENABLE;
34410288eeaSThierry Reding 
34510288eeaSThierry Reding 	if (yuv) {
34610288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
3471087fac1SThierry Reding 		tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
3481087fac1SThierry Reding 		tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
3491087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
3501087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
3511087fac1SThierry Reding 		tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
3521087fac1SThierry Reding 		tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
3531087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
3541087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
35510288eeaSThierry Reding 
35610288eeaSThierry Reding 		value |= CSC_ENABLE;
35710288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
35810288eeaSThierry Reding 		value |= COLOR_EXPAND;
35910288eeaSThierry Reding 	}
36010288eeaSThierry Reding 
36110288eeaSThierry Reding 	if (window->bottom_up)
36210288eeaSThierry Reding 		value |= V_DIRECTION;
36310288eeaSThierry Reding 
3641087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
36510288eeaSThierry Reding 
366ab7d3f58SThierry Reding 	if (dc->soc->supports_blending)
367ab7d3f58SThierry Reding 		tegra_plane_setup_blending(plane, window);
368ab7d3f58SThierry Reding 	else
369ab7d3f58SThierry Reding 		tegra_plane_setup_blending_legacy(plane);
370c7679306SThierry Reding }
371c7679306SThierry Reding 
372511c7023SThierry Reding static const u32 tegra20_primary_formats[] = {
373511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
374511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
375c7679306SThierry Reding 	DRM_FORMAT_RGB565,
376511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
377511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
378511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
379ebae8d07SThierry Reding 	/* non-native formats */
380ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
381ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
382ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
383ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
384511c7023SThierry Reding };
385511c7023SThierry Reding 
386e90124cbSThierry Reding static const u64 tegra20_modifiers[] = {
387e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
388e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
389e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
390e90124cbSThierry Reding };
391e90124cbSThierry Reding 
392511c7023SThierry Reding static const u32 tegra114_primary_formats[] = {
393511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
394511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
395511c7023SThierry Reding 	DRM_FORMAT_RGB565,
396511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
397511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
398511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
399511c7023SThierry Reding 	/* new on Tegra114 */
400511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
401511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
402511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
403511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
404511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
405511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
406511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
407511c7023SThierry Reding 	DRM_FORMAT_BGR565,
408511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
409511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
410511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
411511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
412511c7023SThierry Reding };
413511c7023SThierry Reding 
414511c7023SThierry Reding static const u32 tegra124_primary_formats[] = {
415511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
416511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
417511c7023SThierry Reding 	DRM_FORMAT_RGB565,
418511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
419511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
420511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
421511c7023SThierry Reding 	/* new on Tegra114 */
422511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
423511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
424511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
425511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
426511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
427511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
428511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
429511c7023SThierry Reding 	DRM_FORMAT_BGR565,
430511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
431511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
432511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
433511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
434511c7023SThierry Reding 	/* new on Tegra124 */
435511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
436511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
437c7679306SThierry Reding };
438c7679306SThierry Reding 
439e90124cbSThierry Reding static const u64 tegra124_modifiers[] = {
440e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
441e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
442e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
443e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
444e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
445e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
446e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
447e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
448e90124cbSThierry Reding };
449e90124cbSThierry Reding 
4504aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
4514aa3df71SThierry Reding 				    struct drm_plane_state *state)
4524aa3df71SThierry Reding {
4538f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
4548f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
45547802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
4564aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
457ebae8d07SThierry Reding 	unsigned int format;
458c7679306SThierry Reding 	int err;
459c7679306SThierry Reding 
4604aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
4614aa3df71SThierry Reding 	if (!state->crtc)
4624aa3df71SThierry Reding 		return 0;
4634aa3df71SThierry Reding 
464ebae8d07SThierry Reding 	err = tegra_plane_format(state->fb->format->format, &format,
4658f604f8cSThierry Reding 				 &plane_state->swap);
4664aa3df71SThierry Reding 	if (err < 0)
4674aa3df71SThierry Reding 		return err;
4684aa3df71SThierry Reding 
469ebae8d07SThierry Reding 	/*
470ebae8d07SThierry Reding 	 * Tegra20 and Tegra30 are special cases here because they support
471ebae8d07SThierry Reding 	 * only variants of specific formats with an alpha component, but not
472ebae8d07SThierry Reding 	 * the corresponding opaque formats. However, the opaque formats can
473ebae8d07SThierry Reding 	 * be emulated by disabling alpha blending for the plane.
474ebae8d07SThierry Reding 	 */
475ebae8d07SThierry Reding 	if (!dc->soc->supports_blending) {
476ebae8d07SThierry Reding 		if (!tegra_plane_format_has_alpha(format)) {
477ebae8d07SThierry Reding 			err = tegra_plane_format_get_alpha(format, &format);
478ebae8d07SThierry Reding 			if (err < 0)
479ebae8d07SThierry Reding 				return err;
480ebae8d07SThierry Reding 
481ebae8d07SThierry Reding 			plane_state->opaque = true;
482ebae8d07SThierry Reding 		} else {
483ebae8d07SThierry Reding 			plane_state->opaque = false;
484ebae8d07SThierry Reding 		}
485ebae8d07SThierry Reding 
486ebae8d07SThierry Reding 		tegra_plane_check_dependent(tegra, plane_state);
487ebae8d07SThierry Reding 	}
488ebae8d07SThierry Reding 
489ebae8d07SThierry Reding 	plane_state->format = format;
490ebae8d07SThierry Reding 
4918f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
4928f604f8cSThierry Reding 	if (err < 0)
4938f604f8cSThierry Reding 		return err;
4948f604f8cSThierry Reding 
4958f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
4964aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
4974aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
4984aa3df71SThierry Reding 		return -EINVAL;
4994aa3df71SThierry Reding 	}
5004aa3df71SThierry Reding 
5014aa3df71SThierry Reding 	/*
5024aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
5034aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
5044aa3df71SThierry Reding 	 * configuration.
5054aa3df71SThierry Reding 	 */
506bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
5074aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
5084aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
5094aa3df71SThierry Reding 			return -EINVAL;
5104aa3df71SThierry Reding 		}
5114aa3df71SThierry Reding 	}
5124aa3df71SThierry Reding 
51347802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
51447802b09SThierry Reding 	if (err < 0)
51547802b09SThierry Reding 		return err;
51647802b09SThierry Reding 
5174aa3df71SThierry Reding 	return 0;
5184aa3df71SThierry Reding }
5194aa3df71SThierry Reding 
520a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
521a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
52280d3eef1SDmitry Osipenko {
523a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
52480d3eef1SDmitry Osipenko 	u32 value;
52580d3eef1SDmitry Osipenko 
526a4bfa096SThierry Reding 	/* rien ne va plus */
527a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
528a4bfa096SThierry Reding 		return;
529a4bfa096SThierry Reding 
5301087fac1SThierry Reding 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
53180d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
5321087fac1SThierry Reding 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
53380d3eef1SDmitry Osipenko }
53480d3eef1SDmitry Osipenko 
5354aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
5364aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
5374aa3df71SThierry Reding {
5388f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
5394aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
5404aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
5414aa3df71SThierry Reding 	struct tegra_dc_window window;
5424aa3df71SThierry Reding 	unsigned int i;
5434aa3df71SThierry Reding 
5444aa3df71SThierry Reding 	/* rien ne va plus */
5454aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5464aa3df71SThierry Reding 		return;
5474aa3df71SThierry Reding 
54880d3eef1SDmitry Osipenko 	if (!plane->state->visible)
549a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
55080d3eef1SDmitry Osipenko 
551c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
5527d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
5537d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
5547d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
5557d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
5567d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
5577d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
5587d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
5597d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
560272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
561c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
562c7679306SThierry Reding 
5638f604f8cSThierry Reding 	/* copy from state */
564ab7d3f58SThierry Reding 	window.zpos = plane->state->normalized_zpos;
5658f604f8cSThierry Reding 	window.tiling = state->tiling;
5668f604f8cSThierry Reding 	window.format = state->format;
5678f604f8cSThierry Reding 	window.swap = state->swap;
568c7679306SThierry Reding 
569bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
5704aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
571c7679306SThierry Reding 
5724aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
57308ee0178SDmitry Osipenko 
57408ee0178SDmitry Osipenko 		/*
57508ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
57608ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
57708ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
57808ee0178SDmitry Osipenko 		 */
57908ee0178SDmitry Osipenko 		if (i < 2)
5804aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
581c7679306SThierry Reding 	}
582c7679306SThierry Reding 
5831087fac1SThierry Reding 	tegra_dc_setup_window(p, &window);
5844aa3df71SThierry Reding }
5854aa3df71SThierry Reding 
586a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
5874aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
5884aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
589a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
590c7679306SThierry Reding };
591c7679306SThierry Reding 
59289f65018SThierry Reding static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
593c7679306SThierry Reding {
594518e6227SThierry Reding 	/*
595518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
596518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
597518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
598518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
599518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
600518e6227SThierry Reding 	 * here.
601518e6227SThierry Reding 	 *
602518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
603518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
604518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
605518e6227SThierry Reding 	 */
60689f65018SThierry Reding 	return 1 << drm->mode_config.num_crtc;
60789f65018SThierry Reding }
60889f65018SThierry Reding 
60989f65018SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
61089f65018SThierry Reding 						    struct tegra_dc *dc)
61189f65018SThierry Reding {
61289f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
61347307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
614c7679306SThierry Reding 	struct tegra_plane *plane;
615c7679306SThierry Reding 	unsigned int num_formats;
616e90124cbSThierry Reding 	const u64 *modifiers;
617c7679306SThierry Reding 	const u32 *formats;
618c7679306SThierry Reding 	int err;
619c7679306SThierry Reding 
620c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
621c7679306SThierry Reding 	if (!plane)
622c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
623c7679306SThierry Reding 
6241087fac1SThierry Reding 	/* Always use window A as primary window */
6251087fac1SThierry Reding 	plane->offset = 0xa00;
626c4755fb9SThierry Reding 	plane->index = 0;
6271087fac1SThierry Reding 	plane->dc = dc;
6281087fac1SThierry Reding 
6291087fac1SThierry Reding 	num_formats = dc->soc->num_primary_formats;
6301087fac1SThierry Reding 	formats = dc->soc->primary_formats;
631e90124cbSThierry Reding 	modifiers = dc->soc->modifiers;
632c4755fb9SThierry Reding 
633518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
634c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
635e90124cbSThierry Reding 				       num_formats, modifiers, type, NULL);
636c7679306SThierry Reding 	if (err < 0) {
637c7679306SThierry Reding 		kfree(plane);
638c7679306SThierry Reding 		return ERR_PTR(err);
639c7679306SThierry Reding 	}
640c7679306SThierry Reding 
641a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
6424aa3df71SThierry Reding 
643ab7d3f58SThierry Reding 	if (dc->soc->supports_blending)
644ab7d3f58SThierry Reding 		drm_plane_create_zpos_property(&plane->base, 0, 0, 255);
645ab7d3f58SThierry Reding 
646c7679306SThierry Reding 	return &plane->base;
647c7679306SThierry Reding }
648c7679306SThierry Reding 
649c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
650c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
651c7679306SThierry Reding };
652c7679306SThierry Reding 
6534aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
6544aa3df71SThierry Reding 				     struct drm_plane_state *state)
655c7679306SThierry Reding {
65647802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
65747802b09SThierry Reding 	int err;
65847802b09SThierry Reding 
6594aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6604aa3df71SThierry Reding 	if (!state->crtc)
6614aa3df71SThierry Reding 		return 0;
662c7679306SThierry Reding 
663c7679306SThierry Reding 	/* scaling not supported for cursor */
6644aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
6654aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
666c7679306SThierry Reding 		return -EINVAL;
667c7679306SThierry Reding 
668c7679306SThierry Reding 	/* only square cursors supported */
6694aa3df71SThierry Reding 	if (state->src_w != state->src_h)
670c7679306SThierry Reding 		return -EINVAL;
671c7679306SThierry Reding 
6724aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
6734aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
6744aa3df71SThierry Reding 		return -EINVAL;
6754aa3df71SThierry Reding 
67647802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
67747802b09SThierry Reding 	if (err < 0)
67847802b09SThierry Reding 		return err;
67947802b09SThierry Reding 
6804aa3df71SThierry Reding 	return 0;
6814aa3df71SThierry Reding }
6824aa3df71SThierry Reding 
6834aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
6844aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
6854aa3df71SThierry Reding {
6864aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
6874aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
6884aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
6894aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
6904aa3df71SThierry Reding 
6914aa3df71SThierry Reding 	/* rien ne va plus */
6924aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
6934aa3df71SThierry Reding 		return;
6944aa3df71SThierry Reding 
6954aa3df71SThierry Reding 	switch (state->crtc_w) {
696c7679306SThierry Reding 	case 32:
697c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
698c7679306SThierry Reding 		break;
699c7679306SThierry Reding 
700c7679306SThierry Reding 	case 64:
701c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
702c7679306SThierry Reding 		break;
703c7679306SThierry Reding 
704c7679306SThierry Reding 	case 128:
705c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
706c7679306SThierry Reding 		break;
707c7679306SThierry Reding 
708c7679306SThierry Reding 	case 256:
709c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
710c7679306SThierry Reding 		break;
711c7679306SThierry Reding 
712c7679306SThierry Reding 	default:
7134aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
7144aa3df71SThierry Reding 		     state->crtc_h);
7154aa3df71SThierry Reding 		return;
716c7679306SThierry Reding 	}
717c7679306SThierry Reding 
718c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
719c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
720c7679306SThierry Reding 
721c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
722c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
723c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
724c7679306SThierry Reding #endif
725c7679306SThierry Reding 
726c7679306SThierry Reding 	/* enable cursor and set blend mode */
727c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
728c7679306SThierry Reding 	value |= CURSOR_ENABLE;
729c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
730c7679306SThierry Reding 
731c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
732c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
733c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
734c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
735c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
736c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
737c7679306SThierry Reding 	value |= CURSOR_ALPHA;
738c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
739c7679306SThierry Reding 
740c7679306SThierry Reding 	/* position the cursor */
7414aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
742c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
743c7679306SThierry Reding }
744c7679306SThierry Reding 
7454aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
7464aa3df71SThierry Reding 					struct drm_plane_state *old_state)
747c7679306SThierry Reding {
7484aa3df71SThierry Reding 	struct tegra_dc *dc;
749c7679306SThierry Reding 	u32 value;
750c7679306SThierry Reding 
7514aa3df71SThierry Reding 	/* rien ne va plus */
7524aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
7534aa3df71SThierry Reding 		return;
7544aa3df71SThierry Reding 
7554aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
756c7679306SThierry Reding 
757c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
758c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
759c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
760c7679306SThierry Reding }
761c7679306SThierry Reding 
7624aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
7634aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
7644aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
7654aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
766c7679306SThierry Reding };
767c7679306SThierry Reding 
768c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
769c7679306SThierry Reding 						      struct tegra_dc *dc)
770c7679306SThierry Reding {
77189f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
772c7679306SThierry Reding 	struct tegra_plane *plane;
773c7679306SThierry Reding 	unsigned int num_formats;
774c7679306SThierry Reding 	const u32 *formats;
775c7679306SThierry Reding 	int err;
776c7679306SThierry Reding 
777c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
778c7679306SThierry Reding 	if (!plane)
779c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
780c7679306SThierry Reding 
78147802b09SThierry Reding 	/*
782a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
783a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
784a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
785a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
786a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
78747802b09SThierry Reding 	 */
78847802b09SThierry Reding 	plane->index = 6;
7891087fac1SThierry Reding 	plane->dc = dc;
79047802b09SThierry Reding 
791c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
792c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
793c7679306SThierry Reding 
79489f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
795c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
796e6fc3b68SBen Widawsky 				       num_formats, NULL,
797e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
798c7679306SThierry Reding 	if (err < 0) {
799c7679306SThierry Reding 		kfree(plane);
800c7679306SThierry Reding 		return ERR_PTR(err);
801c7679306SThierry Reding 	}
802c7679306SThierry Reding 
8034aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
8044aa3df71SThierry Reding 
805c7679306SThierry Reding 	return &plane->base;
806c7679306SThierry Reding }
807c7679306SThierry Reding 
808511c7023SThierry Reding static const u32 tegra20_overlay_formats[] = {
809511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
810511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
811dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
812511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
813511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
814511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
815ebae8d07SThierry Reding 	/* non-native formats */
816ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
817ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
818ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
819ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
820511c7023SThierry Reding 	/* planar formats */
821511c7023SThierry Reding 	DRM_FORMAT_UYVY,
822511c7023SThierry Reding 	DRM_FORMAT_YUYV,
823511c7023SThierry Reding 	DRM_FORMAT_YUV420,
824511c7023SThierry Reding 	DRM_FORMAT_YUV422,
825511c7023SThierry Reding };
826511c7023SThierry Reding 
827511c7023SThierry Reding static const u32 tegra114_overlay_formats[] = {
828511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
829511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
830511c7023SThierry Reding 	DRM_FORMAT_RGB565,
831511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
832511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
833511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
834511c7023SThierry Reding 	/* new on Tegra114 */
835511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
836511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
837511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
838511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
839511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
840511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
841511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
842511c7023SThierry Reding 	DRM_FORMAT_BGR565,
843511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
844511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
845511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
846511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
847511c7023SThierry Reding 	/* planar formats */
848511c7023SThierry Reding 	DRM_FORMAT_UYVY,
849511c7023SThierry Reding 	DRM_FORMAT_YUYV,
850511c7023SThierry Reding 	DRM_FORMAT_YUV420,
851511c7023SThierry Reding 	DRM_FORMAT_YUV422,
852511c7023SThierry Reding };
853511c7023SThierry Reding 
854511c7023SThierry Reding static const u32 tegra124_overlay_formats[] = {
855511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
856511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
857511c7023SThierry Reding 	DRM_FORMAT_RGB565,
858511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
859511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
860511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
861511c7023SThierry Reding 	/* new on Tegra114 */
862511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
863511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
864511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
865511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
866511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
867511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
868511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
869511c7023SThierry Reding 	DRM_FORMAT_BGR565,
870511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
871511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
872511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
873511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
874511c7023SThierry Reding 	/* new on Tegra124 */
875511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
876511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
877511c7023SThierry Reding 	/* planar formats */
878dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
879f925390eSThierry Reding 	DRM_FORMAT_YUYV,
880dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
881dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
882dee8268fSThierry Reding };
883dee8268fSThierry Reding 
884c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
885c7679306SThierry Reding 						       struct tegra_dc *dc,
8869f446d83SDmitry Osipenko 						       unsigned int index,
8879f446d83SDmitry Osipenko 						       bool cursor)
888dee8268fSThierry Reding {
88989f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
890dee8268fSThierry Reding 	struct tegra_plane *plane;
891c7679306SThierry Reding 	unsigned int num_formats;
8929f446d83SDmitry Osipenko 	enum drm_plane_type type;
893c7679306SThierry Reding 	const u32 *formats;
894c7679306SThierry Reding 	int err;
895dee8268fSThierry Reding 
896f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
897dee8268fSThierry Reding 	if (!plane)
898c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
899dee8268fSThierry Reding 
9001087fac1SThierry Reding 	plane->offset = 0xa00 + 0x200 * index;
901c7679306SThierry Reding 	plane->index = index;
9021087fac1SThierry Reding 	plane->dc = dc;
903dee8268fSThierry Reding 
904511c7023SThierry Reding 	num_formats = dc->soc->num_overlay_formats;
905511c7023SThierry Reding 	formats = dc->soc->overlay_formats;
906c7679306SThierry Reding 
9079f446d83SDmitry Osipenko 	if (!cursor)
9089f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_OVERLAY;
9099f446d83SDmitry Osipenko 	else
9109f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_CURSOR;
9119f446d83SDmitry Osipenko 
91289f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
913301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
9149f446d83SDmitry Osipenko 				       num_formats, NULL, type, NULL);
915f002abc1SThierry Reding 	if (err < 0) {
916f002abc1SThierry Reding 		kfree(plane);
917c7679306SThierry Reding 		return ERR_PTR(err);
918dee8268fSThierry Reding 	}
919c7679306SThierry Reding 
920a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
9214aa3df71SThierry Reding 
922ab7d3f58SThierry Reding 	if (dc->soc->supports_blending)
923ab7d3f58SThierry Reding 		drm_plane_create_zpos_property(&plane->base, 0, 0, 255);
924ab7d3f58SThierry Reding 
925c7679306SThierry Reding 	return &plane->base;
926c7679306SThierry Reding }
927c7679306SThierry Reding 
92847307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
92947307954SThierry Reding 						    struct tegra_dc *dc)
930c7679306SThierry Reding {
93147307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
93247307954SThierry Reding 	unsigned int i, j;
93347307954SThierry Reding 
93447307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
93547307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
93647307954SThierry Reding 
93747307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
93847307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
93947307954SThierry Reding 				unsigned int index = wgrp->windows[j];
94047307954SThierry Reding 
94147307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
94247307954SThierry Reding 								  wgrp->index,
94347307954SThierry Reding 								  index);
94447307954SThierry Reding 				if (IS_ERR(plane))
94547307954SThierry Reding 					return plane;
94647307954SThierry Reding 
94747307954SThierry Reding 				/*
94847307954SThierry Reding 				 * Choose the first shared plane owned by this
94947307954SThierry Reding 				 * head as the primary plane.
95047307954SThierry Reding 				 */
95147307954SThierry Reding 				if (!primary) {
95247307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
95347307954SThierry Reding 					primary = plane;
95447307954SThierry Reding 				}
95547307954SThierry Reding 			}
95647307954SThierry Reding 		}
95747307954SThierry Reding 	}
95847307954SThierry Reding 
95947307954SThierry Reding 	return primary;
96047307954SThierry Reding }
96147307954SThierry Reding 
96247307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
96347307954SThierry Reding 					     struct tegra_dc *dc)
96447307954SThierry Reding {
9658f62142eSThierry Reding 	struct drm_plane *planes[2], *primary;
9669f446d83SDmitry Osipenko 	unsigned int planes_num;
967c7679306SThierry Reding 	unsigned int i;
9688f62142eSThierry Reding 	int err;
969c7679306SThierry Reding 
97047307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
97147307954SThierry Reding 	if (IS_ERR(primary))
97247307954SThierry Reding 		return primary;
97347307954SThierry Reding 
9749f446d83SDmitry Osipenko 	if (dc->soc->supports_cursor)
9759f446d83SDmitry Osipenko 		planes_num = 2;
9769f446d83SDmitry Osipenko 	else
9779f446d83SDmitry Osipenko 		planes_num = 1;
9789f446d83SDmitry Osipenko 
9799f446d83SDmitry Osipenko 	for (i = 0; i < planes_num; i++) {
9809f446d83SDmitry Osipenko 		planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
9819f446d83SDmitry Osipenko 							  false);
9828f62142eSThierry Reding 		if (IS_ERR(planes[i])) {
9838f62142eSThierry Reding 			err = PTR_ERR(planes[i]);
9848f62142eSThierry Reding 
9858f62142eSThierry Reding 			while (i--)
9868f62142eSThierry Reding 				tegra_plane_funcs.destroy(planes[i]);
9878f62142eSThierry Reding 
9888f62142eSThierry Reding 			tegra_plane_funcs.destroy(primary);
9898f62142eSThierry Reding 			return ERR_PTR(err);
99047307954SThierry Reding 		}
991f002abc1SThierry Reding 	}
992dee8268fSThierry Reding 
99347307954SThierry Reding 	return primary;
994dee8268fSThierry Reding }
995dee8268fSThierry Reding 
996f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
997f002abc1SThierry Reding {
998f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
999f002abc1SThierry Reding }
1000f002abc1SThierry Reding 
1001ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1002ca915b10SThierry Reding {
1003ca915b10SThierry Reding 	struct tegra_dc_state *state;
1004ca915b10SThierry Reding 
10053b59b7acSThierry Reding 	if (crtc->state)
1006ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
10073b59b7acSThierry Reding 
1008ca915b10SThierry Reding 	kfree(crtc->state);
1009ca915b10SThierry Reding 	crtc->state = NULL;
1010ca915b10SThierry Reding 
1011ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1012332bbe70SThierry Reding 	if (state) {
1013ca915b10SThierry Reding 		crtc->state = &state->base;
1014332bbe70SThierry Reding 		crtc->state->crtc = crtc;
1015332bbe70SThierry Reding 	}
101631930d4dSThierry Reding 
101731930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
1018ca915b10SThierry Reding }
1019ca915b10SThierry Reding 
1020ca915b10SThierry Reding static struct drm_crtc_state *
1021ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1022ca915b10SThierry Reding {
1023ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1024ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1025ca915b10SThierry Reding 
10263b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1027ca915b10SThierry Reding 	if (!copy)
1028ca915b10SThierry Reding 		return NULL;
1029ca915b10SThierry Reding 
10303b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
10313b59b7acSThierry Reding 	copy->clk = state->clk;
10323b59b7acSThierry Reding 	copy->pclk = state->pclk;
10333b59b7acSThierry Reding 	copy->div = state->div;
10343b59b7acSThierry Reding 	copy->planes = state->planes;
1035ca915b10SThierry Reding 
1036ca915b10SThierry Reding 	return &copy->base;
1037ca915b10SThierry Reding }
1038ca915b10SThierry Reding 
1039ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1040ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1041ca915b10SThierry Reding {
1042ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1043ca915b10SThierry Reding 	kfree(state);
1044ca915b10SThierry Reding }
1045ca915b10SThierry Reding 
1046b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1047b95800eeSThierry Reding 
1048b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1049b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1050b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1051b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1052b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1053b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1054b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1055b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1056b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1057b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1058b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1059b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1060b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1061b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1062b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1063b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1064b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1065b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1066b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1067b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1068b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1069b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1070b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1071b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1072b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1073b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1074b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1075b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1076b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1077b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1078b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1079b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1080b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1081b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1082b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1083b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1084b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1085b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1086b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1087b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1088b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1089b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1090b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1091b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1092b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1093b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1094b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1095b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1096b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1097b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1098b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1099b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1100b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1101b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1102b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1103b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1104b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1105b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1106b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1107b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1108b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1109b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1110b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1111b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1112b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1113b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1114b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1115b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1116b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1117b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1118b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1119b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1120b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1121b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1122b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1123b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1124b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1125b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1126b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1127b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1128b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1129b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1130b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1131b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1132b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1133b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1134b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1135b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1136b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1137b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1138b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1139b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1140b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1141b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1142b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1143b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1144b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1145b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1146b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1147b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1148b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1149b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1150b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1151b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1152b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1153b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1154b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1155b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1156b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1157b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1158b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1159b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1160b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1161b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1162b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1163b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1164b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1165b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1166b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1167b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1168b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1169b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1170b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1171b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1172b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1173b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1174b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1175b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1176b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1177b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1178b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1179b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1180b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1181b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1182b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1183b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1184b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1185b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1186b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1187b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1188b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1189b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1190b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1191b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1192b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1193b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1194b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1195b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1196b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1197b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1198b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1199b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1200b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1201b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1202b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1203b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1204b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1205b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1206b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1207b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1208b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1209b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1210b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1211b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1212b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1213b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1214b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1215b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1216b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1217b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1218b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1219b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1220b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1221b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1222b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1223b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1224b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1225b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1226b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1227b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1228b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1229b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1230b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1231b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1232b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1233b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1234b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1235b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1236b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1237b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1238b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1239b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1240b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1241b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1242b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1243b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1244b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1245b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1246b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1247b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1248b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1249b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1250b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1251b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1252b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1253b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1254b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1255b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1256b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1257b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1258b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1259b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1260b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1261b95800eeSThierry Reding };
1262b95800eeSThierry Reding 
1263b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1264b95800eeSThierry Reding {
1265b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1266b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1267b95800eeSThierry Reding 	unsigned int i;
1268b95800eeSThierry Reding 	int err = 0;
1269b95800eeSThierry Reding 
1270b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1271b95800eeSThierry Reding 
1272b95800eeSThierry Reding 	if (!dc->base.state->active) {
1273b95800eeSThierry Reding 		err = -EBUSY;
1274b95800eeSThierry Reding 		goto unlock;
1275b95800eeSThierry Reding 	}
1276b95800eeSThierry Reding 
1277b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1278b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1279b95800eeSThierry Reding 
1280b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1281b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1282b95800eeSThierry Reding 	}
1283b95800eeSThierry Reding 
1284b95800eeSThierry Reding unlock:
1285b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1286b95800eeSThierry Reding 	return err;
1287b95800eeSThierry Reding }
1288b95800eeSThierry Reding 
1289b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1290b95800eeSThierry Reding {
1291b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1292b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1293b95800eeSThierry Reding 	int err = 0;
1294b95800eeSThierry Reding 	u32 value;
1295b95800eeSThierry Reding 
1296b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1297b95800eeSThierry Reding 
1298b95800eeSThierry Reding 	if (!dc->base.state->active) {
1299b95800eeSThierry Reding 		err = -EBUSY;
1300b95800eeSThierry Reding 		goto unlock;
1301b95800eeSThierry Reding 	}
1302b95800eeSThierry Reding 
1303b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1304b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1305b95800eeSThierry Reding 	tegra_dc_commit(dc);
1306b95800eeSThierry Reding 
1307b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1308b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1309b95800eeSThierry Reding 
1310b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1311b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1312b95800eeSThierry Reding 
1313b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1314b95800eeSThierry Reding 
1315b95800eeSThierry Reding unlock:
1316b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1317b95800eeSThierry Reding 	return err;
1318b95800eeSThierry Reding }
1319b95800eeSThierry Reding 
1320b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1321b95800eeSThierry Reding {
1322b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1323b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1324b95800eeSThierry Reding 
1325b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1326b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1327b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1328b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1329b95800eeSThierry Reding 
1330b95800eeSThierry Reding 	return 0;
1331b95800eeSThierry Reding }
1332b95800eeSThierry Reding 
1333b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1334b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1335b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1336b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1337b95800eeSThierry Reding };
1338b95800eeSThierry Reding 
1339b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1340b95800eeSThierry Reding {
1341b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1342b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
134339f55c61SArnd Bergmann 	struct dentry *root;
1344b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1345b95800eeSThierry Reding 	int err;
1346b95800eeSThierry Reding 
134739f55c61SArnd Bergmann #ifdef CONFIG_DEBUG_FS
134839f55c61SArnd Bergmann 	root = crtc->debugfs_entry;
134939f55c61SArnd Bergmann #else
135039f55c61SArnd Bergmann 	root = NULL;
135139f55c61SArnd Bergmann #endif
135239f55c61SArnd Bergmann 
1353b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1354b95800eeSThierry Reding 				    GFP_KERNEL);
1355b95800eeSThierry Reding 	if (!dc->debugfs_files)
1356b95800eeSThierry Reding 		return -ENOMEM;
1357b95800eeSThierry Reding 
1358b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1359b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1360b95800eeSThierry Reding 
1361b95800eeSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1362b95800eeSThierry Reding 	if (err < 0)
1363b95800eeSThierry Reding 		goto free;
1364b95800eeSThierry Reding 
1365b95800eeSThierry Reding 	return 0;
1366b95800eeSThierry Reding 
1367b95800eeSThierry Reding free:
1368b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1369b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1370b95800eeSThierry Reding 
1371b95800eeSThierry Reding 	return err;
1372b95800eeSThierry Reding }
1373b95800eeSThierry Reding 
1374b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1375b95800eeSThierry Reding {
1376b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1377b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1378b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1379b95800eeSThierry Reding 
1380b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1381b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1382b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1383b95800eeSThierry Reding }
1384b95800eeSThierry Reding 
1385c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1386c49c81e2SThierry Reding {
1387c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1388c49c81e2SThierry Reding 
138947307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
139047307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1391c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1392c49c81e2SThierry Reding 
1393c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
13943abe2413SDhinakaran Pandiyan 	return (u32)drm_crtc_vblank_count(&dc->base);
1395c49c81e2SThierry Reding }
1396c49c81e2SThierry Reding 
1397c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1398c49c81e2SThierry Reding {
1399c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1400363541e8SThierry Reding 	u32 value;
1401c49c81e2SThierry Reding 
1402c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1403c49c81e2SThierry Reding 	value |= VBLANK_INT;
1404c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1405c49c81e2SThierry Reding 
1406c49c81e2SThierry Reding 	return 0;
1407c49c81e2SThierry Reding }
1408c49c81e2SThierry Reding 
1409c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1410c49c81e2SThierry Reding {
1411c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1412363541e8SThierry Reding 	u32 value;
1413c49c81e2SThierry Reding 
1414c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1415c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1416c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1417c49c81e2SThierry Reding }
1418c49c81e2SThierry Reding 
1419dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
14201503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
142174f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1422f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1423ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1424ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1425ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1426b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1427b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
142810437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
142910437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
143010437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1431dee8268fSThierry Reding };
1432dee8268fSThierry Reding 
1433dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1434dee8268fSThierry Reding 				struct drm_display_mode *mode)
1435dee8268fSThierry Reding {
14360444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
14370444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1438dee8268fSThierry Reding 	unsigned long value;
1439dee8268fSThierry Reding 
144047307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1441dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1442dee8268fSThierry Reding 
1443dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1444dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
144547307954SThierry Reding 	}
1446dee8268fSThierry Reding 
1447dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1448dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1449dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1450dee8268fSThierry Reding 
1451dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1452dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1453dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1454dee8268fSThierry Reding 
1455dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1456dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1457dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1458dee8268fSThierry Reding 
1459dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1460dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1461dee8268fSThierry Reding 
1462dee8268fSThierry Reding 	return 0;
1463dee8268fSThierry Reding }
1464dee8268fSThierry Reding 
14659d910b60SThierry Reding /**
14669d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
14679d910b60SThierry Reding  *     state
14689d910b60SThierry Reding  * @dc: display controller
14699d910b60SThierry Reding  * @crtc_state: CRTC atomic state
14709d910b60SThierry Reding  * @clk: parent clock for display controller
14719d910b60SThierry Reding  * @pclk: pixel clock
14729d910b60SThierry Reding  * @div: shift clock divider
14739d910b60SThierry Reding  *
14749d910b60SThierry Reding  * Returns:
14759d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
14769d910b60SThierry Reding  */
1477ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1478ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1479ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1480ca915b10SThierry Reding 			       unsigned int div)
1481ca915b10SThierry Reding {
1482ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1483ca915b10SThierry Reding 
1484d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1485d2982748SThierry Reding 		return -EINVAL;
1486d2982748SThierry Reding 
1487ca915b10SThierry Reding 	state->clk = clk;
1488ca915b10SThierry Reding 	state->pclk = pclk;
1489ca915b10SThierry Reding 	state->div = div;
1490ca915b10SThierry Reding 
1491ca915b10SThierry Reding 	return 0;
1492ca915b10SThierry Reding }
1493ca915b10SThierry Reding 
149476d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
149576d59ed0SThierry Reding 				  struct tegra_dc_state *state)
149676d59ed0SThierry Reding {
149776d59ed0SThierry Reding 	u32 value;
149876d59ed0SThierry Reding 	int err;
149976d59ed0SThierry Reding 
150076d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
150176d59ed0SThierry Reding 	if (err < 0)
150276d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
150376d59ed0SThierry Reding 
150476d59ed0SThierry Reding 	/*
150576d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
150676d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
150776d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
150876d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
150976d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
151076d59ed0SThierry Reding 	 * should therefore be avoided.
151176d59ed0SThierry Reding 	 */
151276d59ed0SThierry Reding 	if (state->pclk > 0) {
151376d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
151476d59ed0SThierry Reding 		if (err < 0)
151576d59ed0SThierry Reding 			dev_err(dc->dev,
151676d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
151776d59ed0SThierry Reding 				state->pclk);
151876d59ed0SThierry Reding 	}
151976d59ed0SThierry Reding 
152076d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
152176d59ed0SThierry Reding 		      state->div);
152276d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
152376d59ed0SThierry Reding 
152447307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
152576d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
152676d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
152747307954SThierry Reding 	}
152839e08affSThierry Reding 
152939e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
153039e08affSThierry Reding 	if (err < 0)
153139e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
153239e08affSThierry Reding 			dc->clk, state->pclk, err);
153376d59ed0SThierry Reding }
153476d59ed0SThierry Reding 
1535003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1536003fc848SThierry Reding {
1537003fc848SThierry Reding 	u32 value;
1538003fc848SThierry Reding 
1539003fc848SThierry Reding 	/* stop the display controller */
1540003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1541003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1542003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1543003fc848SThierry Reding 
1544003fc848SThierry Reding 	tegra_dc_commit(dc);
1545003fc848SThierry Reding }
1546003fc848SThierry Reding 
1547003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1548003fc848SThierry Reding {
1549003fc848SThierry Reding 	u32 value;
1550003fc848SThierry Reding 
1551003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1552003fc848SThierry Reding 
1553003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1554003fc848SThierry Reding }
1555003fc848SThierry Reding 
1556003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1557003fc848SThierry Reding {
1558003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1559003fc848SThierry Reding 
1560003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1561003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1562003fc848SThierry Reding 			return 0;
1563003fc848SThierry Reding 
1564003fc848SThierry Reding 		usleep_range(1000, 2000);
1565003fc848SThierry Reding 	}
1566003fc848SThierry Reding 
1567003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1568003fc848SThierry Reding 	return -ETIMEDOUT;
1569003fc848SThierry Reding }
1570003fc848SThierry Reding 
157164581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
157264581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1573003fc848SThierry Reding {
1574003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1575003fc848SThierry Reding 	u32 value;
1576003fc848SThierry Reding 
1577003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1578003fc848SThierry Reding 		tegra_dc_stop(dc);
1579003fc848SThierry Reding 
1580003fc848SThierry Reding 		/*
1581003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1582003fc848SThierry Reding 		 * in case this fails.
1583003fc848SThierry Reding 		 */
1584003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1585003fc848SThierry Reding 	}
1586003fc848SThierry Reding 
1587003fc848SThierry Reding 	/*
1588003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1589003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1590003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1591003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1592003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1593003fc848SThierry Reding 	 * to go idle.
1594003fc848SThierry Reding 	 *
1595003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1596003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1597003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1598003fc848SThierry Reding 	 *
1599003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1600003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1601003fc848SThierry Reding 	 * the RGB encoder?
1602003fc848SThierry Reding 	 */
1603003fc848SThierry Reding 	if (dc->rgb) {
1604003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1605003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1606003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1607003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1608003fc848SThierry Reding 	}
1609003fc848SThierry Reding 
1610003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1611003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
161233a8eb8dSThierry Reding 
16139d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
16149d99ab6eSThierry Reding 
16159d99ab6eSThierry Reding 	if (crtc->state->event) {
16169d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
16179d99ab6eSThierry Reding 		crtc->state->event = NULL;
16189d99ab6eSThierry Reding 	}
16199d99ab6eSThierry Reding 
16209d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
16219d99ab6eSThierry Reding 
162233a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1623003fc848SThierry Reding }
1624003fc848SThierry Reding 
16250b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
16260b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1627dee8268fSThierry Reding {
16284aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
162976d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1630dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1631dbb3f2f7SThierry Reding 	u32 value;
1632dee8268fSThierry Reding 
163333a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
163433a8eb8dSThierry Reding 
163533a8eb8dSThierry Reding 	/* initialize display controller */
163633a8eb8dSThierry Reding 	if (dc->syncpt) {
163747307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
163847307954SThierry Reding 
163947307954SThierry Reding 		if (dc->soc->has_nvdisplay)
164047307954SThierry Reding 			enable = 1 << 31;
164147307954SThierry Reding 		else
164247307954SThierry Reding 			enable = 1 << 8;
164333a8eb8dSThierry Reding 
164433a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
164533a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
164633a8eb8dSThierry Reding 
164747307954SThierry Reding 		value = enable | syncpt;
164833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
164933a8eb8dSThierry Reding 	}
165033a8eb8dSThierry Reding 
165147307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
165247307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
165347307954SThierry Reding 			DSC_OBUF_UF_INT;
165447307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
165547307954SThierry Reding 
165647307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
165747307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
165847307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
165947307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
166047307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
166147307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
166247307954SThierry Reding 
166347307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
166447307954SThierry Reding 			FRAME_END_INT;
166547307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
166647307954SThierry Reding 
166747307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
166847307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
166947307954SThierry Reding 
167047307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
167147307954SThierry Reding 	} else {
167233a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
167333a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
167433a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
167533a8eb8dSThierry Reding 
167633a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
167733a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
167833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
167933a8eb8dSThierry Reding 
168033a8eb8dSThierry Reding 		/* initialize timer */
168133a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
168233a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
168333a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
168433a8eb8dSThierry Reding 
168533a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
168633a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
168733a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
168833a8eb8dSThierry Reding 
168933a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
169033a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
169133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
169233a8eb8dSThierry Reding 
169333a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
169433a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
169533a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
169647307954SThierry Reding 	}
169733a8eb8dSThierry Reding 
16987116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
16997116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
17007116e9a8SThierry Reding 	else
170133a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
170233a8eb8dSThierry Reding 
170333a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
170476d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
170576d59ed0SThierry Reding 
1706dee8268fSThierry Reding 	/* program display mode */
1707dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1708dee8268fSThierry Reding 
17098620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
17108620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
17118620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
17128620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
17138620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
17148620fc62SThierry Reding 	}
1715666cb873SThierry Reding 
1716666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1717666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1718666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1719666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1720666cb873SThierry Reding 
172147307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1722666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1723666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1724666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1725666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
172647307954SThierry Reding 	}
172747307954SThierry Reding 
172847307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
172947307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
173047307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
173147307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
173247307954SThierry Reding 	}
1733666cb873SThierry Reding 
1734666cb873SThierry Reding 	tegra_dc_commit(dc);
1735dee8268fSThierry Reding 
17368ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1737dee8268fSThierry Reding }
1738dee8268fSThierry Reding 
1739613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1740613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
17414aa3df71SThierry Reding {
17429d99ab6eSThierry Reding 	unsigned long flags;
17431503ca47SThierry Reding 
17441503ca47SThierry Reding 	if (crtc->state->event) {
17459d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
17461503ca47SThierry Reding 
17479d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
17489d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
17499d99ab6eSThierry Reding 		else
17509d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
17511503ca47SThierry Reding 
17529d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
17539d99ab6eSThierry Reding 
17541503ca47SThierry Reding 		crtc->state->event = NULL;
17551503ca47SThierry Reding 	}
17564aa3df71SThierry Reding }
17574aa3df71SThierry Reding 
1758613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1759613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
17604aa3df71SThierry Reding {
176147802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
176247802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
176347307954SThierry Reding 	u32 value;
176447802b09SThierry Reding 
176547307954SThierry Reding 	value = state->planes << 8 | GENERAL_UPDATE;
176647307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
176747307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
176847307954SThierry Reding 
176947307954SThierry Reding 	value = state->planes | GENERAL_ACT_REQ;
177047307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
177147307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
17724aa3df71SThierry Reding }
17734aa3df71SThierry Reding 
1774dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
17754aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
17764aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
17770b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
177864581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1779dee8268fSThierry Reding };
1780dee8268fSThierry Reding 
1781dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1782dee8268fSThierry Reding {
1783dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1784dee8268fSThierry Reding 	unsigned long status;
1785dee8268fSThierry Reding 
1786dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1787dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1788dee8268fSThierry Reding 
1789dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1790dee8268fSThierry Reding 		/*
1791dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1792dee8268fSThierry Reding 		*/
1793791ddb1eSThierry Reding 		dc->stats.frames++;
1794dee8268fSThierry Reding 	}
1795dee8268fSThierry Reding 
1796dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1797dee8268fSThierry Reding 		/*
1798dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1799dee8268fSThierry Reding 		*/
1800ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1801791ddb1eSThierry Reding 		dc->stats.vblank++;
1802dee8268fSThierry Reding 	}
1803dee8268fSThierry Reding 
1804dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1805dee8268fSThierry Reding 		/*
1806dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1807dee8268fSThierry Reding 		*/
1808791ddb1eSThierry Reding 		dc->stats.underflow++;
1809791ddb1eSThierry Reding 	}
1810791ddb1eSThierry Reding 
1811791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1812791ddb1eSThierry Reding 		/*
1813791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1814791ddb1eSThierry Reding 		*/
1815791ddb1eSThierry Reding 		dc->stats.overflow++;
1816dee8268fSThierry Reding 	}
1817dee8268fSThierry Reding 
181847307954SThierry Reding 	if (status & HEAD_UF_INT) {
181947307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
182047307954SThierry Reding 		dc->stats.underflow++;
182147307954SThierry Reding 	}
182247307954SThierry Reding 
1823dee8268fSThierry Reding 	return IRQ_HANDLED;
1824dee8268fSThierry Reding }
1825dee8268fSThierry Reding 
1826dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1827dee8268fSThierry Reding {
18289910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
18292bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1830dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1831d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1832c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1833c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1834dee8268fSThierry Reding 	int err;
1835dee8268fSThierry Reding 
1836617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
18372bcdcbfaSThierry Reding 	if (!dc->syncpt)
18382bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
18392bcdcbfaSThierry Reding 
18406f75b16bSDmitry Osipenko 	if (tegra->domain) {
18416f75b16bSDmitry Osipenko 		dc->group = iommu_group_get(client->dev);
18426f75b16bSDmitry Osipenko 
18436f75b16bSDmitry Osipenko 		if (dc->group && dc->group != tegra->group) {
18446f75b16bSDmitry Osipenko 			err = iommu_attach_group(tegra->domain, dc->group);
1845df06b759SThierry Reding 			if (err < 0) {
1846bc8828bdSThierry Reding 				dev_err(dc->dev,
1847bc8828bdSThierry Reding 					"failed to attach to domain: %d\n",
1848df06b759SThierry Reding 					err);
18496f75b16bSDmitry Osipenko 				iommu_group_put(dc->group);
1850df06b759SThierry Reding 				return err;
1851df06b759SThierry Reding 			}
1852df06b759SThierry Reding 
18536f75b16bSDmitry Osipenko 			tegra->group = dc->group;
1854bc8828bdSThierry Reding 		}
1855df06b759SThierry Reding 	}
1856df06b759SThierry Reding 
185747307954SThierry Reding 	if (dc->soc->wgrps)
185847307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
185947307954SThierry Reding 	else
186047307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
186147307954SThierry Reding 
1862c7679306SThierry Reding 	if (IS_ERR(primary)) {
1863c7679306SThierry Reding 		err = PTR_ERR(primary);
1864c7679306SThierry Reding 		goto cleanup;
1865c7679306SThierry Reding 	}
1866c7679306SThierry Reding 
1867c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1868c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1869c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1870c7679306SThierry Reding 			err = PTR_ERR(cursor);
1871c7679306SThierry Reding 			goto cleanup;
1872c7679306SThierry Reding 		}
18739f446d83SDmitry Osipenko 	} else {
18749f446d83SDmitry Osipenko 		/* dedicate one overlay to mouse cursor */
18759f446d83SDmitry Osipenko 		cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
18769f446d83SDmitry Osipenko 		if (IS_ERR(cursor)) {
18779f446d83SDmitry Osipenko 			err = PTR_ERR(cursor);
18789f446d83SDmitry Osipenko 			goto cleanup;
18799f446d83SDmitry Osipenko 		}
1880c7679306SThierry Reding 	}
1881c7679306SThierry Reding 
1882c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1883f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1884c7679306SThierry Reding 	if (err < 0)
1885c7679306SThierry Reding 		goto cleanup;
1886c7679306SThierry Reding 
1887dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1888dee8268fSThierry Reding 
1889d1f3e1e0SThierry Reding 	/*
1890d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1891d1f3e1e0SThierry Reding 	 * controllers.
1892d1f3e1e0SThierry Reding 	 */
1893d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1894d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1895d1f3e1e0SThierry Reding 
18969910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1897dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1898dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1899c7679306SThierry Reding 		goto cleanup;
1900dee8268fSThierry Reding 	}
1901dee8268fSThierry Reding 
1902dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1903dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1904dee8268fSThierry Reding 	if (err < 0) {
1905dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1906dee8268fSThierry Reding 			err);
1907c7679306SThierry Reding 		goto cleanup;
1908dee8268fSThierry Reding 	}
1909dee8268fSThierry Reding 
1910dee8268fSThierry Reding 	return 0;
1911c7679306SThierry Reding 
1912c7679306SThierry Reding cleanup:
191347307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
1914c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1915c7679306SThierry Reding 
191647307954SThierry Reding 	if (!IS_ERR(primary))
1917c7679306SThierry Reding 		drm_plane_cleanup(primary);
1918c7679306SThierry Reding 
19196f75b16bSDmitry Osipenko 	if (dc->group) {
19206f75b16bSDmitry Osipenko 		if (dc->group == tegra->group) {
19216f75b16bSDmitry Osipenko 			iommu_detach_group(tegra->domain, dc->group);
1922b1d0b34bSThierry Reding 			tegra->group = NULL;
1923b1d0b34bSThierry Reding 		}
1924b1d0b34bSThierry Reding 
19256f75b16bSDmitry Osipenko 		iommu_group_put(dc->group);
1926c7679306SThierry Reding 	}
1927c7679306SThierry Reding 
1928*fd5ec0dcSThierry Reding 	host1x_syncpt_free(dc->syncpt);
1929*fd5ec0dcSThierry Reding 
1930c7679306SThierry Reding 	return err;
1931dee8268fSThierry Reding }
1932dee8268fSThierry Reding 
1933dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1934dee8268fSThierry Reding {
1935b1d0b34bSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
1936dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1937b1d0b34bSThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1938dee8268fSThierry Reding 	int err;
1939dee8268fSThierry Reding 
1940dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1941dee8268fSThierry Reding 
1942dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1943dee8268fSThierry Reding 	if (err) {
1944dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1945dee8268fSThierry Reding 		return err;
1946dee8268fSThierry Reding 	}
1947dee8268fSThierry Reding 
19486f75b16bSDmitry Osipenko 	if (dc->group) {
19496f75b16bSDmitry Osipenko 		if (dc->group == tegra->group) {
19506f75b16bSDmitry Osipenko 			iommu_detach_group(tegra->domain, dc->group);
1951b1d0b34bSThierry Reding 			tegra->group = NULL;
1952b1d0b34bSThierry Reding 		}
1953b1d0b34bSThierry Reding 
19546f75b16bSDmitry Osipenko 		iommu_group_put(dc->group);
1955df06b759SThierry Reding 	}
1956df06b759SThierry Reding 
19572bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
19582bcdcbfaSThierry Reding 
1959dee8268fSThierry Reding 	return 0;
1960dee8268fSThierry Reding }
1961dee8268fSThierry Reding 
1962dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1963dee8268fSThierry Reding 	.init = tegra_dc_init,
1964dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1965dee8268fSThierry Reding };
1966dee8268fSThierry Reding 
19678620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
19687116e9a8SThierry Reding 	.supports_background_color = false,
19698620fc62SThierry Reding 	.supports_interlacing = false,
1970e687651bSThierry Reding 	.supports_cursor = false,
1971c134f019SThierry Reding 	.supports_block_linear = false,
1972ab7d3f58SThierry Reding 	.supports_blending = false,
1973d1f3e1e0SThierry Reding 	.pitch_align = 8,
19749c012700SThierry Reding 	.has_powergate = false,
1975f68ba691SDmitry Osipenko 	.coupled_pm = true,
197647307954SThierry Reding 	.has_nvdisplay = false,
1977511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
1978511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
1979511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
1980511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
1981e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
19828620fc62SThierry Reding };
19838620fc62SThierry Reding 
19848620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
19857116e9a8SThierry Reding 	.supports_background_color = false,
19868620fc62SThierry Reding 	.supports_interlacing = false,
1987e687651bSThierry Reding 	.supports_cursor = false,
1988c134f019SThierry Reding 	.supports_block_linear = false,
1989ab7d3f58SThierry Reding 	.supports_blending = false,
1990d1f3e1e0SThierry Reding 	.pitch_align = 8,
19919c012700SThierry Reding 	.has_powergate = false,
1992f68ba691SDmitry Osipenko 	.coupled_pm = false,
199347307954SThierry Reding 	.has_nvdisplay = false,
1994511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
1995511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
1996511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
1997511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
1998e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
1999d1f3e1e0SThierry Reding };
2000d1f3e1e0SThierry Reding 
2001d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
20027116e9a8SThierry Reding 	.supports_background_color = false,
2003d1f3e1e0SThierry Reding 	.supports_interlacing = false,
2004d1f3e1e0SThierry Reding 	.supports_cursor = false,
2005d1f3e1e0SThierry Reding 	.supports_block_linear = false,
2006ab7d3f58SThierry Reding 	.supports_blending = false,
2007d1f3e1e0SThierry Reding 	.pitch_align = 64,
20089c012700SThierry Reding 	.has_powergate = true,
2009f68ba691SDmitry Osipenko 	.coupled_pm = false,
201047307954SThierry Reding 	.has_nvdisplay = false,
2011511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2012511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2013511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2014511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2015e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
20168620fc62SThierry Reding };
20178620fc62SThierry Reding 
20188620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
20197116e9a8SThierry Reding 	.supports_background_color = true,
20208620fc62SThierry Reding 	.supports_interlacing = true,
2021e687651bSThierry Reding 	.supports_cursor = true,
2022c134f019SThierry Reding 	.supports_block_linear = true,
2023ab7d3f58SThierry Reding 	.supports_blending = true,
2024d1f3e1e0SThierry Reding 	.pitch_align = 64,
20259c012700SThierry Reding 	.has_powergate = true,
2026f68ba691SDmitry Osipenko 	.coupled_pm = false,
202747307954SThierry Reding 	.has_nvdisplay = false,
2028511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
20299a02d3afSStefan Agner 	.primary_formats = tegra124_primary_formats,
2030511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
20319a02d3afSStefan Agner 	.overlay_formats = tegra124_overlay_formats,
2032e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
20338620fc62SThierry Reding };
20348620fc62SThierry Reding 
20355b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
20367116e9a8SThierry Reding 	.supports_background_color = true,
20375b4f516fSThierry Reding 	.supports_interlacing = true,
20385b4f516fSThierry Reding 	.supports_cursor = true,
20395b4f516fSThierry Reding 	.supports_block_linear = true,
2040ab7d3f58SThierry Reding 	.supports_blending = true,
20415b4f516fSThierry Reding 	.pitch_align = 64,
20425b4f516fSThierry Reding 	.has_powergate = true,
2043f68ba691SDmitry Osipenko 	.coupled_pm = false,
204447307954SThierry Reding 	.has_nvdisplay = false,
2045511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2046511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2047511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2048511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2049e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
205047307954SThierry Reding };
205147307954SThierry Reding 
205247307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
205347307954SThierry Reding 	{
205447307954SThierry Reding 		.index = 0,
205547307954SThierry Reding 		.dc = 0,
205647307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
205747307954SThierry Reding 		.num_windows = 1,
205847307954SThierry Reding 	}, {
205947307954SThierry Reding 		.index = 1,
206047307954SThierry Reding 		.dc = 1,
206147307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
206247307954SThierry Reding 		.num_windows = 1,
206347307954SThierry Reding 	}, {
206447307954SThierry Reding 		.index = 2,
206547307954SThierry Reding 		.dc = 1,
206647307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
206747307954SThierry Reding 		.num_windows = 1,
206847307954SThierry Reding 	}, {
206947307954SThierry Reding 		.index = 3,
207047307954SThierry Reding 		.dc = 2,
207147307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
207247307954SThierry Reding 		.num_windows = 1,
207347307954SThierry Reding 	}, {
207447307954SThierry Reding 		.index = 4,
207547307954SThierry Reding 		.dc = 2,
207647307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
207747307954SThierry Reding 		.num_windows = 1,
207847307954SThierry Reding 	}, {
207947307954SThierry Reding 		.index = 5,
208047307954SThierry Reding 		.dc = 2,
208147307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
208247307954SThierry Reding 		.num_windows = 1,
208347307954SThierry Reding 	},
208447307954SThierry Reding };
208547307954SThierry Reding 
208647307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
208747307954SThierry Reding 	.supports_background_color = true,
208847307954SThierry Reding 	.supports_interlacing = true,
208947307954SThierry Reding 	.supports_cursor = true,
209047307954SThierry Reding 	.supports_block_linear = true,
2091ab7d3f58SThierry Reding 	.supports_blending = true,
209247307954SThierry Reding 	.pitch_align = 64,
209347307954SThierry Reding 	.has_powergate = false,
2094f68ba691SDmitry Osipenko 	.coupled_pm = false,
209547307954SThierry Reding 	.has_nvdisplay = true,
209647307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
209747307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
20985b4f516fSThierry Reding };
20995b4f516fSThierry Reding 
21008620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
21018620fc62SThierry Reding 	{
210247307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
210347307954SThierry Reding 		.data = &tegra186_dc_soc_info,
210447307954SThierry Reding 	}, {
21055b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
21065b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
21075b4f516fSThierry Reding 	}, {
21088620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
21098620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
21108620fc62SThierry Reding 	}, {
21119c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
21129c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
21139c012700SThierry Reding 	}, {
21148620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
21158620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
21168620fc62SThierry Reding 	}, {
21178620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
21188620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
21198620fc62SThierry Reding 	}, {
21208620fc62SThierry Reding 		/* sentinel */
21218620fc62SThierry Reding 	}
21228620fc62SThierry Reding };
2123ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
21248620fc62SThierry Reding 
212513411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
212613411dddSThierry Reding {
212713411dddSThierry Reding 	struct device_node *np;
212813411dddSThierry Reding 	u32 value = 0;
212913411dddSThierry Reding 	int err;
213013411dddSThierry Reding 
213113411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
213213411dddSThierry Reding 	if (err < 0) {
213313411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
213413411dddSThierry Reding 
213513411dddSThierry Reding 		/*
213613411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
213713411dddSThierry Reding 		 * correct head number by looking up the position of this
213813411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
213913411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
214013411dddSThierry Reding 		 * that the translation into a flattened device tree blob
214113411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
214213411dddSThierry Reding 		 * head number.
214313411dddSThierry Reding 		 *
214413411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
214513411dddSThierry Reding 		 * cases where only a single display controller is used.
214613411dddSThierry Reding 		 */
214713411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
2148cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
2149cf6b1744SJulia Lawall 				of_node_put(np);
215013411dddSThierry Reding 				break;
2151cf6b1744SJulia Lawall 			}
215213411dddSThierry Reding 
215313411dddSThierry Reding 			value++;
215413411dddSThierry Reding 		}
215513411dddSThierry Reding 	}
215613411dddSThierry Reding 
215713411dddSThierry Reding 	dc->pipe = value;
215813411dddSThierry Reding 
215913411dddSThierry Reding 	return 0;
216013411dddSThierry Reding }
216113411dddSThierry Reding 
2162f68ba691SDmitry Osipenko static int tegra_dc_match_by_pipe(struct device *dev, void *data)
2163f68ba691SDmitry Osipenko {
2164f68ba691SDmitry Osipenko 	struct tegra_dc *dc = dev_get_drvdata(dev);
2165f68ba691SDmitry Osipenko 	unsigned int pipe = (unsigned long)data;
2166f68ba691SDmitry Osipenko 
2167f68ba691SDmitry Osipenko 	return dc->pipe == pipe;
2168f68ba691SDmitry Osipenko }
2169f68ba691SDmitry Osipenko 
2170f68ba691SDmitry Osipenko static int tegra_dc_couple(struct tegra_dc *dc)
2171f68ba691SDmitry Osipenko {
2172f68ba691SDmitry Osipenko 	/*
2173f68ba691SDmitry Osipenko 	 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2174f68ba691SDmitry Osipenko 	 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2175f68ba691SDmitry Osipenko 	 * POWER_CONTROL registers during CRTC enabling.
2176f68ba691SDmitry Osipenko 	 */
2177f68ba691SDmitry Osipenko 	if (dc->soc->coupled_pm && dc->pipe == 1) {
2178f68ba691SDmitry Osipenko 		u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE;
2179f68ba691SDmitry Osipenko 		struct device_link *link;
2180f68ba691SDmitry Osipenko 		struct device *partner;
2181f68ba691SDmitry Osipenko 
2182ef1b204aSWei Yongjun 		partner = driver_find_device(dc->dev->driver, NULL, NULL,
2183f68ba691SDmitry Osipenko 					     tegra_dc_match_by_pipe);
2184f68ba691SDmitry Osipenko 		if (!partner)
2185f68ba691SDmitry Osipenko 			return -EPROBE_DEFER;
2186f68ba691SDmitry Osipenko 
2187f68ba691SDmitry Osipenko 		link = device_link_add(dc->dev, partner, flags);
2188f68ba691SDmitry Osipenko 		if (!link) {
2189f68ba691SDmitry Osipenko 			dev_err(dc->dev, "failed to link controllers\n");
2190f68ba691SDmitry Osipenko 			return -EINVAL;
2191f68ba691SDmitry Osipenko 		}
2192f68ba691SDmitry Osipenko 
2193f68ba691SDmitry Osipenko 		dev_dbg(dc->dev, "coupled to %s\n", dev_name(partner));
2194f68ba691SDmitry Osipenko 	}
2195f68ba691SDmitry Osipenko 
2196f68ba691SDmitry Osipenko 	return 0;
2197f68ba691SDmitry Osipenko }
2198f68ba691SDmitry Osipenko 
2199dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
2200dee8268fSThierry Reding {
2201dee8268fSThierry Reding 	struct resource *regs;
2202dee8268fSThierry Reding 	struct tegra_dc *dc;
2203dee8268fSThierry Reding 	int err;
2204dee8268fSThierry Reding 
2205dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2206dee8268fSThierry Reding 	if (!dc)
2207dee8268fSThierry Reding 		return -ENOMEM;
2208dee8268fSThierry Reding 
2209b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
22108620fc62SThierry Reding 
2211dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2212dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2213dee8268fSThierry Reding 
221413411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
221513411dddSThierry Reding 	if (err < 0)
221613411dddSThierry Reding 		return err;
221713411dddSThierry Reding 
2218f68ba691SDmitry Osipenko 	err = tegra_dc_couple(dc);
2219f68ba691SDmitry Osipenko 	if (err < 0)
2220f68ba691SDmitry Osipenko 		return err;
2221f68ba691SDmitry Osipenko 
2222dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2223dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
2224dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
2225dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
2226dee8268fSThierry Reding 	}
2227dee8268fSThierry Reding 
2228ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2229ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2230ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2231ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2232ca48080aSStephen Warren 	}
2233ca48080aSStephen Warren 
2234a2f2f740SThierry Reding 	/* assert reset and disable clock */
2235a2f2f740SThierry Reding 	err = clk_prepare_enable(dc->clk);
2236a2f2f740SThierry Reding 	if (err < 0)
2237a2f2f740SThierry Reding 		return err;
2238a2f2f740SThierry Reding 
2239a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2240a2f2f740SThierry Reding 
2241a2f2f740SThierry Reding 	err = reset_control_assert(dc->rst);
2242a2f2f740SThierry Reding 	if (err < 0)
2243a2f2f740SThierry Reding 		return err;
2244a2f2f740SThierry Reding 
2245a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2246a2f2f740SThierry Reding 
2247a2f2f740SThierry Reding 	clk_disable_unprepare(dc->clk);
224833a8eb8dSThierry Reding 
22499c012700SThierry Reding 	if (dc->soc->has_powergate) {
22509c012700SThierry Reding 		if (dc->pipe == 0)
22519c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
22529c012700SThierry Reding 		else
22539c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
22549c012700SThierry Reding 
225533a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
22569c012700SThierry Reding 	}
2257dee8268fSThierry Reding 
2258dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2259dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2260dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2261dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2262dee8268fSThierry Reding 
2263dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2264dee8268fSThierry Reding 	if (dc->irq < 0) {
2265dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2266dee8268fSThierry Reding 		return -ENXIO;
2267dee8268fSThierry Reding 	}
2268dee8268fSThierry Reding 
2269dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2270dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2271dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2272dee8268fSThierry Reding 		return err;
2273dee8268fSThierry Reding 	}
2274dee8268fSThierry Reding 
227533a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
227633a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
227733a8eb8dSThierry Reding 
227833a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
227933a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
228033a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
228133a8eb8dSThierry Reding 
2282dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2283dee8268fSThierry Reding 	if (err < 0) {
2284dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2285dee8268fSThierry Reding 			err);
2286dee8268fSThierry Reding 		return err;
2287dee8268fSThierry Reding 	}
2288dee8268fSThierry Reding 
2289dee8268fSThierry Reding 	return 0;
2290dee8268fSThierry Reding }
2291dee8268fSThierry Reding 
2292dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2293dee8268fSThierry Reding {
2294dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2295dee8268fSThierry Reding 	int err;
2296dee8268fSThierry Reding 
2297dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2298dee8268fSThierry Reding 	if (err < 0) {
2299dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2300dee8268fSThierry Reding 			err);
2301dee8268fSThierry Reding 		return err;
2302dee8268fSThierry Reding 	}
2303dee8268fSThierry Reding 
230459d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
230559d29c0eSThierry Reding 	if (err < 0) {
230659d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
230759d29c0eSThierry Reding 		return err;
230859d29c0eSThierry Reding 	}
230959d29c0eSThierry Reding 
231033a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
231133a8eb8dSThierry Reding 
231233a8eb8dSThierry Reding 	return 0;
231333a8eb8dSThierry Reding }
231433a8eb8dSThierry Reding 
231533a8eb8dSThierry Reding #ifdef CONFIG_PM
231633a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
231733a8eb8dSThierry Reding {
231833a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
231933a8eb8dSThierry Reding 	int err;
232033a8eb8dSThierry Reding 
232133a8eb8dSThierry Reding 	err = reset_control_assert(dc->rst);
232233a8eb8dSThierry Reding 	if (err < 0) {
232333a8eb8dSThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
232433a8eb8dSThierry Reding 		return err;
232533a8eb8dSThierry Reding 	}
23269c012700SThierry Reding 
23279c012700SThierry Reding 	if (dc->soc->has_powergate)
23289c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
23299c012700SThierry Reding 
2330dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2331dee8268fSThierry Reding 
2332dee8268fSThierry Reding 	return 0;
2333dee8268fSThierry Reding }
2334dee8268fSThierry Reding 
233533a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
233633a8eb8dSThierry Reding {
233733a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
233833a8eb8dSThierry Reding 	int err;
233933a8eb8dSThierry Reding 
234033a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
234133a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
234233a8eb8dSThierry Reding 							dc->rst);
234333a8eb8dSThierry Reding 		if (err < 0) {
234433a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
234533a8eb8dSThierry Reding 			return err;
234633a8eb8dSThierry Reding 		}
234733a8eb8dSThierry Reding 	} else {
234833a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
234933a8eb8dSThierry Reding 		if (err < 0) {
235033a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
235133a8eb8dSThierry Reding 			return err;
235233a8eb8dSThierry Reding 		}
235333a8eb8dSThierry Reding 
235433a8eb8dSThierry Reding 		err = reset_control_deassert(dc->rst);
235533a8eb8dSThierry Reding 		if (err < 0) {
2356f68ba691SDmitry Osipenko 			dev_err(dev, "failed to deassert reset: %d\n", err);
235733a8eb8dSThierry Reding 			return err;
235833a8eb8dSThierry Reding 		}
235933a8eb8dSThierry Reding 	}
236033a8eb8dSThierry Reding 
236133a8eb8dSThierry Reding 	return 0;
236233a8eb8dSThierry Reding }
236333a8eb8dSThierry Reding #endif
236433a8eb8dSThierry Reding 
236533a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
236633a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
236733a8eb8dSThierry Reding };
236833a8eb8dSThierry Reding 
2369dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2370dee8268fSThierry Reding 	.driver = {
2371dee8268fSThierry Reding 		.name = "tegra-dc",
2372dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
237333a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2374dee8268fSThierry Reding 	},
2375dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2376dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2377dee8268fSThierry Reding };
2378