xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 04d5d5df9df79f9045e76404775fc8a084aac23d)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dee8268fSThierry Reding /*
3dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
4dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
5dee8268fSThierry Reding  */
6dee8268fSThierry Reding 
7dee8268fSThierry Reding #include <linux/clk.h>
8dee8268fSThierry Reding #include <linux/debugfs.h>
9eb1df694SSam Ravnborg #include <linux/delay.h>
10df06b759SThierry Reding #include <linux/iommu.h>
11*04d5d5dfSDmitry Osipenko #include <linux/interconnect.h>
12eb1df694SSam Ravnborg #include <linux/module.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 
19eb1df694SSam Ravnborg #include <drm/drm_atomic.h>
20eb1df694SSam Ravnborg #include <drm/drm_atomic_helper.h>
21eb1df694SSam Ravnborg #include <drm/drm_debugfs.h>
22eb1df694SSam Ravnborg #include <drm/drm_fourcc.h>
23eb1df694SSam Ravnborg #include <drm/drm_plane_helper.h>
24eb1df694SSam Ravnborg #include <drm/drm_vblank.h>
25eb1df694SSam Ravnborg 
26dee8268fSThierry Reding #include "dc.h"
27dee8268fSThierry Reding #include "drm.h"
28dee8268fSThierry Reding #include "gem.h"
2947307954SThierry Reding #include "hub.h"
305acd3514SThierry Reding #include "plane.h"
31dee8268fSThierry Reding 
32b7e0b04aSMaarten Lankhorst static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
33b7e0b04aSMaarten Lankhorst 					    struct drm_crtc_state *state);
34b7e0b04aSMaarten Lankhorst 
35791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
36791ddb1eSThierry Reding {
37791ddb1eSThierry Reding 	stats->frames = 0;
38791ddb1eSThierry Reding 	stats->vblank = 0;
39791ddb1eSThierry Reding 	stats->underflow = 0;
40791ddb1eSThierry Reding 	stats->overflow = 0;
41791ddb1eSThierry Reding }
42791ddb1eSThierry Reding 
431087fac1SThierry Reding /* Reads the active copy of a register. */
4486df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
4586df256fSThierry Reding {
4686df256fSThierry Reding 	u32 value;
4786df256fSThierry Reding 
4886df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
4986df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
5086df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
5186df256fSThierry Reding 
5286df256fSThierry Reding 	return value;
5386df256fSThierry Reding }
5486df256fSThierry Reding 
551087fac1SThierry Reding static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
561087fac1SThierry Reding 					      unsigned int offset)
571087fac1SThierry Reding {
581087fac1SThierry Reding 	if (offset >= 0x500 && offset <= 0x638) {
591087fac1SThierry Reding 		offset = 0x000 + (offset - 0x500);
601087fac1SThierry Reding 		return plane->offset + offset;
611087fac1SThierry Reding 	}
621087fac1SThierry Reding 
631087fac1SThierry Reding 	if (offset >= 0x700 && offset <= 0x719) {
641087fac1SThierry Reding 		offset = 0x180 + (offset - 0x700);
651087fac1SThierry Reding 		return plane->offset + offset;
661087fac1SThierry Reding 	}
671087fac1SThierry Reding 
681087fac1SThierry Reding 	if (offset >= 0x800 && offset <= 0x839) {
691087fac1SThierry Reding 		offset = 0x1c0 + (offset - 0x800);
701087fac1SThierry Reding 		return plane->offset + offset;
711087fac1SThierry Reding 	}
721087fac1SThierry Reding 
731087fac1SThierry Reding 	dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
741087fac1SThierry Reding 
751087fac1SThierry Reding 	return plane->offset + offset;
761087fac1SThierry Reding }
771087fac1SThierry Reding 
781087fac1SThierry Reding static inline u32 tegra_plane_readl(struct tegra_plane *plane,
791087fac1SThierry Reding 				    unsigned int offset)
801087fac1SThierry Reding {
811087fac1SThierry Reding 	return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
821087fac1SThierry Reding }
831087fac1SThierry Reding 
841087fac1SThierry Reding static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
851087fac1SThierry Reding 				      unsigned int offset)
861087fac1SThierry Reding {
871087fac1SThierry Reding 	tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
881087fac1SThierry Reding }
891087fac1SThierry Reding 
90c57997bcSThierry Reding bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
91c57997bcSThierry Reding {
92c57997bcSThierry Reding 	struct device_node *np = dc->dev->of_node;
93c57997bcSThierry Reding 	struct of_phandle_iterator it;
94c57997bcSThierry Reding 	int err;
95c57997bcSThierry Reding 
96c57997bcSThierry Reding 	of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
97c57997bcSThierry Reding 		if (it.node == dev->of_node)
98c57997bcSThierry Reding 			return true;
99c57997bcSThierry Reding 
100c57997bcSThierry Reding 	return false;
101c57997bcSThierry Reding }
102c57997bcSThierry Reding 
10386df256fSThierry Reding /*
104d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
105d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
106d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
107d700ba7aSThierry Reding  * on the next frame boundary otherwise.
108d700ba7aSThierry Reding  *
109d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
110d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
111d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
112d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
113d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
114d700ba7aSThierry Reding  */
11562b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
116205d48edSThierry Reding {
117205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
118205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
119205d48edSThierry Reding }
120205d48edSThierry Reding 
12110288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
12210288eeaSThierry Reding 				  unsigned int bpp)
12310288eeaSThierry Reding {
12410288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
12510288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
12610288eeaSThierry Reding 	u32 dda_inc;
12710288eeaSThierry Reding 	int max;
12810288eeaSThierry Reding 
12910288eeaSThierry Reding 	if (v)
13010288eeaSThierry Reding 		max = 15;
13110288eeaSThierry Reding 	else {
13210288eeaSThierry Reding 		switch (bpp) {
13310288eeaSThierry Reding 		case 2:
13410288eeaSThierry Reding 			max = 8;
13510288eeaSThierry Reding 			break;
13610288eeaSThierry Reding 
13710288eeaSThierry Reding 		default:
13810288eeaSThierry Reding 			WARN_ON_ONCE(1);
139df561f66SGustavo A. R. Silva 			fallthrough;
14010288eeaSThierry Reding 		case 4:
14110288eeaSThierry Reding 			max = 4;
14210288eeaSThierry Reding 			break;
14310288eeaSThierry Reding 		}
14410288eeaSThierry Reding 	}
14510288eeaSThierry Reding 
14610288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
14710288eeaSThierry Reding 	inf.full -= dfixed_const(1);
14810288eeaSThierry Reding 
14910288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
15010288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
15110288eeaSThierry Reding 
15210288eeaSThierry Reding 	return dda_inc;
15310288eeaSThierry Reding }
15410288eeaSThierry Reding 
15510288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
15610288eeaSThierry Reding {
15710288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
15810288eeaSThierry Reding 	return dfixed_frac(inf);
15910288eeaSThierry Reding }
16010288eeaSThierry Reding 
161ab7d3f58SThierry Reding static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
162ab7d3f58SThierry Reding {
163ebae8d07SThierry Reding 	u32 background[3] = {
164ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
165ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
166ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
167ebae8d07SThierry Reding 	};
168ebae8d07SThierry Reding 	u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
169ebae8d07SThierry Reding 			 BLEND_COLOR_KEY_NONE;
170ebae8d07SThierry Reding 	u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
171ebae8d07SThierry Reding 	struct tegra_plane_state *state;
1723dae08bcSDmitry Osipenko 	u32 blending[2];
173ebae8d07SThierry Reding 	unsigned int i;
174ebae8d07SThierry Reding 
1753dae08bcSDmitry Osipenko 	/* disable blending for non-overlapping case */
176ebae8d07SThierry Reding 	tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
177ebae8d07SThierry Reding 	tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
178ab7d3f58SThierry Reding 
1793dae08bcSDmitry Osipenko 	state = to_tegra_plane_state(plane->base.state);
1803dae08bcSDmitry Osipenko 
1813dae08bcSDmitry Osipenko 	if (state->opaque) {
1823dae08bcSDmitry Osipenko 		/*
1833dae08bcSDmitry Osipenko 		 * Since custom fix-weight blending isn't utilized and weight
1843dae08bcSDmitry Osipenko 		 * of top window is set to max, we can enforce dependent
1853dae08bcSDmitry Osipenko 		 * blending which in this case results in transparent bottom
1863dae08bcSDmitry Osipenko 		 * window if top window is opaque and if top window enables
1873dae08bcSDmitry Osipenko 		 * alpha blending, then bottom window is getting alpha value
1883dae08bcSDmitry Osipenko 		 * of 1 minus the sum of alpha components of the overlapping
1893dae08bcSDmitry Osipenko 		 * plane.
1903dae08bcSDmitry Osipenko 		 */
1913dae08bcSDmitry Osipenko 		background[0] |= BLEND_CONTROL_DEPENDENT;
1923dae08bcSDmitry Osipenko 		background[1] |= BLEND_CONTROL_DEPENDENT;
1933dae08bcSDmitry Osipenko 
1943dae08bcSDmitry Osipenko 		/*
1953dae08bcSDmitry Osipenko 		 * The region where three windows overlap is the intersection
1963dae08bcSDmitry Osipenko 		 * of the two regions where two windows overlap. It contributes
1973dae08bcSDmitry Osipenko 		 * to the area if all of the windows on top of it have an alpha
1983dae08bcSDmitry Osipenko 		 * component.
1993dae08bcSDmitry Osipenko 		 */
2003dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
2013dae08bcSDmitry Osipenko 		case 0:
2023dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2033dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
2043dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
2053dae08bcSDmitry Osipenko 			break;
2063dae08bcSDmitry Osipenko 
2073dae08bcSDmitry Osipenko 		case 1:
2083dae08bcSDmitry Osipenko 			background[2] |= BLEND_CONTROL_DEPENDENT;
2093dae08bcSDmitry Osipenko 			break;
2103dae08bcSDmitry Osipenko 		}
2113dae08bcSDmitry Osipenko 	} else {
2123dae08bcSDmitry Osipenko 		/*
2133dae08bcSDmitry Osipenko 		 * Enable alpha blending if pixel format has an alpha
2143dae08bcSDmitry Osipenko 		 * component.
2153dae08bcSDmitry Osipenko 		 */
2163dae08bcSDmitry Osipenko 		foreground |= BLEND_CONTROL_ALPHA;
2173dae08bcSDmitry Osipenko 
2183dae08bcSDmitry Osipenko 		/*
2193dae08bcSDmitry Osipenko 		 * If any of the windows on top of this window is opaque, it
2203dae08bcSDmitry Osipenko 		 * will completely conceal this window within that area. If
2213dae08bcSDmitry Osipenko 		 * top window has an alpha component, it is blended over the
2223dae08bcSDmitry Osipenko 		 * bottom window.
2233dae08bcSDmitry Osipenko 		 */
2243dae08bcSDmitry Osipenko 		for (i = 0; i < 2; i++) {
2253dae08bcSDmitry Osipenko 			if (state->blending[i].alpha &&
2263dae08bcSDmitry Osipenko 			    state->blending[i].top)
2273dae08bcSDmitry Osipenko 				background[i] |= BLEND_CONTROL_DEPENDENT;
2283dae08bcSDmitry Osipenko 		}
2293dae08bcSDmitry Osipenko 
2303dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
2313dae08bcSDmitry Osipenko 		case 0:
2323dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2333dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
2343dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
2353dae08bcSDmitry Osipenko 			break;
2363dae08bcSDmitry Osipenko 
2373dae08bcSDmitry Osipenko 		case 1:
2383dae08bcSDmitry Osipenko 			/*
2393dae08bcSDmitry Osipenko 			 * When both middle and topmost windows have an alpha,
2403dae08bcSDmitry Osipenko 			 * these windows a mixed together and then the result
2413dae08bcSDmitry Osipenko 			 * is blended over the bottom window.
2423dae08bcSDmitry Osipenko 			 */
2433dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2443dae08bcSDmitry Osipenko 			    state->blending[0].top)
2453dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2463dae08bcSDmitry Osipenko 
2473dae08bcSDmitry Osipenko 			if (state->blending[1].alpha &&
2483dae08bcSDmitry Osipenko 			    state->blending[1].top)
2493dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2503dae08bcSDmitry Osipenko 			break;
2513dae08bcSDmitry Osipenko 		}
2523dae08bcSDmitry Osipenko 	}
2533dae08bcSDmitry Osipenko 
2543dae08bcSDmitry Osipenko 	switch (state->base.normalized_zpos) {
255ab7d3f58SThierry Reding 	case 0:
256ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
257ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
258ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
259ab7d3f58SThierry Reding 		break;
260ab7d3f58SThierry Reding 
261ab7d3f58SThierry Reding 	case 1:
2623dae08bcSDmitry Osipenko 		/*
2633dae08bcSDmitry Osipenko 		 * If window B / C is topmost, then X / Y registers are
2643dae08bcSDmitry Osipenko 		 * matching the order of blending[...] state indices,
2653dae08bcSDmitry Osipenko 		 * otherwise a swap is required.
2663dae08bcSDmitry Osipenko 		 */
2673dae08bcSDmitry Osipenko 		if (!state->blending[0].top && state->blending[1].top) {
2683dae08bcSDmitry Osipenko 			blending[0] = foreground;
2693dae08bcSDmitry Osipenko 			blending[1] = background[1];
2703dae08bcSDmitry Osipenko 		} else {
2713dae08bcSDmitry Osipenko 			blending[0] = background[0];
2723dae08bcSDmitry Osipenko 			blending[1] = foreground;
2733dae08bcSDmitry Osipenko 		}
2743dae08bcSDmitry Osipenko 
2753dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
2763dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
277ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
278ab7d3f58SThierry Reding 		break;
279ab7d3f58SThierry Reding 
280ab7d3f58SThierry Reding 	case 2:
281ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
282ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
283ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
284ab7d3f58SThierry Reding 		break;
285ab7d3f58SThierry Reding 	}
286ab7d3f58SThierry Reding }
287ab7d3f58SThierry Reding 
288ab7d3f58SThierry Reding static void tegra_plane_setup_blending(struct tegra_plane *plane,
289ab7d3f58SThierry Reding 				       const struct tegra_dc_window *window)
290ab7d3f58SThierry Reding {
291ab7d3f58SThierry Reding 	u32 value;
292ab7d3f58SThierry Reding 
293ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
294ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
295ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
296ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
297ab7d3f58SThierry Reding 
298ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
299ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
300ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
301ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
302ab7d3f58SThierry Reding 
303ab7d3f58SThierry Reding 	value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
304ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
305ab7d3f58SThierry Reding }
306ab7d3f58SThierry Reding 
307acc6a3a9SDmitry Osipenko static bool
308acc6a3a9SDmitry Osipenko tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
309acc6a3a9SDmitry Osipenko 				     const struct tegra_dc_window *window)
310acc6a3a9SDmitry Osipenko {
311acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
312acc6a3a9SDmitry Osipenko 
313acc6a3a9SDmitry Osipenko 	if (window->src.w == window->dst.w)
314acc6a3a9SDmitry Osipenko 		return false;
315acc6a3a9SDmitry Osipenko 
316acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
317acc6a3a9SDmitry Osipenko 		return false;
318acc6a3a9SDmitry Osipenko 
319acc6a3a9SDmitry Osipenko 	return true;
320acc6a3a9SDmitry Osipenko }
321acc6a3a9SDmitry Osipenko 
322acc6a3a9SDmitry Osipenko static bool
323acc6a3a9SDmitry Osipenko tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
324acc6a3a9SDmitry Osipenko 				   const struct tegra_dc_window *window)
325acc6a3a9SDmitry Osipenko {
326acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
327acc6a3a9SDmitry Osipenko 
328acc6a3a9SDmitry Osipenko 	if (window->src.h == window->dst.h)
329acc6a3a9SDmitry Osipenko 		return false;
330acc6a3a9SDmitry Osipenko 
331acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
332acc6a3a9SDmitry Osipenko 		return false;
333acc6a3a9SDmitry Osipenko 
334acc6a3a9SDmitry Osipenko 	if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
335acc6a3a9SDmitry Osipenko 		return false;
336acc6a3a9SDmitry Osipenko 
337acc6a3a9SDmitry Osipenko 	return true;
338acc6a3a9SDmitry Osipenko }
339acc6a3a9SDmitry Osipenko 
3401087fac1SThierry Reding static void tegra_dc_setup_window(struct tegra_plane *plane,
34110288eeaSThierry Reding 				  const struct tegra_dc_window *window)
34210288eeaSThierry Reding {
34310288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
3441087fac1SThierry Reding 	struct tegra_dc *dc = plane->dc;
34510288eeaSThierry Reding 	bool yuv, planar;
3461087fac1SThierry Reding 	u32 value;
34710288eeaSThierry Reding 
34810288eeaSThierry Reding 	/*
34910288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
35010288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
35110288eeaSThierry Reding 	 */
352e16efff4SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar, NULL);
35310288eeaSThierry Reding 	if (!yuv)
35410288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
35510288eeaSThierry Reding 	else
35610288eeaSThierry Reding 		bpp = planar ? 1 : 2;
35710288eeaSThierry Reding 
3581087fac1SThierry Reding 	tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
3591087fac1SThierry Reding 	tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
36010288eeaSThierry Reding 
36110288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
3621087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_POSITION);
36310288eeaSThierry Reding 
36410288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
3651087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_SIZE);
36610288eeaSThierry Reding 
36710288eeaSThierry Reding 	h_offset = window->src.x * bpp;
36810288eeaSThierry Reding 	v_offset = window->src.y;
36910288eeaSThierry Reding 	h_size = window->src.w * bpp;
37010288eeaSThierry Reding 	v_size = window->src.h;
37110288eeaSThierry Reding 
372cd740777SDmitry Osipenko 	if (window->reflect_x)
373cd740777SDmitry Osipenko 		h_offset += (window->src.w - 1) * bpp;
374cd740777SDmitry Osipenko 
375cd740777SDmitry Osipenko 	if (window->reflect_y)
376cd740777SDmitry Osipenko 		v_offset += window->src.h - 1;
377cd740777SDmitry Osipenko 
37810288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
3791087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
38010288eeaSThierry Reding 
38110288eeaSThierry Reding 	/*
38210288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
38310288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
38410288eeaSThierry Reding 	 */
38510288eeaSThierry Reding 	if (yuv && planar)
38610288eeaSThierry Reding 		bpp = 2;
38710288eeaSThierry Reding 
38810288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
38910288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
39010288eeaSThierry Reding 
39110288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
3921087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
39310288eeaSThierry Reding 
39410288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
39510288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
39610288eeaSThierry Reding 
3971087fac1SThierry Reding 	tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
3981087fac1SThierry Reding 	tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
39910288eeaSThierry Reding 
4001087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
4011087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
40210288eeaSThierry Reding 
4031087fac1SThierry Reding 	tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
40410288eeaSThierry Reding 
40510288eeaSThierry Reding 	if (yuv && planar) {
4061087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
4071087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
40810288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
4091087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
41010288eeaSThierry Reding 	} else {
4111087fac1SThierry Reding 		tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
41210288eeaSThierry Reding 	}
41310288eeaSThierry Reding 
4141087fac1SThierry Reding 	tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
4151087fac1SThierry Reding 	tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
41610288eeaSThierry Reding 
417c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
418c134f019SThierry Reding 		unsigned long height = window->tiling.value;
419c134f019SThierry Reding 
420c134f019SThierry Reding 		switch (window->tiling.mode) {
421c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
422c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
423c134f019SThierry Reding 			break;
424c134f019SThierry Reding 
425c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
426c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
427c134f019SThierry Reding 			break;
428c134f019SThierry Reding 
429c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
430c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
431c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
432c134f019SThierry Reding 			break;
433c134f019SThierry Reding 		}
434c134f019SThierry Reding 
4351087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
43610288eeaSThierry Reding 	} else {
437c134f019SThierry Reding 		switch (window->tiling.mode) {
438c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
43910288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
44010288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
441c134f019SThierry Reding 			break;
442c134f019SThierry Reding 
443c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
444c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
445c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
446c134f019SThierry Reding 			break;
447c134f019SThierry Reding 
448c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
4494aa3df71SThierry Reding 			/*
4504aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
4514aa3df71SThierry Reding 			 * will already have filtered it out.
4524aa3df71SThierry Reding 			 */
4534aa3df71SThierry Reding 			break;
45410288eeaSThierry Reding 		}
45510288eeaSThierry Reding 
4561087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
457c134f019SThierry Reding 	}
45810288eeaSThierry Reding 
45910288eeaSThierry Reding 	value = WIN_ENABLE;
46010288eeaSThierry Reding 
46110288eeaSThierry Reding 	if (yuv) {
46210288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
4631087fac1SThierry Reding 		tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
4641087fac1SThierry Reding 		tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
4651087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
4661087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
4671087fac1SThierry Reding 		tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
4681087fac1SThierry Reding 		tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
4691087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
4701087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
47110288eeaSThierry Reding 
47210288eeaSThierry Reding 		value |= CSC_ENABLE;
47310288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
47410288eeaSThierry Reding 		value |= COLOR_EXPAND;
47510288eeaSThierry Reding 	}
47610288eeaSThierry Reding 
477cd740777SDmitry Osipenko 	if (window->reflect_x)
478cd740777SDmitry Osipenko 		value |= H_DIRECTION;
479cd740777SDmitry Osipenko 
480e9e476f7SDmitry Osipenko 	if (window->reflect_y)
48110288eeaSThierry Reding 		value |= V_DIRECTION;
48210288eeaSThierry Reding 
483acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_horizontal_filtering(plane, window)) {
484acc6a3a9SDmitry Osipenko 		/*
485acc6a3a9SDmitry Osipenko 		 * Enable horizontal 6-tap filter and set filtering
486acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
487acc6a3a9SDmitry Osipenko 		 */
488acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
489acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
490acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
491acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
492acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
493acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
494acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
495acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
496acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
497acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
498acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
499acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
500acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
501acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
502acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
503acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
504acc6a3a9SDmitry Osipenko 
505acc6a3a9SDmitry Osipenko 		value |= H_FILTER;
506acc6a3a9SDmitry Osipenko 	}
507acc6a3a9SDmitry Osipenko 
508acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_vertical_filtering(plane, window)) {
509acc6a3a9SDmitry Osipenko 		unsigned int i, k;
510acc6a3a9SDmitry Osipenko 
511acc6a3a9SDmitry Osipenko 		/*
512acc6a3a9SDmitry Osipenko 		 * Enable vertical 2-tap filter and set filtering
513acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
514acc6a3a9SDmitry Osipenko 		 */
515acc6a3a9SDmitry Osipenko 		for (i = 0, k = 128; i < 16; i++, k -= 8)
516acc6a3a9SDmitry Osipenko 			tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
517acc6a3a9SDmitry Osipenko 
518acc6a3a9SDmitry Osipenko 		value |= V_FILTER;
519acc6a3a9SDmitry Osipenko 	}
520acc6a3a9SDmitry Osipenko 
5211087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
52210288eeaSThierry Reding 
523a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending)
524ab7d3f58SThierry Reding 		tegra_plane_setup_blending_legacy(plane);
525a43d0a00SDmitry Osipenko 	else
526a43d0a00SDmitry Osipenko 		tegra_plane_setup_blending(plane, window);
527c7679306SThierry Reding }
528c7679306SThierry Reding 
529511c7023SThierry Reding static const u32 tegra20_primary_formats[] = {
530511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
531511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
532c7679306SThierry Reding 	DRM_FORMAT_RGB565,
533511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
534511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
535511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
536ebae8d07SThierry Reding 	/* non-native formats */
537ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
538ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
539ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
540ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
541511c7023SThierry Reding };
542511c7023SThierry Reding 
543e90124cbSThierry Reding static const u64 tegra20_modifiers[] = {
544e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
545e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
546e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
547e90124cbSThierry Reding };
548e90124cbSThierry Reding 
549511c7023SThierry Reding static const u32 tegra114_primary_formats[] = {
550511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
551511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
552511c7023SThierry Reding 	DRM_FORMAT_RGB565,
553511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
554511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
555511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
556511c7023SThierry Reding 	/* new on Tegra114 */
557511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
558511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
559511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
560511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
561511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
562511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
563511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
564511c7023SThierry Reding 	DRM_FORMAT_BGR565,
565511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
566511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
567511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
568511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
569511c7023SThierry Reding };
570511c7023SThierry Reding 
571511c7023SThierry Reding static const u32 tegra124_primary_formats[] = {
572511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
573511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
574511c7023SThierry Reding 	DRM_FORMAT_RGB565,
575511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
576511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
577511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
578511c7023SThierry Reding 	/* new on Tegra114 */
579511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
580511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
581511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
582511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
583511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
584511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
585511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
586511c7023SThierry Reding 	DRM_FORMAT_BGR565,
587511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
588511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
589511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
590511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
591511c7023SThierry Reding 	/* new on Tegra124 */
592511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
593511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
594c7679306SThierry Reding };
595c7679306SThierry Reding 
596e90124cbSThierry Reding static const u64 tegra124_modifiers[] = {
597e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
598e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
599e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
600e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
601e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
602e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
603e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
604e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
605e90124cbSThierry Reding };
606e90124cbSThierry Reding 
6074aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
6087c11b99aSMaxime Ripard 				    struct drm_atomic_state *state)
6094aa3df71SThierry Reding {
6107c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
6117c11b99aSMaxime Ripard 										 plane);
612ba5c1649SMaxime Ripard 	struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
613cd740777SDmitry Osipenko 	unsigned int supported_rotation = DRM_MODE_ROTATE_0 |
614cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_X |
615cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_Y;
616ba5c1649SMaxime Ripard 	unsigned int rotation = new_plane_state->rotation;
6178f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
61847802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
619ba5c1649SMaxime Ripard 	struct tegra_dc *dc = to_tegra_dc(new_plane_state->crtc);
620c7679306SThierry Reding 	int err;
621c7679306SThierry Reding 
622*04d5d5dfSDmitry Osipenko 	plane_state->peak_memory_bandwidth = 0;
623*04d5d5dfSDmitry Osipenko 	plane_state->avg_memory_bandwidth = 0;
624*04d5d5dfSDmitry Osipenko 
6254aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
626*04d5d5dfSDmitry Osipenko 	if (!new_plane_state->crtc) {
627*04d5d5dfSDmitry Osipenko 		plane_state->total_peak_memory_bandwidth = 0;
6284aa3df71SThierry Reding 		return 0;
629*04d5d5dfSDmitry Osipenko 	}
6304aa3df71SThierry Reding 
631ba5c1649SMaxime Ripard 	err = tegra_plane_format(new_plane_state->fb->format->format,
6323dae08bcSDmitry Osipenko 				 &plane_state->format,
6338f604f8cSThierry Reding 				 &plane_state->swap);
6344aa3df71SThierry Reding 	if (err < 0)
6354aa3df71SThierry Reding 		return err;
6364aa3df71SThierry Reding 
637ebae8d07SThierry Reding 	/*
638ebae8d07SThierry Reding 	 * Tegra20 and Tegra30 are special cases here because they support
639ebae8d07SThierry Reding 	 * only variants of specific formats with an alpha component, but not
640ebae8d07SThierry Reding 	 * the corresponding opaque formats. However, the opaque formats can
641ebae8d07SThierry Reding 	 * be emulated by disabling alpha blending for the plane.
642ebae8d07SThierry Reding 	 */
643a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending) {
6443dae08bcSDmitry Osipenko 		err = tegra_plane_setup_legacy_state(tegra, plane_state);
645ebae8d07SThierry Reding 		if (err < 0)
646ebae8d07SThierry Reding 			return err;
647ebae8d07SThierry Reding 	}
648ebae8d07SThierry Reding 
649ba5c1649SMaxime Ripard 	err = tegra_fb_get_tiling(new_plane_state->fb, tiling);
6508f604f8cSThierry Reding 	if (err < 0)
6518f604f8cSThierry Reding 		return err;
6528f604f8cSThierry Reding 
6538f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
6544aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
6554aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
6564aa3df71SThierry Reding 		return -EINVAL;
6574aa3df71SThierry Reding 	}
6584aa3df71SThierry Reding 
659cd740777SDmitry Osipenko 	/*
660cd740777SDmitry Osipenko 	 * Older userspace used custom BO flag in order to specify the Y
661cd740777SDmitry Osipenko 	 * reflection, while modern userspace uses the generic DRM rotation
662cd740777SDmitry Osipenko 	 * property in order to achieve the same result.  The legacy BO flag
663cd740777SDmitry Osipenko 	 * duplicates the DRM rotation property when both are set.
664cd740777SDmitry Osipenko 	 */
665ba5c1649SMaxime Ripard 	if (tegra_fb_is_bottom_up(new_plane_state->fb))
666cd740777SDmitry Osipenko 		rotation |= DRM_MODE_REFLECT_Y;
667cd740777SDmitry Osipenko 
668cd740777SDmitry Osipenko 	rotation = drm_rotation_simplify(rotation, supported_rotation);
669cd740777SDmitry Osipenko 
670cd740777SDmitry Osipenko 	if (rotation & DRM_MODE_REFLECT_X)
671cd740777SDmitry Osipenko 		plane_state->reflect_x = true;
672cd740777SDmitry Osipenko 	else
673cd740777SDmitry Osipenko 		plane_state->reflect_x = false;
674995c5a50SThierry Reding 
675995c5a50SThierry Reding 	if (rotation & DRM_MODE_REFLECT_Y)
676e9e476f7SDmitry Osipenko 		plane_state->reflect_y = true;
677995c5a50SThierry Reding 	else
678e9e476f7SDmitry Osipenko 		plane_state->reflect_y = false;
679995c5a50SThierry Reding 
6804aa3df71SThierry Reding 	/*
6814aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
6824aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
6834aa3df71SThierry Reding 	 * configuration.
6844aa3df71SThierry Reding 	 */
685ba5c1649SMaxime Ripard 	if (new_plane_state->fb->format->num_planes > 2) {
686ba5c1649SMaxime Ripard 		if (new_plane_state->fb->pitches[2] != new_plane_state->fb->pitches[1]) {
6874aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
6884aa3df71SThierry Reding 			return -EINVAL;
6894aa3df71SThierry Reding 		}
6904aa3df71SThierry Reding 	}
6914aa3df71SThierry Reding 
692ba5c1649SMaxime Ripard 	err = tegra_plane_state_add(tegra, new_plane_state);
69347802b09SThierry Reding 	if (err < 0)
69447802b09SThierry Reding 		return err;
69547802b09SThierry Reding 
6964aa3df71SThierry Reding 	return 0;
6974aa3df71SThierry Reding }
6984aa3df71SThierry Reding 
699a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
700977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
70180d3eef1SDmitry Osipenko {
702977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
703977697e2SMaxime Ripard 									   plane);
704a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
70580d3eef1SDmitry Osipenko 	u32 value;
70680d3eef1SDmitry Osipenko 
707a4bfa096SThierry Reding 	/* rien ne va plus */
708a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
709a4bfa096SThierry Reding 		return;
710a4bfa096SThierry Reding 
7111087fac1SThierry Reding 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
71280d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
7131087fac1SThierry Reding 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
71480d3eef1SDmitry Osipenko }
71580d3eef1SDmitry Osipenko 
7164aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
717977697e2SMaxime Ripard 				      struct drm_atomic_state *state)
7184aa3df71SThierry Reding {
71937418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
72037418bf1SMaxime Ripard 									   plane);
72141016fe1SMaxime Ripard 	struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
722e05162c0SMaxime Ripard 	struct drm_framebuffer *fb = new_state->fb;
7234aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
7244aa3df71SThierry Reding 	struct tegra_dc_window window;
7254aa3df71SThierry Reding 	unsigned int i;
7264aa3df71SThierry Reding 
7274aa3df71SThierry Reding 	/* rien ne va plus */
728e05162c0SMaxime Ripard 	if (!new_state->crtc || !new_state->fb)
7294aa3df71SThierry Reding 		return;
7304aa3df71SThierry Reding 
731e05162c0SMaxime Ripard 	if (!new_state->visible)
732977697e2SMaxime Ripard 		return tegra_plane_atomic_disable(plane, state);
73380d3eef1SDmitry Osipenko 
734c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
735e05162c0SMaxime Ripard 	window.src.x = new_state->src.x1 >> 16;
736e05162c0SMaxime Ripard 	window.src.y = new_state->src.y1 >> 16;
737e05162c0SMaxime Ripard 	window.src.w = drm_rect_width(&new_state->src) >> 16;
738e05162c0SMaxime Ripard 	window.src.h = drm_rect_height(&new_state->src) >> 16;
739e05162c0SMaxime Ripard 	window.dst.x = new_state->dst.x1;
740e05162c0SMaxime Ripard 	window.dst.y = new_state->dst.y1;
741e05162c0SMaxime Ripard 	window.dst.w = drm_rect_width(&new_state->dst);
742e05162c0SMaxime Ripard 	window.dst.h = drm_rect_height(&new_state->dst);
743272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
74441016fe1SMaxime Ripard 	window.reflect_x = tegra_plane_state->reflect_x;
74541016fe1SMaxime Ripard 	window.reflect_y = tegra_plane_state->reflect_y;
746c7679306SThierry Reding 
7478f604f8cSThierry Reding 	/* copy from state */
748e05162c0SMaxime Ripard 	window.zpos = new_state->normalized_zpos;
74941016fe1SMaxime Ripard 	window.tiling = tegra_plane_state->tiling;
75041016fe1SMaxime Ripard 	window.format = tegra_plane_state->format;
75141016fe1SMaxime Ripard 	window.swap = tegra_plane_state->swap;
752c7679306SThierry Reding 
753bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
75441016fe1SMaxime Ripard 		window.base[i] = tegra_plane_state->iova[i] + fb->offsets[i];
75508ee0178SDmitry Osipenko 
75608ee0178SDmitry Osipenko 		/*
75708ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
75808ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
75908ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
76008ee0178SDmitry Osipenko 		 */
76108ee0178SDmitry Osipenko 		if (i < 2)
7624aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
763c7679306SThierry Reding 	}
764c7679306SThierry Reding 
7651087fac1SThierry Reding 	tegra_dc_setup_window(p, &window);
7664aa3df71SThierry Reding }
7674aa3df71SThierry Reding 
768a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
7692e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
7702e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
7714aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
7724aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
773a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
774c7679306SThierry Reding };
775c7679306SThierry Reding 
77689f65018SThierry Reding static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
777c7679306SThierry Reding {
778518e6227SThierry Reding 	/*
779518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
780518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
781518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
782518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
783518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
784518e6227SThierry Reding 	 * here.
785518e6227SThierry Reding 	 *
786518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
787518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
788518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
789518e6227SThierry Reding 	 */
79089f65018SThierry Reding 	return 1 << drm->mode_config.num_crtc;
79189f65018SThierry Reding }
79289f65018SThierry Reding 
79389f65018SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
79489f65018SThierry Reding 						    struct tegra_dc *dc)
79589f65018SThierry Reding {
79689f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
79747307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
798c7679306SThierry Reding 	struct tegra_plane *plane;
799c7679306SThierry Reding 	unsigned int num_formats;
800e90124cbSThierry Reding 	const u64 *modifiers;
801c7679306SThierry Reding 	const u32 *formats;
802c7679306SThierry Reding 	int err;
803c7679306SThierry Reding 
804c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
805c7679306SThierry Reding 	if (!plane)
806c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
807c7679306SThierry Reding 
8081087fac1SThierry Reding 	/* Always use window A as primary window */
8091087fac1SThierry Reding 	plane->offset = 0xa00;
810c4755fb9SThierry Reding 	plane->index = 0;
8111087fac1SThierry Reding 	plane->dc = dc;
8121087fac1SThierry Reding 
8131087fac1SThierry Reding 	num_formats = dc->soc->num_primary_formats;
8141087fac1SThierry Reding 	formats = dc->soc->primary_formats;
815e90124cbSThierry Reding 	modifiers = dc->soc->modifiers;
816c4755fb9SThierry Reding 
817*04d5d5dfSDmitry Osipenko 	err = tegra_plane_interconnect_init(plane);
818*04d5d5dfSDmitry Osipenko 	if (err) {
819*04d5d5dfSDmitry Osipenko 		kfree(plane);
820*04d5d5dfSDmitry Osipenko 		return ERR_PTR(err);
821*04d5d5dfSDmitry Osipenko 	}
822*04d5d5dfSDmitry Osipenko 
823518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
824c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
825e90124cbSThierry Reding 				       num_formats, modifiers, type, NULL);
826c7679306SThierry Reding 	if (err < 0) {
827c7679306SThierry Reding 		kfree(plane);
828c7679306SThierry Reding 		return ERR_PTR(err);
829c7679306SThierry Reding 	}
830c7679306SThierry Reding 
831a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
8323dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
833ab7d3f58SThierry Reding 
834995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
835995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
836995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
8374fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
838cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
839995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
840995c5a50SThierry Reding 	if (err < 0)
841995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
842995c5a50SThierry Reding 			err);
843995c5a50SThierry Reding 
844c7679306SThierry Reding 	return &plane->base;
845c7679306SThierry Reding }
846c7679306SThierry Reding 
847d5ec699dSThierry Reding static const u32 tegra_legacy_cursor_plane_formats[] = {
848c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
849c7679306SThierry Reding };
850c7679306SThierry Reding 
851d5ec699dSThierry Reding static const u32 tegra_cursor_plane_formats[] = {
852d5ec699dSThierry Reding 	DRM_FORMAT_ARGB8888,
853d5ec699dSThierry Reding };
854d5ec699dSThierry Reding 
8554aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
8567c11b99aSMaxime Ripard 				     struct drm_atomic_state *state)
857c7679306SThierry Reding {
8587c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
8597c11b99aSMaxime Ripard 										 plane);
860*04d5d5dfSDmitry Osipenko 	struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
86147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
86247802b09SThierry Reding 	int err;
86347802b09SThierry Reding 
864*04d5d5dfSDmitry Osipenko 	plane_state->peak_memory_bandwidth = 0;
865*04d5d5dfSDmitry Osipenko 	plane_state->avg_memory_bandwidth = 0;
866*04d5d5dfSDmitry Osipenko 
8674aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
868*04d5d5dfSDmitry Osipenko 	if (!new_plane_state->crtc) {
869*04d5d5dfSDmitry Osipenko 		plane_state->total_peak_memory_bandwidth = 0;
8704aa3df71SThierry Reding 		return 0;
871*04d5d5dfSDmitry Osipenko 	}
872c7679306SThierry Reding 
873c7679306SThierry Reding 	/* scaling not supported for cursor */
874ba5c1649SMaxime Ripard 	if ((new_plane_state->src_w >> 16 != new_plane_state->crtc_w) ||
875ba5c1649SMaxime Ripard 	    (new_plane_state->src_h >> 16 != new_plane_state->crtc_h))
876c7679306SThierry Reding 		return -EINVAL;
877c7679306SThierry Reding 
878c7679306SThierry Reding 	/* only square cursors supported */
879ba5c1649SMaxime Ripard 	if (new_plane_state->src_w != new_plane_state->src_h)
880c7679306SThierry Reding 		return -EINVAL;
881c7679306SThierry Reding 
882ba5c1649SMaxime Ripard 	if (new_plane_state->crtc_w != 32 && new_plane_state->crtc_w != 64 &&
883ba5c1649SMaxime Ripard 	    new_plane_state->crtc_w != 128 && new_plane_state->crtc_w != 256)
8844aa3df71SThierry Reding 		return -EINVAL;
8854aa3df71SThierry Reding 
886ba5c1649SMaxime Ripard 	err = tegra_plane_state_add(tegra, new_plane_state);
88747802b09SThierry Reding 	if (err < 0)
88847802b09SThierry Reding 		return err;
88947802b09SThierry Reding 
8904aa3df71SThierry Reding 	return 0;
8914aa3df71SThierry Reding }
8924aa3df71SThierry Reding 
8934aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
894977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
8954aa3df71SThierry Reding {
89637418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
89737418bf1SMaxime Ripard 									   plane);
89841016fe1SMaxime Ripard 	struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
899e05162c0SMaxime Ripard 	struct tegra_dc *dc = to_tegra_dc(new_state->crtc);
900d5ec699dSThierry Reding 	struct tegra_drm *tegra = plane->dev->dev_private;
901d5ec699dSThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
902d5ec699dSThierry Reding 	u64 dma_mask = *dc->dev->dma_mask;
903d5ec699dSThierry Reding #endif
904d5ec699dSThierry Reding 	unsigned int x, y;
905d5ec699dSThierry Reding 	u32 value = 0;
9064aa3df71SThierry Reding 
9074aa3df71SThierry Reding 	/* rien ne va plus */
908e05162c0SMaxime Ripard 	if (!new_state->crtc || !new_state->fb)
9094aa3df71SThierry Reding 		return;
9104aa3df71SThierry Reding 
911d5ec699dSThierry Reding 	/*
912d5ec699dSThierry Reding 	 * Legacy display supports hardware clipping of the cursor, but
913d5ec699dSThierry Reding 	 * nvdisplay relies on software to clip the cursor to the screen.
914d5ec699dSThierry Reding 	 */
915d5ec699dSThierry Reding 	if (!dc->soc->has_nvdisplay)
916d5ec699dSThierry Reding 		value |= CURSOR_CLIP_DISPLAY;
917d5ec699dSThierry Reding 
918e05162c0SMaxime Ripard 	switch (new_state->crtc_w) {
919c7679306SThierry Reding 	case 32:
920c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
921c7679306SThierry Reding 		break;
922c7679306SThierry Reding 
923c7679306SThierry Reding 	case 64:
924c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
925c7679306SThierry Reding 		break;
926c7679306SThierry Reding 
927c7679306SThierry Reding 	case 128:
928c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
929c7679306SThierry Reding 		break;
930c7679306SThierry Reding 
931c7679306SThierry Reding 	case 256:
932c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
933c7679306SThierry Reding 		break;
934c7679306SThierry Reding 
935c7679306SThierry Reding 	default:
936c52e167bSThierry Reding 		WARN(1, "cursor size %ux%u not supported\n",
937e05162c0SMaxime Ripard 		     new_state->crtc_w, new_state->crtc_h);
9384aa3df71SThierry Reding 		return;
939c7679306SThierry Reding 	}
940c7679306SThierry Reding 
94141016fe1SMaxime Ripard 	value |= (tegra_plane_state->iova[0] >> 10) & 0x3fffff;
942c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
943c7679306SThierry Reding 
944c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
945d5ec699dSThierry Reding 	value = (tegra_plane_state->iova[0] >> 32) & (dma_mask >> 32);
946c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
947c7679306SThierry Reding #endif
948c7679306SThierry Reding 
949c7679306SThierry Reding 	/* enable cursor and set blend mode */
950c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
951c7679306SThierry Reding 	value |= CURSOR_ENABLE;
952c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
953c7679306SThierry Reding 
954c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
955c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
956c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
957d5ec699dSThierry Reding 
958d5ec699dSThierry Reding 	if (dc->soc->has_nvdisplay)
959d5ec699dSThierry Reding 		value &= ~CURSOR_COMPOSITION_MODE_XOR;
960d5ec699dSThierry Reding 	else
961c7679306SThierry Reding 		value |= CURSOR_MODE_NORMAL;
962d5ec699dSThierry Reding 
963c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
964c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
965c7679306SThierry Reding 	value |= CURSOR_ALPHA;
966c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
967c7679306SThierry Reding 
968d5ec699dSThierry Reding 	/* nvdisplay relies on software for clipping */
969d5ec699dSThierry Reding 	if (dc->soc->has_nvdisplay) {
970d5ec699dSThierry Reding 		struct drm_rect src;
971d5ec699dSThierry Reding 
972d5ec699dSThierry Reding 		x = new_state->dst.x1;
973d5ec699dSThierry Reding 		y = new_state->dst.y1;
974d5ec699dSThierry Reding 
975d5ec699dSThierry Reding 		drm_rect_fp_to_int(&src, &new_state->src);
976d5ec699dSThierry Reding 
977d5ec699dSThierry Reding 		value = (src.y1 & tegra->vmask) << 16 | (src.x1 & tegra->hmask);
978d5ec699dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_POINT_IN_CURSOR);
979d5ec699dSThierry Reding 
980d5ec699dSThierry Reding 		value = (drm_rect_height(&src) & tegra->vmask) << 16 |
981d5ec699dSThierry Reding 			(drm_rect_width(&src) & tegra->hmask);
982d5ec699dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_SIZE_IN_CURSOR);
983d5ec699dSThierry Reding 	} else {
984d5ec699dSThierry Reding 		x = new_state->crtc_x;
985d5ec699dSThierry Reding 		y = new_state->crtc_y;
986d5ec699dSThierry Reding 	}
987d5ec699dSThierry Reding 
988c7679306SThierry Reding 	/* position the cursor */
989d5ec699dSThierry Reding 	value = ((y & tegra->vmask) << 16) | (x & tegra->hmask);
990c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
991c7679306SThierry Reding }
992c7679306SThierry Reding 
9934aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
994977697e2SMaxime Ripard 					struct drm_atomic_state *state)
995c7679306SThierry Reding {
996977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
997977697e2SMaxime Ripard 									   plane);
9984aa3df71SThierry Reding 	struct tegra_dc *dc;
999c7679306SThierry Reding 	u32 value;
1000c7679306SThierry Reding 
10014aa3df71SThierry Reding 	/* rien ne va plus */
10024aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
10034aa3df71SThierry Reding 		return;
10044aa3df71SThierry Reding 
10054aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
1006c7679306SThierry Reding 
1007c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1008c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
1009c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1010c7679306SThierry Reding }
1011c7679306SThierry Reding 
10124aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
10132e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
10142e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
10154aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
10164aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
10174aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
1018c7679306SThierry Reding };
1019c7679306SThierry Reding 
1020be4306adSDaniel Vetter static const uint64_t linear_modifiers[] = {
1021be4306adSDaniel Vetter 	DRM_FORMAT_MOD_LINEAR,
1022be4306adSDaniel Vetter 	DRM_FORMAT_MOD_INVALID
1023be4306adSDaniel Vetter };
1024be4306adSDaniel Vetter 
1025c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
1026c7679306SThierry Reding 						      struct tegra_dc *dc)
1027c7679306SThierry Reding {
102889f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1029c7679306SThierry Reding 	struct tegra_plane *plane;
1030c7679306SThierry Reding 	unsigned int num_formats;
1031c7679306SThierry Reding 	const u32 *formats;
1032c7679306SThierry Reding 	int err;
1033c7679306SThierry Reding 
1034c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1035c7679306SThierry Reding 	if (!plane)
1036c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
1037c7679306SThierry Reding 
103847802b09SThierry Reding 	/*
1039a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
1040a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
1041a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
1042a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
1043a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
104447802b09SThierry Reding 	 */
104547802b09SThierry Reding 	plane->index = 6;
10461087fac1SThierry Reding 	plane->dc = dc;
104747802b09SThierry Reding 
1048d5ec699dSThierry Reding 	if (!dc->soc->has_nvdisplay) {
1049d5ec699dSThierry Reding 		num_formats = ARRAY_SIZE(tegra_legacy_cursor_plane_formats);
1050d5ec699dSThierry Reding 		formats = tegra_legacy_cursor_plane_formats;
1051*04d5d5dfSDmitry Osipenko 
1052*04d5d5dfSDmitry Osipenko 		err = tegra_plane_interconnect_init(plane);
1053*04d5d5dfSDmitry Osipenko 		if (err) {
1054*04d5d5dfSDmitry Osipenko 			kfree(plane);
1055*04d5d5dfSDmitry Osipenko 			return ERR_PTR(err);
1056*04d5d5dfSDmitry Osipenko 		}
1057d5ec699dSThierry Reding 	} else {
1058c7679306SThierry Reding 		num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
1059c7679306SThierry Reding 		formats = tegra_cursor_plane_formats;
1060d5ec699dSThierry Reding 	}
1061c7679306SThierry Reding 
106289f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1063c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
1064be4306adSDaniel Vetter 				       num_formats, linear_modifiers,
1065e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
1066c7679306SThierry Reding 	if (err < 0) {
1067c7679306SThierry Reding 		kfree(plane);
1068c7679306SThierry Reding 		return ERR_PTR(err);
1069c7679306SThierry Reding 	}
1070c7679306SThierry Reding 
10714aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
1072fce3a51dSThierry Reding 	drm_plane_create_zpos_immutable_property(&plane->base, 255);
10734aa3df71SThierry Reding 
1074c7679306SThierry Reding 	return &plane->base;
1075c7679306SThierry Reding }
1076c7679306SThierry Reding 
1077511c7023SThierry Reding static const u32 tegra20_overlay_formats[] = {
1078511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1079511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1080dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
1081511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1082511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1083511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1084ebae8d07SThierry Reding 	/* non-native formats */
1085ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
1086ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
1087ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
1088ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
1089511c7023SThierry Reding 	/* planar formats */
1090511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1091511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1092511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1093511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1094511c7023SThierry Reding };
1095511c7023SThierry Reding 
1096511c7023SThierry Reding static const u32 tegra114_overlay_formats[] = {
1097511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1098511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1099511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1100511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1101511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1102511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1103511c7023SThierry Reding 	/* new on Tegra114 */
1104511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1105511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1106511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1107511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1108511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1109511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1110511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1111511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1112511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1113511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1114511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1115511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1116511c7023SThierry Reding 	/* planar formats */
1117511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1118511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1119511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1120511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1121511c7023SThierry Reding };
1122511c7023SThierry Reding 
1123511c7023SThierry Reding static const u32 tegra124_overlay_formats[] = {
1124511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1125511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1126511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1127511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1128511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1129511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1130511c7023SThierry Reding 	/* new on Tegra114 */
1131511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1132511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1133511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1134511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1135511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1136511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1137511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1138511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1139511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1140511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1141511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1142511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1143511c7023SThierry Reding 	/* new on Tegra124 */
1144511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
1145511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
1146511c7023SThierry Reding 	/* planar formats */
1147dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
1148f925390eSThierry Reding 	DRM_FORMAT_YUYV,
1149dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
1150dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
1151dee8268fSThierry Reding };
1152dee8268fSThierry Reding 
1153c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
1154c7679306SThierry Reding 						       struct tegra_dc *dc,
11559f446d83SDmitry Osipenko 						       unsigned int index,
11569f446d83SDmitry Osipenko 						       bool cursor)
1157dee8268fSThierry Reding {
115889f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1159dee8268fSThierry Reding 	struct tegra_plane *plane;
1160c7679306SThierry Reding 	unsigned int num_formats;
11619f446d83SDmitry Osipenko 	enum drm_plane_type type;
1162c7679306SThierry Reding 	const u32 *formats;
1163c7679306SThierry Reding 	int err;
1164dee8268fSThierry Reding 
1165f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1166dee8268fSThierry Reding 	if (!plane)
1167c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
1168dee8268fSThierry Reding 
11691087fac1SThierry Reding 	plane->offset = 0xa00 + 0x200 * index;
1170c7679306SThierry Reding 	plane->index = index;
11711087fac1SThierry Reding 	plane->dc = dc;
1172dee8268fSThierry Reding 
1173511c7023SThierry Reding 	num_formats = dc->soc->num_overlay_formats;
1174511c7023SThierry Reding 	formats = dc->soc->overlay_formats;
1175c7679306SThierry Reding 
1176*04d5d5dfSDmitry Osipenko 	err = tegra_plane_interconnect_init(plane);
1177*04d5d5dfSDmitry Osipenko 	if (err) {
1178*04d5d5dfSDmitry Osipenko 		kfree(plane);
1179*04d5d5dfSDmitry Osipenko 		return ERR_PTR(err);
1180*04d5d5dfSDmitry Osipenko 	}
1181*04d5d5dfSDmitry Osipenko 
11829f446d83SDmitry Osipenko 	if (!cursor)
11839f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_OVERLAY;
11849f446d83SDmitry Osipenko 	else
11859f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_CURSOR;
11869f446d83SDmitry Osipenko 
118789f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1188301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
1189be4306adSDaniel Vetter 				       num_formats, linear_modifiers,
1190be4306adSDaniel Vetter 				       type, NULL);
1191f002abc1SThierry Reding 	if (err < 0) {
1192f002abc1SThierry Reding 		kfree(plane);
1193c7679306SThierry Reding 		return ERR_PTR(err);
1194dee8268fSThierry Reding 	}
1195c7679306SThierry Reding 
1196a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
11973dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
1198ab7d3f58SThierry Reding 
1199995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
1200995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
1201995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
12024fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
1203cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
1204995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
1205995c5a50SThierry Reding 	if (err < 0)
1206995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
1207995c5a50SThierry Reding 			err);
1208995c5a50SThierry Reding 
1209c7679306SThierry Reding 	return &plane->base;
1210c7679306SThierry Reding }
1211c7679306SThierry Reding 
121247307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
121347307954SThierry Reding 						    struct tegra_dc *dc)
1214c7679306SThierry Reding {
121547307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
121647307954SThierry Reding 	unsigned int i, j;
121747307954SThierry Reding 
121847307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
121947307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
122047307954SThierry Reding 
122147307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
122247307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
122347307954SThierry Reding 				unsigned int index = wgrp->windows[j];
122447307954SThierry Reding 
122547307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
122647307954SThierry Reding 								  wgrp->index,
122747307954SThierry Reding 								  index);
122847307954SThierry Reding 				if (IS_ERR(plane))
122947307954SThierry Reding 					return plane;
123047307954SThierry Reding 
123147307954SThierry Reding 				/*
123247307954SThierry Reding 				 * Choose the first shared plane owned by this
123347307954SThierry Reding 				 * head as the primary plane.
123447307954SThierry Reding 				 */
123547307954SThierry Reding 				if (!primary) {
123647307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
123747307954SThierry Reding 					primary = plane;
123847307954SThierry Reding 				}
123947307954SThierry Reding 			}
124047307954SThierry Reding 		}
124147307954SThierry Reding 	}
124247307954SThierry Reding 
124347307954SThierry Reding 	return primary;
124447307954SThierry Reding }
124547307954SThierry Reding 
124647307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
124747307954SThierry Reding 					     struct tegra_dc *dc)
124847307954SThierry Reding {
12498f62142eSThierry Reding 	struct drm_plane *planes[2], *primary;
12509f446d83SDmitry Osipenko 	unsigned int planes_num;
1251c7679306SThierry Reding 	unsigned int i;
12528f62142eSThierry Reding 	int err;
1253c7679306SThierry Reding 
125447307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
125547307954SThierry Reding 	if (IS_ERR(primary))
125647307954SThierry Reding 		return primary;
125747307954SThierry Reding 
12589f446d83SDmitry Osipenko 	if (dc->soc->supports_cursor)
12599f446d83SDmitry Osipenko 		planes_num = 2;
12609f446d83SDmitry Osipenko 	else
12619f446d83SDmitry Osipenko 		planes_num = 1;
12629f446d83SDmitry Osipenko 
12639f446d83SDmitry Osipenko 	for (i = 0; i < planes_num; i++) {
12649f446d83SDmitry Osipenko 		planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
12659f446d83SDmitry Osipenko 							  false);
12668f62142eSThierry Reding 		if (IS_ERR(planes[i])) {
12678f62142eSThierry Reding 			err = PTR_ERR(planes[i]);
12688f62142eSThierry Reding 
12698f62142eSThierry Reding 			while (i--)
12708f62142eSThierry Reding 				tegra_plane_funcs.destroy(planes[i]);
12718f62142eSThierry Reding 
12728f62142eSThierry Reding 			tegra_plane_funcs.destroy(primary);
12738f62142eSThierry Reding 			return ERR_PTR(err);
127447307954SThierry Reding 		}
1275f002abc1SThierry Reding 	}
1276dee8268fSThierry Reding 
127747307954SThierry Reding 	return primary;
1278dee8268fSThierry Reding }
1279dee8268fSThierry Reding 
1280f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1281f002abc1SThierry Reding {
1282f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1283f002abc1SThierry Reding }
1284f002abc1SThierry Reding 
1285ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1286ca915b10SThierry Reding {
1287b7e0b04aSMaarten Lankhorst 	struct tegra_dc_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
1288ca915b10SThierry Reding 
12893b59b7acSThierry Reding 	if (crtc->state)
1290b7e0b04aSMaarten Lankhorst 		tegra_crtc_atomic_destroy_state(crtc, crtc->state);
12913b59b7acSThierry Reding 
1292b7e0b04aSMaarten Lankhorst 	__drm_atomic_helper_crtc_reset(crtc, &state->base);
1293ca915b10SThierry Reding }
1294ca915b10SThierry Reding 
1295ca915b10SThierry Reding static struct drm_crtc_state *
1296ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1297ca915b10SThierry Reding {
1298ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1299ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1300ca915b10SThierry Reding 
13013b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1302ca915b10SThierry Reding 	if (!copy)
1303ca915b10SThierry Reding 		return NULL;
1304ca915b10SThierry Reding 
13053b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
13063b59b7acSThierry Reding 	copy->clk = state->clk;
13073b59b7acSThierry Reding 	copy->pclk = state->pclk;
13083b59b7acSThierry Reding 	copy->div = state->div;
13093b59b7acSThierry Reding 	copy->planes = state->planes;
1310ca915b10SThierry Reding 
1311ca915b10SThierry Reding 	return &copy->base;
1312ca915b10SThierry Reding }
1313ca915b10SThierry Reding 
1314ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1315ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1316ca915b10SThierry Reding {
1317ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1318ca915b10SThierry Reding 	kfree(state);
1319ca915b10SThierry Reding }
1320ca915b10SThierry Reding 
1321b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1322b95800eeSThierry Reding 
1323b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1324b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1325b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1326b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1327b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1328b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1329b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1330b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1331b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1332b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1333b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1334b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1335b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1336b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1337b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1338b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1339b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1340b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1341b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1342b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1343b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1344b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1345b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1346b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1347b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1348b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1349b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1350b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1351b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1352b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1353b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1354b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1355b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1356b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1357b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1358b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1359b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1360b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1361b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1362b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1363b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1364b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1365b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1366b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1367b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1368b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1369b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1370b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1371b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1372b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1373b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1374b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1375b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1376b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1377b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1378b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1379b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1380b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1381b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1382b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1383b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1384b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1385b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1386b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1387b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1388b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1389b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1390b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1391b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1392b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1393b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1394b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1395b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1396b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1397b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1398b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1399b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1400b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1401b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1402b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1403b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1404b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1405b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1406b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1407b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1408b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1409b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1410b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1411b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1412b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1413b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1414b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1415b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1416b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1417b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1418b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1419b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1420b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1421b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1422b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1423b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1424b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1425b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1426b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1427b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1428b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1429b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1430b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1431b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1432b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1433b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1434b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1435b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1436b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1437b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1438b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1439b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1440b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1441b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1442b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1443b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1444b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1445b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1446b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1447b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1448b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1449b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1450b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1451b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1452b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1453b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1454b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1455b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1456b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1457b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1458b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1459b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1460b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1461b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1462b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1463b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1464b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1465b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1466b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1467b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1468b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1469b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1470b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1471b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1472b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1473b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1474b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1475b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1476b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1477b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1478b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1479b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1480b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1481b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1482b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1483b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1484b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1485b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1486b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1487b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1488b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1489b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1490b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1491b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1492b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1493b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1494b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1495b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1496b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1497b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1498b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1499b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1500b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1501b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1502b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1503b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1504b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1505b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1506b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1507b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1508b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1509b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1510b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1511b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1512b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1513b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1514b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1515b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1516b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1517b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1518b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1519b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1520b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1521b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1522b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1523b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1524b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1525b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1526b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1527b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1528b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1529b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1530b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1531b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1532b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1533b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1534b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1535b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1536b95800eeSThierry Reding };
1537b95800eeSThierry Reding 
1538b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1539b95800eeSThierry Reding {
1540b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1541b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1542b95800eeSThierry Reding 	unsigned int i;
1543b95800eeSThierry Reding 	int err = 0;
1544b95800eeSThierry Reding 
1545b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1546b95800eeSThierry Reding 
1547b95800eeSThierry Reding 	if (!dc->base.state->active) {
1548b95800eeSThierry Reding 		err = -EBUSY;
1549b95800eeSThierry Reding 		goto unlock;
1550b95800eeSThierry Reding 	}
1551b95800eeSThierry Reding 
1552b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1553b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1554b95800eeSThierry Reding 
1555b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1556b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1557b95800eeSThierry Reding 	}
1558b95800eeSThierry Reding 
1559b95800eeSThierry Reding unlock:
1560b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1561b95800eeSThierry Reding 	return err;
1562b95800eeSThierry Reding }
1563b95800eeSThierry Reding 
1564b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1565b95800eeSThierry Reding {
1566b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1567b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1568b95800eeSThierry Reding 	int err = 0;
1569b95800eeSThierry Reding 	u32 value;
1570b95800eeSThierry Reding 
1571b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1572b95800eeSThierry Reding 
1573b95800eeSThierry Reding 	if (!dc->base.state->active) {
1574b95800eeSThierry Reding 		err = -EBUSY;
1575b95800eeSThierry Reding 		goto unlock;
1576b95800eeSThierry Reding 	}
1577b95800eeSThierry Reding 
1578b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1579b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1580b95800eeSThierry Reding 	tegra_dc_commit(dc);
1581b95800eeSThierry Reding 
1582b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1583b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1584b95800eeSThierry Reding 
1585b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1586b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1587b95800eeSThierry Reding 
1588b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1589b95800eeSThierry Reding 
1590b95800eeSThierry Reding unlock:
1591b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1592b95800eeSThierry Reding 	return err;
1593b95800eeSThierry Reding }
1594b95800eeSThierry Reding 
1595b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1596b95800eeSThierry Reding {
1597b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1598b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1599b95800eeSThierry Reding 
1600b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1601b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1602b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1603b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1604b95800eeSThierry Reding 
1605b95800eeSThierry Reding 	return 0;
1606b95800eeSThierry Reding }
1607b95800eeSThierry Reding 
1608b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1609b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1610b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1611b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1612b95800eeSThierry Reding };
1613b95800eeSThierry Reding 
1614b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1615b95800eeSThierry Reding {
1616b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1617b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
161839f55c61SArnd Bergmann 	struct dentry *root;
1619b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1620b95800eeSThierry Reding 
162139f55c61SArnd Bergmann #ifdef CONFIG_DEBUG_FS
162239f55c61SArnd Bergmann 	root = crtc->debugfs_entry;
162339f55c61SArnd Bergmann #else
162439f55c61SArnd Bergmann 	root = NULL;
162539f55c61SArnd Bergmann #endif
162639f55c61SArnd Bergmann 
1627b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1628b95800eeSThierry Reding 				    GFP_KERNEL);
1629b95800eeSThierry Reding 	if (!dc->debugfs_files)
1630b95800eeSThierry Reding 		return -ENOMEM;
1631b95800eeSThierry Reding 
1632b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1633b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1634b95800eeSThierry Reding 
1635ad6d94f2SWambui Karuga 	drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1636b95800eeSThierry Reding 
1637b95800eeSThierry Reding 	return 0;
1638b95800eeSThierry Reding }
1639b95800eeSThierry Reding 
1640b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1641b95800eeSThierry Reding {
1642b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1643b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1644b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1645b95800eeSThierry Reding 
1646b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1647b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1648b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1649b95800eeSThierry Reding }
1650b95800eeSThierry Reding 
1651c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1652c49c81e2SThierry Reding {
1653c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1654c49c81e2SThierry Reding 
165547307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
165647307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1657c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1658c49c81e2SThierry Reding 
1659c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
16603abe2413SDhinakaran Pandiyan 	return (u32)drm_crtc_vblank_count(&dc->base);
1661c49c81e2SThierry Reding }
1662c49c81e2SThierry Reding 
1663c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1664c49c81e2SThierry Reding {
1665c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1666363541e8SThierry Reding 	u32 value;
1667c49c81e2SThierry Reding 
1668c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1669c49c81e2SThierry Reding 	value |= VBLANK_INT;
1670c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1671c49c81e2SThierry Reding 
1672c49c81e2SThierry Reding 	return 0;
1673c49c81e2SThierry Reding }
1674c49c81e2SThierry Reding 
1675c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1676c49c81e2SThierry Reding {
1677c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1678363541e8SThierry Reding 	u32 value;
1679c49c81e2SThierry Reding 
1680c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1681c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1682c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1683c49c81e2SThierry Reding }
1684c49c81e2SThierry Reding 
1685dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
16861503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
168774f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1688f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1689ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1690ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1691ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1692b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1693b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
169410437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
169510437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
169610437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1697dee8268fSThierry Reding };
1698dee8268fSThierry Reding 
1699dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1700dee8268fSThierry Reding 				struct drm_display_mode *mode)
1701dee8268fSThierry Reding {
17020444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
17030444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1704dee8268fSThierry Reding 	unsigned long value;
1705dee8268fSThierry Reding 
170647307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1707dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1708dee8268fSThierry Reding 
1709dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1710dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
171147307954SThierry Reding 	}
1712dee8268fSThierry Reding 
1713dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1714dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1715dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1716dee8268fSThierry Reding 
1717dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1718dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1719dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1720dee8268fSThierry Reding 
1721dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1722dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1723dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1724dee8268fSThierry Reding 
1725dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1726dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1727dee8268fSThierry Reding 
1728dee8268fSThierry Reding 	return 0;
1729dee8268fSThierry Reding }
1730dee8268fSThierry Reding 
17319d910b60SThierry Reding /**
17329d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
17339d910b60SThierry Reding  *     state
17349d910b60SThierry Reding  * @dc: display controller
17359d910b60SThierry Reding  * @crtc_state: CRTC atomic state
17369d910b60SThierry Reding  * @clk: parent clock for display controller
17379d910b60SThierry Reding  * @pclk: pixel clock
17389d910b60SThierry Reding  * @div: shift clock divider
17399d910b60SThierry Reding  *
17409d910b60SThierry Reding  * Returns:
17419d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
17429d910b60SThierry Reding  */
1743ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1744ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1745ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1746ca915b10SThierry Reding 			       unsigned int div)
1747ca915b10SThierry Reding {
1748ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1749ca915b10SThierry Reding 
1750d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1751d2982748SThierry Reding 		return -EINVAL;
1752d2982748SThierry Reding 
1753ca915b10SThierry Reding 	state->clk = clk;
1754ca915b10SThierry Reding 	state->pclk = pclk;
1755ca915b10SThierry Reding 	state->div = div;
1756ca915b10SThierry Reding 
1757ca915b10SThierry Reding 	return 0;
1758ca915b10SThierry Reding }
1759ca915b10SThierry Reding 
176076d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
176176d59ed0SThierry Reding 				  struct tegra_dc_state *state)
176276d59ed0SThierry Reding {
176376d59ed0SThierry Reding 	u32 value;
176476d59ed0SThierry Reding 	int err;
176576d59ed0SThierry Reding 
176676d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
176776d59ed0SThierry Reding 	if (err < 0)
176876d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
176976d59ed0SThierry Reding 
177076d59ed0SThierry Reding 	/*
177176d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
177276d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
177376d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
177476d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
177576d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
177676d59ed0SThierry Reding 	 * should therefore be avoided.
177776d59ed0SThierry Reding 	 */
177876d59ed0SThierry Reding 	if (state->pclk > 0) {
177976d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
178076d59ed0SThierry Reding 		if (err < 0)
178176d59ed0SThierry Reding 			dev_err(dc->dev,
178276d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
178376d59ed0SThierry Reding 				state->pclk);
1784f8fb97c9SDmitry Osipenko 
1785f8fb97c9SDmitry Osipenko 		err = clk_set_rate(dc->clk, state->pclk);
1786f8fb97c9SDmitry Osipenko 		if (err < 0)
1787f8fb97c9SDmitry Osipenko 			dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1788f8fb97c9SDmitry Osipenko 				dc->clk, state->pclk, err);
178976d59ed0SThierry Reding 	}
179076d59ed0SThierry Reding 
179176d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
179276d59ed0SThierry Reding 		      state->div);
179376d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
179476d59ed0SThierry Reding 
179547307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
179676d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
179776d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
179847307954SThierry Reding 	}
179976d59ed0SThierry Reding }
180076d59ed0SThierry Reding 
1801003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1802003fc848SThierry Reding {
1803003fc848SThierry Reding 	u32 value;
1804003fc848SThierry Reding 
1805003fc848SThierry Reding 	/* stop the display controller */
1806003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1807003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1808003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1809003fc848SThierry Reding 
1810003fc848SThierry Reding 	tegra_dc_commit(dc);
1811003fc848SThierry Reding }
1812003fc848SThierry Reding 
1813003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1814003fc848SThierry Reding {
1815003fc848SThierry Reding 	u32 value;
1816003fc848SThierry Reding 
1817003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1818003fc848SThierry Reding 
1819003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1820003fc848SThierry Reding }
1821003fc848SThierry Reding 
1822003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1823003fc848SThierry Reding {
1824003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1825003fc848SThierry Reding 
1826003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1827003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1828003fc848SThierry Reding 			return 0;
1829003fc848SThierry Reding 
1830003fc848SThierry Reding 		usleep_range(1000, 2000);
1831003fc848SThierry Reding 	}
1832003fc848SThierry Reding 
1833003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1834003fc848SThierry Reding 	return -ETIMEDOUT;
1835003fc848SThierry Reding }
1836003fc848SThierry Reding 
1837*04d5d5dfSDmitry Osipenko static void
1838*04d5d5dfSDmitry Osipenko tegra_crtc_update_memory_bandwidth(struct drm_crtc *crtc,
1839*04d5d5dfSDmitry Osipenko 				   struct drm_atomic_state *state,
1840*04d5d5dfSDmitry Osipenko 				   bool prepare_bandwidth_transition)
1841*04d5d5dfSDmitry Osipenko {
1842*04d5d5dfSDmitry Osipenko 	const struct tegra_plane_state *old_tegra_state, *new_tegra_state;
1843*04d5d5dfSDmitry Osipenko 	const struct tegra_dc_state *old_dc_state, *new_dc_state;
1844*04d5d5dfSDmitry Osipenko 	u32 i, new_avg_bw, old_avg_bw, new_peak_bw, old_peak_bw;
1845*04d5d5dfSDmitry Osipenko 	const struct drm_plane_state *old_plane_state;
1846*04d5d5dfSDmitry Osipenko 	const struct drm_crtc_state *old_crtc_state;
1847*04d5d5dfSDmitry Osipenko 	struct tegra_dc_window window, old_window;
1848*04d5d5dfSDmitry Osipenko 	struct tegra_dc *dc = to_tegra_dc(crtc);
1849*04d5d5dfSDmitry Osipenko 	struct tegra_plane *tegra;
1850*04d5d5dfSDmitry Osipenko 	struct drm_plane *plane;
1851*04d5d5dfSDmitry Osipenko 
1852*04d5d5dfSDmitry Osipenko 	if (dc->soc->has_nvdisplay)
1853*04d5d5dfSDmitry Osipenko 		return;
1854*04d5d5dfSDmitry Osipenko 
1855*04d5d5dfSDmitry Osipenko 	old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1856*04d5d5dfSDmitry Osipenko 	old_dc_state = to_const_dc_state(old_crtc_state);
1857*04d5d5dfSDmitry Osipenko 	new_dc_state = to_const_dc_state(crtc->state);
1858*04d5d5dfSDmitry Osipenko 
1859*04d5d5dfSDmitry Osipenko 	if (!crtc->state->active) {
1860*04d5d5dfSDmitry Osipenko 		if (!old_crtc_state->active)
1861*04d5d5dfSDmitry Osipenko 			return;
1862*04d5d5dfSDmitry Osipenko 
1863*04d5d5dfSDmitry Osipenko 		/*
1864*04d5d5dfSDmitry Osipenko 		 * When CRTC is disabled on DPMS, the state of attached planes
1865*04d5d5dfSDmitry Osipenko 		 * is kept unchanged. Hence we need to enforce removal of the
1866*04d5d5dfSDmitry Osipenko 		 * bandwidths from the ICC paths.
1867*04d5d5dfSDmitry Osipenko 		 */
1868*04d5d5dfSDmitry Osipenko 		drm_atomic_crtc_for_each_plane(plane, crtc) {
1869*04d5d5dfSDmitry Osipenko 			tegra = to_tegra_plane(plane);
1870*04d5d5dfSDmitry Osipenko 
1871*04d5d5dfSDmitry Osipenko 			icc_set_bw(tegra->icc_mem, 0, 0);
1872*04d5d5dfSDmitry Osipenko 			icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
1873*04d5d5dfSDmitry Osipenko 		}
1874*04d5d5dfSDmitry Osipenko 
1875*04d5d5dfSDmitry Osipenko 		return;
1876*04d5d5dfSDmitry Osipenko 	}
1877*04d5d5dfSDmitry Osipenko 
1878*04d5d5dfSDmitry Osipenko 	for_each_old_plane_in_state(old_crtc_state->state, plane,
1879*04d5d5dfSDmitry Osipenko 				    old_plane_state, i) {
1880*04d5d5dfSDmitry Osipenko 		old_tegra_state = to_const_tegra_plane_state(old_plane_state);
1881*04d5d5dfSDmitry Osipenko 		new_tegra_state = to_const_tegra_plane_state(plane->state);
1882*04d5d5dfSDmitry Osipenko 		tegra = to_tegra_plane(plane);
1883*04d5d5dfSDmitry Osipenko 
1884*04d5d5dfSDmitry Osipenko 		/*
1885*04d5d5dfSDmitry Osipenko 		 * We're iterating over the global atomic state and it contains
1886*04d5d5dfSDmitry Osipenko 		 * planes from another CRTC, hence we need to filter out the
1887*04d5d5dfSDmitry Osipenko 		 * planes unrelated to this CRTC.
1888*04d5d5dfSDmitry Osipenko 		 */
1889*04d5d5dfSDmitry Osipenko 		if (tegra->dc != dc)
1890*04d5d5dfSDmitry Osipenko 			continue;
1891*04d5d5dfSDmitry Osipenko 
1892*04d5d5dfSDmitry Osipenko 		new_avg_bw = new_tegra_state->avg_memory_bandwidth;
1893*04d5d5dfSDmitry Osipenko 		old_avg_bw = old_tegra_state->avg_memory_bandwidth;
1894*04d5d5dfSDmitry Osipenko 
1895*04d5d5dfSDmitry Osipenko 		new_peak_bw = new_tegra_state->total_peak_memory_bandwidth;
1896*04d5d5dfSDmitry Osipenko 		old_peak_bw = old_tegra_state->total_peak_memory_bandwidth;
1897*04d5d5dfSDmitry Osipenko 
1898*04d5d5dfSDmitry Osipenko 		/*
1899*04d5d5dfSDmitry Osipenko 		 * See the comment related to !crtc->state->active above,
1900*04d5d5dfSDmitry Osipenko 		 * which explains why bandwidths need to be updated when
1901*04d5d5dfSDmitry Osipenko 		 * CRTC is turning ON.
1902*04d5d5dfSDmitry Osipenko 		 */
1903*04d5d5dfSDmitry Osipenko 		if (new_avg_bw == old_avg_bw && new_peak_bw == old_peak_bw &&
1904*04d5d5dfSDmitry Osipenko 		    old_crtc_state->active)
1905*04d5d5dfSDmitry Osipenko 			continue;
1906*04d5d5dfSDmitry Osipenko 
1907*04d5d5dfSDmitry Osipenko 		window.src.h = drm_rect_height(&plane->state->src) >> 16;
1908*04d5d5dfSDmitry Osipenko 		window.dst.h = drm_rect_height(&plane->state->dst);
1909*04d5d5dfSDmitry Osipenko 
1910*04d5d5dfSDmitry Osipenko 		old_window.src.h = drm_rect_height(&old_plane_state->src) >> 16;
1911*04d5d5dfSDmitry Osipenko 		old_window.dst.h = drm_rect_height(&old_plane_state->dst);
1912*04d5d5dfSDmitry Osipenko 
1913*04d5d5dfSDmitry Osipenko 		/*
1914*04d5d5dfSDmitry Osipenko 		 * During the preparation phase (atomic_begin), the memory
1915*04d5d5dfSDmitry Osipenko 		 * freq should go high before the DC changes are committed
1916*04d5d5dfSDmitry Osipenko 		 * if bandwidth requirement goes up, otherwise memory freq
1917*04d5d5dfSDmitry Osipenko 		 * should to stay high if BW requirement goes down.  The
1918*04d5d5dfSDmitry Osipenko 		 * opposite applies to the completion phase (post_commit).
1919*04d5d5dfSDmitry Osipenko 		 */
1920*04d5d5dfSDmitry Osipenko 		if (prepare_bandwidth_transition) {
1921*04d5d5dfSDmitry Osipenko 			new_avg_bw = max(old_avg_bw, new_avg_bw);
1922*04d5d5dfSDmitry Osipenko 			new_peak_bw = max(old_peak_bw, new_peak_bw);
1923*04d5d5dfSDmitry Osipenko 
1924*04d5d5dfSDmitry Osipenko 			if (tegra_plane_use_vertical_filtering(tegra, &old_window))
1925*04d5d5dfSDmitry Osipenko 				window = old_window;
1926*04d5d5dfSDmitry Osipenko 		}
1927*04d5d5dfSDmitry Osipenko 
1928*04d5d5dfSDmitry Osipenko 		icc_set_bw(tegra->icc_mem, new_avg_bw, new_peak_bw);
1929*04d5d5dfSDmitry Osipenko 
1930*04d5d5dfSDmitry Osipenko 		if (tegra_plane_use_vertical_filtering(tegra, &window))
1931*04d5d5dfSDmitry Osipenko 			icc_set_bw(tegra->icc_mem_vfilter, new_avg_bw, new_peak_bw);
1932*04d5d5dfSDmitry Osipenko 		else
1933*04d5d5dfSDmitry Osipenko 			icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
1934*04d5d5dfSDmitry Osipenko 	}
1935*04d5d5dfSDmitry Osipenko }
1936*04d5d5dfSDmitry Osipenko 
193764581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1938351f950dSMaxime Ripard 				      struct drm_atomic_state *state)
1939003fc848SThierry Reding {
1940003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1941003fc848SThierry Reding 	u32 value;
1942fd67e9c6SThierry Reding 	int err;
1943003fc848SThierry Reding 
1944003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1945003fc848SThierry Reding 		tegra_dc_stop(dc);
1946003fc848SThierry Reding 
1947003fc848SThierry Reding 		/*
1948003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1949003fc848SThierry Reding 		 * in case this fails.
1950003fc848SThierry Reding 		 */
1951003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1952003fc848SThierry Reding 	}
1953003fc848SThierry Reding 
1954003fc848SThierry Reding 	/*
1955003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1956003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1957003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1958003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1959003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1960003fc848SThierry Reding 	 * to go idle.
1961003fc848SThierry Reding 	 *
1962003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1963003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1964003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1965003fc848SThierry Reding 	 *
1966003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1967003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1968003fc848SThierry Reding 	 * the RGB encoder?
1969003fc848SThierry Reding 	 */
1970003fc848SThierry Reding 	if (dc->rgb) {
1971003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1972003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1973003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1974003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1975003fc848SThierry Reding 	}
1976003fc848SThierry Reding 
1977003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1978003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
197933a8eb8dSThierry Reding 
19809d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
19819d99ab6eSThierry Reding 
19829d99ab6eSThierry Reding 	if (crtc->state->event) {
19839d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
19849d99ab6eSThierry Reding 		crtc->state->event = NULL;
19859d99ab6eSThierry Reding 	}
19869d99ab6eSThierry Reding 
19879d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
19889d99ab6eSThierry Reding 
1989fd67e9c6SThierry Reding 	err = host1x_client_suspend(&dc->client);
1990fd67e9c6SThierry Reding 	if (err < 0)
1991fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to suspend: %d\n", err);
1992003fc848SThierry Reding }
1993003fc848SThierry Reding 
19940b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1995351f950dSMaxime Ripard 				     struct drm_atomic_state *state)
1996dee8268fSThierry Reding {
19974aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1998351f950dSMaxime Ripard 	struct tegra_dc_state *crtc_state = to_dc_state(crtc->state);
1999dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
2000dbb3f2f7SThierry Reding 	u32 value;
2001fd67e9c6SThierry Reding 	int err;
2002dee8268fSThierry Reding 
2003fd67e9c6SThierry Reding 	err = host1x_client_resume(&dc->client);
2004fd67e9c6SThierry Reding 	if (err < 0) {
2005fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to resume: %d\n", err);
2006fd67e9c6SThierry Reding 		return;
2007fd67e9c6SThierry Reding 	}
200833a8eb8dSThierry Reding 
200933a8eb8dSThierry Reding 	/* initialize display controller */
201033a8eb8dSThierry Reding 	if (dc->syncpt) {
201147307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
201247307954SThierry Reding 
201347307954SThierry Reding 		if (dc->soc->has_nvdisplay)
201447307954SThierry Reding 			enable = 1 << 31;
201547307954SThierry Reding 		else
201647307954SThierry Reding 			enable = 1 << 8;
201733a8eb8dSThierry Reding 
201833a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
201933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
202033a8eb8dSThierry Reding 
202147307954SThierry Reding 		value = enable | syncpt;
202233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
202333a8eb8dSThierry Reding 	}
202433a8eb8dSThierry Reding 
202547307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
202647307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
202747307954SThierry Reding 			DSC_OBUF_UF_INT;
202847307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
202947307954SThierry Reding 
203047307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
203147307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
203247307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
203347307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
203447307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
203547307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
203647307954SThierry Reding 
203747307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
203847307954SThierry Reding 			FRAME_END_INT;
203947307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
204047307954SThierry Reding 
204147307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
204247307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
204347307954SThierry Reding 
204447307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
204547307954SThierry Reding 	} else {
204633a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
204733a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
204833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
204933a8eb8dSThierry Reding 
205033a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
205133a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
205233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
205333a8eb8dSThierry Reding 
205433a8eb8dSThierry Reding 		/* initialize timer */
205533a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
205633a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
205733a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
205833a8eb8dSThierry Reding 
205933a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
206033a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
206133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
206233a8eb8dSThierry Reding 
206333a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
206433a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
206533a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
206633a8eb8dSThierry Reding 
206733a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
206833a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
206933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
207047307954SThierry Reding 	}
207133a8eb8dSThierry Reding 
20727116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
20737116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
20747116e9a8SThierry Reding 	else
207533a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
207633a8eb8dSThierry Reding 
207733a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
2078351f950dSMaxime Ripard 	tegra_dc_commit_state(dc, crtc_state);
207976d59ed0SThierry Reding 
2080dee8268fSThierry Reding 	/* program display mode */
2081dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
2082dee8268fSThierry Reding 
20838620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
20848620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
20858620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
20868620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
20878620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
20888620fc62SThierry Reding 	}
2089666cb873SThierry Reding 
2090666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
2091666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
2092666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
2093666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
2094666cb873SThierry Reding 
209547307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
2096666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
2097666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
2098666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
2099666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
210047307954SThierry Reding 	}
210147307954SThierry Reding 
210247307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
210347307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
210447307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
210547307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
210647307954SThierry Reding 	}
2107666cb873SThierry Reding 
2108666cb873SThierry Reding 	tegra_dc_commit(dc);
2109dee8268fSThierry Reding 
21108ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
2111dee8268fSThierry Reding }
2112dee8268fSThierry Reding 
2113613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
2114f6ebe9f9SMaxime Ripard 				    struct drm_atomic_state *state)
21154aa3df71SThierry Reding {
21169d99ab6eSThierry Reding 	unsigned long flags;
21171503ca47SThierry Reding 
2118*04d5d5dfSDmitry Osipenko 	tegra_crtc_update_memory_bandwidth(crtc, state, true);
2119*04d5d5dfSDmitry Osipenko 
21201503ca47SThierry Reding 	if (crtc->state->event) {
21219d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
21221503ca47SThierry Reding 
21239d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
21249d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
21259d99ab6eSThierry Reding 		else
21269d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
21271503ca47SThierry Reding 
21289d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
21299d99ab6eSThierry Reding 
21301503ca47SThierry Reding 		crtc->state->event = NULL;
21311503ca47SThierry Reding 	}
21324aa3df71SThierry Reding }
21334aa3df71SThierry Reding 
2134613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
2135f6ebe9f9SMaxime Ripard 				    struct drm_atomic_state *state)
21364aa3df71SThierry Reding {
2137253f28b6SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
2138253f28b6SMaxime Ripard 									  crtc);
2139253f28b6SMaxime Ripard 	struct tegra_dc_state *dc_state = to_dc_state(crtc_state);
214047802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
214147307954SThierry Reding 	u32 value;
214247802b09SThierry Reding 
2143253f28b6SMaxime Ripard 	value = dc_state->planes << 8 | GENERAL_UPDATE;
214447307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
214547307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
214647307954SThierry Reding 
2147253f28b6SMaxime Ripard 	value = dc_state->planes | GENERAL_ACT_REQ;
214847307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
214947307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
21504aa3df71SThierry Reding }
21514aa3df71SThierry Reding 
2152*04d5d5dfSDmitry Osipenko static bool tegra_plane_is_cursor(const struct drm_plane_state *state)
2153*04d5d5dfSDmitry Osipenko {
2154*04d5d5dfSDmitry Osipenko 	const struct tegra_dc_soc_info *soc = to_tegra_dc(state->crtc)->soc;
2155*04d5d5dfSDmitry Osipenko 	const struct drm_format_info *fmt = state->fb->format;
2156*04d5d5dfSDmitry Osipenko 	unsigned int src_w = drm_rect_width(&state->src) >> 16;
2157*04d5d5dfSDmitry Osipenko 	unsigned int dst_w = drm_rect_width(&state->dst);
2158*04d5d5dfSDmitry Osipenko 
2159*04d5d5dfSDmitry Osipenko 	if (state->plane->type != DRM_PLANE_TYPE_CURSOR)
2160*04d5d5dfSDmitry Osipenko 		return false;
2161*04d5d5dfSDmitry Osipenko 
2162*04d5d5dfSDmitry Osipenko 	if (soc->supports_cursor)
2163*04d5d5dfSDmitry Osipenko 		return true;
2164*04d5d5dfSDmitry Osipenko 
2165*04d5d5dfSDmitry Osipenko 	if (src_w != dst_w || fmt->num_planes != 1 || src_w * fmt->cpp[0] > 256)
2166*04d5d5dfSDmitry Osipenko 		return false;
2167*04d5d5dfSDmitry Osipenko 
2168*04d5d5dfSDmitry Osipenko 	return true;
2169*04d5d5dfSDmitry Osipenko }
2170*04d5d5dfSDmitry Osipenko 
2171*04d5d5dfSDmitry Osipenko static unsigned long
2172*04d5d5dfSDmitry Osipenko tegra_plane_overlap_mask(struct drm_crtc_state *state,
2173*04d5d5dfSDmitry Osipenko 			 const struct drm_plane_state *plane_state)
2174*04d5d5dfSDmitry Osipenko {
2175*04d5d5dfSDmitry Osipenko 	const struct drm_plane_state *other_state;
2176*04d5d5dfSDmitry Osipenko 	const struct tegra_plane *tegra;
2177*04d5d5dfSDmitry Osipenko 	unsigned long overlap_mask = 0;
2178*04d5d5dfSDmitry Osipenko 	struct drm_plane *plane;
2179*04d5d5dfSDmitry Osipenko 	struct drm_rect rect;
2180*04d5d5dfSDmitry Osipenko 
2181*04d5d5dfSDmitry Osipenko 	if (!plane_state->visible || !plane_state->fb)
2182*04d5d5dfSDmitry Osipenko 		return 0;
2183*04d5d5dfSDmitry Osipenko 
2184*04d5d5dfSDmitry Osipenko 	/*
2185*04d5d5dfSDmitry Osipenko 	 * Data-prefetch FIFO will easily help to overcome temporal memory
2186*04d5d5dfSDmitry Osipenko 	 * pressure if other plane overlaps with the cursor plane.
2187*04d5d5dfSDmitry Osipenko 	 */
2188*04d5d5dfSDmitry Osipenko 	if (tegra_plane_is_cursor(plane_state))
2189*04d5d5dfSDmitry Osipenko 		return 0;
2190*04d5d5dfSDmitry Osipenko 
2191*04d5d5dfSDmitry Osipenko 	drm_atomic_crtc_state_for_each_plane_state(plane, other_state, state) {
2192*04d5d5dfSDmitry Osipenko 		rect = plane_state->dst;
2193*04d5d5dfSDmitry Osipenko 
2194*04d5d5dfSDmitry Osipenko 		tegra = to_tegra_plane(other_state->plane);
2195*04d5d5dfSDmitry Osipenko 
2196*04d5d5dfSDmitry Osipenko 		if (!other_state->visible || !other_state->fb)
2197*04d5d5dfSDmitry Osipenko 			continue;
2198*04d5d5dfSDmitry Osipenko 
2199*04d5d5dfSDmitry Osipenko 		/*
2200*04d5d5dfSDmitry Osipenko 		 * Ignore cursor plane overlaps because it's not practical to
2201*04d5d5dfSDmitry Osipenko 		 * assume that it contributes to the bandwidth in overlapping
2202*04d5d5dfSDmitry Osipenko 		 * area if window width is small.
2203*04d5d5dfSDmitry Osipenko 		 */
2204*04d5d5dfSDmitry Osipenko 		if (tegra_plane_is_cursor(other_state))
2205*04d5d5dfSDmitry Osipenko 			continue;
2206*04d5d5dfSDmitry Osipenko 
2207*04d5d5dfSDmitry Osipenko 		if (drm_rect_intersect(&rect, &other_state->dst))
2208*04d5d5dfSDmitry Osipenko 			overlap_mask |= BIT(tegra->index);
2209*04d5d5dfSDmitry Osipenko 	}
2210*04d5d5dfSDmitry Osipenko 
2211*04d5d5dfSDmitry Osipenko 	return overlap_mask;
2212*04d5d5dfSDmitry Osipenko }
2213*04d5d5dfSDmitry Osipenko 
2214*04d5d5dfSDmitry Osipenko static int tegra_crtc_calculate_memory_bandwidth(struct drm_crtc *crtc,
2215*04d5d5dfSDmitry Osipenko 						 struct drm_atomic_state *state)
2216*04d5d5dfSDmitry Osipenko {
2217*04d5d5dfSDmitry Osipenko 	ulong overlap_mask[TEGRA_DC_LEGACY_PLANES_NUM] = {}, mask;
2218*04d5d5dfSDmitry Osipenko 	u32 plane_peak_bw[TEGRA_DC_LEGACY_PLANES_NUM] = {};
2219*04d5d5dfSDmitry Osipenko 	bool all_planes_overlap_simultaneously = true;
2220*04d5d5dfSDmitry Osipenko 	const struct tegra_plane_state *tegra_state;
2221*04d5d5dfSDmitry Osipenko 	const struct drm_plane_state *plane_state;
2222*04d5d5dfSDmitry Osipenko 	struct tegra_dc *dc = to_tegra_dc(crtc);
2223*04d5d5dfSDmitry Osipenko 	const struct drm_crtc_state *old_state;
2224*04d5d5dfSDmitry Osipenko 	struct drm_crtc_state *new_state;
2225*04d5d5dfSDmitry Osipenko 	struct tegra_plane *tegra;
2226*04d5d5dfSDmitry Osipenko 	struct drm_plane *plane;
2227*04d5d5dfSDmitry Osipenko 
2228*04d5d5dfSDmitry Osipenko 	/*
2229*04d5d5dfSDmitry Osipenko 	 * The nv-display uses shared planes.  The algorithm below assumes
2230*04d5d5dfSDmitry Osipenko 	 * maximum 3 planes per-CRTC, this assumption isn't applicable to
2231*04d5d5dfSDmitry Osipenko 	 * the nv-display.  Note that T124 support has additional windows,
2232*04d5d5dfSDmitry Osipenko 	 * but currently they aren't supported by the driver.
2233*04d5d5dfSDmitry Osipenko 	 */
2234*04d5d5dfSDmitry Osipenko 	if (dc->soc->has_nvdisplay)
2235*04d5d5dfSDmitry Osipenko 		return 0;
2236*04d5d5dfSDmitry Osipenko 
2237*04d5d5dfSDmitry Osipenko 	new_state = drm_atomic_get_new_crtc_state(state, crtc);
2238*04d5d5dfSDmitry Osipenko 	old_state = drm_atomic_get_old_crtc_state(state, crtc);
2239*04d5d5dfSDmitry Osipenko 
2240*04d5d5dfSDmitry Osipenko 	/*
2241*04d5d5dfSDmitry Osipenko 	 * For overlapping planes pixel's data is fetched for each plane at
2242*04d5d5dfSDmitry Osipenko 	 * the same time, hence bandwidths are accumulated in this case.
2243*04d5d5dfSDmitry Osipenko 	 * This needs to be taken into account for calculating total bandwidth
2244*04d5d5dfSDmitry Osipenko 	 * consumed by all planes.
2245*04d5d5dfSDmitry Osipenko 	 *
2246*04d5d5dfSDmitry Osipenko 	 * Here we get the overlapping state of each plane, which is a
2247*04d5d5dfSDmitry Osipenko 	 * bitmask of plane indices telling with what planes there is an
2248*04d5d5dfSDmitry Osipenko 	 * overlap. Note that bitmask[plane] includes BIT(plane) in order
2249*04d5d5dfSDmitry Osipenko 	 * to make further code nicer and simpler.
2250*04d5d5dfSDmitry Osipenko 	 */
2251*04d5d5dfSDmitry Osipenko 	drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
2252*04d5d5dfSDmitry Osipenko 		tegra_state = to_const_tegra_plane_state(plane_state);
2253*04d5d5dfSDmitry Osipenko 		tegra = to_tegra_plane(plane);
2254*04d5d5dfSDmitry Osipenko 
2255*04d5d5dfSDmitry Osipenko 		if (WARN_ON_ONCE(tegra->index >= TEGRA_DC_LEGACY_PLANES_NUM))
2256*04d5d5dfSDmitry Osipenko 			return -EINVAL;
2257*04d5d5dfSDmitry Osipenko 
2258*04d5d5dfSDmitry Osipenko 		plane_peak_bw[tegra->index] = tegra_state->peak_memory_bandwidth;
2259*04d5d5dfSDmitry Osipenko 		mask = tegra_plane_overlap_mask(new_state, plane_state);
2260*04d5d5dfSDmitry Osipenko 		overlap_mask[tegra->index] = mask;
2261*04d5d5dfSDmitry Osipenko 
2262*04d5d5dfSDmitry Osipenko 		if (hweight_long(mask) != 3)
2263*04d5d5dfSDmitry Osipenko 			all_planes_overlap_simultaneously = false;
2264*04d5d5dfSDmitry Osipenko 	}
2265*04d5d5dfSDmitry Osipenko 
2266*04d5d5dfSDmitry Osipenko 	/*
2267*04d5d5dfSDmitry Osipenko 	 * Then we calculate maximum bandwidth of each plane state.
2268*04d5d5dfSDmitry Osipenko 	 * The bandwidth includes the plane BW + BW of the "simultaneously"
2269*04d5d5dfSDmitry Osipenko 	 * overlapping planes, where "simultaneously" means areas where DC
2270*04d5d5dfSDmitry Osipenko 	 * fetches from the planes simultaneously during of scan-out process.
2271*04d5d5dfSDmitry Osipenko 	 *
2272*04d5d5dfSDmitry Osipenko 	 * For example, if plane A overlaps with planes B and C, but B and C
2273*04d5d5dfSDmitry Osipenko 	 * don't overlap, then the peak bandwidth will be either in area where
2274*04d5d5dfSDmitry Osipenko 	 * A-and-B or A-and-C planes overlap.
2275*04d5d5dfSDmitry Osipenko 	 *
2276*04d5d5dfSDmitry Osipenko 	 * The plane_peak_bw[] contains peak memory bandwidth values of
2277*04d5d5dfSDmitry Osipenko 	 * each plane, this information is needed by interconnect provider
2278*04d5d5dfSDmitry Osipenko 	 * in order to set up latency allowance based on the peak BW, see
2279*04d5d5dfSDmitry Osipenko 	 * tegra_crtc_update_memory_bandwidth().
2280*04d5d5dfSDmitry Osipenko 	 */
2281*04d5d5dfSDmitry Osipenko 	drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
2282*04d5d5dfSDmitry Osipenko 		u32 i, old_peak_bw, new_peak_bw, overlap_bw = 0;
2283*04d5d5dfSDmitry Osipenko 
2284*04d5d5dfSDmitry Osipenko 		/*
2285*04d5d5dfSDmitry Osipenko 		 * Note that plane's atomic check doesn't touch the
2286*04d5d5dfSDmitry Osipenko 		 * total_peak_memory_bandwidth of enabled plane, hence the
2287*04d5d5dfSDmitry Osipenko 		 * current state contains the old bandwidth state from the
2288*04d5d5dfSDmitry Osipenko 		 * previous CRTC commit.
2289*04d5d5dfSDmitry Osipenko 		 */
2290*04d5d5dfSDmitry Osipenko 		tegra_state = to_const_tegra_plane_state(plane_state);
2291*04d5d5dfSDmitry Osipenko 		tegra = to_tegra_plane(plane);
2292*04d5d5dfSDmitry Osipenko 
2293*04d5d5dfSDmitry Osipenko 		for_each_set_bit(i, &overlap_mask[tegra->index], 3) {
2294*04d5d5dfSDmitry Osipenko 			if (i == tegra->index)
2295*04d5d5dfSDmitry Osipenko 				continue;
2296*04d5d5dfSDmitry Osipenko 
2297*04d5d5dfSDmitry Osipenko 			if (all_planes_overlap_simultaneously)
2298*04d5d5dfSDmitry Osipenko 				overlap_bw += plane_peak_bw[i];
2299*04d5d5dfSDmitry Osipenko 			else
2300*04d5d5dfSDmitry Osipenko 				overlap_bw = max(overlap_bw, plane_peak_bw[i]);
2301*04d5d5dfSDmitry Osipenko 		}
2302*04d5d5dfSDmitry Osipenko 
2303*04d5d5dfSDmitry Osipenko 		new_peak_bw = plane_peak_bw[tegra->index] + overlap_bw;
2304*04d5d5dfSDmitry Osipenko 		old_peak_bw = tegra_state->total_peak_memory_bandwidth;
2305*04d5d5dfSDmitry Osipenko 
2306*04d5d5dfSDmitry Osipenko 		/*
2307*04d5d5dfSDmitry Osipenko 		 * If plane's peak bandwidth changed (for example plane isn't
2308*04d5d5dfSDmitry Osipenko 		 * overlapped anymore) and plane isn't in the atomic state,
2309*04d5d5dfSDmitry Osipenko 		 * then add plane to the state in order to have the bandwidth
2310*04d5d5dfSDmitry Osipenko 		 * updated.
2311*04d5d5dfSDmitry Osipenko 		 */
2312*04d5d5dfSDmitry Osipenko 		if (old_peak_bw != new_peak_bw) {
2313*04d5d5dfSDmitry Osipenko 			struct tegra_plane_state *new_tegra_state;
2314*04d5d5dfSDmitry Osipenko 			struct drm_plane_state *new_plane_state;
2315*04d5d5dfSDmitry Osipenko 
2316*04d5d5dfSDmitry Osipenko 			new_plane_state = drm_atomic_get_plane_state(state, plane);
2317*04d5d5dfSDmitry Osipenko 			if (IS_ERR(new_plane_state))
2318*04d5d5dfSDmitry Osipenko 				return PTR_ERR(new_plane_state);
2319*04d5d5dfSDmitry Osipenko 
2320*04d5d5dfSDmitry Osipenko 			new_tegra_state = to_tegra_plane_state(new_plane_state);
2321*04d5d5dfSDmitry Osipenko 			new_tegra_state->total_peak_memory_bandwidth = new_peak_bw;
2322*04d5d5dfSDmitry Osipenko 		}
2323*04d5d5dfSDmitry Osipenko 	}
2324*04d5d5dfSDmitry Osipenko 
2325*04d5d5dfSDmitry Osipenko 	return 0;
2326*04d5d5dfSDmitry Osipenko }
2327*04d5d5dfSDmitry Osipenko 
2328*04d5d5dfSDmitry Osipenko static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
2329*04d5d5dfSDmitry Osipenko 				   struct drm_atomic_state *state)
2330*04d5d5dfSDmitry Osipenko {
2331*04d5d5dfSDmitry Osipenko 	int err;
2332*04d5d5dfSDmitry Osipenko 
2333*04d5d5dfSDmitry Osipenko 	err = tegra_crtc_calculate_memory_bandwidth(crtc, state);
2334*04d5d5dfSDmitry Osipenko 	if (err)
2335*04d5d5dfSDmitry Osipenko 		return err;
2336*04d5d5dfSDmitry Osipenko 
2337*04d5d5dfSDmitry Osipenko 	return 0;
2338*04d5d5dfSDmitry Osipenko }
2339*04d5d5dfSDmitry Osipenko 
2340*04d5d5dfSDmitry Osipenko void tegra_crtc_atomic_post_commit(struct drm_crtc *crtc,
2341*04d5d5dfSDmitry Osipenko 				   struct drm_atomic_state *state)
2342*04d5d5dfSDmitry Osipenko {
2343*04d5d5dfSDmitry Osipenko 	/*
2344*04d5d5dfSDmitry Osipenko 	 * Display bandwidth is allowed to go down only once hardware state
2345*04d5d5dfSDmitry Osipenko 	 * is known to be armed, i.e. state was committed and VBLANK event
2346*04d5d5dfSDmitry Osipenko 	 * received.
2347*04d5d5dfSDmitry Osipenko 	 */
2348*04d5d5dfSDmitry Osipenko 	tegra_crtc_update_memory_bandwidth(crtc, state, false);
2349*04d5d5dfSDmitry Osipenko }
2350*04d5d5dfSDmitry Osipenko 
2351dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
2352*04d5d5dfSDmitry Osipenko 	.atomic_check = tegra_crtc_atomic_check,
23534aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
23544aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
23550b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
235664581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
2357dee8268fSThierry Reding };
2358dee8268fSThierry Reding 
2359dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
2360dee8268fSThierry Reding {
2361dee8268fSThierry Reding 	struct tegra_dc *dc = data;
2362dee8268fSThierry Reding 	unsigned long status;
2363dee8268fSThierry Reding 
2364dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
2365dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
2366dee8268fSThierry Reding 
2367dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
2368dee8268fSThierry Reding 		/*
2369dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
2370dee8268fSThierry Reding 		*/
2371791ddb1eSThierry Reding 		dc->stats.frames++;
2372dee8268fSThierry Reding 	}
2373dee8268fSThierry Reding 
2374dee8268fSThierry Reding 	if (status & VBLANK_INT) {
2375dee8268fSThierry Reding 		/*
2376dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
2377dee8268fSThierry Reding 		*/
2378ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
2379791ddb1eSThierry Reding 		dc->stats.vblank++;
2380dee8268fSThierry Reding 	}
2381dee8268fSThierry Reding 
2382dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
2383dee8268fSThierry Reding 		/*
2384dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
2385dee8268fSThierry Reding 		*/
2386791ddb1eSThierry Reding 		dc->stats.underflow++;
2387791ddb1eSThierry Reding 	}
2388791ddb1eSThierry Reding 
2389791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
2390791ddb1eSThierry Reding 		/*
2391791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
2392791ddb1eSThierry Reding 		*/
2393791ddb1eSThierry Reding 		dc->stats.overflow++;
2394dee8268fSThierry Reding 	}
2395dee8268fSThierry Reding 
239647307954SThierry Reding 	if (status & HEAD_UF_INT) {
239747307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
239847307954SThierry Reding 		dc->stats.underflow++;
239947307954SThierry Reding 	}
240047307954SThierry Reding 
2401dee8268fSThierry Reding 	return IRQ_HANDLED;
2402dee8268fSThierry Reding }
2403dee8268fSThierry Reding 
2404e75d0477SThierry Reding static bool tegra_dc_has_window_groups(struct tegra_dc *dc)
2405e75d0477SThierry Reding {
2406e75d0477SThierry Reding 	unsigned int i;
2407e75d0477SThierry Reding 
2408e75d0477SThierry Reding 	if (!dc->soc->wgrps)
2409e75d0477SThierry Reding 		return true;
2410e75d0477SThierry Reding 
2411e75d0477SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
2412e75d0477SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
2413e75d0477SThierry Reding 
2414e75d0477SThierry Reding 		if (wgrp->dc == dc->pipe && wgrp->num_windows > 0)
2415e75d0477SThierry Reding 			return true;
2416e75d0477SThierry Reding 	}
2417e75d0477SThierry Reding 
2418e75d0477SThierry Reding 	return false;
2419e75d0477SThierry Reding }
2420e75d0477SThierry Reding 
242105d1adfeSThierry Reding static int tegra_dc_early_init(struct host1x_client *client)
242205d1adfeSThierry Reding {
242305d1adfeSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
242405d1adfeSThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
242505d1adfeSThierry Reding 
242605d1adfeSThierry Reding 	tegra->num_crtcs++;
242705d1adfeSThierry Reding 
242805d1adfeSThierry Reding 	return 0;
242905d1adfeSThierry Reding }
243005d1adfeSThierry Reding 
2431dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
2432dee8268fSThierry Reding {
2433608f43adSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
24342bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
2435dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2436d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
2437c7679306SThierry Reding 	struct drm_plane *primary = NULL;
2438c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
2439dee8268fSThierry Reding 	int err;
2440dee8268fSThierry Reding 
2441759d706fSThierry Reding 	/*
2442f5ba33fbSMikko Perttunen 	 * DC has been reset by now, so VBLANK syncpoint can be released
2443f5ba33fbSMikko Perttunen 	 * for general use.
2444f5ba33fbSMikko Perttunen 	 */
2445f5ba33fbSMikko Perttunen 	host1x_syncpt_release_vblank_reservation(client, 26 + dc->pipe);
2446f5ba33fbSMikko Perttunen 
2447f5ba33fbSMikko Perttunen 	/*
2448759d706fSThierry Reding 	 * XXX do not register DCs with no window groups because we cannot
2449759d706fSThierry Reding 	 * assign a primary plane to them, which in turn will cause KMS to
2450759d706fSThierry Reding 	 * crash.
2451759d706fSThierry Reding 	 */
2452e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2453759d706fSThierry Reding 		return 0;
2454759d706fSThierry Reding 
2455fd67e9c6SThierry Reding 	/*
2456fd67e9c6SThierry Reding 	 * Set the display hub as the host1x client parent for the display
2457fd67e9c6SThierry Reding 	 * controller. This is needed for the runtime reference counting that
2458fd67e9c6SThierry Reding 	 * ensures the display hub is always powered when any of the display
2459fd67e9c6SThierry Reding 	 * controllers are.
2460fd67e9c6SThierry Reding 	 */
2461fd67e9c6SThierry Reding 	if (dc->soc->has_nvdisplay)
2462fd67e9c6SThierry Reding 		client->parent = &tegra->hub->client;
2463fd67e9c6SThierry Reding 
2464617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
24652bcdcbfaSThierry Reding 	if (!dc->syncpt)
24662bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
24672bcdcbfaSThierry Reding 
24687edd7961SThierry Reding 	err = host1x_client_iommu_attach(client);
2469a8817489SThierry Reding 	if (err < 0 && err != -ENODEV) {
24700c407de5SThierry Reding 		dev_err(client->dev, "failed to attach to domain: %d\n", err);
2471df06b759SThierry Reding 		return err;
2472df06b759SThierry Reding 	}
2473df06b759SThierry Reding 
247447307954SThierry Reding 	if (dc->soc->wgrps)
247547307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
247647307954SThierry Reding 	else
247747307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
247847307954SThierry Reding 
2479c7679306SThierry Reding 	if (IS_ERR(primary)) {
2480c7679306SThierry Reding 		err = PTR_ERR(primary);
2481c7679306SThierry Reding 		goto cleanup;
2482c7679306SThierry Reding 	}
2483c7679306SThierry Reding 
2484c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
2485c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
2486c7679306SThierry Reding 		if (IS_ERR(cursor)) {
2487c7679306SThierry Reding 			err = PTR_ERR(cursor);
2488c7679306SThierry Reding 			goto cleanup;
2489c7679306SThierry Reding 		}
24909f446d83SDmitry Osipenko 	} else {
24919f446d83SDmitry Osipenko 		/* dedicate one overlay to mouse cursor */
24929f446d83SDmitry Osipenko 		cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
24939f446d83SDmitry Osipenko 		if (IS_ERR(cursor)) {
24949f446d83SDmitry Osipenko 			err = PTR_ERR(cursor);
24959f446d83SDmitry Osipenko 			goto cleanup;
24969f446d83SDmitry Osipenko 		}
2497c7679306SThierry Reding 	}
2498c7679306SThierry Reding 
2499c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
2500f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
2501c7679306SThierry Reding 	if (err < 0)
2502c7679306SThierry Reding 		goto cleanup;
2503c7679306SThierry Reding 
2504dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
2505dee8268fSThierry Reding 
2506d1f3e1e0SThierry Reding 	/*
2507d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
2508d1f3e1e0SThierry Reding 	 * controllers.
2509d1f3e1e0SThierry Reding 	 */
2510d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
2511d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
2512d1f3e1e0SThierry Reding 
2513042c0bd7SThierry Reding 	/* track maximum resolution */
2514042c0bd7SThierry Reding 	if (dc->soc->has_nvdisplay)
2515042c0bd7SThierry Reding 		drm->mode_config.max_width = drm->mode_config.max_height = 16384;
2516042c0bd7SThierry Reding 	else
2517042c0bd7SThierry Reding 		drm->mode_config.max_width = drm->mode_config.max_height = 4096;
2518042c0bd7SThierry Reding 
25199910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
2520dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2521dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
2522c7679306SThierry Reding 		goto cleanup;
2523dee8268fSThierry Reding 	}
2524dee8268fSThierry Reding 
2525dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
2526dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
2527dee8268fSThierry Reding 	if (err < 0) {
2528dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
2529dee8268fSThierry Reding 			err);
2530c7679306SThierry Reding 		goto cleanup;
2531dee8268fSThierry Reding 	}
2532dee8268fSThierry Reding 
253347b15779SThierry Reding 	/*
253447b15779SThierry Reding 	 * Inherit the DMA parameters (such as maximum segment size) from the
2535608f43adSThierry Reding 	 * parent host1x device.
253647b15779SThierry Reding 	 */
2537608f43adSThierry Reding 	client->dev->dma_parms = client->host->dma_parms;
253847b15779SThierry Reding 
2539dee8268fSThierry Reding 	return 0;
2540c7679306SThierry Reding 
2541c7679306SThierry Reding cleanup:
254247307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
2543c7679306SThierry Reding 		drm_plane_cleanup(cursor);
2544c7679306SThierry Reding 
254547307954SThierry Reding 	if (!IS_ERR(primary))
2546c7679306SThierry Reding 		drm_plane_cleanup(primary);
2547c7679306SThierry Reding 
2548aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
25492aed4f5aSMikko Perttunen 	host1x_syncpt_put(dc->syncpt);
2550fd5ec0dcSThierry Reding 
2551c7679306SThierry Reding 	return err;
2552dee8268fSThierry Reding }
2553dee8268fSThierry Reding 
2554dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
2555dee8268fSThierry Reding {
2556dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2557dee8268fSThierry Reding 	int err;
2558dee8268fSThierry Reding 
2559e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2560e75d0477SThierry Reding 		return 0;
2561e75d0477SThierry Reding 
256247b15779SThierry Reding 	/* avoid a dangling pointer just in case this disappears */
256347b15779SThierry Reding 	client->dev->dma_parms = NULL;
256447b15779SThierry Reding 
2565dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
2566dee8268fSThierry Reding 
2567dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
2568dee8268fSThierry Reding 	if (err) {
2569dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
2570dee8268fSThierry Reding 		return err;
2571dee8268fSThierry Reding 	}
2572dee8268fSThierry Reding 
2573aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
25742aed4f5aSMikko Perttunen 	host1x_syncpt_put(dc->syncpt);
25752bcdcbfaSThierry Reding 
2576dee8268fSThierry Reding 	return 0;
2577dee8268fSThierry Reding }
2578dee8268fSThierry Reding 
257905d1adfeSThierry Reding static int tegra_dc_late_exit(struct host1x_client *client)
258005d1adfeSThierry Reding {
258105d1adfeSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
258205d1adfeSThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
258305d1adfeSThierry Reding 
258405d1adfeSThierry Reding 	tegra->num_crtcs--;
2585dee8268fSThierry Reding 
2586dee8268fSThierry Reding 	return 0;
2587dee8268fSThierry Reding }
2588dee8268fSThierry Reding 
2589fd67e9c6SThierry Reding static int tegra_dc_runtime_suspend(struct host1x_client *client)
2590fd67e9c6SThierry Reding {
2591fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2592fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2593fd67e9c6SThierry Reding 	int err;
2594fd67e9c6SThierry Reding 
2595fd67e9c6SThierry Reding 	err = reset_control_assert(dc->rst);
2596fd67e9c6SThierry Reding 	if (err < 0) {
2597fd67e9c6SThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
2598fd67e9c6SThierry Reding 		return err;
2599fd67e9c6SThierry Reding 	}
2600fd67e9c6SThierry Reding 
2601fd67e9c6SThierry Reding 	if (dc->soc->has_powergate)
2602fd67e9c6SThierry Reding 		tegra_powergate_power_off(dc->powergate);
2603fd67e9c6SThierry Reding 
2604fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2605fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2606fd67e9c6SThierry Reding 
2607fd67e9c6SThierry Reding 	return 0;
2608fd67e9c6SThierry Reding }
2609fd67e9c6SThierry Reding 
2610fd67e9c6SThierry Reding static int tegra_dc_runtime_resume(struct host1x_client *client)
2611fd67e9c6SThierry Reding {
2612fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2613fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2614fd67e9c6SThierry Reding 	int err;
2615fd67e9c6SThierry Reding 
2616dcdfe271SQinglang Miao 	err = pm_runtime_resume_and_get(dev);
2617fd67e9c6SThierry Reding 	if (err < 0) {
2618fd67e9c6SThierry Reding 		dev_err(dev, "failed to get runtime PM: %d\n", err);
2619fd67e9c6SThierry Reding 		return err;
2620fd67e9c6SThierry Reding 	}
2621fd67e9c6SThierry Reding 
2622fd67e9c6SThierry Reding 	if (dc->soc->has_powergate) {
2623fd67e9c6SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2624fd67e9c6SThierry Reding 							dc->rst);
2625fd67e9c6SThierry Reding 		if (err < 0) {
2626fd67e9c6SThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
2627fd67e9c6SThierry Reding 			goto put_rpm;
2628fd67e9c6SThierry Reding 		}
2629fd67e9c6SThierry Reding 	} else {
2630fd67e9c6SThierry Reding 		err = clk_prepare_enable(dc->clk);
2631fd67e9c6SThierry Reding 		if (err < 0) {
2632fd67e9c6SThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
2633fd67e9c6SThierry Reding 			goto put_rpm;
2634fd67e9c6SThierry Reding 		}
2635fd67e9c6SThierry Reding 
2636fd67e9c6SThierry Reding 		err = reset_control_deassert(dc->rst);
2637fd67e9c6SThierry Reding 		if (err < 0) {
2638fd67e9c6SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
2639fd67e9c6SThierry Reding 			goto disable_clk;
2640fd67e9c6SThierry Reding 		}
2641fd67e9c6SThierry Reding 	}
2642fd67e9c6SThierry Reding 
2643fd67e9c6SThierry Reding 	return 0;
2644fd67e9c6SThierry Reding 
2645fd67e9c6SThierry Reding disable_clk:
2646fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2647fd67e9c6SThierry Reding put_rpm:
2648fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2649fd67e9c6SThierry Reding 	return err;
2650fd67e9c6SThierry Reding }
2651fd67e9c6SThierry Reding 
2652dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
265305d1adfeSThierry Reding 	.early_init = tegra_dc_early_init,
2654dee8268fSThierry Reding 	.init = tegra_dc_init,
2655dee8268fSThierry Reding 	.exit = tegra_dc_exit,
265605d1adfeSThierry Reding 	.late_exit = tegra_dc_late_exit,
2657fd67e9c6SThierry Reding 	.suspend = tegra_dc_runtime_suspend,
2658fd67e9c6SThierry Reding 	.resume = tegra_dc_runtime_resume,
2659dee8268fSThierry Reding };
2660dee8268fSThierry Reding 
26618620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
26627116e9a8SThierry Reding 	.supports_background_color = false,
26638620fc62SThierry Reding 	.supports_interlacing = false,
2664e687651bSThierry Reding 	.supports_cursor = false,
2665c134f019SThierry Reding 	.supports_block_linear = false,
26667b6f8467SThierry Reding 	.supports_sector_layout = false,
2667a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2668d1f3e1e0SThierry Reding 	.pitch_align = 8,
26699c012700SThierry Reding 	.has_powergate = false,
2670f68ba691SDmitry Osipenko 	.coupled_pm = true,
267147307954SThierry Reding 	.has_nvdisplay = false,
2672511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2673511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2674511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2675511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2676e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2677acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = true,
2678*04d5d5dfSDmitry Osipenko 	.has_win_b_vfilter_mem_client = true,
2679acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = true,
2680*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = false,
26818620fc62SThierry Reding };
26828620fc62SThierry Reding 
26838620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
26847116e9a8SThierry Reding 	.supports_background_color = false,
26858620fc62SThierry Reding 	.supports_interlacing = false,
2686e687651bSThierry Reding 	.supports_cursor = false,
2687c134f019SThierry Reding 	.supports_block_linear = false,
26887b6f8467SThierry Reding 	.supports_sector_layout = false,
2689a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2690d1f3e1e0SThierry Reding 	.pitch_align = 8,
26919c012700SThierry Reding 	.has_powergate = false,
2692f68ba691SDmitry Osipenko 	.coupled_pm = false,
269347307954SThierry Reding 	.has_nvdisplay = false,
2694511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2695511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2696511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2697511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2698e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2699acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2700*04d5d5dfSDmitry Osipenko 	.has_win_b_vfilter_mem_client = true,
2701acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2702*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = true,
2703d1f3e1e0SThierry Reding };
2704d1f3e1e0SThierry Reding 
2705d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
27067116e9a8SThierry Reding 	.supports_background_color = false,
2707d1f3e1e0SThierry Reding 	.supports_interlacing = false,
2708d1f3e1e0SThierry Reding 	.supports_cursor = false,
2709d1f3e1e0SThierry Reding 	.supports_block_linear = false,
27107b6f8467SThierry Reding 	.supports_sector_layout = false,
2711a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2712d1f3e1e0SThierry Reding 	.pitch_align = 64,
27139c012700SThierry Reding 	.has_powergate = true,
2714f68ba691SDmitry Osipenko 	.coupled_pm = false,
271547307954SThierry Reding 	.has_nvdisplay = false,
2716511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2717511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2718511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2719511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2720e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2721acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2722*04d5d5dfSDmitry Osipenko 	.has_win_b_vfilter_mem_client = false,
2723acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2724*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = true,
27258620fc62SThierry Reding };
27268620fc62SThierry Reding 
27278620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
27287116e9a8SThierry Reding 	.supports_background_color = true,
27298620fc62SThierry Reding 	.supports_interlacing = true,
2730e687651bSThierry Reding 	.supports_cursor = true,
2731c134f019SThierry Reding 	.supports_block_linear = true,
27327b6f8467SThierry Reding 	.supports_sector_layout = false,
2733a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
2734d1f3e1e0SThierry Reding 	.pitch_align = 64,
27359c012700SThierry Reding 	.has_powergate = true,
2736f68ba691SDmitry Osipenko 	.coupled_pm = false,
273747307954SThierry Reding 	.has_nvdisplay = false,
2738511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
27399a02d3afSStefan Agner 	.primary_formats = tegra124_primary_formats,
2740511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
27419a02d3afSStefan Agner 	.overlay_formats = tegra124_overlay_formats,
2742e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2743acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2744*04d5d5dfSDmitry Osipenko 	.has_win_b_vfilter_mem_client = false,
2745acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2746*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = false,
27478620fc62SThierry Reding };
27488620fc62SThierry Reding 
27495b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
27507116e9a8SThierry Reding 	.supports_background_color = true,
27515b4f516fSThierry Reding 	.supports_interlacing = true,
27525b4f516fSThierry Reding 	.supports_cursor = true,
27535b4f516fSThierry Reding 	.supports_block_linear = true,
27547b6f8467SThierry Reding 	.supports_sector_layout = false,
2755a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
27565b4f516fSThierry Reding 	.pitch_align = 64,
27575b4f516fSThierry Reding 	.has_powergate = true,
2758f68ba691SDmitry Osipenko 	.coupled_pm = false,
275947307954SThierry Reding 	.has_nvdisplay = false,
2760511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2761511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2762511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2763511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2764e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2765acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2766*04d5d5dfSDmitry Osipenko 	.has_win_b_vfilter_mem_client = false,
2767acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2768*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = false,
276947307954SThierry Reding };
277047307954SThierry Reding 
277147307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
277247307954SThierry Reding 	{
277347307954SThierry Reding 		.index = 0,
277447307954SThierry Reding 		.dc = 0,
277547307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
277647307954SThierry Reding 		.num_windows = 1,
277747307954SThierry Reding 	}, {
277847307954SThierry Reding 		.index = 1,
277947307954SThierry Reding 		.dc = 1,
278047307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
278147307954SThierry Reding 		.num_windows = 1,
278247307954SThierry Reding 	}, {
278347307954SThierry Reding 		.index = 2,
278447307954SThierry Reding 		.dc = 1,
278547307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
278647307954SThierry Reding 		.num_windows = 1,
278747307954SThierry Reding 	}, {
278847307954SThierry Reding 		.index = 3,
278947307954SThierry Reding 		.dc = 2,
279047307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
279147307954SThierry Reding 		.num_windows = 1,
279247307954SThierry Reding 	}, {
279347307954SThierry Reding 		.index = 4,
279447307954SThierry Reding 		.dc = 2,
279547307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
279647307954SThierry Reding 		.num_windows = 1,
279747307954SThierry Reding 	}, {
279847307954SThierry Reding 		.index = 5,
279947307954SThierry Reding 		.dc = 2,
280047307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
280147307954SThierry Reding 		.num_windows = 1,
280247307954SThierry Reding 	},
280347307954SThierry Reding };
280447307954SThierry Reding 
280547307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
280647307954SThierry Reding 	.supports_background_color = true,
280747307954SThierry Reding 	.supports_interlacing = true,
280847307954SThierry Reding 	.supports_cursor = true,
280947307954SThierry Reding 	.supports_block_linear = true,
28107b6f8467SThierry Reding 	.supports_sector_layout = false,
2811a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
281247307954SThierry Reding 	.pitch_align = 64,
281347307954SThierry Reding 	.has_powergate = false,
2814f68ba691SDmitry Osipenko 	.coupled_pm = false,
281547307954SThierry Reding 	.has_nvdisplay = true,
281647307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
281747307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
2818*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = false,
28195b4f516fSThierry Reding };
28205b4f516fSThierry Reding 
282147443196SThierry Reding static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
282247443196SThierry Reding 	{
282347443196SThierry Reding 		.index = 0,
282447443196SThierry Reding 		.dc = 0,
282547443196SThierry Reding 		.windows = (const unsigned int[]) { 0 },
282647443196SThierry Reding 		.num_windows = 1,
282747443196SThierry Reding 	}, {
282847443196SThierry Reding 		.index = 1,
282947443196SThierry Reding 		.dc = 1,
283047443196SThierry Reding 		.windows = (const unsigned int[]) { 1 },
283147443196SThierry Reding 		.num_windows = 1,
283247443196SThierry Reding 	}, {
283347443196SThierry Reding 		.index = 2,
283447443196SThierry Reding 		.dc = 1,
283547443196SThierry Reding 		.windows = (const unsigned int[]) { 2 },
283647443196SThierry Reding 		.num_windows = 1,
283747443196SThierry Reding 	}, {
283847443196SThierry Reding 		.index = 3,
283947443196SThierry Reding 		.dc = 2,
284047443196SThierry Reding 		.windows = (const unsigned int[]) { 3 },
284147443196SThierry Reding 		.num_windows = 1,
284247443196SThierry Reding 	}, {
284347443196SThierry Reding 		.index = 4,
284447443196SThierry Reding 		.dc = 2,
284547443196SThierry Reding 		.windows = (const unsigned int[]) { 4 },
284647443196SThierry Reding 		.num_windows = 1,
284747443196SThierry Reding 	}, {
284847443196SThierry Reding 		.index = 5,
284947443196SThierry Reding 		.dc = 2,
285047443196SThierry Reding 		.windows = (const unsigned int[]) { 5 },
285147443196SThierry Reding 		.num_windows = 1,
285247443196SThierry Reding 	},
285347443196SThierry Reding };
285447443196SThierry Reding 
285547443196SThierry Reding static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
285647443196SThierry Reding 	.supports_background_color = true,
285747443196SThierry Reding 	.supports_interlacing = true,
285847443196SThierry Reding 	.supports_cursor = true,
285947443196SThierry Reding 	.supports_block_linear = true,
28607b6f8467SThierry Reding 	.supports_sector_layout = true,
286147443196SThierry Reding 	.has_legacy_blending = false,
286247443196SThierry Reding 	.pitch_align = 64,
286347443196SThierry Reding 	.has_powergate = false,
286447443196SThierry Reding 	.coupled_pm = false,
286547443196SThierry Reding 	.has_nvdisplay = true,
286647443196SThierry Reding 	.wgrps = tegra194_dc_wgrps,
286747443196SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
2868*04d5d5dfSDmitry Osipenko 	.plane_tiled_memory_bandwidth_x2 = false,
286947443196SThierry Reding };
287047443196SThierry Reding 
28718620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
28728620fc62SThierry Reding 	{
287347443196SThierry Reding 		.compatible = "nvidia,tegra194-dc",
287447443196SThierry Reding 		.data = &tegra194_dc_soc_info,
287547443196SThierry Reding 	}, {
287647307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
287747307954SThierry Reding 		.data = &tegra186_dc_soc_info,
287847307954SThierry Reding 	}, {
28795b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
28805b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
28815b4f516fSThierry Reding 	}, {
28828620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
28838620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
28848620fc62SThierry Reding 	}, {
28859c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
28869c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
28879c012700SThierry Reding 	}, {
28888620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
28898620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
28908620fc62SThierry Reding 	}, {
28918620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
28928620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
28938620fc62SThierry Reding 	}, {
28948620fc62SThierry Reding 		/* sentinel */
28958620fc62SThierry Reding 	}
28968620fc62SThierry Reding };
2897ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
28988620fc62SThierry Reding 
289913411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
290013411dddSThierry Reding {
290113411dddSThierry Reding 	struct device_node *np;
290213411dddSThierry Reding 	u32 value = 0;
290313411dddSThierry Reding 	int err;
290413411dddSThierry Reding 
290513411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
290613411dddSThierry Reding 	if (err < 0) {
290713411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
290813411dddSThierry Reding 
290913411dddSThierry Reding 		/*
291013411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
291113411dddSThierry Reding 		 * correct head number by looking up the position of this
291213411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
291313411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
291413411dddSThierry Reding 		 * that the translation into a flattened device tree blob
291513411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
291613411dddSThierry Reding 		 * head number.
291713411dddSThierry Reding 		 *
291813411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
291913411dddSThierry Reding 		 * cases where only a single display controller is used.
292013411dddSThierry Reding 		 */
292113411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
2922cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
2923cf6b1744SJulia Lawall 				of_node_put(np);
292413411dddSThierry Reding 				break;
2925cf6b1744SJulia Lawall 			}
292613411dddSThierry Reding 
292713411dddSThierry Reding 			value++;
292813411dddSThierry Reding 		}
292913411dddSThierry Reding 	}
293013411dddSThierry Reding 
293113411dddSThierry Reding 	dc->pipe = value;
293213411dddSThierry Reding 
293313411dddSThierry Reding 	return 0;
293413411dddSThierry Reding }
293513411dddSThierry Reding 
293692ce7e83SSuzuki K Poulose static int tegra_dc_match_by_pipe(struct device *dev, const void *data)
2937f68ba691SDmitry Osipenko {
2938f68ba691SDmitry Osipenko 	struct tegra_dc *dc = dev_get_drvdata(dev);
293992ce7e83SSuzuki K Poulose 	unsigned int pipe = (unsigned long)(void *)data;
2940f68ba691SDmitry Osipenko 
2941f68ba691SDmitry Osipenko 	return dc->pipe == pipe;
2942f68ba691SDmitry Osipenko }
2943f68ba691SDmitry Osipenko 
2944f68ba691SDmitry Osipenko static int tegra_dc_couple(struct tegra_dc *dc)
2945f68ba691SDmitry Osipenko {
2946f68ba691SDmitry Osipenko 	/*
2947f68ba691SDmitry Osipenko 	 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2948f68ba691SDmitry Osipenko 	 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2949f68ba691SDmitry Osipenko 	 * POWER_CONTROL registers during CRTC enabling.
2950f68ba691SDmitry Osipenko 	 */
2951f68ba691SDmitry Osipenko 	if (dc->soc->coupled_pm && dc->pipe == 1) {
2952a31500feSThierry Reding 		struct device *companion;
2953a31500feSThierry Reding 		struct tegra_dc *parent;
2954f68ba691SDmitry Osipenko 
2955a31500feSThierry Reding 		companion = driver_find_device(dc->dev->driver, NULL, (const void *)0,
2956f68ba691SDmitry Osipenko 					       tegra_dc_match_by_pipe);
2957a31500feSThierry Reding 		if (!companion)
2958f68ba691SDmitry Osipenko 			return -EPROBE_DEFER;
2959f68ba691SDmitry Osipenko 
2960a31500feSThierry Reding 		parent = dev_get_drvdata(companion);
2961a31500feSThierry Reding 		dc->client.parent = &parent->client;
2962f68ba691SDmitry Osipenko 
2963a31500feSThierry Reding 		dev_dbg(dc->dev, "coupled to %s\n", dev_name(companion));
2964f68ba691SDmitry Osipenko 	}
2965f68ba691SDmitry Osipenko 
2966f68ba691SDmitry Osipenko 	return 0;
2967f68ba691SDmitry Osipenko }
2968f68ba691SDmitry Osipenko 
2969dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
2970dee8268fSThierry Reding {
297186044e74SThierry Reding 	u64 dma_mask = dma_get_mask(pdev->dev.parent);
2972dee8268fSThierry Reding 	struct tegra_dc *dc;
2973dee8268fSThierry Reding 	int err;
2974dee8268fSThierry Reding 
297586044e74SThierry Reding 	err = dma_coerce_mask_and_coherent(&pdev->dev, dma_mask);
297686044e74SThierry Reding 	if (err < 0) {
297786044e74SThierry Reding 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
297886044e74SThierry Reding 		return err;
297986044e74SThierry Reding 	}
298086044e74SThierry Reding 
2981dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2982dee8268fSThierry Reding 	if (!dc)
2983dee8268fSThierry Reding 		return -ENOMEM;
2984dee8268fSThierry Reding 
2985b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
29868620fc62SThierry Reding 
2987dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2988dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2989dee8268fSThierry Reding 
299013411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
299113411dddSThierry Reding 	if (err < 0)
299213411dddSThierry Reding 		return err;
299313411dddSThierry Reding 
2994f68ba691SDmitry Osipenko 	err = tegra_dc_couple(dc);
2995f68ba691SDmitry Osipenko 	if (err < 0)
2996f68ba691SDmitry Osipenko 		return err;
2997f68ba691SDmitry Osipenko 
2998dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2999dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
3000dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
3001dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
3002dee8268fSThierry Reding 	}
3003dee8268fSThierry Reding 
3004ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
3005ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
3006ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
3007ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
3008ca48080aSStephen Warren 	}
3009ca48080aSStephen Warren 
3010a2f2f740SThierry Reding 	/* assert reset and disable clock */
3011a2f2f740SThierry Reding 	err = clk_prepare_enable(dc->clk);
3012a2f2f740SThierry Reding 	if (err < 0)
3013a2f2f740SThierry Reding 		return err;
3014a2f2f740SThierry Reding 
3015a2f2f740SThierry Reding 	usleep_range(2000, 4000);
3016a2f2f740SThierry Reding 
3017a2f2f740SThierry Reding 	err = reset_control_assert(dc->rst);
3018a2f2f740SThierry Reding 	if (err < 0)
3019a2f2f740SThierry Reding 		return err;
3020a2f2f740SThierry Reding 
3021a2f2f740SThierry Reding 	usleep_range(2000, 4000);
3022a2f2f740SThierry Reding 
3023a2f2f740SThierry Reding 	clk_disable_unprepare(dc->clk);
302433a8eb8dSThierry Reding 
30259c012700SThierry Reding 	if (dc->soc->has_powergate) {
30269c012700SThierry Reding 		if (dc->pipe == 0)
30279c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
30289c012700SThierry Reding 		else
30299c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
30309c012700SThierry Reding 
303133a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
30329c012700SThierry Reding 	}
3033dee8268fSThierry Reding 
3034a858ac8fSDmitry Osipenko 	dc->regs = devm_platform_ioremap_resource(pdev, 0);
3035dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
3036dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
3037dee8268fSThierry Reding 
3038dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
30395f1df70fSTang Bin 	if (dc->irq < 0)
3040dee8268fSThierry Reding 		return -ENXIO;
3041dee8268fSThierry Reding 
3042dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
3043dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
30448f839fb6SDmitry Osipenko 		const char *level = KERN_ERR;
30458f839fb6SDmitry Osipenko 
30468f839fb6SDmitry Osipenko 		if (err == -EPROBE_DEFER)
30478f839fb6SDmitry Osipenko 			level = KERN_DEBUG;
30488f839fb6SDmitry Osipenko 
30498f839fb6SDmitry Osipenko 		dev_printk(level, dc->dev, "failed to probe RGB output: %d\n",
30508f839fb6SDmitry Osipenko 			   err);
3051dee8268fSThierry Reding 		return err;
3052dee8268fSThierry Reding 	}
3053dee8268fSThierry Reding 
305433a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
305533a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
305633a8eb8dSThierry Reding 
305733a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
305833a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
305933a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
306033a8eb8dSThierry Reding 
3061dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
3062dee8268fSThierry Reding 	if (err < 0) {
3063dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3064dee8268fSThierry Reding 			err);
30650411ea89SDmitry Osipenko 		goto disable_pm;
3066dee8268fSThierry Reding 	}
3067dee8268fSThierry Reding 
3068dee8268fSThierry Reding 	return 0;
30690411ea89SDmitry Osipenko 
30700411ea89SDmitry Osipenko disable_pm:
30710411ea89SDmitry Osipenko 	pm_runtime_disable(&pdev->dev);
30720411ea89SDmitry Osipenko 	tegra_dc_rgb_remove(dc);
30730411ea89SDmitry Osipenko 
30740411ea89SDmitry Osipenko 	return err;
3075dee8268fSThierry Reding }
3076dee8268fSThierry Reding 
3077dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
3078dee8268fSThierry Reding {
3079dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
3080dee8268fSThierry Reding 	int err;
3081dee8268fSThierry Reding 
3082dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
3083dee8268fSThierry Reding 	if (err < 0) {
3084dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3085dee8268fSThierry Reding 			err);
3086dee8268fSThierry Reding 		return err;
3087dee8268fSThierry Reding 	}
3088dee8268fSThierry Reding 
308959d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
309059d29c0eSThierry Reding 	if (err < 0) {
309159d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
309259d29c0eSThierry Reding 		return err;
309359d29c0eSThierry Reding 	}
309459d29c0eSThierry Reding 
309533a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
309633a8eb8dSThierry Reding 
309733a8eb8dSThierry Reding 	return 0;
309833a8eb8dSThierry Reding }
309933a8eb8dSThierry Reding 
3100dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
3101dee8268fSThierry Reding 	.driver = {
3102dee8268fSThierry Reding 		.name = "tegra-dc",
3103dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
3104dee8268fSThierry Reding 	},
3105dee8268fSThierry Reding 	.probe = tegra_dc_probe,
3106dee8268fSThierry Reding 	.remove = tegra_dc_remove,
3107dee8268fSThierry Reding };
3108