xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 474431968507d437d340de35b709aa3b41f8c996)
1dee8268fSThierry Reding /*
2dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
3dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
4dee8268fSThierry Reding  *
5dee8268fSThierry Reding  * This program is free software; you can redistribute it and/or modify
6dee8268fSThierry Reding  * it under the terms of the GNU General Public License version 2 as
7dee8268fSThierry Reding  * published by the Free Software Foundation.
8dee8268fSThierry Reding  */
9dee8268fSThierry Reding 
10dee8268fSThierry Reding #include <linux/clk.h>
11dee8268fSThierry Reding #include <linux/debugfs.h>
12df06b759SThierry Reding #include <linux/iommu.h>
13b9ff7aeaSThierry Reding #include <linux/of_device.h>
1433a8eb8dSThierry Reding #include <linux/pm_runtime.h>
15ca48080aSStephen Warren #include <linux/reset.h>
16dee8268fSThierry Reding 
179c012700SThierry Reding #include <soc/tegra/pmc.h>
189c012700SThierry Reding 
19dee8268fSThierry Reding #include "dc.h"
20dee8268fSThierry Reding #include "drm.h"
21dee8268fSThierry Reding #include "gem.h"
2247307954SThierry Reding #include "hub.h"
235acd3514SThierry Reding #include "plane.h"
24dee8268fSThierry Reding 
259d44189fSThierry Reding #include <drm/drm_atomic.h>
264aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
273cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
283cb9ae4fSDaniel Vetter 
29791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
30791ddb1eSThierry Reding {
31791ddb1eSThierry Reding 	stats->frames = 0;
32791ddb1eSThierry Reding 	stats->vblank = 0;
33791ddb1eSThierry Reding 	stats->underflow = 0;
34791ddb1eSThierry Reding 	stats->overflow = 0;
35791ddb1eSThierry Reding }
36791ddb1eSThierry Reding 
371087fac1SThierry Reding /* Reads the active copy of a register. */
3886df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
3986df256fSThierry Reding {
4086df256fSThierry Reding 	u32 value;
4186df256fSThierry Reding 
4286df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
4386df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
4486df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
4586df256fSThierry Reding 
4686df256fSThierry Reding 	return value;
4786df256fSThierry Reding }
4886df256fSThierry Reding 
491087fac1SThierry Reding static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
501087fac1SThierry Reding 					      unsigned int offset)
511087fac1SThierry Reding {
521087fac1SThierry Reding 	if (offset >= 0x500 && offset <= 0x638) {
531087fac1SThierry Reding 		offset = 0x000 + (offset - 0x500);
541087fac1SThierry Reding 		return plane->offset + offset;
551087fac1SThierry Reding 	}
561087fac1SThierry Reding 
571087fac1SThierry Reding 	if (offset >= 0x700 && offset <= 0x719) {
581087fac1SThierry Reding 		offset = 0x180 + (offset - 0x700);
591087fac1SThierry Reding 		return plane->offset + offset;
601087fac1SThierry Reding 	}
611087fac1SThierry Reding 
621087fac1SThierry Reding 	if (offset >= 0x800 && offset <= 0x839) {
631087fac1SThierry Reding 		offset = 0x1c0 + (offset - 0x800);
641087fac1SThierry Reding 		return plane->offset + offset;
651087fac1SThierry Reding 	}
661087fac1SThierry Reding 
671087fac1SThierry Reding 	dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
681087fac1SThierry Reding 
691087fac1SThierry Reding 	return plane->offset + offset;
701087fac1SThierry Reding }
711087fac1SThierry Reding 
721087fac1SThierry Reding static inline u32 tegra_plane_readl(struct tegra_plane *plane,
731087fac1SThierry Reding 				    unsigned int offset)
741087fac1SThierry Reding {
751087fac1SThierry Reding 	return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
761087fac1SThierry Reding }
771087fac1SThierry Reding 
781087fac1SThierry Reding static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
791087fac1SThierry Reding 				      unsigned int offset)
801087fac1SThierry Reding {
811087fac1SThierry Reding 	tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
821087fac1SThierry Reding }
831087fac1SThierry Reding 
84c57997bcSThierry Reding bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
85c57997bcSThierry Reding {
86c57997bcSThierry Reding 	struct device_node *np = dc->dev->of_node;
87c57997bcSThierry Reding 	struct of_phandle_iterator it;
88c57997bcSThierry Reding 	int err;
89c57997bcSThierry Reding 
90c57997bcSThierry Reding 	of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
91c57997bcSThierry Reding 		if (it.node == dev->of_node)
92c57997bcSThierry Reding 			return true;
93c57997bcSThierry Reding 
94c57997bcSThierry Reding 	return false;
95c57997bcSThierry Reding }
96c57997bcSThierry Reding 
9786df256fSThierry Reding /*
98d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
99d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
100d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
101d700ba7aSThierry Reding  * on the next frame boundary otherwise.
102d700ba7aSThierry Reding  *
103d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
104d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
105d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
106d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
107d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
108d700ba7aSThierry Reding  */
10962b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
110205d48edSThierry Reding {
111205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
112205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
113205d48edSThierry Reding }
114205d48edSThierry Reding 
11510288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
11610288eeaSThierry Reding 				  unsigned int bpp)
11710288eeaSThierry Reding {
11810288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
11910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
12010288eeaSThierry Reding 	u32 dda_inc;
12110288eeaSThierry Reding 	int max;
12210288eeaSThierry Reding 
12310288eeaSThierry Reding 	if (v)
12410288eeaSThierry Reding 		max = 15;
12510288eeaSThierry Reding 	else {
12610288eeaSThierry Reding 		switch (bpp) {
12710288eeaSThierry Reding 		case 2:
12810288eeaSThierry Reding 			max = 8;
12910288eeaSThierry Reding 			break;
13010288eeaSThierry Reding 
13110288eeaSThierry Reding 		default:
13210288eeaSThierry Reding 			WARN_ON_ONCE(1);
13310288eeaSThierry Reding 			/* fallthrough */
13410288eeaSThierry Reding 		case 4:
13510288eeaSThierry Reding 			max = 4;
13610288eeaSThierry Reding 			break;
13710288eeaSThierry Reding 		}
13810288eeaSThierry Reding 	}
13910288eeaSThierry Reding 
14010288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
14110288eeaSThierry Reding 	inf.full -= dfixed_const(1);
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
14410288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
14510288eeaSThierry Reding 
14610288eeaSThierry Reding 	return dda_inc;
14710288eeaSThierry Reding }
14810288eeaSThierry Reding 
14910288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
15010288eeaSThierry Reding {
15110288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
15210288eeaSThierry Reding 	return dfixed_frac(inf);
15310288eeaSThierry Reding }
15410288eeaSThierry Reding 
155ab7d3f58SThierry Reding static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
156ab7d3f58SThierry Reding {
157ebae8d07SThierry Reding 	u32 background[3] = {
158ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
159ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
160ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
161ebae8d07SThierry Reding 	};
162ebae8d07SThierry Reding 	u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
163ebae8d07SThierry Reding 			 BLEND_COLOR_KEY_NONE;
164ebae8d07SThierry Reding 	u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
165ebae8d07SThierry Reding 	struct tegra_plane_state *state;
1663dae08bcSDmitry Osipenko 	u32 blending[2];
167ebae8d07SThierry Reding 	unsigned int i;
168ebae8d07SThierry Reding 
1693dae08bcSDmitry Osipenko 	/* disable blending for non-overlapping case */
170ebae8d07SThierry Reding 	tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
171ebae8d07SThierry Reding 	tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
172ab7d3f58SThierry Reding 
1733dae08bcSDmitry Osipenko 	state = to_tegra_plane_state(plane->base.state);
1743dae08bcSDmitry Osipenko 
1753dae08bcSDmitry Osipenko 	if (state->opaque) {
1763dae08bcSDmitry Osipenko 		/*
1773dae08bcSDmitry Osipenko 		 * Since custom fix-weight blending isn't utilized and weight
1783dae08bcSDmitry Osipenko 		 * of top window is set to max, we can enforce dependent
1793dae08bcSDmitry Osipenko 		 * blending which in this case results in transparent bottom
1803dae08bcSDmitry Osipenko 		 * window if top window is opaque and if top window enables
1813dae08bcSDmitry Osipenko 		 * alpha blending, then bottom window is getting alpha value
1823dae08bcSDmitry Osipenko 		 * of 1 minus the sum of alpha components of the overlapping
1833dae08bcSDmitry Osipenko 		 * plane.
1843dae08bcSDmitry Osipenko 		 */
1853dae08bcSDmitry Osipenko 		background[0] |= BLEND_CONTROL_DEPENDENT;
1863dae08bcSDmitry Osipenko 		background[1] |= BLEND_CONTROL_DEPENDENT;
1873dae08bcSDmitry Osipenko 
1883dae08bcSDmitry Osipenko 		/*
1893dae08bcSDmitry Osipenko 		 * The region where three windows overlap is the intersection
1903dae08bcSDmitry Osipenko 		 * of the two regions where two windows overlap. It contributes
1913dae08bcSDmitry Osipenko 		 * to the area if all of the windows on top of it have an alpha
1923dae08bcSDmitry Osipenko 		 * component.
1933dae08bcSDmitry Osipenko 		 */
1943dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
1953dae08bcSDmitry Osipenko 		case 0:
1963dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
1973dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
1983dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
1993dae08bcSDmitry Osipenko 			break;
2003dae08bcSDmitry Osipenko 
2013dae08bcSDmitry Osipenko 		case 1:
2023dae08bcSDmitry Osipenko 			background[2] |= BLEND_CONTROL_DEPENDENT;
2033dae08bcSDmitry Osipenko 			break;
2043dae08bcSDmitry Osipenko 		}
2053dae08bcSDmitry Osipenko 	} else {
2063dae08bcSDmitry Osipenko 		/*
2073dae08bcSDmitry Osipenko 		 * Enable alpha blending if pixel format has an alpha
2083dae08bcSDmitry Osipenko 		 * component.
2093dae08bcSDmitry Osipenko 		 */
2103dae08bcSDmitry Osipenko 		foreground |= BLEND_CONTROL_ALPHA;
2113dae08bcSDmitry Osipenko 
2123dae08bcSDmitry Osipenko 		/*
2133dae08bcSDmitry Osipenko 		 * If any of the windows on top of this window is opaque, it
2143dae08bcSDmitry Osipenko 		 * will completely conceal this window within that area. If
2153dae08bcSDmitry Osipenko 		 * top window has an alpha component, it is blended over the
2163dae08bcSDmitry Osipenko 		 * bottom window.
2173dae08bcSDmitry Osipenko 		 */
2183dae08bcSDmitry Osipenko 		for (i = 0; i < 2; i++) {
2193dae08bcSDmitry Osipenko 			if (state->blending[i].alpha &&
2203dae08bcSDmitry Osipenko 			    state->blending[i].top)
2213dae08bcSDmitry Osipenko 				background[i] |= BLEND_CONTROL_DEPENDENT;
2223dae08bcSDmitry Osipenko 		}
2233dae08bcSDmitry Osipenko 
2243dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
2253dae08bcSDmitry Osipenko 		case 0:
2263dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2273dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
2283dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
2293dae08bcSDmitry Osipenko 			break;
2303dae08bcSDmitry Osipenko 
2313dae08bcSDmitry Osipenko 		case 1:
2323dae08bcSDmitry Osipenko 			/*
2333dae08bcSDmitry Osipenko 			 * When both middle and topmost windows have an alpha,
2343dae08bcSDmitry Osipenko 			 * these windows a mixed together and then the result
2353dae08bcSDmitry Osipenko 			 * is blended over the bottom window.
2363dae08bcSDmitry Osipenko 			 */
2373dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2383dae08bcSDmitry Osipenko 			    state->blending[0].top)
2393dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2403dae08bcSDmitry Osipenko 
2413dae08bcSDmitry Osipenko 			if (state->blending[1].alpha &&
2423dae08bcSDmitry Osipenko 			    state->blending[1].top)
2433dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2443dae08bcSDmitry Osipenko 			break;
2453dae08bcSDmitry Osipenko 		}
2463dae08bcSDmitry Osipenko 	}
2473dae08bcSDmitry Osipenko 
2483dae08bcSDmitry Osipenko 	switch (state->base.normalized_zpos) {
249ab7d3f58SThierry Reding 	case 0:
250ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
251ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
252ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
253ab7d3f58SThierry Reding 		break;
254ab7d3f58SThierry Reding 
255ab7d3f58SThierry Reding 	case 1:
2563dae08bcSDmitry Osipenko 		/*
2573dae08bcSDmitry Osipenko 		 * If window B / C is topmost, then X / Y registers are
2583dae08bcSDmitry Osipenko 		 * matching the order of blending[...] state indices,
2593dae08bcSDmitry Osipenko 		 * otherwise a swap is required.
2603dae08bcSDmitry Osipenko 		 */
2613dae08bcSDmitry Osipenko 		if (!state->blending[0].top && state->blending[1].top) {
2623dae08bcSDmitry Osipenko 			blending[0] = foreground;
2633dae08bcSDmitry Osipenko 			blending[1] = background[1];
2643dae08bcSDmitry Osipenko 		} else {
2653dae08bcSDmitry Osipenko 			blending[0] = background[0];
2663dae08bcSDmitry Osipenko 			blending[1] = foreground;
2673dae08bcSDmitry Osipenko 		}
2683dae08bcSDmitry Osipenko 
2693dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
2703dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
271ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
272ab7d3f58SThierry Reding 		break;
273ab7d3f58SThierry Reding 
274ab7d3f58SThierry Reding 	case 2:
275ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
276ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
277ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
278ab7d3f58SThierry Reding 		break;
279ab7d3f58SThierry Reding 	}
280ab7d3f58SThierry Reding }
281ab7d3f58SThierry Reding 
282ab7d3f58SThierry Reding static void tegra_plane_setup_blending(struct tegra_plane *plane,
283ab7d3f58SThierry Reding 				       const struct tegra_dc_window *window)
284ab7d3f58SThierry Reding {
285ab7d3f58SThierry Reding 	u32 value;
286ab7d3f58SThierry Reding 
287ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
288ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
289ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
290ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
291ab7d3f58SThierry Reding 
292ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
293ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
294ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
295ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
296ab7d3f58SThierry Reding 
297ab7d3f58SThierry Reding 	value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
298ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
299ab7d3f58SThierry Reding }
300ab7d3f58SThierry Reding 
301acc6a3a9SDmitry Osipenko static bool
302acc6a3a9SDmitry Osipenko tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
303acc6a3a9SDmitry Osipenko 				     const struct tegra_dc_window *window)
304acc6a3a9SDmitry Osipenko {
305acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
306acc6a3a9SDmitry Osipenko 
307acc6a3a9SDmitry Osipenko 	if (window->src.w == window->dst.w)
308acc6a3a9SDmitry Osipenko 		return false;
309acc6a3a9SDmitry Osipenko 
310acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
311acc6a3a9SDmitry Osipenko 		return false;
312acc6a3a9SDmitry Osipenko 
313acc6a3a9SDmitry Osipenko 	return true;
314acc6a3a9SDmitry Osipenko }
315acc6a3a9SDmitry Osipenko 
316acc6a3a9SDmitry Osipenko static bool
317acc6a3a9SDmitry Osipenko tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
318acc6a3a9SDmitry Osipenko 				   const struct tegra_dc_window *window)
319acc6a3a9SDmitry Osipenko {
320acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
321acc6a3a9SDmitry Osipenko 
322acc6a3a9SDmitry Osipenko 	if (window->src.h == window->dst.h)
323acc6a3a9SDmitry Osipenko 		return false;
324acc6a3a9SDmitry Osipenko 
325acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
326acc6a3a9SDmitry Osipenko 		return false;
327acc6a3a9SDmitry Osipenko 
328acc6a3a9SDmitry Osipenko 	if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
329acc6a3a9SDmitry Osipenko 		return false;
330acc6a3a9SDmitry Osipenko 
331acc6a3a9SDmitry Osipenko 	return true;
332acc6a3a9SDmitry Osipenko }
333acc6a3a9SDmitry Osipenko 
3341087fac1SThierry Reding static void tegra_dc_setup_window(struct tegra_plane *plane,
33510288eeaSThierry Reding 				  const struct tegra_dc_window *window)
33610288eeaSThierry Reding {
33710288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
3381087fac1SThierry Reding 	struct tegra_dc *dc = plane->dc;
33910288eeaSThierry Reding 	bool yuv, planar;
3401087fac1SThierry Reding 	u32 value;
34110288eeaSThierry Reding 
34210288eeaSThierry Reding 	/*
34310288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
34410288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
34510288eeaSThierry Reding 	 */
3465acd3514SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar);
34710288eeaSThierry Reding 	if (!yuv)
34810288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
34910288eeaSThierry Reding 	else
35010288eeaSThierry Reding 		bpp = planar ? 1 : 2;
35110288eeaSThierry Reding 
3521087fac1SThierry Reding 	tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
3531087fac1SThierry Reding 	tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
35410288eeaSThierry Reding 
35510288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
3561087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_POSITION);
35710288eeaSThierry Reding 
35810288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
3591087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_SIZE);
36010288eeaSThierry Reding 
36110288eeaSThierry Reding 	h_offset = window->src.x * bpp;
36210288eeaSThierry Reding 	v_offset = window->src.y;
36310288eeaSThierry Reding 	h_size = window->src.w * bpp;
36410288eeaSThierry Reding 	v_size = window->src.h;
36510288eeaSThierry Reding 
36610288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
3671087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
36810288eeaSThierry Reding 
36910288eeaSThierry Reding 	/*
37010288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
37110288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
37210288eeaSThierry Reding 	 */
37310288eeaSThierry Reding 	if (yuv && planar)
37410288eeaSThierry Reding 		bpp = 2;
37510288eeaSThierry Reding 
37610288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
37710288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
37810288eeaSThierry Reding 
37910288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
3801087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
38110288eeaSThierry Reding 
38210288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
38310288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
38410288eeaSThierry Reding 
3851087fac1SThierry Reding 	tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
3861087fac1SThierry Reding 	tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
38710288eeaSThierry Reding 
3881087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
3891087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
39010288eeaSThierry Reding 
3911087fac1SThierry Reding 	tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
39210288eeaSThierry Reding 
39310288eeaSThierry Reding 	if (yuv && planar) {
3941087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
3951087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
39610288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
3971087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
39810288eeaSThierry Reding 	} else {
3991087fac1SThierry Reding 		tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
40010288eeaSThierry Reding 	}
40110288eeaSThierry Reding 
40210288eeaSThierry Reding 	if (window->bottom_up)
40310288eeaSThierry Reding 		v_offset += window->src.h - 1;
40410288eeaSThierry Reding 
4051087fac1SThierry Reding 	tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
4061087fac1SThierry Reding 	tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
40710288eeaSThierry Reding 
408c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
409c134f019SThierry Reding 		unsigned long height = window->tiling.value;
410c134f019SThierry Reding 
411c134f019SThierry Reding 		switch (window->tiling.mode) {
412c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
413c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
414c134f019SThierry Reding 			break;
415c134f019SThierry Reding 
416c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
417c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
418c134f019SThierry Reding 			break;
419c134f019SThierry Reding 
420c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
421c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
422c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
423c134f019SThierry Reding 			break;
424c134f019SThierry Reding 		}
425c134f019SThierry Reding 
4261087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
42710288eeaSThierry Reding 	} else {
428c134f019SThierry Reding 		switch (window->tiling.mode) {
429c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
43010288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
43110288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
432c134f019SThierry Reding 			break;
433c134f019SThierry Reding 
434c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
435c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
436c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
437c134f019SThierry Reding 			break;
438c134f019SThierry Reding 
439c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
4404aa3df71SThierry Reding 			/*
4414aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
4424aa3df71SThierry Reding 			 * will already have filtered it out.
4434aa3df71SThierry Reding 			 */
4444aa3df71SThierry Reding 			break;
44510288eeaSThierry Reding 		}
44610288eeaSThierry Reding 
4471087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
448c134f019SThierry Reding 	}
44910288eeaSThierry Reding 
45010288eeaSThierry Reding 	value = WIN_ENABLE;
45110288eeaSThierry Reding 
45210288eeaSThierry Reding 	if (yuv) {
45310288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
4541087fac1SThierry Reding 		tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
4551087fac1SThierry Reding 		tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
4561087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
4571087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
4581087fac1SThierry Reding 		tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
4591087fac1SThierry Reding 		tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
4601087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
4611087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
46210288eeaSThierry Reding 
46310288eeaSThierry Reding 		value |= CSC_ENABLE;
46410288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
46510288eeaSThierry Reding 		value |= COLOR_EXPAND;
46610288eeaSThierry Reding 	}
46710288eeaSThierry Reding 
46810288eeaSThierry Reding 	if (window->bottom_up)
46910288eeaSThierry Reding 		value |= V_DIRECTION;
47010288eeaSThierry Reding 
471acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_horizontal_filtering(plane, window)) {
472acc6a3a9SDmitry Osipenko 		/*
473acc6a3a9SDmitry Osipenko 		 * Enable horizontal 6-tap filter and set filtering
474acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
475acc6a3a9SDmitry Osipenko 		 */
476acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
477acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
478acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
479acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
480acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
481acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
482acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
483acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
484acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
485acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
486acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
487acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
488acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
489acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
490acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
491acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
492acc6a3a9SDmitry Osipenko 
493acc6a3a9SDmitry Osipenko 		value |= H_FILTER;
494acc6a3a9SDmitry Osipenko 	}
495acc6a3a9SDmitry Osipenko 
496acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_vertical_filtering(plane, window)) {
497acc6a3a9SDmitry Osipenko 		unsigned int i, k;
498acc6a3a9SDmitry Osipenko 
499acc6a3a9SDmitry Osipenko 		/*
500acc6a3a9SDmitry Osipenko 		 * Enable vertical 2-tap filter and set filtering
501acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
502acc6a3a9SDmitry Osipenko 		 */
503acc6a3a9SDmitry Osipenko 		for (i = 0, k = 128; i < 16; i++, k -= 8)
504acc6a3a9SDmitry Osipenko 			tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
505acc6a3a9SDmitry Osipenko 
506acc6a3a9SDmitry Osipenko 		value |= V_FILTER;
507acc6a3a9SDmitry Osipenko 	}
508acc6a3a9SDmitry Osipenko 
5091087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
51010288eeaSThierry Reding 
511a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending)
512ab7d3f58SThierry Reding 		tegra_plane_setup_blending_legacy(plane);
513a43d0a00SDmitry Osipenko 	else
514a43d0a00SDmitry Osipenko 		tegra_plane_setup_blending(plane, window);
515c7679306SThierry Reding }
516c7679306SThierry Reding 
517511c7023SThierry Reding static const u32 tegra20_primary_formats[] = {
518511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
519511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
520c7679306SThierry Reding 	DRM_FORMAT_RGB565,
521511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
522511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
523511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
524ebae8d07SThierry Reding 	/* non-native formats */
525ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
526ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
527ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
528ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
529511c7023SThierry Reding };
530511c7023SThierry Reding 
531e90124cbSThierry Reding static const u64 tegra20_modifiers[] = {
532e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
533e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
534e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
535e90124cbSThierry Reding };
536e90124cbSThierry Reding 
537511c7023SThierry Reding static const u32 tegra114_primary_formats[] = {
538511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
539511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
540511c7023SThierry Reding 	DRM_FORMAT_RGB565,
541511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
542511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
543511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
544511c7023SThierry Reding 	/* new on Tegra114 */
545511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
546511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
547511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
548511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
549511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
550511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
551511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
552511c7023SThierry Reding 	DRM_FORMAT_BGR565,
553511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
554511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
555511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
556511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
557511c7023SThierry Reding };
558511c7023SThierry Reding 
559511c7023SThierry Reding static const u32 tegra124_primary_formats[] = {
560511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
561511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
562511c7023SThierry Reding 	DRM_FORMAT_RGB565,
563511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
564511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
565511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
566511c7023SThierry Reding 	/* new on Tegra114 */
567511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
568511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
569511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
570511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
571511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
572511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
573511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
574511c7023SThierry Reding 	DRM_FORMAT_BGR565,
575511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
576511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
577511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
578511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
579511c7023SThierry Reding 	/* new on Tegra124 */
580511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
581511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
582c7679306SThierry Reding };
583c7679306SThierry Reding 
584e90124cbSThierry Reding static const u64 tegra124_modifiers[] = {
585e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
586e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
587e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
588e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
589e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
590e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
591e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
592e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
593e90124cbSThierry Reding };
594e90124cbSThierry Reding 
5954aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
5964aa3df71SThierry Reding 				    struct drm_plane_state *state)
5974aa3df71SThierry Reding {
5988f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
599995c5a50SThierry Reding 	unsigned int rotation = DRM_MODE_ROTATE_0 | DRM_MODE_REFLECT_Y;
6008f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
60147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
6024aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
603c7679306SThierry Reding 	int err;
604c7679306SThierry Reding 
6054aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6064aa3df71SThierry Reding 	if (!state->crtc)
6074aa3df71SThierry Reding 		return 0;
6084aa3df71SThierry Reding 
6093dae08bcSDmitry Osipenko 	err = tegra_plane_format(state->fb->format->format,
6103dae08bcSDmitry Osipenko 				 &plane_state->format,
6118f604f8cSThierry Reding 				 &plane_state->swap);
6124aa3df71SThierry Reding 	if (err < 0)
6134aa3df71SThierry Reding 		return err;
6144aa3df71SThierry Reding 
615ebae8d07SThierry Reding 	/*
616ebae8d07SThierry Reding 	 * Tegra20 and Tegra30 are special cases here because they support
617ebae8d07SThierry Reding 	 * only variants of specific formats with an alpha component, but not
618ebae8d07SThierry Reding 	 * the corresponding opaque formats. However, the opaque formats can
619ebae8d07SThierry Reding 	 * be emulated by disabling alpha blending for the plane.
620ebae8d07SThierry Reding 	 */
621a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending) {
6223dae08bcSDmitry Osipenko 		err = tegra_plane_setup_legacy_state(tegra, plane_state);
623ebae8d07SThierry Reding 		if (err < 0)
624ebae8d07SThierry Reding 			return err;
625ebae8d07SThierry Reding 	}
626ebae8d07SThierry Reding 
6278f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
6288f604f8cSThierry Reding 	if (err < 0)
6298f604f8cSThierry Reding 		return err;
6308f604f8cSThierry Reding 
6318f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
6324aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
6334aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
6344aa3df71SThierry Reding 		return -EINVAL;
6354aa3df71SThierry Reding 	}
6364aa3df71SThierry Reding 
637995c5a50SThierry Reding 	rotation = drm_rotation_simplify(state->rotation, rotation);
638995c5a50SThierry Reding 
639995c5a50SThierry Reding 	if (rotation & DRM_MODE_REFLECT_Y)
640995c5a50SThierry Reding 		plane_state->bottom_up = true;
641995c5a50SThierry Reding 	else
642995c5a50SThierry Reding 		plane_state->bottom_up = false;
643995c5a50SThierry Reding 
6444aa3df71SThierry Reding 	/*
6454aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
6464aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
6474aa3df71SThierry Reding 	 * configuration.
6484aa3df71SThierry Reding 	 */
649bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
6504aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
6514aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
6524aa3df71SThierry Reding 			return -EINVAL;
6534aa3df71SThierry Reding 		}
6544aa3df71SThierry Reding 	}
6554aa3df71SThierry Reding 
65647802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
65747802b09SThierry Reding 	if (err < 0)
65847802b09SThierry Reding 		return err;
65947802b09SThierry Reding 
6604aa3df71SThierry Reding 	return 0;
6614aa3df71SThierry Reding }
6624aa3df71SThierry Reding 
663a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
664a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
66580d3eef1SDmitry Osipenko {
666a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
66780d3eef1SDmitry Osipenko 	u32 value;
66880d3eef1SDmitry Osipenko 
669a4bfa096SThierry Reding 	/* rien ne va plus */
670a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
671a4bfa096SThierry Reding 		return;
672a4bfa096SThierry Reding 
6731087fac1SThierry Reding 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
67480d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
6751087fac1SThierry Reding 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
67680d3eef1SDmitry Osipenko }
67780d3eef1SDmitry Osipenko 
6784aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
6794aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
6804aa3df71SThierry Reding {
6818f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
6824aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
6834aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
6844aa3df71SThierry Reding 	struct tegra_dc_window window;
6854aa3df71SThierry Reding 	unsigned int i;
6864aa3df71SThierry Reding 
6874aa3df71SThierry Reding 	/* rien ne va plus */
6884aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
6894aa3df71SThierry Reding 		return;
6904aa3df71SThierry Reding 
69180d3eef1SDmitry Osipenko 	if (!plane->state->visible)
692a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
69380d3eef1SDmitry Osipenko 
694c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
6957d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
6967d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
6977d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
6987d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
6997d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
7007d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
7017d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
7027d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
703272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
704995c5a50SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb) || state->bottom_up;
705c7679306SThierry Reding 
7068f604f8cSThierry Reding 	/* copy from state */
707ab7d3f58SThierry Reding 	window.zpos = plane->state->normalized_zpos;
7088f604f8cSThierry Reding 	window.tiling = state->tiling;
7098f604f8cSThierry Reding 	window.format = state->format;
7108f604f8cSThierry Reding 	window.swap = state->swap;
711c7679306SThierry Reding 
712bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
7134aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
714c7679306SThierry Reding 
7154aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
71608ee0178SDmitry Osipenko 
71708ee0178SDmitry Osipenko 		/*
71808ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
71908ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
72008ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
72108ee0178SDmitry Osipenko 		 */
72208ee0178SDmitry Osipenko 		if (i < 2)
7234aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
724c7679306SThierry Reding 	}
725c7679306SThierry Reding 
7261087fac1SThierry Reding 	tegra_dc_setup_window(p, &window);
7274aa3df71SThierry Reding }
7284aa3df71SThierry Reding 
729a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
7304aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
7314aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
732a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
733c7679306SThierry Reding };
734c7679306SThierry Reding 
73589f65018SThierry Reding static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
736c7679306SThierry Reding {
737518e6227SThierry Reding 	/*
738518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
739518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
740518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
741518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
742518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
743518e6227SThierry Reding 	 * here.
744518e6227SThierry Reding 	 *
745518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
746518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
747518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
748518e6227SThierry Reding 	 */
74989f65018SThierry Reding 	return 1 << drm->mode_config.num_crtc;
75089f65018SThierry Reding }
75189f65018SThierry Reding 
75289f65018SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
75389f65018SThierry Reding 						    struct tegra_dc *dc)
75489f65018SThierry Reding {
75589f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
75647307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
757c7679306SThierry Reding 	struct tegra_plane *plane;
758c7679306SThierry Reding 	unsigned int num_formats;
759e90124cbSThierry Reding 	const u64 *modifiers;
760c7679306SThierry Reding 	const u32 *formats;
761c7679306SThierry Reding 	int err;
762c7679306SThierry Reding 
763c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
764c7679306SThierry Reding 	if (!plane)
765c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
766c7679306SThierry Reding 
7671087fac1SThierry Reding 	/* Always use window A as primary window */
7681087fac1SThierry Reding 	plane->offset = 0xa00;
769c4755fb9SThierry Reding 	plane->index = 0;
7701087fac1SThierry Reding 	plane->dc = dc;
7711087fac1SThierry Reding 
7721087fac1SThierry Reding 	num_formats = dc->soc->num_primary_formats;
7731087fac1SThierry Reding 	formats = dc->soc->primary_formats;
774e90124cbSThierry Reding 	modifiers = dc->soc->modifiers;
775c4755fb9SThierry Reding 
776518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
777c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
778e90124cbSThierry Reding 				       num_formats, modifiers, type, NULL);
779c7679306SThierry Reding 	if (err < 0) {
780c7679306SThierry Reding 		kfree(plane);
781c7679306SThierry Reding 		return ERR_PTR(err);
782c7679306SThierry Reding 	}
783c7679306SThierry Reding 
784a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
7853dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
786ab7d3f58SThierry Reding 
787995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
788995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
789995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
790995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
791995c5a50SThierry Reding 	if (err < 0)
792995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
793995c5a50SThierry Reding 			err);
794995c5a50SThierry Reding 
795c7679306SThierry Reding 	return &plane->base;
796c7679306SThierry Reding }
797c7679306SThierry Reding 
798c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
799c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
800c7679306SThierry Reding };
801c7679306SThierry Reding 
8024aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
8034aa3df71SThierry Reding 				     struct drm_plane_state *state)
804c7679306SThierry Reding {
80547802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
80647802b09SThierry Reding 	int err;
80747802b09SThierry Reding 
8084aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
8094aa3df71SThierry Reding 	if (!state->crtc)
8104aa3df71SThierry Reding 		return 0;
811c7679306SThierry Reding 
812c7679306SThierry Reding 	/* scaling not supported for cursor */
8134aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
8144aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
815c7679306SThierry Reding 		return -EINVAL;
816c7679306SThierry Reding 
817c7679306SThierry Reding 	/* only square cursors supported */
8184aa3df71SThierry Reding 	if (state->src_w != state->src_h)
819c7679306SThierry Reding 		return -EINVAL;
820c7679306SThierry Reding 
8214aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
8224aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
8234aa3df71SThierry Reding 		return -EINVAL;
8244aa3df71SThierry Reding 
82547802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
82647802b09SThierry Reding 	if (err < 0)
82747802b09SThierry Reding 		return err;
82847802b09SThierry Reding 
8294aa3df71SThierry Reding 	return 0;
8304aa3df71SThierry Reding }
8314aa3df71SThierry Reding 
8324aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
8334aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
8344aa3df71SThierry Reding {
8354aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
8364aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
8374aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
8384aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
8394aa3df71SThierry Reding 
8404aa3df71SThierry Reding 	/* rien ne va plus */
8414aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
8424aa3df71SThierry Reding 		return;
8434aa3df71SThierry Reding 
8444aa3df71SThierry Reding 	switch (state->crtc_w) {
845c7679306SThierry Reding 	case 32:
846c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
847c7679306SThierry Reding 		break;
848c7679306SThierry Reding 
849c7679306SThierry Reding 	case 64:
850c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
851c7679306SThierry Reding 		break;
852c7679306SThierry Reding 
853c7679306SThierry Reding 	case 128:
854c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
855c7679306SThierry Reding 		break;
856c7679306SThierry Reding 
857c7679306SThierry Reding 	case 256:
858c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
859c7679306SThierry Reding 		break;
860c7679306SThierry Reding 
861c7679306SThierry Reding 	default:
8624aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
8634aa3df71SThierry Reding 		     state->crtc_h);
8644aa3df71SThierry Reding 		return;
865c7679306SThierry Reding 	}
866c7679306SThierry Reding 
867c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
868c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
869c7679306SThierry Reding 
870c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
871c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
872c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
873c7679306SThierry Reding #endif
874c7679306SThierry Reding 
875c7679306SThierry Reding 	/* enable cursor and set blend mode */
876c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
877c7679306SThierry Reding 	value |= CURSOR_ENABLE;
878c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
879c7679306SThierry Reding 
880c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
881c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
882c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
883c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
884c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
885c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
886c7679306SThierry Reding 	value |= CURSOR_ALPHA;
887c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
888c7679306SThierry Reding 
889c7679306SThierry Reding 	/* position the cursor */
8904aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
891c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
892c7679306SThierry Reding }
893c7679306SThierry Reding 
8944aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
8954aa3df71SThierry Reding 					struct drm_plane_state *old_state)
896c7679306SThierry Reding {
8974aa3df71SThierry Reding 	struct tegra_dc *dc;
898c7679306SThierry Reding 	u32 value;
899c7679306SThierry Reding 
9004aa3df71SThierry Reding 	/* rien ne va plus */
9014aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
9024aa3df71SThierry Reding 		return;
9034aa3df71SThierry Reding 
9044aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
905c7679306SThierry Reding 
906c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
907c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
908c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
909c7679306SThierry Reding }
910c7679306SThierry Reding 
9114aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
9124aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
9134aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
9144aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
915c7679306SThierry Reding };
916c7679306SThierry Reding 
917c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
918c7679306SThierry Reding 						      struct tegra_dc *dc)
919c7679306SThierry Reding {
92089f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
921c7679306SThierry Reding 	struct tegra_plane *plane;
922c7679306SThierry Reding 	unsigned int num_formats;
923c7679306SThierry Reding 	const u32 *formats;
924c7679306SThierry Reding 	int err;
925c7679306SThierry Reding 
926c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
927c7679306SThierry Reding 	if (!plane)
928c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
929c7679306SThierry Reding 
93047802b09SThierry Reding 	/*
931a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
932a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
933a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
934a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
935a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
93647802b09SThierry Reding 	 */
93747802b09SThierry Reding 	plane->index = 6;
9381087fac1SThierry Reding 	plane->dc = dc;
93947802b09SThierry Reding 
940c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
941c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
942c7679306SThierry Reding 
94389f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
944c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
945e6fc3b68SBen Widawsky 				       num_formats, NULL,
946e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
947c7679306SThierry Reding 	if (err < 0) {
948c7679306SThierry Reding 		kfree(plane);
949c7679306SThierry Reding 		return ERR_PTR(err);
950c7679306SThierry Reding 	}
951c7679306SThierry Reding 
9524aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
9534aa3df71SThierry Reding 
954c7679306SThierry Reding 	return &plane->base;
955c7679306SThierry Reding }
956c7679306SThierry Reding 
957511c7023SThierry Reding static const u32 tegra20_overlay_formats[] = {
958511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
959511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
960dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
961511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
962511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
963511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
964ebae8d07SThierry Reding 	/* non-native formats */
965ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
966ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
967ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
968ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
969511c7023SThierry Reding 	/* planar formats */
970511c7023SThierry Reding 	DRM_FORMAT_UYVY,
971511c7023SThierry Reding 	DRM_FORMAT_YUYV,
972511c7023SThierry Reding 	DRM_FORMAT_YUV420,
973511c7023SThierry Reding 	DRM_FORMAT_YUV422,
974511c7023SThierry Reding };
975511c7023SThierry Reding 
976511c7023SThierry Reding static const u32 tegra114_overlay_formats[] = {
977511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
978511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
979511c7023SThierry Reding 	DRM_FORMAT_RGB565,
980511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
981511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
982511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
983511c7023SThierry Reding 	/* new on Tegra114 */
984511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
985511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
986511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
987511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
988511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
989511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
990511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
991511c7023SThierry Reding 	DRM_FORMAT_BGR565,
992511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
993511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
994511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
995511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
996511c7023SThierry Reding 	/* planar formats */
997511c7023SThierry Reding 	DRM_FORMAT_UYVY,
998511c7023SThierry Reding 	DRM_FORMAT_YUYV,
999511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1000511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1001511c7023SThierry Reding };
1002511c7023SThierry Reding 
1003511c7023SThierry Reding static const u32 tegra124_overlay_formats[] = {
1004511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1005511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1006511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1007511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1008511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1009511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1010511c7023SThierry Reding 	/* new on Tegra114 */
1011511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1012511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1013511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1014511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1015511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1016511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1017511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1018511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1019511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1020511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1021511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1022511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1023511c7023SThierry Reding 	/* new on Tegra124 */
1024511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
1025511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
1026511c7023SThierry Reding 	/* planar formats */
1027dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
1028f925390eSThierry Reding 	DRM_FORMAT_YUYV,
1029dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
1030dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
1031dee8268fSThierry Reding };
1032dee8268fSThierry Reding 
1033c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
1034c7679306SThierry Reding 						       struct tegra_dc *dc,
10359f446d83SDmitry Osipenko 						       unsigned int index,
10369f446d83SDmitry Osipenko 						       bool cursor)
1037dee8268fSThierry Reding {
103889f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1039dee8268fSThierry Reding 	struct tegra_plane *plane;
1040c7679306SThierry Reding 	unsigned int num_formats;
10419f446d83SDmitry Osipenko 	enum drm_plane_type type;
1042c7679306SThierry Reding 	const u32 *formats;
1043c7679306SThierry Reding 	int err;
1044dee8268fSThierry Reding 
1045f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1046dee8268fSThierry Reding 	if (!plane)
1047c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
1048dee8268fSThierry Reding 
10491087fac1SThierry Reding 	plane->offset = 0xa00 + 0x200 * index;
1050c7679306SThierry Reding 	plane->index = index;
10511087fac1SThierry Reding 	plane->dc = dc;
1052dee8268fSThierry Reding 
1053511c7023SThierry Reding 	num_formats = dc->soc->num_overlay_formats;
1054511c7023SThierry Reding 	formats = dc->soc->overlay_formats;
1055c7679306SThierry Reding 
10569f446d83SDmitry Osipenko 	if (!cursor)
10579f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_OVERLAY;
10589f446d83SDmitry Osipenko 	else
10599f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_CURSOR;
10609f446d83SDmitry Osipenko 
106189f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1062301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
10639f446d83SDmitry Osipenko 				       num_formats, NULL, type, NULL);
1064f002abc1SThierry Reding 	if (err < 0) {
1065f002abc1SThierry Reding 		kfree(plane);
1066c7679306SThierry Reding 		return ERR_PTR(err);
1067dee8268fSThierry Reding 	}
1068c7679306SThierry Reding 
1069a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
10703dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
1071ab7d3f58SThierry Reding 
1072995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
1073995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
1074995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
1075995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
1076995c5a50SThierry Reding 	if (err < 0)
1077995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
1078995c5a50SThierry Reding 			err);
1079995c5a50SThierry Reding 
1080c7679306SThierry Reding 	return &plane->base;
1081c7679306SThierry Reding }
1082c7679306SThierry Reding 
108347307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
108447307954SThierry Reding 						    struct tegra_dc *dc)
1085c7679306SThierry Reding {
108647307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
108747307954SThierry Reding 	unsigned int i, j;
108847307954SThierry Reding 
108947307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
109047307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
109147307954SThierry Reding 
109247307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
109347307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
109447307954SThierry Reding 				unsigned int index = wgrp->windows[j];
109547307954SThierry Reding 
109647307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
109747307954SThierry Reding 								  wgrp->index,
109847307954SThierry Reding 								  index);
109947307954SThierry Reding 				if (IS_ERR(plane))
110047307954SThierry Reding 					return plane;
110147307954SThierry Reding 
110247307954SThierry Reding 				/*
110347307954SThierry Reding 				 * Choose the first shared plane owned by this
110447307954SThierry Reding 				 * head as the primary plane.
110547307954SThierry Reding 				 */
110647307954SThierry Reding 				if (!primary) {
110747307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
110847307954SThierry Reding 					primary = plane;
110947307954SThierry Reding 				}
111047307954SThierry Reding 			}
111147307954SThierry Reding 		}
111247307954SThierry Reding 	}
111347307954SThierry Reding 
111447307954SThierry Reding 	return primary;
111547307954SThierry Reding }
111647307954SThierry Reding 
111747307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
111847307954SThierry Reding 					     struct tegra_dc *dc)
111947307954SThierry Reding {
11208f62142eSThierry Reding 	struct drm_plane *planes[2], *primary;
11219f446d83SDmitry Osipenko 	unsigned int planes_num;
1122c7679306SThierry Reding 	unsigned int i;
11238f62142eSThierry Reding 	int err;
1124c7679306SThierry Reding 
112547307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
112647307954SThierry Reding 	if (IS_ERR(primary))
112747307954SThierry Reding 		return primary;
112847307954SThierry Reding 
11299f446d83SDmitry Osipenko 	if (dc->soc->supports_cursor)
11309f446d83SDmitry Osipenko 		planes_num = 2;
11319f446d83SDmitry Osipenko 	else
11329f446d83SDmitry Osipenko 		planes_num = 1;
11339f446d83SDmitry Osipenko 
11349f446d83SDmitry Osipenko 	for (i = 0; i < planes_num; i++) {
11359f446d83SDmitry Osipenko 		planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
11369f446d83SDmitry Osipenko 							  false);
11378f62142eSThierry Reding 		if (IS_ERR(planes[i])) {
11388f62142eSThierry Reding 			err = PTR_ERR(planes[i]);
11398f62142eSThierry Reding 
11408f62142eSThierry Reding 			while (i--)
11418f62142eSThierry Reding 				tegra_plane_funcs.destroy(planes[i]);
11428f62142eSThierry Reding 
11438f62142eSThierry Reding 			tegra_plane_funcs.destroy(primary);
11448f62142eSThierry Reding 			return ERR_PTR(err);
114547307954SThierry Reding 		}
1146f002abc1SThierry Reding 	}
1147dee8268fSThierry Reding 
114847307954SThierry Reding 	return primary;
1149dee8268fSThierry Reding }
1150dee8268fSThierry Reding 
1151f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1152f002abc1SThierry Reding {
1153f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1154f002abc1SThierry Reding }
1155f002abc1SThierry Reding 
1156ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1157ca915b10SThierry Reding {
1158ca915b10SThierry Reding 	struct tegra_dc_state *state;
1159ca915b10SThierry Reding 
11603b59b7acSThierry Reding 	if (crtc->state)
1161ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
11623b59b7acSThierry Reding 
1163ca915b10SThierry Reding 	kfree(crtc->state);
1164ca915b10SThierry Reding 	crtc->state = NULL;
1165ca915b10SThierry Reding 
1166ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1167332bbe70SThierry Reding 	if (state) {
1168ca915b10SThierry Reding 		crtc->state = &state->base;
1169332bbe70SThierry Reding 		crtc->state->crtc = crtc;
1170332bbe70SThierry Reding 	}
117131930d4dSThierry Reding 
117231930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
1173ca915b10SThierry Reding }
1174ca915b10SThierry Reding 
1175ca915b10SThierry Reding static struct drm_crtc_state *
1176ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1177ca915b10SThierry Reding {
1178ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1179ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1180ca915b10SThierry Reding 
11813b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1182ca915b10SThierry Reding 	if (!copy)
1183ca915b10SThierry Reding 		return NULL;
1184ca915b10SThierry Reding 
11853b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
11863b59b7acSThierry Reding 	copy->clk = state->clk;
11873b59b7acSThierry Reding 	copy->pclk = state->pclk;
11883b59b7acSThierry Reding 	copy->div = state->div;
11893b59b7acSThierry Reding 	copy->planes = state->planes;
1190ca915b10SThierry Reding 
1191ca915b10SThierry Reding 	return &copy->base;
1192ca915b10SThierry Reding }
1193ca915b10SThierry Reding 
1194ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1195ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1196ca915b10SThierry Reding {
1197ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1198ca915b10SThierry Reding 	kfree(state);
1199ca915b10SThierry Reding }
1200ca915b10SThierry Reding 
1201b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1202b95800eeSThierry Reding 
1203b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1204b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1205b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1206b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1207b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1208b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1209b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1210b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1211b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1212b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1213b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1214b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1215b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1216b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1217b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1218b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1219b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1220b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1221b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1222b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1223b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1224b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1225b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1226b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1227b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1228b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1229b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1230b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1231b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1232b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1233b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1234b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1235b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1236b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1237b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1238b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1239b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1240b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1241b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1242b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1243b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1244b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1245b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1246b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1247b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1248b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1249b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1250b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1251b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1252b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1253b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1254b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1255b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1256b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1257b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1258b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1259b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1260b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1261b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1262b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1263b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1264b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1265b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1266b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1267b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1268b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1269b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1270b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1271b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1272b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1273b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1274b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1275b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1276b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1277b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1278b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1279b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1280b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1281b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1282b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1283b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1284b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1285b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1286b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1287b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1288b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1289b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1290b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1291b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1292b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1293b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1294b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1295b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1296b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1297b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1298b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1299b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1300b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1301b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1302b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1303b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1304b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1305b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1306b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1307b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1308b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1309b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1310b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1311b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1312b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1313b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1314b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1315b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1316b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1317b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1318b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1319b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1320b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1321b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1322b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1323b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1324b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1325b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1326b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1327b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1328b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1329b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1330b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1331b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1332b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1333b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1334b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1335b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1336b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1337b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1338b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1339b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1340b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1341b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1342b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1343b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1344b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1345b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1346b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1347b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1348b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1349b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1350b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1351b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1352b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1353b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1354b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1355b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1356b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1357b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1358b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1359b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1360b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1361b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1362b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1363b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1364b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1365b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1366b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1367b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1368b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1369b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1370b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1371b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1372b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1373b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1374b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1375b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1376b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1377b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1378b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1379b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1380b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1381b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1382b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1383b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1384b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1385b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1386b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1387b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1388b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1389b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1390b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1391b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1392b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1393b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1394b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1395b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1396b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1397b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1398b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1399b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1400b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1401b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1402b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1403b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1404b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1405b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1406b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1407b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1408b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1409b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1410b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1411b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1412b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1413b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1414b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1415b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1416b95800eeSThierry Reding };
1417b95800eeSThierry Reding 
1418b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1419b95800eeSThierry Reding {
1420b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1421b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1422b95800eeSThierry Reding 	unsigned int i;
1423b95800eeSThierry Reding 	int err = 0;
1424b95800eeSThierry Reding 
1425b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1426b95800eeSThierry Reding 
1427b95800eeSThierry Reding 	if (!dc->base.state->active) {
1428b95800eeSThierry Reding 		err = -EBUSY;
1429b95800eeSThierry Reding 		goto unlock;
1430b95800eeSThierry Reding 	}
1431b95800eeSThierry Reding 
1432b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1433b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1434b95800eeSThierry Reding 
1435b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1436b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1437b95800eeSThierry Reding 	}
1438b95800eeSThierry Reding 
1439b95800eeSThierry Reding unlock:
1440b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1441b95800eeSThierry Reding 	return err;
1442b95800eeSThierry Reding }
1443b95800eeSThierry Reding 
1444b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1445b95800eeSThierry Reding {
1446b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1447b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1448b95800eeSThierry Reding 	int err = 0;
1449b95800eeSThierry Reding 	u32 value;
1450b95800eeSThierry Reding 
1451b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1452b95800eeSThierry Reding 
1453b95800eeSThierry Reding 	if (!dc->base.state->active) {
1454b95800eeSThierry Reding 		err = -EBUSY;
1455b95800eeSThierry Reding 		goto unlock;
1456b95800eeSThierry Reding 	}
1457b95800eeSThierry Reding 
1458b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1459b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1460b95800eeSThierry Reding 	tegra_dc_commit(dc);
1461b95800eeSThierry Reding 
1462b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1463b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1464b95800eeSThierry Reding 
1465b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1466b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1467b95800eeSThierry Reding 
1468b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1469b95800eeSThierry Reding 
1470b95800eeSThierry Reding unlock:
1471b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1472b95800eeSThierry Reding 	return err;
1473b95800eeSThierry Reding }
1474b95800eeSThierry Reding 
1475b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1476b95800eeSThierry Reding {
1477b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1478b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1479b95800eeSThierry Reding 
1480b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1481b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1482b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1483b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1484b95800eeSThierry Reding 
1485b95800eeSThierry Reding 	return 0;
1486b95800eeSThierry Reding }
1487b95800eeSThierry Reding 
1488b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1489b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1490b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1491b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1492b95800eeSThierry Reding };
1493b95800eeSThierry Reding 
1494b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1495b95800eeSThierry Reding {
1496b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1497b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
149839f55c61SArnd Bergmann 	struct dentry *root;
1499b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1500b95800eeSThierry Reding 	int err;
1501b95800eeSThierry Reding 
150239f55c61SArnd Bergmann #ifdef CONFIG_DEBUG_FS
150339f55c61SArnd Bergmann 	root = crtc->debugfs_entry;
150439f55c61SArnd Bergmann #else
150539f55c61SArnd Bergmann 	root = NULL;
150639f55c61SArnd Bergmann #endif
150739f55c61SArnd Bergmann 
1508b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1509b95800eeSThierry Reding 				    GFP_KERNEL);
1510b95800eeSThierry Reding 	if (!dc->debugfs_files)
1511b95800eeSThierry Reding 		return -ENOMEM;
1512b95800eeSThierry Reding 
1513b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1514b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1515b95800eeSThierry Reding 
1516b95800eeSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1517b95800eeSThierry Reding 	if (err < 0)
1518b95800eeSThierry Reding 		goto free;
1519b95800eeSThierry Reding 
1520b95800eeSThierry Reding 	return 0;
1521b95800eeSThierry Reding 
1522b95800eeSThierry Reding free:
1523b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1524b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1525b95800eeSThierry Reding 
1526b95800eeSThierry Reding 	return err;
1527b95800eeSThierry Reding }
1528b95800eeSThierry Reding 
1529b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1530b95800eeSThierry Reding {
1531b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1532b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1533b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1534b95800eeSThierry Reding 
1535b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1536b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1537b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1538b95800eeSThierry Reding }
1539b95800eeSThierry Reding 
1540c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1541c49c81e2SThierry Reding {
1542c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1543c49c81e2SThierry Reding 
154447307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
154547307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1546c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1547c49c81e2SThierry Reding 
1548c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
15493abe2413SDhinakaran Pandiyan 	return (u32)drm_crtc_vblank_count(&dc->base);
1550c49c81e2SThierry Reding }
1551c49c81e2SThierry Reding 
1552c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1553c49c81e2SThierry Reding {
1554c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1555363541e8SThierry Reding 	u32 value;
1556c49c81e2SThierry Reding 
1557c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1558c49c81e2SThierry Reding 	value |= VBLANK_INT;
1559c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1560c49c81e2SThierry Reding 
1561c49c81e2SThierry Reding 	return 0;
1562c49c81e2SThierry Reding }
1563c49c81e2SThierry Reding 
1564c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1565c49c81e2SThierry Reding {
1566c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1567363541e8SThierry Reding 	u32 value;
1568c49c81e2SThierry Reding 
1569c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1570c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1571c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1572c49c81e2SThierry Reding }
1573c49c81e2SThierry Reding 
1574dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
15751503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
157674f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1577f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1578ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1579ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1580ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1581b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1582b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
158310437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
158410437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
158510437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1586dee8268fSThierry Reding };
1587dee8268fSThierry Reding 
1588dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1589dee8268fSThierry Reding 				struct drm_display_mode *mode)
1590dee8268fSThierry Reding {
15910444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
15920444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1593dee8268fSThierry Reding 	unsigned long value;
1594dee8268fSThierry Reding 
159547307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1596dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1597dee8268fSThierry Reding 
1598dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1599dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
160047307954SThierry Reding 	}
1601dee8268fSThierry Reding 
1602dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1603dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1604dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1605dee8268fSThierry Reding 
1606dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1607dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1608dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1609dee8268fSThierry Reding 
1610dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1611dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1612dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1613dee8268fSThierry Reding 
1614dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1615dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1616dee8268fSThierry Reding 
1617dee8268fSThierry Reding 	return 0;
1618dee8268fSThierry Reding }
1619dee8268fSThierry Reding 
16209d910b60SThierry Reding /**
16219d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
16229d910b60SThierry Reding  *     state
16239d910b60SThierry Reding  * @dc: display controller
16249d910b60SThierry Reding  * @crtc_state: CRTC atomic state
16259d910b60SThierry Reding  * @clk: parent clock for display controller
16269d910b60SThierry Reding  * @pclk: pixel clock
16279d910b60SThierry Reding  * @div: shift clock divider
16289d910b60SThierry Reding  *
16299d910b60SThierry Reding  * Returns:
16309d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
16319d910b60SThierry Reding  */
1632ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1633ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1634ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1635ca915b10SThierry Reding 			       unsigned int div)
1636ca915b10SThierry Reding {
1637ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1638ca915b10SThierry Reding 
1639d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1640d2982748SThierry Reding 		return -EINVAL;
1641d2982748SThierry Reding 
1642ca915b10SThierry Reding 	state->clk = clk;
1643ca915b10SThierry Reding 	state->pclk = pclk;
1644ca915b10SThierry Reding 	state->div = div;
1645ca915b10SThierry Reding 
1646ca915b10SThierry Reding 	return 0;
1647ca915b10SThierry Reding }
1648ca915b10SThierry Reding 
164976d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
165076d59ed0SThierry Reding 				  struct tegra_dc_state *state)
165176d59ed0SThierry Reding {
165276d59ed0SThierry Reding 	u32 value;
165376d59ed0SThierry Reding 	int err;
165476d59ed0SThierry Reding 
165576d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
165676d59ed0SThierry Reding 	if (err < 0)
165776d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
165876d59ed0SThierry Reding 
165976d59ed0SThierry Reding 	/*
166076d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
166176d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
166276d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
166376d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
166476d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
166576d59ed0SThierry Reding 	 * should therefore be avoided.
166676d59ed0SThierry Reding 	 */
166776d59ed0SThierry Reding 	if (state->pclk > 0) {
166876d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
166976d59ed0SThierry Reding 		if (err < 0)
167076d59ed0SThierry Reding 			dev_err(dc->dev,
167176d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
167276d59ed0SThierry Reding 				state->pclk);
167376d59ed0SThierry Reding 	}
167476d59ed0SThierry Reding 
167576d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
167676d59ed0SThierry Reding 		      state->div);
167776d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
167876d59ed0SThierry Reding 
167947307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
168076d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
168176d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
168247307954SThierry Reding 	}
168339e08affSThierry Reding 
168439e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
168539e08affSThierry Reding 	if (err < 0)
168639e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
168739e08affSThierry Reding 			dc->clk, state->pclk, err);
168876d59ed0SThierry Reding }
168976d59ed0SThierry Reding 
1690003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1691003fc848SThierry Reding {
1692003fc848SThierry Reding 	u32 value;
1693003fc848SThierry Reding 
1694003fc848SThierry Reding 	/* stop the display controller */
1695003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1696003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1697003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1698003fc848SThierry Reding 
1699003fc848SThierry Reding 	tegra_dc_commit(dc);
1700003fc848SThierry Reding }
1701003fc848SThierry Reding 
1702003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1703003fc848SThierry Reding {
1704003fc848SThierry Reding 	u32 value;
1705003fc848SThierry Reding 
1706003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1707003fc848SThierry Reding 
1708003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1709003fc848SThierry Reding }
1710003fc848SThierry Reding 
1711003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1712003fc848SThierry Reding {
1713003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1714003fc848SThierry Reding 
1715003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1716003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1717003fc848SThierry Reding 			return 0;
1718003fc848SThierry Reding 
1719003fc848SThierry Reding 		usleep_range(1000, 2000);
1720003fc848SThierry Reding 	}
1721003fc848SThierry Reding 
1722003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1723003fc848SThierry Reding 	return -ETIMEDOUT;
1724003fc848SThierry Reding }
1725003fc848SThierry Reding 
172664581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
172764581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1728003fc848SThierry Reding {
1729003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1730003fc848SThierry Reding 	u32 value;
1731003fc848SThierry Reding 
1732003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1733003fc848SThierry Reding 		tegra_dc_stop(dc);
1734003fc848SThierry Reding 
1735003fc848SThierry Reding 		/*
1736003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1737003fc848SThierry Reding 		 * in case this fails.
1738003fc848SThierry Reding 		 */
1739003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1740003fc848SThierry Reding 	}
1741003fc848SThierry Reding 
1742003fc848SThierry Reding 	/*
1743003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1744003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1745003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1746003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1747003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1748003fc848SThierry Reding 	 * to go idle.
1749003fc848SThierry Reding 	 *
1750003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1751003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1752003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1753003fc848SThierry Reding 	 *
1754003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1755003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1756003fc848SThierry Reding 	 * the RGB encoder?
1757003fc848SThierry Reding 	 */
1758003fc848SThierry Reding 	if (dc->rgb) {
1759003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1760003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1761003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1762003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1763003fc848SThierry Reding 	}
1764003fc848SThierry Reding 
1765003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1766003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
176733a8eb8dSThierry Reding 
17689d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
17699d99ab6eSThierry Reding 
17709d99ab6eSThierry Reding 	if (crtc->state->event) {
17719d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
17729d99ab6eSThierry Reding 		crtc->state->event = NULL;
17739d99ab6eSThierry Reding 	}
17749d99ab6eSThierry Reding 
17759d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
17769d99ab6eSThierry Reding 
177733a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1778003fc848SThierry Reding }
1779003fc848SThierry Reding 
17800b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
17810b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1782dee8268fSThierry Reding {
17834aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
178476d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1785dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1786dbb3f2f7SThierry Reding 	u32 value;
1787dee8268fSThierry Reding 
178833a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
178933a8eb8dSThierry Reding 
179033a8eb8dSThierry Reding 	/* initialize display controller */
179133a8eb8dSThierry Reding 	if (dc->syncpt) {
179247307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
179347307954SThierry Reding 
179447307954SThierry Reding 		if (dc->soc->has_nvdisplay)
179547307954SThierry Reding 			enable = 1 << 31;
179647307954SThierry Reding 		else
179747307954SThierry Reding 			enable = 1 << 8;
179833a8eb8dSThierry Reding 
179933a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
180033a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
180133a8eb8dSThierry Reding 
180247307954SThierry Reding 		value = enable | syncpt;
180333a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
180433a8eb8dSThierry Reding 	}
180533a8eb8dSThierry Reding 
180647307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
180747307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
180847307954SThierry Reding 			DSC_OBUF_UF_INT;
180947307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
181047307954SThierry Reding 
181147307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
181247307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
181347307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
181447307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
181547307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
181647307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
181747307954SThierry Reding 
181847307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
181947307954SThierry Reding 			FRAME_END_INT;
182047307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
182147307954SThierry Reding 
182247307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
182347307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
182447307954SThierry Reding 
182547307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
182647307954SThierry Reding 	} else {
182733a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
182833a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
182933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
183033a8eb8dSThierry Reding 
183133a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
183233a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
183333a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
183433a8eb8dSThierry Reding 
183533a8eb8dSThierry Reding 		/* initialize timer */
183633a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
183733a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
183833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
183933a8eb8dSThierry Reding 
184033a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
184133a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
184233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
184333a8eb8dSThierry Reding 
184433a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
184533a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
184633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
184733a8eb8dSThierry Reding 
184833a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
184933a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
185033a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
185147307954SThierry Reding 	}
185233a8eb8dSThierry Reding 
18537116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
18547116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
18557116e9a8SThierry Reding 	else
185633a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
185733a8eb8dSThierry Reding 
185833a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
185976d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
186076d59ed0SThierry Reding 
1861dee8268fSThierry Reding 	/* program display mode */
1862dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1863dee8268fSThierry Reding 
18648620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
18658620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
18668620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
18678620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
18688620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
18698620fc62SThierry Reding 	}
1870666cb873SThierry Reding 
1871666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1872666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1873666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1874666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1875666cb873SThierry Reding 
187647307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1877666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1878666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1879666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1880666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
188147307954SThierry Reding 	}
188247307954SThierry Reding 
188347307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
188447307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
188547307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
188647307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
188747307954SThierry Reding 	}
1888666cb873SThierry Reding 
1889666cb873SThierry Reding 	tegra_dc_commit(dc);
1890dee8268fSThierry Reding 
18918ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1892dee8268fSThierry Reding }
1893dee8268fSThierry Reding 
1894613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1895613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
18964aa3df71SThierry Reding {
18979d99ab6eSThierry Reding 	unsigned long flags;
18981503ca47SThierry Reding 
18991503ca47SThierry Reding 	if (crtc->state->event) {
19009d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
19011503ca47SThierry Reding 
19029d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
19039d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
19049d99ab6eSThierry Reding 		else
19059d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
19061503ca47SThierry Reding 
19079d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
19089d99ab6eSThierry Reding 
19091503ca47SThierry Reding 		crtc->state->event = NULL;
19101503ca47SThierry Reding 	}
19114aa3df71SThierry Reding }
19124aa3df71SThierry Reding 
1913613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1914613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
19154aa3df71SThierry Reding {
191647802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
191747802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
191847307954SThierry Reding 	u32 value;
191947802b09SThierry Reding 
192047307954SThierry Reding 	value = state->planes << 8 | GENERAL_UPDATE;
192147307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
192247307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
192347307954SThierry Reding 
192447307954SThierry Reding 	value = state->planes | GENERAL_ACT_REQ;
192547307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
192647307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
19274aa3df71SThierry Reding }
19284aa3df71SThierry Reding 
1929dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
19304aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
19314aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
19320b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
193364581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1934dee8268fSThierry Reding };
1935dee8268fSThierry Reding 
1936dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1937dee8268fSThierry Reding {
1938dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1939dee8268fSThierry Reding 	unsigned long status;
1940dee8268fSThierry Reding 
1941dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1942dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1943dee8268fSThierry Reding 
1944dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1945dee8268fSThierry Reding 		/*
1946dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1947dee8268fSThierry Reding 		*/
1948791ddb1eSThierry Reding 		dc->stats.frames++;
1949dee8268fSThierry Reding 	}
1950dee8268fSThierry Reding 
1951dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1952dee8268fSThierry Reding 		/*
1953dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1954dee8268fSThierry Reding 		*/
1955ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1956791ddb1eSThierry Reding 		dc->stats.vblank++;
1957dee8268fSThierry Reding 	}
1958dee8268fSThierry Reding 
1959dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1960dee8268fSThierry Reding 		/*
1961dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1962dee8268fSThierry Reding 		*/
1963791ddb1eSThierry Reding 		dc->stats.underflow++;
1964791ddb1eSThierry Reding 	}
1965791ddb1eSThierry Reding 
1966791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1967791ddb1eSThierry Reding 		/*
1968791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1969791ddb1eSThierry Reding 		*/
1970791ddb1eSThierry Reding 		dc->stats.overflow++;
1971dee8268fSThierry Reding 	}
1972dee8268fSThierry Reding 
197347307954SThierry Reding 	if (status & HEAD_UF_INT) {
197447307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
197547307954SThierry Reding 		dc->stats.underflow++;
197647307954SThierry Reding 	}
197747307954SThierry Reding 
1978dee8268fSThierry Reding 	return IRQ_HANDLED;
1979dee8268fSThierry Reding }
1980dee8268fSThierry Reding 
1981dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1982dee8268fSThierry Reding {
19839910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
19842bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1985dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1986d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1987c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1988c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1989dee8268fSThierry Reding 	int err;
1990dee8268fSThierry Reding 
1991759d706fSThierry Reding 	/*
1992759d706fSThierry Reding 	 * XXX do not register DCs with no window groups because we cannot
1993759d706fSThierry Reding 	 * assign a primary plane to them, which in turn will cause KMS to
1994759d706fSThierry Reding 	 * crash.
1995759d706fSThierry Reding 	 */
1996759d706fSThierry Reding 	if (dc->soc->wgrps) {
1997759d706fSThierry Reding 		bool has_wgrps = false;
1998759d706fSThierry Reding 		unsigned int i;
1999759d706fSThierry Reding 
2000759d706fSThierry Reding 		for (i = 0; i < dc->soc->num_wgrps; i++) {
2001759d706fSThierry Reding 			const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
2002759d706fSThierry Reding 
2003759d706fSThierry Reding 			if (wgrp->dc == dc->pipe && wgrp->num_windows > 0) {
2004759d706fSThierry Reding 				has_wgrps = true;
2005759d706fSThierry Reding 				break;
2006759d706fSThierry Reding 			}
2007759d706fSThierry Reding 		}
2008759d706fSThierry Reding 
2009759d706fSThierry Reding 		if (!has_wgrps)
2010759d706fSThierry Reding 			return 0;
2011759d706fSThierry Reding 	}
2012759d706fSThierry Reding 
2013617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
20142bcdcbfaSThierry Reding 	if (!dc->syncpt)
20152bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
20162bcdcbfaSThierry Reding 
20170c407de5SThierry Reding 	dc->group = host1x_client_iommu_attach(client, true);
20180c407de5SThierry Reding 	if (IS_ERR(dc->group)) {
20190c407de5SThierry Reding 		err = PTR_ERR(dc->group);
20200c407de5SThierry Reding 		dev_err(client->dev, "failed to attach to domain: %d\n", err);
2021df06b759SThierry Reding 		return err;
2022df06b759SThierry Reding 	}
2023df06b759SThierry Reding 
202447307954SThierry Reding 	if (dc->soc->wgrps)
202547307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
202647307954SThierry Reding 	else
202747307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
202847307954SThierry Reding 
2029c7679306SThierry Reding 	if (IS_ERR(primary)) {
2030c7679306SThierry Reding 		err = PTR_ERR(primary);
2031c7679306SThierry Reding 		goto cleanup;
2032c7679306SThierry Reding 	}
2033c7679306SThierry Reding 
2034c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
2035c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
2036c7679306SThierry Reding 		if (IS_ERR(cursor)) {
2037c7679306SThierry Reding 			err = PTR_ERR(cursor);
2038c7679306SThierry Reding 			goto cleanup;
2039c7679306SThierry Reding 		}
20409f446d83SDmitry Osipenko 	} else {
20419f446d83SDmitry Osipenko 		/* dedicate one overlay to mouse cursor */
20429f446d83SDmitry Osipenko 		cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
20439f446d83SDmitry Osipenko 		if (IS_ERR(cursor)) {
20449f446d83SDmitry Osipenko 			err = PTR_ERR(cursor);
20459f446d83SDmitry Osipenko 			goto cleanup;
20469f446d83SDmitry Osipenko 		}
2047c7679306SThierry Reding 	}
2048c7679306SThierry Reding 
2049c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
2050f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
2051c7679306SThierry Reding 	if (err < 0)
2052c7679306SThierry Reding 		goto cleanup;
2053c7679306SThierry Reding 
2054dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
2055dee8268fSThierry Reding 
2056d1f3e1e0SThierry Reding 	/*
2057d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
2058d1f3e1e0SThierry Reding 	 * controllers.
2059d1f3e1e0SThierry Reding 	 */
2060d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
2061d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
2062d1f3e1e0SThierry Reding 
20639910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
2064dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2065dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
2066c7679306SThierry Reding 		goto cleanup;
2067dee8268fSThierry Reding 	}
2068dee8268fSThierry Reding 
2069dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
2070dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
2071dee8268fSThierry Reding 	if (err < 0) {
2072dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
2073dee8268fSThierry Reding 			err);
2074c7679306SThierry Reding 		goto cleanup;
2075dee8268fSThierry Reding 	}
2076dee8268fSThierry Reding 
2077dee8268fSThierry Reding 	return 0;
2078c7679306SThierry Reding 
2079c7679306SThierry Reding cleanup:
208047307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
2081c7679306SThierry Reding 		drm_plane_cleanup(cursor);
2082c7679306SThierry Reding 
208347307954SThierry Reding 	if (!IS_ERR(primary))
2084c7679306SThierry Reding 		drm_plane_cleanup(primary);
2085c7679306SThierry Reding 
20860c407de5SThierry Reding 	host1x_client_iommu_detach(client, dc->group);
2087fd5ec0dcSThierry Reding 	host1x_syncpt_free(dc->syncpt);
2088fd5ec0dcSThierry Reding 
2089c7679306SThierry Reding 	return err;
2090dee8268fSThierry Reding }
2091dee8268fSThierry Reding 
2092dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
2093dee8268fSThierry Reding {
2094dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2095dee8268fSThierry Reding 	int err;
2096dee8268fSThierry Reding 
2097dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
2098dee8268fSThierry Reding 
2099dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
2100dee8268fSThierry Reding 	if (err) {
2101dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
2102dee8268fSThierry Reding 		return err;
2103dee8268fSThierry Reding 	}
2104dee8268fSThierry Reding 
21050c407de5SThierry Reding 	host1x_client_iommu_detach(client, dc->group);
21062bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
21072bcdcbfaSThierry Reding 
2108dee8268fSThierry Reding 	return 0;
2109dee8268fSThierry Reding }
2110dee8268fSThierry Reding 
2111dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
2112dee8268fSThierry Reding 	.init = tegra_dc_init,
2113dee8268fSThierry Reding 	.exit = tegra_dc_exit,
2114dee8268fSThierry Reding };
2115dee8268fSThierry Reding 
21168620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
21177116e9a8SThierry Reding 	.supports_background_color = false,
21188620fc62SThierry Reding 	.supports_interlacing = false,
2119e687651bSThierry Reding 	.supports_cursor = false,
2120c134f019SThierry Reding 	.supports_block_linear = false,
2121a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2122d1f3e1e0SThierry Reding 	.pitch_align = 8,
21239c012700SThierry Reding 	.has_powergate = false,
2124f68ba691SDmitry Osipenko 	.coupled_pm = true,
212547307954SThierry Reding 	.has_nvdisplay = false,
2126511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2127511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2128511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2129511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2130e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2131acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = true,
2132acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = true,
21338620fc62SThierry Reding };
21348620fc62SThierry Reding 
21358620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
21367116e9a8SThierry Reding 	.supports_background_color = false,
21378620fc62SThierry Reding 	.supports_interlacing = false,
2138e687651bSThierry Reding 	.supports_cursor = false,
2139c134f019SThierry Reding 	.supports_block_linear = false,
2140a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2141d1f3e1e0SThierry Reding 	.pitch_align = 8,
21429c012700SThierry Reding 	.has_powergate = false,
2143f68ba691SDmitry Osipenko 	.coupled_pm = false,
214447307954SThierry Reding 	.has_nvdisplay = false,
2145511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2146511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2147511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2148511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2149e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2150acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2151acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2152d1f3e1e0SThierry Reding };
2153d1f3e1e0SThierry Reding 
2154d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
21557116e9a8SThierry Reding 	.supports_background_color = false,
2156d1f3e1e0SThierry Reding 	.supports_interlacing = false,
2157d1f3e1e0SThierry Reding 	.supports_cursor = false,
2158d1f3e1e0SThierry Reding 	.supports_block_linear = false,
2159a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2160d1f3e1e0SThierry Reding 	.pitch_align = 64,
21619c012700SThierry Reding 	.has_powergate = true,
2162f68ba691SDmitry Osipenko 	.coupled_pm = false,
216347307954SThierry Reding 	.has_nvdisplay = false,
2164511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2165511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2166511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2167511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2168e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2169acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2170acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
21718620fc62SThierry Reding };
21728620fc62SThierry Reding 
21738620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
21747116e9a8SThierry Reding 	.supports_background_color = true,
21758620fc62SThierry Reding 	.supports_interlacing = true,
2176e687651bSThierry Reding 	.supports_cursor = true,
2177c134f019SThierry Reding 	.supports_block_linear = true,
2178a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
2179d1f3e1e0SThierry Reding 	.pitch_align = 64,
21809c012700SThierry Reding 	.has_powergate = true,
2181f68ba691SDmitry Osipenko 	.coupled_pm = false,
218247307954SThierry Reding 	.has_nvdisplay = false,
2183511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
21849a02d3afSStefan Agner 	.primary_formats = tegra124_primary_formats,
2185511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
21869a02d3afSStefan Agner 	.overlay_formats = tegra124_overlay_formats,
2187e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2188acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2189acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
21908620fc62SThierry Reding };
21918620fc62SThierry Reding 
21925b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
21937116e9a8SThierry Reding 	.supports_background_color = true,
21945b4f516fSThierry Reding 	.supports_interlacing = true,
21955b4f516fSThierry Reding 	.supports_cursor = true,
21965b4f516fSThierry Reding 	.supports_block_linear = true,
2197a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
21985b4f516fSThierry Reding 	.pitch_align = 64,
21995b4f516fSThierry Reding 	.has_powergate = true,
2200f68ba691SDmitry Osipenko 	.coupled_pm = false,
220147307954SThierry Reding 	.has_nvdisplay = false,
2202511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2203511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2204511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2205511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2206e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2207acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2208acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
220947307954SThierry Reding };
221047307954SThierry Reding 
221147307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
221247307954SThierry Reding 	{
221347307954SThierry Reding 		.index = 0,
221447307954SThierry Reding 		.dc = 0,
221547307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
221647307954SThierry Reding 		.num_windows = 1,
221747307954SThierry Reding 	}, {
221847307954SThierry Reding 		.index = 1,
221947307954SThierry Reding 		.dc = 1,
222047307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
222147307954SThierry Reding 		.num_windows = 1,
222247307954SThierry Reding 	}, {
222347307954SThierry Reding 		.index = 2,
222447307954SThierry Reding 		.dc = 1,
222547307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
222647307954SThierry Reding 		.num_windows = 1,
222747307954SThierry Reding 	}, {
222847307954SThierry Reding 		.index = 3,
222947307954SThierry Reding 		.dc = 2,
223047307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
223147307954SThierry Reding 		.num_windows = 1,
223247307954SThierry Reding 	}, {
223347307954SThierry Reding 		.index = 4,
223447307954SThierry Reding 		.dc = 2,
223547307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
223647307954SThierry Reding 		.num_windows = 1,
223747307954SThierry Reding 	}, {
223847307954SThierry Reding 		.index = 5,
223947307954SThierry Reding 		.dc = 2,
224047307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
224147307954SThierry Reding 		.num_windows = 1,
224247307954SThierry Reding 	},
224347307954SThierry Reding };
224447307954SThierry Reding 
224547307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
224647307954SThierry Reding 	.supports_background_color = true,
224747307954SThierry Reding 	.supports_interlacing = true,
224847307954SThierry Reding 	.supports_cursor = true,
224947307954SThierry Reding 	.supports_block_linear = true,
2250a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
225147307954SThierry Reding 	.pitch_align = 64,
225247307954SThierry Reding 	.has_powergate = false,
2253f68ba691SDmitry Osipenko 	.coupled_pm = false,
225447307954SThierry Reding 	.has_nvdisplay = true,
225547307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
225647307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
22575b4f516fSThierry Reding };
22585b4f516fSThierry Reding 
2259*47443196SThierry Reding static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
2260*47443196SThierry Reding 	{
2261*47443196SThierry Reding 		.index = 0,
2262*47443196SThierry Reding 		.dc = 0,
2263*47443196SThierry Reding 		.windows = (const unsigned int[]) { 0 },
2264*47443196SThierry Reding 		.num_windows = 1,
2265*47443196SThierry Reding 	}, {
2266*47443196SThierry Reding 		.index = 1,
2267*47443196SThierry Reding 		.dc = 1,
2268*47443196SThierry Reding 		.windows = (const unsigned int[]) { 1 },
2269*47443196SThierry Reding 		.num_windows = 1,
2270*47443196SThierry Reding 	}, {
2271*47443196SThierry Reding 		.index = 2,
2272*47443196SThierry Reding 		.dc = 1,
2273*47443196SThierry Reding 		.windows = (const unsigned int[]) { 2 },
2274*47443196SThierry Reding 		.num_windows = 1,
2275*47443196SThierry Reding 	}, {
2276*47443196SThierry Reding 		.index = 3,
2277*47443196SThierry Reding 		.dc = 2,
2278*47443196SThierry Reding 		.windows = (const unsigned int[]) { 3 },
2279*47443196SThierry Reding 		.num_windows = 1,
2280*47443196SThierry Reding 	}, {
2281*47443196SThierry Reding 		.index = 4,
2282*47443196SThierry Reding 		.dc = 2,
2283*47443196SThierry Reding 		.windows = (const unsigned int[]) { 4 },
2284*47443196SThierry Reding 		.num_windows = 1,
2285*47443196SThierry Reding 	}, {
2286*47443196SThierry Reding 		.index = 5,
2287*47443196SThierry Reding 		.dc = 2,
2288*47443196SThierry Reding 		.windows = (const unsigned int[]) { 5 },
2289*47443196SThierry Reding 		.num_windows = 1,
2290*47443196SThierry Reding 	},
2291*47443196SThierry Reding };
2292*47443196SThierry Reding 
2293*47443196SThierry Reding static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
2294*47443196SThierry Reding 	.supports_background_color = true,
2295*47443196SThierry Reding 	.supports_interlacing = true,
2296*47443196SThierry Reding 	.supports_cursor = true,
2297*47443196SThierry Reding 	.supports_block_linear = true,
2298*47443196SThierry Reding 	.has_legacy_blending = false,
2299*47443196SThierry Reding 	.pitch_align = 64,
2300*47443196SThierry Reding 	.has_powergate = false,
2301*47443196SThierry Reding 	.coupled_pm = false,
2302*47443196SThierry Reding 	.has_nvdisplay = true,
2303*47443196SThierry Reding 	.wgrps = tegra194_dc_wgrps,
2304*47443196SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
2305*47443196SThierry Reding };
2306*47443196SThierry Reding 
23078620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
23088620fc62SThierry Reding 	{
2309*47443196SThierry Reding 		.compatible = "nvidia,tegra194-dc",
2310*47443196SThierry Reding 		.data = &tegra194_dc_soc_info,
2311*47443196SThierry Reding 	}, {
231247307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
231347307954SThierry Reding 		.data = &tegra186_dc_soc_info,
231447307954SThierry Reding 	}, {
23155b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
23165b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
23175b4f516fSThierry Reding 	}, {
23188620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
23198620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
23208620fc62SThierry Reding 	}, {
23219c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
23229c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
23239c012700SThierry Reding 	}, {
23248620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
23258620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
23268620fc62SThierry Reding 	}, {
23278620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
23288620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
23298620fc62SThierry Reding 	}, {
23308620fc62SThierry Reding 		/* sentinel */
23318620fc62SThierry Reding 	}
23328620fc62SThierry Reding };
2333ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
23348620fc62SThierry Reding 
233513411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
233613411dddSThierry Reding {
233713411dddSThierry Reding 	struct device_node *np;
233813411dddSThierry Reding 	u32 value = 0;
233913411dddSThierry Reding 	int err;
234013411dddSThierry Reding 
234113411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
234213411dddSThierry Reding 	if (err < 0) {
234313411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
234413411dddSThierry Reding 
234513411dddSThierry Reding 		/*
234613411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
234713411dddSThierry Reding 		 * correct head number by looking up the position of this
234813411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
234913411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
235013411dddSThierry Reding 		 * that the translation into a flattened device tree blob
235113411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
235213411dddSThierry Reding 		 * head number.
235313411dddSThierry Reding 		 *
235413411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
235513411dddSThierry Reding 		 * cases where only a single display controller is used.
235613411dddSThierry Reding 		 */
235713411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
2358cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
2359cf6b1744SJulia Lawall 				of_node_put(np);
236013411dddSThierry Reding 				break;
2361cf6b1744SJulia Lawall 			}
236213411dddSThierry Reding 
236313411dddSThierry Reding 			value++;
236413411dddSThierry Reding 		}
236513411dddSThierry Reding 	}
236613411dddSThierry Reding 
236713411dddSThierry Reding 	dc->pipe = value;
236813411dddSThierry Reding 
236913411dddSThierry Reding 	return 0;
237013411dddSThierry Reding }
237113411dddSThierry Reding 
2372f68ba691SDmitry Osipenko static int tegra_dc_match_by_pipe(struct device *dev, void *data)
2373f68ba691SDmitry Osipenko {
2374f68ba691SDmitry Osipenko 	struct tegra_dc *dc = dev_get_drvdata(dev);
2375f68ba691SDmitry Osipenko 	unsigned int pipe = (unsigned long)data;
2376f68ba691SDmitry Osipenko 
2377f68ba691SDmitry Osipenko 	return dc->pipe == pipe;
2378f68ba691SDmitry Osipenko }
2379f68ba691SDmitry Osipenko 
2380f68ba691SDmitry Osipenko static int tegra_dc_couple(struct tegra_dc *dc)
2381f68ba691SDmitry Osipenko {
2382f68ba691SDmitry Osipenko 	/*
2383f68ba691SDmitry Osipenko 	 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2384f68ba691SDmitry Osipenko 	 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2385f68ba691SDmitry Osipenko 	 * POWER_CONTROL registers during CRTC enabling.
2386f68ba691SDmitry Osipenko 	 */
2387f68ba691SDmitry Osipenko 	if (dc->soc->coupled_pm && dc->pipe == 1) {
2388e88728f4SVivek Gautam 		u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
2389f68ba691SDmitry Osipenko 		struct device_link *link;
2390f68ba691SDmitry Osipenko 		struct device *partner;
2391f68ba691SDmitry Osipenko 
2392ef1b204aSWei Yongjun 		partner = driver_find_device(dc->dev->driver, NULL, NULL,
2393f68ba691SDmitry Osipenko 					     tegra_dc_match_by_pipe);
2394f68ba691SDmitry Osipenko 		if (!partner)
2395f68ba691SDmitry Osipenko 			return -EPROBE_DEFER;
2396f68ba691SDmitry Osipenko 
2397f68ba691SDmitry Osipenko 		link = device_link_add(dc->dev, partner, flags);
2398f68ba691SDmitry Osipenko 		if (!link) {
2399f68ba691SDmitry Osipenko 			dev_err(dc->dev, "failed to link controllers\n");
2400f68ba691SDmitry Osipenko 			return -EINVAL;
2401f68ba691SDmitry Osipenko 		}
2402f68ba691SDmitry Osipenko 
2403f68ba691SDmitry Osipenko 		dev_dbg(dc->dev, "coupled to %s\n", dev_name(partner));
2404f68ba691SDmitry Osipenko 	}
2405f68ba691SDmitry Osipenko 
2406f68ba691SDmitry Osipenko 	return 0;
2407f68ba691SDmitry Osipenko }
2408f68ba691SDmitry Osipenko 
2409dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
2410dee8268fSThierry Reding {
2411dee8268fSThierry Reding 	struct resource *regs;
2412dee8268fSThierry Reding 	struct tegra_dc *dc;
2413dee8268fSThierry Reding 	int err;
2414dee8268fSThierry Reding 
2415dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2416dee8268fSThierry Reding 	if (!dc)
2417dee8268fSThierry Reding 		return -ENOMEM;
2418dee8268fSThierry Reding 
2419b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
24208620fc62SThierry Reding 
2421dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2422dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2423dee8268fSThierry Reding 
242413411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
242513411dddSThierry Reding 	if (err < 0)
242613411dddSThierry Reding 		return err;
242713411dddSThierry Reding 
2428f68ba691SDmitry Osipenko 	err = tegra_dc_couple(dc);
2429f68ba691SDmitry Osipenko 	if (err < 0)
2430f68ba691SDmitry Osipenko 		return err;
2431f68ba691SDmitry Osipenko 
2432dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2433dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
2434dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
2435dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
2436dee8268fSThierry Reding 	}
2437dee8268fSThierry Reding 
2438ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2439ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2440ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2441ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2442ca48080aSStephen Warren 	}
2443ca48080aSStephen Warren 
2444a2f2f740SThierry Reding 	/* assert reset and disable clock */
2445a2f2f740SThierry Reding 	err = clk_prepare_enable(dc->clk);
2446a2f2f740SThierry Reding 	if (err < 0)
2447a2f2f740SThierry Reding 		return err;
2448a2f2f740SThierry Reding 
2449a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2450a2f2f740SThierry Reding 
2451a2f2f740SThierry Reding 	err = reset_control_assert(dc->rst);
2452a2f2f740SThierry Reding 	if (err < 0)
2453a2f2f740SThierry Reding 		return err;
2454a2f2f740SThierry Reding 
2455a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2456a2f2f740SThierry Reding 
2457a2f2f740SThierry Reding 	clk_disable_unprepare(dc->clk);
245833a8eb8dSThierry Reding 
24599c012700SThierry Reding 	if (dc->soc->has_powergate) {
24609c012700SThierry Reding 		if (dc->pipe == 0)
24619c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
24629c012700SThierry Reding 		else
24639c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
24649c012700SThierry Reding 
246533a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
24669c012700SThierry Reding 	}
2467dee8268fSThierry Reding 
2468dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2469dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2470dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2471dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2472dee8268fSThierry Reding 
2473dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2474dee8268fSThierry Reding 	if (dc->irq < 0) {
2475dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2476dee8268fSThierry Reding 		return -ENXIO;
2477dee8268fSThierry Reding 	}
2478dee8268fSThierry Reding 
2479dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2480dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2481dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2482dee8268fSThierry Reding 		return err;
2483dee8268fSThierry Reding 	}
2484dee8268fSThierry Reding 
248533a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
248633a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
248733a8eb8dSThierry Reding 
248833a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
248933a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
249033a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
249133a8eb8dSThierry Reding 
2492dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2493dee8268fSThierry Reding 	if (err < 0) {
2494dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2495dee8268fSThierry Reding 			err);
2496dee8268fSThierry Reding 		return err;
2497dee8268fSThierry Reding 	}
2498dee8268fSThierry Reding 
2499dee8268fSThierry Reding 	return 0;
2500dee8268fSThierry Reding }
2501dee8268fSThierry Reding 
2502dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2503dee8268fSThierry Reding {
2504dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2505dee8268fSThierry Reding 	int err;
2506dee8268fSThierry Reding 
2507dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2508dee8268fSThierry Reding 	if (err < 0) {
2509dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2510dee8268fSThierry Reding 			err);
2511dee8268fSThierry Reding 		return err;
2512dee8268fSThierry Reding 	}
2513dee8268fSThierry Reding 
251459d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
251559d29c0eSThierry Reding 	if (err < 0) {
251659d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
251759d29c0eSThierry Reding 		return err;
251859d29c0eSThierry Reding 	}
251959d29c0eSThierry Reding 
252033a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
252133a8eb8dSThierry Reding 
252233a8eb8dSThierry Reding 	return 0;
252333a8eb8dSThierry Reding }
252433a8eb8dSThierry Reding 
252533a8eb8dSThierry Reding #ifdef CONFIG_PM
252633a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
252733a8eb8dSThierry Reding {
252833a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
252933a8eb8dSThierry Reding 	int err;
253033a8eb8dSThierry Reding 
253133a8eb8dSThierry Reding 	err = reset_control_assert(dc->rst);
253233a8eb8dSThierry Reding 	if (err < 0) {
253333a8eb8dSThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
253433a8eb8dSThierry Reding 		return err;
253533a8eb8dSThierry Reding 	}
25369c012700SThierry Reding 
25379c012700SThierry Reding 	if (dc->soc->has_powergate)
25389c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
25399c012700SThierry Reding 
2540dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2541dee8268fSThierry Reding 
2542dee8268fSThierry Reding 	return 0;
2543dee8268fSThierry Reding }
2544dee8268fSThierry Reding 
254533a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
254633a8eb8dSThierry Reding {
254733a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
254833a8eb8dSThierry Reding 	int err;
254933a8eb8dSThierry Reding 
255033a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
255133a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
255233a8eb8dSThierry Reding 							dc->rst);
255333a8eb8dSThierry Reding 		if (err < 0) {
255433a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
255533a8eb8dSThierry Reding 			return err;
255633a8eb8dSThierry Reding 		}
255733a8eb8dSThierry Reding 	} else {
255833a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
255933a8eb8dSThierry Reding 		if (err < 0) {
256033a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
256133a8eb8dSThierry Reding 			return err;
256233a8eb8dSThierry Reding 		}
256333a8eb8dSThierry Reding 
256433a8eb8dSThierry Reding 		err = reset_control_deassert(dc->rst);
256533a8eb8dSThierry Reding 		if (err < 0) {
2566f68ba691SDmitry Osipenko 			dev_err(dev, "failed to deassert reset: %d\n", err);
256733a8eb8dSThierry Reding 			return err;
256833a8eb8dSThierry Reding 		}
256933a8eb8dSThierry Reding 	}
257033a8eb8dSThierry Reding 
257133a8eb8dSThierry Reding 	return 0;
257233a8eb8dSThierry Reding }
257333a8eb8dSThierry Reding #endif
257433a8eb8dSThierry Reding 
257533a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
257633a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
257733a8eb8dSThierry Reding };
257833a8eb8dSThierry Reding 
2579dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2580dee8268fSThierry Reding 	.driver = {
2581dee8268fSThierry Reding 		.name = "tegra-dc",
2582dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
258333a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2584dee8268fSThierry Reding 	},
2585dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2586dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2587dee8268fSThierry Reding };
2588