xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision df561f6688fef775baa341a0f5d960becd248b11)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dee8268fSThierry Reding /*
3dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
4dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
5dee8268fSThierry Reding  */
6dee8268fSThierry Reding 
7dee8268fSThierry Reding #include <linux/clk.h>
8dee8268fSThierry Reding #include <linux/debugfs.h>
9eb1df694SSam Ravnborg #include <linux/delay.h>
10df06b759SThierry Reding #include <linux/iommu.h>
11eb1df694SSam Ravnborg #include <linux/module.h>
12b9ff7aeaSThierry Reding #include <linux/of_device.h>
1333a8eb8dSThierry Reding #include <linux/pm_runtime.h>
14ca48080aSStephen Warren #include <linux/reset.h>
15dee8268fSThierry Reding 
169c012700SThierry Reding #include <soc/tegra/pmc.h>
179c012700SThierry Reding 
18eb1df694SSam Ravnborg #include <drm/drm_atomic.h>
19eb1df694SSam Ravnborg #include <drm/drm_atomic_helper.h>
20eb1df694SSam Ravnborg #include <drm/drm_debugfs.h>
21eb1df694SSam Ravnborg #include <drm/drm_fourcc.h>
22eb1df694SSam Ravnborg #include <drm/drm_plane_helper.h>
23eb1df694SSam Ravnborg #include <drm/drm_vblank.h>
24eb1df694SSam Ravnborg 
25dee8268fSThierry Reding #include "dc.h"
26dee8268fSThierry Reding #include "drm.h"
27dee8268fSThierry Reding #include "gem.h"
2847307954SThierry Reding #include "hub.h"
295acd3514SThierry Reding #include "plane.h"
30dee8268fSThierry Reding 
31b7e0b04aSMaarten Lankhorst static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
32b7e0b04aSMaarten Lankhorst 					    struct drm_crtc_state *state);
33b7e0b04aSMaarten Lankhorst 
34791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
35791ddb1eSThierry Reding {
36791ddb1eSThierry Reding 	stats->frames = 0;
37791ddb1eSThierry Reding 	stats->vblank = 0;
38791ddb1eSThierry Reding 	stats->underflow = 0;
39791ddb1eSThierry Reding 	stats->overflow = 0;
40791ddb1eSThierry Reding }
41791ddb1eSThierry Reding 
421087fac1SThierry Reding /* Reads the active copy of a register. */
4386df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
4486df256fSThierry Reding {
4586df256fSThierry Reding 	u32 value;
4686df256fSThierry Reding 
4786df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
4886df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
4986df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
5086df256fSThierry Reding 
5186df256fSThierry Reding 	return value;
5286df256fSThierry Reding }
5386df256fSThierry Reding 
541087fac1SThierry Reding static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
551087fac1SThierry Reding 					      unsigned int offset)
561087fac1SThierry Reding {
571087fac1SThierry Reding 	if (offset >= 0x500 && offset <= 0x638) {
581087fac1SThierry Reding 		offset = 0x000 + (offset - 0x500);
591087fac1SThierry Reding 		return plane->offset + offset;
601087fac1SThierry Reding 	}
611087fac1SThierry Reding 
621087fac1SThierry Reding 	if (offset >= 0x700 && offset <= 0x719) {
631087fac1SThierry Reding 		offset = 0x180 + (offset - 0x700);
641087fac1SThierry Reding 		return plane->offset + offset;
651087fac1SThierry Reding 	}
661087fac1SThierry Reding 
671087fac1SThierry Reding 	if (offset >= 0x800 && offset <= 0x839) {
681087fac1SThierry Reding 		offset = 0x1c0 + (offset - 0x800);
691087fac1SThierry Reding 		return plane->offset + offset;
701087fac1SThierry Reding 	}
711087fac1SThierry Reding 
721087fac1SThierry Reding 	dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
731087fac1SThierry Reding 
741087fac1SThierry Reding 	return plane->offset + offset;
751087fac1SThierry Reding }
761087fac1SThierry Reding 
771087fac1SThierry Reding static inline u32 tegra_plane_readl(struct tegra_plane *plane,
781087fac1SThierry Reding 				    unsigned int offset)
791087fac1SThierry Reding {
801087fac1SThierry Reding 	return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
811087fac1SThierry Reding }
821087fac1SThierry Reding 
831087fac1SThierry Reding static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
841087fac1SThierry Reding 				      unsigned int offset)
851087fac1SThierry Reding {
861087fac1SThierry Reding 	tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
871087fac1SThierry Reding }
881087fac1SThierry Reding 
89c57997bcSThierry Reding bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
90c57997bcSThierry Reding {
91c57997bcSThierry Reding 	struct device_node *np = dc->dev->of_node;
92c57997bcSThierry Reding 	struct of_phandle_iterator it;
93c57997bcSThierry Reding 	int err;
94c57997bcSThierry Reding 
95c57997bcSThierry Reding 	of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
96c57997bcSThierry Reding 		if (it.node == dev->of_node)
97c57997bcSThierry Reding 			return true;
98c57997bcSThierry Reding 
99c57997bcSThierry Reding 	return false;
100c57997bcSThierry Reding }
101c57997bcSThierry Reding 
10286df256fSThierry Reding /*
103d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
104d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
105d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
106d700ba7aSThierry Reding  * on the next frame boundary otherwise.
107d700ba7aSThierry Reding  *
108d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
109d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
110d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
111d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
112d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
113d700ba7aSThierry Reding  */
11462b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
115205d48edSThierry Reding {
116205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
117205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
118205d48edSThierry Reding }
119205d48edSThierry Reding 
12010288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
12110288eeaSThierry Reding 				  unsigned int bpp)
12210288eeaSThierry Reding {
12310288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
12410288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
12510288eeaSThierry Reding 	u32 dda_inc;
12610288eeaSThierry Reding 	int max;
12710288eeaSThierry Reding 
12810288eeaSThierry Reding 	if (v)
12910288eeaSThierry Reding 		max = 15;
13010288eeaSThierry Reding 	else {
13110288eeaSThierry Reding 		switch (bpp) {
13210288eeaSThierry Reding 		case 2:
13310288eeaSThierry Reding 			max = 8;
13410288eeaSThierry Reding 			break;
13510288eeaSThierry Reding 
13610288eeaSThierry Reding 		default:
13710288eeaSThierry Reding 			WARN_ON_ONCE(1);
138*df561f66SGustavo A. R. Silva 			fallthrough;
13910288eeaSThierry Reding 		case 4:
14010288eeaSThierry Reding 			max = 4;
14110288eeaSThierry Reding 			break;
14210288eeaSThierry Reding 		}
14310288eeaSThierry Reding 	}
14410288eeaSThierry Reding 
14510288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
14610288eeaSThierry Reding 	inf.full -= dfixed_const(1);
14710288eeaSThierry Reding 
14810288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
14910288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
15010288eeaSThierry Reding 
15110288eeaSThierry Reding 	return dda_inc;
15210288eeaSThierry Reding }
15310288eeaSThierry Reding 
15410288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
15510288eeaSThierry Reding {
15610288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
15710288eeaSThierry Reding 	return dfixed_frac(inf);
15810288eeaSThierry Reding }
15910288eeaSThierry Reding 
160ab7d3f58SThierry Reding static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
161ab7d3f58SThierry Reding {
162ebae8d07SThierry Reding 	u32 background[3] = {
163ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
164ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
165ebae8d07SThierry Reding 		BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
166ebae8d07SThierry Reding 	};
167ebae8d07SThierry Reding 	u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
168ebae8d07SThierry Reding 			 BLEND_COLOR_KEY_NONE;
169ebae8d07SThierry Reding 	u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
170ebae8d07SThierry Reding 	struct tegra_plane_state *state;
1713dae08bcSDmitry Osipenko 	u32 blending[2];
172ebae8d07SThierry Reding 	unsigned int i;
173ebae8d07SThierry Reding 
1743dae08bcSDmitry Osipenko 	/* disable blending for non-overlapping case */
175ebae8d07SThierry Reding 	tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
176ebae8d07SThierry Reding 	tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
177ab7d3f58SThierry Reding 
1783dae08bcSDmitry Osipenko 	state = to_tegra_plane_state(plane->base.state);
1793dae08bcSDmitry Osipenko 
1803dae08bcSDmitry Osipenko 	if (state->opaque) {
1813dae08bcSDmitry Osipenko 		/*
1823dae08bcSDmitry Osipenko 		 * Since custom fix-weight blending isn't utilized and weight
1833dae08bcSDmitry Osipenko 		 * of top window is set to max, we can enforce dependent
1843dae08bcSDmitry Osipenko 		 * blending which in this case results in transparent bottom
1853dae08bcSDmitry Osipenko 		 * window if top window is opaque and if top window enables
1863dae08bcSDmitry Osipenko 		 * alpha blending, then bottom window is getting alpha value
1873dae08bcSDmitry Osipenko 		 * of 1 minus the sum of alpha components of the overlapping
1883dae08bcSDmitry Osipenko 		 * plane.
1893dae08bcSDmitry Osipenko 		 */
1903dae08bcSDmitry Osipenko 		background[0] |= BLEND_CONTROL_DEPENDENT;
1913dae08bcSDmitry Osipenko 		background[1] |= BLEND_CONTROL_DEPENDENT;
1923dae08bcSDmitry Osipenko 
1933dae08bcSDmitry Osipenko 		/*
1943dae08bcSDmitry Osipenko 		 * The region where three windows overlap is the intersection
1953dae08bcSDmitry Osipenko 		 * of the two regions where two windows overlap. It contributes
1963dae08bcSDmitry Osipenko 		 * to the area if all of the windows on top of it have an alpha
1973dae08bcSDmitry Osipenko 		 * component.
1983dae08bcSDmitry Osipenko 		 */
1993dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
2003dae08bcSDmitry Osipenko 		case 0:
2013dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2023dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
2033dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
2043dae08bcSDmitry Osipenko 			break;
2053dae08bcSDmitry Osipenko 
2063dae08bcSDmitry Osipenko 		case 1:
2073dae08bcSDmitry Osipenko 			background[2] |= BLEND_CONTROL_DEPENDENT;
2083dae08bcSDmitry Osipenko 			break;
2093dae08bcSDmitry Osipenko 		}
2103dae08bcSDmitry Osipenko 	} else {
2113dae08bcSDmitry Osipenko 		/*
2123dae08bcSDmitry Osipenko 		 * Enable alpha blending if pixel format has an alpha
2133dae08bcSDmitry Osipenko 		 * component.
2143dae08bcSDmitry Osipenko 		 */
2153dae08bcSDmitry Osipenko 		foreground |= BLEND_CONTROL_ALPHA;
2163dae08bcSDmitry Osipenko 
2173dae08bcSDmitry Osipenko 		/*
2183dae08bcSDmitry Osipenko 		 * If any of the windows on top of this window is opaque, it
2193dae08bcSDmitry Osipenko 		 * will completely conceal this window within that area. If
2203dae08bcSDmitry Osipenko 		 * top window has an alpha component, it is blended over the
2213dae08bcSDmitry Osipenko 		 * bottom window.
2223dae08bcSDmitry Osipenko 		 */
2233dae08bcSDmitry Osipenko 		for (i = 0; i < 2; i++) {
2243dae08bcSDmitry Osipenko 			if (state->blending[i].alpha &&
2253dae08bcSDmitry Osipenko 			    state->blending[i].top)
2263dae08bcSDmitry Osipenko 				background[i] |= BLEND_CONTROL_DEPENDENT;
2273dae08bcSDmitry Osipenko 		}
2283dae08bcSDmitry Osipenko 
2293dae08bcSDmitry Osipenko 		switch (state->base.normalized_zpos) {
2303dae08bcSDmitry Osipenko 		case 0:
2313dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2323dae08bcSDmitry Osipenko 			    state->blending[1].alpha)
2333dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_DEPENDENT;
2343dae08bcSDmitry Osipenko 			break;
2353dae08bcSDmitry Osipenko 
2363dae08bcSDmitry Osipenko 		case 1:
2373dae08bcSDmitry Osipenko 			/*
2383dae08bcSDmitry Osipenko 			 * When both middle and topmost windows have an alpha,
2393dae08bcSDmitry Osipenko 			 * these windows a mixed together and then the result
2403dae08bcSDmitry Osipenko 			 * is blended over the bottom window.
2413dae08bcSDmitry Osipenko 			 */
2423dae08bcSDmitry Osipenko 			if (state->blending[0].alpha &&
2433dae08bcSDmitry Osipenko 			    state->blending[0].top)
2443dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2453dae08bcSDmitry Osipenko 
2463dae08bcSDmitry Osipenko 			if (state->blending[1].alpha &&
2473dae08bcSDmitry Osipenko 			    state->blending[1].top)
2483dae08bcSDmitry Osipenko 				background[2] |= BLEND_CONTROL_ALPHA;
2493dae08bcSDmitry Osipenko 			break;
2503dae08bcSDmitry Osipenko 		}
2513dae08bcSDmitry Osipenko 	}
2523dae08bcSDmitry Osipenko 
2533dae08bcSDmitry Osipenko 	switch (state->base.normalized_zpos) {
254ab7d3f58SThierry Reding 	case 0:
255ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
256ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
257ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
258ab7d3f58SThierry Reding 		break;
259ab7d3f58SThierry Reding 
260ab7d3f58SThierry Reding 	case 1:
2613dae08bcSDmitry Osipenko 		/*
2623dae08bcSDmitry Osipenko 		 * If window B / C is topmost, then X / Y registers are
2633dae08bcSDmitry Osipenko 		 * matching the order of blending[...] state indices,
2643dae08bcSDmitry Osipenko 		 * otherwise a swap is required.
2653dae08bcSDmitry Osipenko 		 */
2663dae08bcSDmitry Osipenko 		if (!state->blending[0].top && state->blending[1].top) {
2673dae08bcSDmitry Osipenko 			blending[0] = foreground;
2683dae08bcSDmitry Osipenko 			blending[1] = background[1];
2693dae08bcSDmitry Osipenko 		} else {
2703dae08bcSDmitry Osipenko 			blending[0] = background[0];
2713dae08bcSDmitry Osipenko 			blending[1] = foreground;
2723dae08bcSDmitry Osipenko 		}
2733dae08bcSDmitry Osipenko 
2743dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
2753dae08bcSDmitry Osipenko 		tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
276ebae8d07SThierry Reding 		tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
277ab7d3f58SThierry Reding 		break;
278ab7d3f58SThierry Reding 
279ab7d3f58SThierry Reding 	case 2:
280ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
281ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
282ebae8d07SThierry Reding 		tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
283ab7d3f58SThierry Reding 		break;
284ab7d3f58SThierry Reding 	}
285ab7d3f58SThierry Reding }
286ab7d3f58SThierry Reding 
287ab7d3f58SThierry Reding static void tegra_plane_setup_blending(struct tegra_plane *plane,
288ab7d3f58SThierry Reding 				       const struct tegra_dc_window *window)
289ab7d3f58SThierry Reding {
290ab7d3f58SThierry Reding 	u32 value;
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_MATCH_SELECT);
296ab7d3f58SThierry Reding 
297ab7d3f58SThierry Reding 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
298ab7d3f58SThierry Reding 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
299ab7d3f58SThierry Reding 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
300ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
301ab7d3f58SThierry Reding 
302ab7d3f58SThierry Reding 	value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
303ab7d3f58SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
304ab7d3f58SThierry Reding }
305ab7d3f58SThierry Reding 
306acc6a3a9SDmitry Osipenko static bool
307acc6a3a9SDmitry Osipenko tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
308acc6a3a9SDmitry Osipenko 				     const struct tegra_dc_window *window)
309acc6a3a9SDmitry Osipenko {
310acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
311acc6a3a9SDmitry Osipenko 
312acc6a3a9SDmitry Osipenko 	if (window->src.w == window->dst.w)
313acc6a3a9SDmitry Osipenko 		return false;
314acc6a3a9SDmitry Osipenko 
315acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
316acc6a3a9SDmitry Osipenko 		return false;
317acc6a3a9SDmitry Osipenko 
318acc6a3a9SDmitry Osipenko 	return true;
319acc6a3a9SDmitry Osipenko }
320acc6a3a9SDmitry Osipenko 
321acc6a3a9SDmitry Osipenko static bool
322acc6a3a9SDmitry Osipenko tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
323acc6a3a9SDmitry Osipenko 				   const struct tegra_dc_window *window)
324acc6a3a9SDmitry Osipenko {
325acc6a3a9SDmitry Osipenko 	struct tegra_dc *dc = plane->dc;
326acc6a3a9SDmitry Osipenko 
327acc6a3a9SDmitry Osipenko 	if (window->src.h == window->dst.h)
328acc6a3a9SDmitry Osipenko 		return false;
329acc6a3a9SDmitry Osipenko 
330acc6a3a9SDmitry Osipenko 	if (plane->index == 0 && dc->soc->has_win_a_without_filters)
331acc6a3a9SDmitry Osipenko 		return false;
332acc6a3a9SDmitry Osipenko 
333acc6a3a9SDmitry Osipenko 	if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
334acc6a3a9SDmitry Osipenko 		return false;
335acc6a3a9SDmitry Osipenko 
336acc6a3a9SDmitry Osipenko 	return true;
337acc6a3a9SDmitry Osipenko }
338acc6a3a9SDmitry Osipenko 
3391087fac1SThierry Reding static void tegra_dc_setup_window(struct tegra_plane *plane,
34010288eeaSThierry Reding 				  const struct tegra_dc_window *window)
34110288eeaSThierry Reding {
34210288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
3431087fac1SThierry Reding 	struct tegra_dc *dc = plane->dc;
34410288eeaSThierry Reding 	bool yuv, planar;
3451087fac1SThierry Reding 	u32 value;
34610288eeaSThierry Reding 
34710288eeaSThierry Reding 	/*
34810288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
34910288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
35010288eeaSThierry Reding 	 */
3515acd3514SThierry Reding 	yuv = tegra_plane_format_is_yuv(window->format, &planar);
35210288eeaSThierry Reding 	if (!yuv)
35310288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
35410288eeaSThierry Reding 	else
35510288eeaSThierry Reding 		bpp = planar ? 1 : 2;
35610288eeaSThierry Reding 
3571087fac1SThierry Reding 	tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
3581087fac1SThierry Reding 	tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
35910288eeaSThierry Reding 
36010288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
3611087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_POSITION);
36210288eeaSThierry Reding 
36310288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
3641087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_SIZE);
36510288eeaSThierry Reding 
36610288eeaSThierry Reding 	h_offset = window->src.x * bpp;
36710288eeaSThierry Reding 	v_offset = window->src.y;
36810288eeaSThierry Reding 	h_size = window->src.w * bpp;
36910288eeaSThierry Reding 	v_size = window->src.h;
37010288eeaSThierry Reding 
371cd740777SDmitry Osipenko 	if (window->reflect_x)
372cd740777SDmitry Osipenko 		h_offset += (window->src.w - 1) * bpp;
373cd740777SDmitry Osipenko 
374cd740777SDmitry Osipenko 	if (window->reflect_y)
375cd740777SDmitry Osipenko 		v_offset += window->src.h - 1;
376cd740777SDmitry Osipenko 
37710288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
3781087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
37910288eeaSThierry Reding 
38010288eeaSThierry Reding 	/*
38110288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
38210288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
38310288eeaSThierry Reding 	 */
38410288eeaSThierry Reding 	if (yuv && planar)
38510288eeaSThierry Reding 		bpp = 2;
38610288eeaSThierry Reding 
38710288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
38810288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
38910288eeaSThierry Reding 
39010288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
3911087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
39210288eeaSThierry Reding 
39310288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
39410288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
39510288eeaSThierry Reding 
3961087fac1SThierry Reding 	tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
3971087fac1SThierry Reding 	tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
39810288eeaSThierry Reding 
3991087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
4001087fac1SThierry Reding 	tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
40110288eeaSThierry Reding 
4021087fac1SThierry Reding 	tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
40310288eeaSThierry Reding 
40410288eeaSThierry Reding 	if (yuv && planar) {
4051087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
4061087fac1SThierry Reding 		tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
40710288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
4081087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
40910288eeaSThierry Reding 	} else {
4101087fac1SThierry Reding 		tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
41110288eeaSThierry Reding 	}
41210288eeaSThierry Reding 
4131087fac1SThierry Reding 	tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
4141087fac1SThierry Reding 	tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
41510288eeaSThierry Reding 
416c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
417c134f019SThierry Reding 		unsigned long height = window->tiling.value;
418c134f019SThierry Reding 
419c134f019SThierry Reding 		switch (window->tiling.mode) {
420c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
421c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
422c134f019SThierry Reding 			break;
423c134f019SThierry Reding 
424c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
425c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
426c134f019SThierry Reding 			break;
427c134f019SThierry Reding 
428c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
429c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
430c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
431c134f019SThierry Reding 			break;
432c134f019SThierry Reding 		}
433c134f019SThierry Reding 
4341087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
43510288eeaSThierry Reding 	} else {
436c134f019SThierry Reding 		switch (window->tiling.mode) {
437c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
43810288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
43910288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
440c134f019SThierry Reding 			break;
441c134f019SThierry Reding 
442c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
443c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
444c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
445c134f019SThierry Reding 			break;
446c134f019SThierry Reding 
447c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
4484aa3df71SThierry Reding 			/*
4494aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
4504aa3df71SThierry Reding 			 * will already have filtered it out.
4514aa3df71SThierry Reding 			 */
4524aa3df71SThierry Reding 			break;
45310288eeaSThierry Reding 		}
45410288eeaSThierry Reding 
4551087fac1SThierry Reding 		tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
456c134f019SThierry Reding 	}
45710288eeaSThierry Reding 
45810288eeaSThierry Reding 	value = WIN_ENABLE;
45910288eeaSThierry Reding 
46010288eeaSThierry Reding 	if (yuv) {
46110288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
4621087fac1SThierry Reding 		tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
4631087fac1SThierry Reding 		tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
4641087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
4651087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
4661087fac1SThierry Reding 		tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
4671087fac1SThierry Reding 		tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
4681087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
4691087fac1SThierry Reding 		tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
47010288eeaSThierry Reding 
47110288eeaSThierry Reding 		value |= CSC_ENABLE;
47210288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
47310288eeaSThierry Reding 		value |= COLOR_EXPAND;
47410288eeaSThierry Reding 	}
47510288eeaSThierry Reding 
476cd740777SDmitry Osipenko 	if (window->reflect_x)
477cd740777SDmitry Osipenko 		value |= H_DIRECTION;
478cd740777SDmitry Osipenko 
479e9e476f7SDmitry Osipenko 	if (window->reflect_y)
48010288eeaSThierry Reding 		value |= V_DIRECTION;
48110288eeaSThierry Reding 
482acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_horizontal_filtering(plane, window)) {
483acc6a3a9SDmitry Osipenko 		/*
484acc6a3a9SDmitry Osipenko 		 * Enable horizontal 6-tap filter and set filtering
485acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
486acc6a3a9SDmitry Osipenko 		 */
487acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
488acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
489acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
490acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
491acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
492acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
493acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
494acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
495acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
496acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
497acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
498acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
499acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
500acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
501acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
502acc6a3a9SDmitry Osipenko 		tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
503acc6a3a9SDmitry Osipenko 
504acc6a3a9SDmitry Osipenko 		value |= H_FILTER;
505acc6a3a9SDmitry Osipenko 	}
506acc6a3a9SDmitry Osipenko 
507acc6a3a9SDmitry Osipenko 	if (tegra_plane_use_vertical_filtering(plane, window)) {
508acc6a3a9SDmitry Osipenko 		unsigned int i, k;
509acc6a3a9SDmitry Osipenko 
510acc6a3a9SDmitry Osipenko 		/*
511acc6a3a9SDmitry Osipenko 		 * Enable vertical 2-tap filter and set filtering
512acc6a3a9SDmitry Osipenko 		 * coefficients to the default values defined in TRM.
513acc6a3a9SDmitry Osipenko 		 */
514acc6a3a9SDmitry Osipenko 		for (i = 0, k = 128; i < 16; i++, k -= 8)
515acc6a3a9SDmitry Osipenko 			tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
516acc6a3a9SDmitry Osipenko 
517acc6a3a9SDmitry Osipenko 		value |= V_FILTER;
518acc6a3a9SDmitry Osipenko 	}
519acc6a3a9SDmitry Osipenko 
5201087fac1SThierry Reding 	tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
52110288eeaSThierry Reding 
522a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending)
523ab7d3f58SThierry Reding 		tegra_plane_setup_blending_legacy(plane);
524a43d0a00SDmitry Osipenko 	else
525a43d0a00SDmitry Osipenko 		tegra_plane_setup_blending(plane, window);
526c7679306SThierry Reding }
527c7679306SThierry Reding 
528511c7023SThierry Reding static const u32 tegra20_primary_formats[] = {
529511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
530511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
531c7679306SThierry Reding 	DRM_FORMAT_RGB565,
532511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
533511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
534511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
535ebae8d07SThierry Reding 	/* non-native formats */
536ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
537ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
538ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
539ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
540511c7023SThierry Reding };
541511c7023SThierry Reding 
542e90124cbSThierry Reding static const u64 tegra20_modifiers[] = {
543e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
544e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
545e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
546e90124cbSThierry Reding };
547e90124cbSThierry Reding 
548511c7023SThierry Reding static const u32 tegra114_primary_formats[] = {
549511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
550511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
551511c7023SThierry Reding 	DRM_FORMAT_RGB565,
552511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
553511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
554511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
555511c7023SThierry Reding 	/* new on Tegra114 */
556511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
557511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
558511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
559511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
560511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
561511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
562511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
563511c7023SThierry Reding 	DRM_FORMAT_BGR565,
564511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
565511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
566511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
567511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
568511c7023SThierry Reding };
569511c7023SThierry Reding 
570511c7023SThierry Reding static const u32 tegra124_primary_formats[] = {
571511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
572511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
573511c7023SThierry Reding 	DRM_FORMAT_RGB565,
574511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
575511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
576511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
577511c7023SThierry Reding 	/* new on Tegra114 */
578511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
579511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
580511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
581511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
582511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
583511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
584511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
585511c7023SThierry Reding 	DRM_FORMAT_BGR565,
586511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
587511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
588511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
589511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
590511c7023SThierry Reding 	/* new on Tegra124 */
591511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
592511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
593c7679306SThierry Reding };
594c7679306SThierry Reding 
595e90124cbSThierry Reding static const u64 tegra124_modifiers[] = {
596e90124cbSThierry Reding 	DRM_FORMAT_MOD_LINEAR,
597e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
598e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
599e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
600e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
601e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
602e90124cbSThierry Reding 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
603e90124cbSThierry Reding 	DRM_FORMAT_MOD_INVALID
604e90124cbSThierry Reding };
605e90124cbSThierry Reding 
6064aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
6074aa3df71SThierry Reding 				    struct drm_plane_state *state)
6084aa3df71SThierry Reding {
6098f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
610cd740777SDmitry Osipenko 	unsigned int supported_rotation = DRM_MODE_ROTATE_0 |
611cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_X |
612cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_Y;
613cd740777SDmitry Osipenko 	unsigned int rotation = state->rotation;
6148f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
61547802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
6164aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
617c7679306SThierry Reding 	int err;
618c7679306SThierry Reding 
6194aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6204aa3df71SThierry Reding 	if (!state->crtc)
6214aa3df71SThierry Reding 		return 0;
6224aa3df71SThierry Reding 
6233dae08bcSDmitry Osipenko 	err = tegra_plane_format(state->fb->format->format,
6243dae08bcSDmitry Osipenko 				 &plane_state->format,
6258f604f8cSThierry Reding 				 &plane_state->swap);
6264aa3df71SThierry Reding 	if (err < 0)
6274aa3df71SThierry Reding 		return err;
6284aa3df71SThierry Reding 
629ebae8d07SThierry Reding 	/*
630ebae8d07SThierry Reding 	 * Tegra20 and Tegra30 are special cases here because they support
631ebae8d07SThierry Reding 	 * only variants of specific formats with an alpha component, but not
632ebae8d07SThierry Reding 	 * the corresponding opaque formats. However, the opaque formats can
633ebae8d07SThierry Reding 	 * be emulated by disabling alpha blending for the plane.
634ebae8d07SThierry Reding 	 */
635a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending) {
6363dae08bcSDmitry Osipenko 		err = tegra_plane_setup_legacy_state(tegra, plane_state);
637ebae8d07SThierry Reding 		if (err < 0)
638ebae8d07SThierry Reding 			return err;
639ebae8d07SThierry Reding 	}
640ebae8d07SThierry Reding 
6418f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
6428f604f8cSThierry Reding 	if (err < 0)
6438f604f8cSThierry Reding 		return err;
6448f604f8cSThierry Reding 
6458f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
6464aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
6474aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
6484aa3df71SThierry Reding 		return -EINVAL;
6494aa3df71SThierry Reding 	}
6504aa3df71SThierry Reding 
651cd740777SDmitry Osipenko 	/*
652cd740777SDmitry Osipenko 	 * Older userspace used custom BO flag in order to specify the Y
653cd740777SDmitry Osipenko 	 * reflection, while modern userspace uses the generic DRM rotation
654cd740777SDmitry Osipenko 	 * property in order to achieve the same result.  The legacy BO flag
655cd740777SDmitry Osipenko 	 * duplicates the DRM rotation property when both are set.
656cd740777SDmitry Osipenko 	 */
657cd740777SDmitry Osipenko 	if (tegra_fb_is_bottom_up(state->fb))
658cd740777SDmitry Osipenko 		rotation |= DRM_MODE_REFLECT_Y;
659cd740777SDmitry Osipenko 
660cd740777SDmitry Osipenko 	rotation = drm_rotation_simplify(rotation, supported_rotation);
661cd740777SDmitry Osipenko 
662cd740777SDmitry Osipenko 	if (rotation & DRM_MODE_REFLECT_X)
663cd740777SDmitry Osipenko 		plane_state->reflect_x = true;
664cd740777SDmitry Osipenko 	else
665cd740777SDmitry Osipenko 		plane_state->reflect_x = false;
666995c5a50SThierry Reding 
667995c5a50SThierry Reding 	if (rotation & DRM_MODE_REFLECT_Y)
668e9e476f7SDmitry Osipenko 		plane_state->reflect_y = true;
669995c5a50SThierry Reding 	else
670e9e476f7SDmitry Osipenko 		plane_state->reflect_y = false;
671995c5a50SThierry Reding 
6724aa3df71SThierry Reding 	/*
6734aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
6744aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
6754aa3df71SThierry Reding 	 * configuration.
6764aa3df71SThierry Reding 	 */
677bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
6784aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
6794aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
6804aa3df71SThierry Reding 			return -EINVAL;
6814aa3df71SThierry Reding 		}
6824aa3df71SThierry Reding 	}
6834aa3df71SThierry Reding 
68447802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
68547802b09SThierry Reding 	if (err < 0)
68647802b09SThierry Reding 		return err;
68747802b09SThierry Reding 
6884aa3df71SThierry Reding 	return 0;
6894aa3df71SThierry Reding }
6904aa3df71SThierry Reding 
691a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
692a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
69380d3eef1SDmitry Osipenko {
694a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
69580d3eef1SDmitry Osipenko 	u32 value;
69680d3eef1SDmitry Osipenko 
697a4bfa096SThierry Reding 	/* rien ne va plus */
698a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
699a4bfa096SThierry Reding 		return;
700a4bfa096SThierry Reding 
7011087fac1SThierry Reding 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
70280d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
7031087fac1SThierry Reding 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
70480d3eef1SDmitry Osipenko }
70580d3eef1SDmitry Osipenko 
7064aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
7074aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
7084aa3df71SThierry Reding {
7098f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
7104aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
7114aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
7124aa3df71SThierry Reding 	struct tegra_dc_window window;
7134aa3df71SThierry Reding 	unsigned int i;
7144aa3df71SThierry Reding 
7154aa3df71SThierry Reding 	/* rien ne va plus */
7164aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
7174aa3df71SThierry Reding 		return;
7184aa3df71SThierry Reding 
71980d3eef1SDmitry Osipenko 	if (!plane->state->visible)
720a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
72180d3eef1SDmitry Osipenko 
722c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
7237d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
7247d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
7257d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
7267d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
7277d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
7287d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
7297d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
7307d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
731272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
732cd740777SDmitry Osipenko 	window.reflect_x = state->reflect_x;
733cd740777SDmitry Osipenko 	window.reflect_y = state->reflect_y;
734c7679306SThierry Reding 
7358f604f8cSThierry Reding 	/* copy from state */
736ab7d3f58SThierry Reding 	window.zpos = plane->state->normalized_zpos;
7378f604f8cSThierry Reding 	window.tiling = state->tiling;
7388f604f8cSThierry Reding 	window.format = state->format;
7398f604f8cSThierry Reding 	window.swap = state->swap;
740c7679306SThierry Reding 
741bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
7422e8d8749SThierry Reding 		window.base[i] = state->iova[i] + fb->offsets[i];
74308ee0178SDmitry Osipenko 
74408ee0178SDmitry Osipenko 		/*
74508ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
74608ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
74708ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
74808ee0178SDmitry Osipenko 		 */
74908ee0178SDmitry Osipenko 		if (i < 2)
7504aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
751c7679306SThierry Reding 	}
752c7679306SThierry Reding 
7531087fac1SThierry Reding 	tegra_dc_setup_window(p, &window);
7544aa3df71SThierry Reding }
7554aa3df71SThierry Reding 
756a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
7572e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
7582e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
7594aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
7604aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
761a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
762c7679306SThierry Reding };
763c7679306SThierry Reding 
76489f65018SThierry Reding static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
765c7679306SThierry Reding {
766518e6227SThierry Reding 	/*
767518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
768518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
769518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
770518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
771518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
772518e6227SThierry Reding 	 * here.
773518e6227SThierry Reding 	 *
774518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
775518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
776518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
777518e6227SThierry Reding 	 */
77889f65018SThierry Reding 	return 1 << drm->mode_config.num_crtc;
77989f65018SThierry Reding }
78089f65018SThierry Reding 
78189f65018SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
78289f65018SThierry Reding 						    struct tegra_dc *dc)
78389f65018SThierry Reding {
78489f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
78547307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
786c7679306SThierry Reding 	struct tegra_plane *plane;
787c7679306SThierry Reding 	unsigned int num_formats;
788e90124cbSThierry Reding 	const u64 *modifiers;
789c7679306SThierry Reding 	const u32 *formats;
790c7679306SThierry Reding 	int err;
791c7679306SThierry Reding 
792c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
793c7679306SThierry Reding 	if (!plane)
794c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
795c7679306SThierry Reding 
7961087fac1SThierry Reding 	/* Always use window A as primary window */
7971087fac1SThierry Reding 	plane->offset = 0xa00;
798c4755fb9SThierry Reding 	plane->index = 0;
7991087fac1SThierry Reding 	plane->dc = dc;
8001087fac1SThierry Reding 
8011087fac1SThierry Reding 	num_formats = dc->soc->num_primary_formats;
8021087fac1SThierry Reding 	formats = dc->soc->primary_formats;
803e90124cbSThierry Reding 	modifiers = dc->soc->modifiers;
804c4755fb9SThierry Reding 
805518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
806c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
807e90124cbSThierry Reding 				       num_formats, modifiers, type, NULL);
808c7679306SThierry Reding 	if (err < 0) {
809c7679306SThierry Reding 		kfree(plane);
810c7679306SThierry Reding 		return ERR_PTR(err);
811c7679306SThierry Reding 	}
812c7679306SThierry Reding 
813a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
8143dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
815ab7d3f58SThierry Reding 
816995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
817995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
818995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
8194fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
820cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
821995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
822995c5a50SThierry Reding 	if (err < 0)
823995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
824995c5a50SThierry Reding 			err);
825995c5a50SThierry Reding 
826c7679306SThierry Reding 	return &plane->base;
827c7679306SThierry Reding }
828c7679306SThierry Reding 
829c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
830c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
831c7679306SThierry Reding };
832c7679306SThierry Reding 
8334aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
8344aa3df71SThierry Reding 				     struct drm_plane_state *state)
835c7679306SThierry Reding {
83647802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
83747802b09SThierry Reding 	int err;
83847802b09SThierry Reding 
8394aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
8404aa3df71SThierry Reding 	if (!state->crtc)
8414aa3df71SThierry Reding 		return 0;
842c7679306SThierry Reding 
843c7679306SThierry Reding 	/* scaling not supported for cursor */
8444aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
8454aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
846c7679306SThierry Reding 		return -EINVAL;
847c7679306SThierry Reding 
848c7679306SThierry Reding 	/* only square cursors supported */
8494aa3df71SThierry Reding 	if (state->src_w != state->src_h)
850c7679306SThierry Reding 		return -EINVAL;
851c7679306SThierry Reding 
8524aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
8534aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
8544aa3df71SThierry Reding 		return -EINVAL;
8554aa3df71SThierry Reding 
85647802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
85747802b09SThierry Reding 	if (err < 0)
85847802b09SThierry Reding 		return err;
85947802b09SThierry Reding 
8604aa3df71SThierry Reding 	return 0;
8614aa3df71SThierry Reding }
8624aa3df71SThierry Reding 
8634aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
8644aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
8654aa3df71SThierry Reding {
866c52e167bSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
8674aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
8684aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
8694aa3df71SThierry Reding 
8704aa3df71SThierry Reding 	/* rien ne va plus */
8714aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
8724aa3df71SThierry Reding 		return;
8734aa3df71SThierry Reding 
874c52e167bSThierry Reding 	switch (plane->state->crtc_w) {
875c7679306SThierry Reding 	case 32:
876c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
877c7679306SThierry Reding 		break;
878c7679306SThierry Reding 
879c7679306SThierry Reding 	case 64:
880c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
881c7679306SThierry Reding 		break;
882c7679306SThierry Reding 
883c7679306SThierry Reding 	case 128:
884c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
885c7679306SThierry Reding 		break;
886c7679306SThierry Reding 
887c7679306SThierry Reding 	case 256:
888c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
889c7679306SThierry Reding 		break;
890c7679306SThierry Reding 
891c7679306SThierry Reding 	default:
892c52e167bSThierry Reding 		WARN(1, "cursor size %ux%u not supported\n",
893c52e167bSThierry Reding 		     plane->state->crtc_w, plane->state->crtc_h);
8944aa3df71SThierry Reding 		return;
895c7679306SThierry Reding 	}
896c7679306SThierry Reding 
897c52e167bSThierry Reding 	value |= (state->iova[0] >> 10) & 0x3fffff;
898c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
899c7679306SThierry Reding 
900c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
901c52e167bSThierry Reding 	value = (state->iova[0] >> 32) & 0x3;
902c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
903c7679306SThierry Reding #endif
904c7679306SThierry Reding 
905c7679306SThierry Reding 	/* enable cursor and set blend mode */
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 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
911c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
912c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
913c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
914c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
915c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
916c7679306SThierry Reding 	value |= CURSOR_ALPHA;
917c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
918c7679306SThierry Reding 
919c7679306SThierry Reding 	/* position the cursor */
920c52e167bSThierry Reding 	value = (plane->state->crtc_y & 0x3fff) << 16 |
921c52e167bSThierry Reding 		(plane->state->crtc_x & 0x3fff);
922c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
923c7679306SThierry Reding }
924c7679306SThierry Reding 
9254aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
9264aa3df71SThierry Reding 					struct drm_plane_state *old_state)
927c7679306SThierry Reding {
9284aa3df71SThierry Reding 	struct tegra_dc *dc;
929c7679306SThierry Reding 	u32 value;
930c7679306SThierry Reding 
9314aa3df71SThierry Reding 	/* rien ne va plus */
9324aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
9334aa3df71SThierry Reding 		return;
9344aa3df71SThierry Reding 
9354aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
936c7679306SThierry Reding 
937c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
938c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
939c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
940c7679306SThierry Reding }
941c7679306SThierry Reding 
9424aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
9432e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
9442e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
9454aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
9464aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
9474aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
948c7679306SThierry Reding };
949c7679306SThierry Reding 
950c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
951c7679306SThierry Reding 						      struct tegra_dc *dc)
952c7679306SThierry Reding {
95389f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
954c7679306SThierry Reding 	struct tegra_plane *plane;
955c7679306SThierry Reding 	unsigned int num_formats;
956c7679306SThierry Reding 	const u32 *formats;
957c7679306SThierry Reding 	int err;
958c7679306SThierry Reding 
959c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
960c7679306SThierry Reding 	if (!plane)
961c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
962c7679306SThierry Reding 
96347802b09SThierry Reding 	/*
964a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
965a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
966a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
967a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
968a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
96947802b09SThierry Reding 	 */
97047802b09SThierry Reding 	plane->index = 6;
9711087fac1SThierry Reding 	plane->dc = dc;
97247802b09SThierry Reding 
973c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
974c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
975c7679306SThierry Reding 
97689f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
977c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
978e6fc3b68SBen Widawsky 				       num_formats, NULL,
979e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
980c7679306SThierry Reding 	if (err < 0) {
981c7679306SThierry Reding 		kfree(plane);
982c7679306SThierry Reding 		return ERR_PTR(err);
983c7679306SThierry Reding 	}
984c7679306SThierry Reding 
9854aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
986fce3a51dSThierry Reding 	drm_plane_create_zpos_immutable_property(&plane->base, 255);
9874aa3df71SThierry Reding 
988c7679306SThierry Reding 	return &plane->base;
989c7679306SThierry Reding }
990c7679306SThierry Reding 
991511c7023SThierry Reding static const u32 tegra20_overlay_formats[] = {
992511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
993511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
994dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
995511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
996511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
997511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
998ebae8d07SThierry Reding 	/* non-native formats */
999ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
1000ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
1001ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
1002ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
1003511c7023SThierry Reding 	/* planar formats */
1004511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1005511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1006511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1007511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1008511c7023SThierry Reding };
1009511c7023SThierry Reding 
1010511c7023SThierry Reding static const u32 tegra114_overlay_formats[] = {
1011511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1012511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1013511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1014511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1015511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1016511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1017511c7023SThierry Reding 	/* new on Tegra114 */
1018511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1019511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1020511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1021511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1022511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1023511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1024511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1025511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1026511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1027511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1028511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1029511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1030511c7023SThierry Reding 	/* planar formats */
1031511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1032511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1033511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1034511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1035511c7023SThierry Reding };
1036511c7023SThierry Reding 
1037511c7023SThierry Reding static const u32 tegra124_overlay_formats[] = {
1038511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1039511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1040511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1041511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1042511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1043511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1044511c7023SThierry Reding 	/* new on Tegra114 */
1045511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1046511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1047511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1048511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1049511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1050511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1051511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1052511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1053511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1054511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1055511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1056511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1057511c7023SThierry Reding 	/* new on Tegra124 */
1058511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
1059511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
1060511c7023SThierry Reding 	/* planar formats */
1061dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
1062f925390eSThierry Reding 	DRM_FORMAT_YUYV,
1063dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
1064dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
1065dee8268fSThierry Reding };
1066dee8268fSThierry Reding 
1067c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
1068c7679306SThierry Reding 						       struct tegra_dc *dc,
10699f446d83SDmitry Osipenko 						       unsigned int index,
10709f446d83SDmitry Osipenko 						       bool cursor)
1071dee8268fSThierry Reding {
107289f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1073dee8268fSThierry Reding 	struct tegra_plane *plane;
1074c7679306SThierry Reding 	unsigned int num_formats;
10759f446d83SDmitry Osipenko 	enum drm_plane_type type;
1076c7679306SThierry Reding 	const u32 *formats;
1077c7679306SThierry Reding 	int err;
1078dee8268fSThierry Reding 
1079f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1080dee8268fSThierry Reding 	if (!plane)
1081c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
1082dee8268fSThierry Reding 
10831087fac1SThierry Reding 	plane->offset = 0xa00 + 0x200 * index;
1084c7679306SThierry Reding 	plane->index = index;
10851087fac1SThierry Reding 	plane->dc = dc;
1086dee8268fSThierry Reding 
1087511c7023SThierry Reding 	num_formats = dc->soc->num_overlay_formats;
1088511c7023SThierry Reding 	formats = dc->soc->overlay_formats;
1089c7679306SThierry Reding 
10909f446d83SDmitry Osipenko 	if (!cursor)
10919f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_OVERLAY;
10929f446d83SDmitry Osipenko 	else
10939f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_CURSOR;
10949f446d83SDmitry Osipenko 
109589f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1096301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
10979f446d83SDmitry Osipenko 				       num_formats, NULL, type, NULL);
1098f002abc1SThierry Reding 	if (err < 0) {
1099f002abc1SThierry Reding 		kfree(plane);
1100c7679306SThierry Reding 		return ERR_PTR(err);
1101dee8268fSThierry Reding 	}
1102c7679306SThierry Reding 
1103a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
11043dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
1105ab7d3f58SThierry Reding 
1106995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
1107995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
1108995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
11094fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
1110cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
1111995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
1112995c5a50SThierry Reding 	if (err < 0)
1113995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
1114995c5a50SThierry Reding 			err);
1115995c5a50SThierry Reding 
1116c7679306SThierry Reding 	return &plane->base;
1117c7679306SThierry Reding }
1118c7679306SThierry Reding 
111947307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
112047307954SThierry Reding 						    struct tegra_dc *dc)
1121c7679306SThierry Reding {
112247307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
112347307954SThierry Reding 	unsigned int i, j;
112447307954SThierry Reding 
112547307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
112647307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
112747307954SThierry Reding 
112847307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
112947307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
113047307954SThierry Reding 				unsigned int index = wgrp->windows[j];
113147307954SThierry Reding 
113247307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
113347307954SThierry Reding 								  wgrp->index,
113447307954SThierry Reding 								  index);
113547307954SThierry Reding 				if (IS_ERR(plane))
113647307954SThierry Reding 					return plane;
113747307954SThierry Reding 
113847307954SThierry Reding 				/*
113947307954SThierry Reding 				 * Choose the first shared plane owned by this
114047307954SThierry Reding 				 * head as the primary plane.
114147307954SThierry Reding 				 */
114247307954SThierry Reding 				if (!primary) {
114347307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
114447307954SThierry Reding 					primary = plane;
114547307954SThierry Reding 				}
114647307954SThierry Reding 			}
114747307954SThierry Reding 		}
114847307954SThierry Reding 	}
114947307954SThierry Reding 
115047307954SThierry Reding 	return primary;
115147307954SThierry Reding }
115247307954SThierry Reding 
115347307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
115447307954SThierry Reding 					     struct tegra_dc *dc)
115547307954SThierry Reding {
11568f62142eSThierry Reding 	struct drm_plane *planes[2], *primary;
11579f446d83SDmitry Osipenko 	unsigned int planes_num;
1158c7679306SThierry Reding 	unsigned int i;
11598f62142eSThierry Reding 	int err;
1160c7679306SThierry Reding 
116147307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
116247307954SThierry Reding 	if (IS_ERR(primary))
116347307954SThierry Reding 		return primary;
116447307954SThierry Reding 
11659f446d83SDmitry Osipenko 	if (dc->soc->supports_cursor)
11669f446d83SDmitry Osipenko 		planes_num = 2;
11679f446d83SDmitry Osipenko 	else
11689f446d83SDmitry Osipenko 		planes_num = 1;
11699f446d83SDmitry Osipenko 
11709f446d83SDmitry Osipenko 	for (i = 0; i < planes_num; i++) {
11719f446d83SDmitry Osipenko 		planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
11729f446d83SDmitry Osipenko 							  false);
11738f62142eSThierry Reding 		if (IS_ERR(planes[i])) {
11748f62142eSThierry Reding 			err = PTR_ERR(planes[i]);
11758f62142eSThierry Reding 
11768f62142eSThierry Reding 			while (i--)
11778f62142eSThierry Reding 				tegra_plane_funcs.destroy(planes[i]);
11788f62142eSThierry Reding 
11798f62142eSThierry Reding 			tegra_plane_funcs.destroy(primary);
11808f62142eSThierry Reding 			return ERR_PTR(err);
118147307954SThierry Reding 		}
1182f002abc1SThierry Reding 	}
1183dee8268fSThierry Reding 
118447307954SThierry Reding 	return primary;
1185dee8268fSThierry Reding }
1186dee8268fSThierry Reding 
1187f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1188f002abc1SThierry Reding {
1189f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1190f002abc1SThierry Reding }
1191f002abc1SThierry Reding 
1192ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1193ca915b10SThierry Reding {
1194b7e0b04aSMaarten Lankhorst 	struct tegra_dc_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
1195ca915b10SThierry Reding 
11963b59b7acSThierry Reding 	if (crtc->state)
1197b7e0b04aSMaarten Lankhorst 		tegra_crtc_atomic_destroy_state(crtc, crtc->state);
11983b59b7acSThierry Reding 
1199b7e0b04aSMaarten Lankhorst 	__drm_atomic_helper_crtc_reset(crtc, &state->base);
1200ca915b10SThierry Reding }
1201ca915b10SThierry Reding 
1202ca915b10SThierry Reding static struct drm_crtc_state *
1203ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1204ca915b10SThierry Reding {
1205ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1206ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1207ca915b10SThierry Reding 
12083b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1209ca915b10SThierry Reding 	if (!copy)
1210ca915b10SThierry Reding 		return NULL;
1211ca915b10SThierry Reding 
12123b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
12133b59b7acSThierry Reding 	copy->clk = state->clk;
12143b59b7acSThierry Reding 	copy->pclk = state->pclk;
12153b59b7acSThierry Reding 	copy->div = state->div;
12163b59b7acSThierry Reding 	copy->planes = state->planes;
1217ca915b10SThierry Reding 
1218ca915b10SThierry Reding 	return &copy->base;
1219ca915b10SThierry Reding }
1220ca915b10SThierry Reding 
1221ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1222ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1223ca915b10SThierry Reding {
1224ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1225ca915b10SThierry Reding 	kfree(state);
1226ca915b10SThierry Reding }
1227ca915b10SThierry Reding 
1228b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1229b95800eeSThierry Reding 
1230b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1231b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1232b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1233b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1234b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1235b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1236b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1237b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1238b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1239b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1240b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1241b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1242b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1243b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1244b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1245b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1246b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1247b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1248b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1249b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1250b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1251b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1252b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1253b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1254b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1255b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1256b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1257b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1258b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1259b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1260b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1261b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1262b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1263b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1264b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1265b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1266b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1267b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1268b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1269b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1270b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1271b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1272b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1273b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1274b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1275b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1276b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1277b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1278b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1279b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1280b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1281b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1282b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1283b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1284b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1285b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1286b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1287b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1288b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1289b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1290b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1291b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1292b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1293b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1294b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1295b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1296b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1297b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1298b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1299b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1300b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1301b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1302b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1303b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1304b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1305b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1306b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1307b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1308b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1309b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1310b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1311b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1312b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1313b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1314b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1315b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1316b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1317b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1318b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1319b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1320b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1321b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1322b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1323b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1324b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1325b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1326b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1327b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1328b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1329b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1330b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1331b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1332b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1333b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1334b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1335b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1336b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1337b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1338b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1339b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1340b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1341b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1342b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1343b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1344b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1345b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1346b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1347b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1348b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1349b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1350b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1351b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1352b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1353b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1354b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1355b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1356b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1357b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1358b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1359b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1360b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1361b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1362b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1363b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1364b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1365b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1366b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1367b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1368b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1369b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1370b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1371b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1372b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1373b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1374b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1375b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1376b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1377b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1378b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1379b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1380b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1381b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1382b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1383b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1384b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1385b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1386b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1387b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1388b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1389b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1390b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1391b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1392b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1393b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1394b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1395b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1396b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1397b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1398b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1399b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1400b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1401b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1402b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1403b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1404b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1405b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1406b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1407b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1408b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1409b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1410b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1411b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1412b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1413b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1414b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1415b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1416b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1417b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1418b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1419b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1420b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1421b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1422b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1423b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1424b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1425b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1426b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1427b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1428b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1429b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1430b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1431b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1432b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1433b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1434b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1435b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1436b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1437b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1438b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1439b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1440b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1441b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1442b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1443b95800eeSThierry Reding };
1444b95800eeSThierry Reding 
1445b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1446b95800eeSThierry Reding {
1447b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1448b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1449b95800eeSThierry Reding 	unsigned int i;
1450b95800eeSThierry Reding 	int err = 0;
1451b95800eeSThierry Reding 
1452b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1453b95800eeSThierry Reding 
1454b95800eeSThierry Reding 	if (!dc->base.state->active) {
1455b95800eeSThierry Reding 		err = -EBUSY;
1456b95800eeSThierry Reding 		goto unlock;
1457b95800eeSThierry Reding 	}
1458b95800eeSThierry Reding 
1459b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1460b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1461b95800eeSThierry Reding 
1462b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1463b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1464b95800eeSThierry Reding 	}
1465b95800eeSThierry Reding 
1466b95800eeSThierry Reding unlock:
1467b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1468b95800eeSThierry Reding 	return err;
1469b95800eeSThierry Reding }
1470b95800eeSThierry Reding 
1471b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1472b95800eeSThierry Reding {
1473b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1474b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1475b95800eeSThierry Reding 	int err = 0;
1476b95800eeSThierry Reding 	u32 value;
1477b95800eeSThierry Reding 
1478b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1479b95800eeSThierry Reding 
1480b95800eeSThierry Reding 	if (!dc->base.state->active) {
1481b95800eeSThierry Reding 		err = -EBUSY;
1482b95800eeSThierry Reding 		goto unlock;
1483b95800eeSThierry Reding 	}
1484b95800eeSThierry Reding 
1485b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1486b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1487b95800eeSThierry Reding 	tegra_dc_commit(dc);
1488b95800eeSThierry Reding 
1489b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1490b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1491b95800eeSThierry Reding 
1492b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1493b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1494b95800eeSThierry Reding 
1495b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1496b95800eeSThierry Reding 
1497b95800eeSThierry Reding unlock:
1498b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1499b95800eeSThierry Reding 	return err;
1500b95800eeSThierry Reding }
1501b95800eeSThierry Reding 
1502b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1503b95800eeSThierry Reding {
1504b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1505b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1506b95800eeSThierry Reding 
1507b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1508b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1509b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1510b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1511b95800eeSThierry Reding 
1512b95800eeSThierry Reding 	return 0;
1513b95800eeSThierry Reding }
1514b95800eeSThierry Reding 
1515b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1516b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1517b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1518b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1519b95800eeSThierry Reding };
1520b95800eeSThierry Reding 
1521b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1522b95800eeSThierry Reding {
1523b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1524b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
152539f55c61SArnd Bergmann 	struct dentry *root;
1526b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1527b95800eeSThierry Reding 
152839f55c61SArnd Bergmann #ifdef CONFIG_DEBUG_FS
152939f55c61SArnd Bergmann 	root = crtc->debugfs_entry;
153039f55c61SArnd Bergmann #else
153139f55c61SArnd Bergmann 	root = NULL;
153239f55c61SArnd Bergmann #endif
153339f55c61SArnd Bergmann 
1534b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1535b95800eeSThierry Reding 				    GFP_KERNEL);
1536b95800eeSThierry Reding 	if (!dc->debugfs_files)
1537b95800eeSThierry Reding 		return -ENOMEM;
1538b95800eeSThierry Reding 
1539b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1540b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1541b95800eeSThierry Reding 
1542ad6d94f2SWambui Karuga 	drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1543b95800eeSThierry Reding 
1544b95800eeSThierry Reding 	return 0;
1545b95800eeSThierry Reding }
1546b95800eeSThierry Reding 
1547b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1548b95800eeSThierry Reding {
1549b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1550b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1551b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1552b95800eeSThierry Reding 
1553b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1554b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1555b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1556b95800eeSThierry Reding }
1557b95800eeSThierry Reding 
1558c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1559c49c81e2SThierry Reding {
1560c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1561c49c81e2SThierry Reding 
156247307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
156347307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1564c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1565c49c81e2SThierry Reding 
1566c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
15673abe2413SDhinakaran Pandiyan 	return (u32)drm_crtc_vblank_count(&dc->base);
1568c49c81e2SThierry Reding }
1569c49c81e2SThierry Reding 
1570c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1571c49c81e2SThierry Reding {
1572c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1573363541e8SThierry Reding 	u32 value;
1574c49c81e2SThierry Reding 
1575c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1576c49c81e2SThierry Reding 	value |= VBLANK_INT;
1577c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1578c49c81e2SThierry Reding 
1579c49c81e2SThierry Reding 	return 0;
1580c49c81e2SThierry Reding }
1581c49c81e2SThierry Reding 
1582c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1583c49c81e2SThierry Reding {
1584c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1585363541e8SThierry Reding 	u32 value;
1586c49c81e2SThierry Reding 
1587c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1588c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1589c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1590c49c81e2SThierry Reding }
1591c49c81e2SThierry Reding 
1592dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
15931503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
159474f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1595f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1596ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1597ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1598ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1599b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1600b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
160110437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
160210437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
160310437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1604dee8268fSThierry Reding };
1605dee8268fSThierry Reding 
1606dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1607dee8268fSThierry Reding 				struct drm_display_mode *mode)
1608dee8268fSThierry Reding {
16090444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
16100444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1611dee8268fSThierry Reding 	unsigned long value;
1612dee8268fSThierry Reding 
161347307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1614dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1615dee8268fSThierry Reding 
1616dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1617dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
161847307954SThierry Reding 	}
1619dee8268fSThierry Reding 
1620dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1621dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1622dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1623dee8268fSThierry Reding 
1624dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1625dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1626dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1627dee8268fSThierry Reding 
1628dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1629dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1630dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1631dee8268fSThierry Reding 
1632dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1633dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1634dee8268fSThierry Reding 
1635dee8268fSThierry Reding 	return 0;
1636dee8268fSThierry Reding }
1637dee8268fSThierry Reding 
16389d910b60SThierry Reding /**
16399d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
16409d910b60SThierry Reding  *     state
16419d910b60SThierry Reding  * @dc: display controller
16429d910b60SThierry Reding  * @crtc_state: CRTC atomic state
16439d910b60SThierry Reding  * @clk: parent clock for display controller
16449d910b60SThierry Reding  * @pclk: pixel clock
16459d910b60SThierry Reding  * @div: shift clock divider
16469d910b60SThierry Reding  *
16479d910b60SThierry Reding  * Returns:
16489d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
16499d910b60SThierry Reding  */
1650ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1651ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1652ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1653ca915b10SThierry Reding 			       unsigned int div)
1654ca915b10SThierry Reding {
1655ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1656ca915b10SThierry Reding 
1657d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1658d2982748SThierry Reding 		return -EINVAL;
1659d2982748SThierry Reding 
1660ca915b10SThierry Reding 	state->clk = clk;
1661ca915b10SThierry Reding 	state->pclk = pclk;
1662ca915b10SThierry Reding 	state->div = div;
1663ca915b10SThierry Reding 
1664ca915b10SThierry Reding 	return 0;
1665ca915b10SThierry Reding }
1666ca915b10SThierry Reding 
166776d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
166876d59ed0SThierry Reding 				  struct tegra_dc_state *state)
166976d59ed0SThierry Reding {
167076d59ed0SThierry Reding 	u32 value;
167176d59ed0SThierry Reding 	int err;
167276d59ed0SThierry Reding 
167376d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
167476d59ed0SThierry Reding 	if (err < 0)
167576d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
167676d59ed0SThierry Reding 
167776d59ed0SThierry Reding 	/*
167876d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
167976d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
168076d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
168176d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
168276d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
168376d59ed0SThierry Reding 	 * should therefore be avoided.
168476d59ed0SThierry Reding 	 */
168576d59ed0SThierry Reding 	if (state->pclk > 0) {
168676d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
168776d59ed0SThierry Reding 		if (err < 0)
168876d59ed0SThierry Reding 			dev_err(dc->dev,
168976d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
169076d59ed0SThierry Reding 				state->pclk);
169176d59ed0SThierry Reding 	}
169276d59ed0SThierry Reding 
169376d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
169476d59ed0SThierry Reding 		      state->div);
169576d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
169676d59ed0SThierry Reding 
169747307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
169876d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
169976d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
170047307954SThierry Reding 	}
170139e08affSThierry Reding 
170239e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
170339e08affSThierry Reding 	if (err < 0)
170439e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
170539e08affSThierry Reding 			dc->clk, state->pclk, err);
170676d59ed0SThierry Reding }
170776d59ed0SThierry Reding 
1708003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1709003fc848SThierry Reding {
1710003fc848SThierry Reding 	u32 value;
1711003fc848SThierry Reding 
1712003fc848SThierry Reding 	/* stop the display controller */
1713003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1714003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1715003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1716003fc848SThierry Reding 
1717003fc848SThierry Reding 	tegra_dc_commit(dc);
1718003fc848SThierry Reding }
1719003fc848SThierry Reding 
1720003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1721003fc848SThierry Reding {
1722003fc848SThierry Reding 	u32 value;
1723003fc848SThierry Reding 
1724003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1725003fc848SThierry Reding 
1726003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1727003fc848SThierry Reding }
1728003fc848SThierry Reding 
1729003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1730003fc848SThierry Reding {
1731003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1732003fc848SThierry Reding 
1733003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1734003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1735003fc848SThierry Reding 			return 0;
1736003fc848SThierry Reding 
1737003fc848SThierry Reding 		usleep_range(1000, 2000);
1738003fc848SThierry Reding 	}
1739003fc848SThierry Reding 
1740003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1741003fc848SThierry Reding 	return -ETIMEDOUT;
1742003fc848SThierry Reding }
1743003fc848SThierry Reding 
174464581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
174564581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1746003fc848SThierry Reding {
1747003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1748003fc848SThierry Reding 	u32 value;
1749fd67e9c6SThierry Reding 	int err;
1750003fc848SThierry Reding 
1751003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1752003fc848SThierry Reding 		tegra_dc_stop(dc);
1753003fc848SThierry Reding 
1754003fc848SThierry Reding 		/*
1755003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1756003fc848SThierry Reding 		 * in case this fails.
1757003fc848SThierry Reding 		 */
1758003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1759003fc848SThierry Reding 	}
1760003fc848SThierry Reding 
1761003fc848SThierry Reding 	/*
1762003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1763003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1764003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1765003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1766003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1767003fc848SThierry Reding 	 * to go idle.
1768003fc848SThierry Reding 	 *
1769003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1770003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1771003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1772003fc848SThierry Reding 	 *
1773003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1774003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1775003fc848SThierry Reding 	 * the RGB encoder?
1776003fc848SThierry Reding 	 */
1777003fc848SThierry Reding 	if (dc->rgb) {
1778003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1779003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1780003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1781003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1782003fc848SThierry Reding 	}
1783003fc848SThierry Reding 
1784003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1785003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
178633a8eb8dSThierry Reding 
17879d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
17889d99ab6eSThierry Reding 
17899d99ab6eSThierry Reding 	if (crtc->state->event) {
17909d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
17919d99ab6eSThierry Reding 		crtc->state->event = NULL;
17929d99ab6eSThierry Reding 	}
17939d99ab6eSThierry Reding 
17949d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
17959d99ab6eSThierry Reding 
1796fd67e9c6SThierry Reding 	err = host1x_client_suspend(&dc->client);
1797fd67e9c6SThierry Reding 	if (err < 0)
1798fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to suspend: %d\n", err);
1799003fc848SThierry Reding }
1800003fc848SThierry Reding 
18010b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
18020b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1803dee8268fSThierry Reding {
18044aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
180576d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1806dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1807dbb3f2f7SThierry Reding 	u32 value;
1808fd67e9c6SThierry Reding 	int err;
1809dee8268fSThierry Reding 
1810fd67e9c6SThierry Reding 	err = host1x_client_resume(&dc->client);
1811fd67e9c6SThierry Reding 	if (err < 0) {
1812fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to resume: %d\n", err);
1813fd67e9c6SThierry Reding 		return;
1814fd67e9c6SThierry Reding 	}
181533a8eb8dSThierry Reding 
181633a8eb8dSThierry Reding 	/* initialize display controller */
181733a8eb8dSThierry Reding 	if (dc->syncpt) {
181847307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
181947307954SThierry Reding 
182047307954SThierry Reding 		if (dc->soc->has_nvdisplay)
182147307954SThierry Reding 			enable = 1 << 31;
182247307954SThierry Reding 		else
182347307954SThierry Reding 			enable = 1 << 8;
182433a8eb8dSThierry Reding 
182533a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
182633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
182733a8eb8dSThierry Reding 
182847307954SThierry Reding 		value = enable | syncpt;
182933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
183033a8eb8dSThierry Reding 	}
183133a8eb8dSThierry Reding 
183247307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
183347307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
183447307954SThierry Reding 			DSC_OBUF_UF_INT;
183547307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
183647307954SThierry Reding 
183747307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
183847307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
183947307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
184047307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
184147307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
184247307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
184347307954SThierry Reding 
184447307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
184547307954SThierry Reding 			FRAME_END_INT;
184647307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
184747307954SThierry Reding 
184847307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
184947307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
185047307954SThierry Reding 
185147307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
185247307954SThierry Reding 	} else {
185333a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
185433a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
185533a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
185633a8eb8dSThierry Reding 
185733a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
185833a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
185933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
186033a8eb8dSThierry Reding 
186133a8eb8dSThierry Reding 		/* initialize timer */
186233a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
186333a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
186433a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
186533a8eb8dSThierry Reding 
186633a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
186733a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
186833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
186933a8eb8dSThierry Reding 
187033a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
187133a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
187233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
187333a8eb8dSThierry Reding 
187433a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
187533a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
187633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
187747307954SThierry Reding 	}
187833a8eb8dSThierry Reding 
18797116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
18807116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
18817116e9a8SThierry Reding 	else
188233a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
188333a8eb8dSThierry Reding 
188433a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
188576d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
188676d59ed0SThierry Reding 
1887dee8268fSThierry Reding 	/* program display mode */
1888dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1889dee8268fSThierry Reding 
18908620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
18918620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
18928620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
18938620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
18948620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
18958620fc62SThierry Reding 	}
1896666cb873SThierry Reding 
1897666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1898666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1899666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1900666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1901666cb873SThierry Reding 
190247307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1903666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1904666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1905666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1906666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
190747307954SThierry Reding 	}
190847307954SThierry Reding 
190947307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
191047307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
191147307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
191247307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
191347307954SThierry Reding 	}
1914666cb873SThierry Reding 
1915666cb873SThierry Reding 	tegra_dc_commit(dc);
1916dee8268fSThierry Reding 
19178ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1918dee8268fSThierry Reding }
1919dee8268fSThierry Reding 
1920613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1921613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
19224aa3df71SThierry Reding {
19239d99ab6eSThierry Reding 	unsigned long flags;
19241503ca47SThierry Reding 
19251503ca47SThierry Reding 	if (crtc->state->event) {
19269d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
19271503ca47SThierry Reding 
19289d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
19299d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
19309d99ab6eSThierry Reding 		else
19319d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
19321503ca47SThierry Reding 
19339d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
19349d99ab6eSThierry Reding 
19351503ca47SThierry Reding 		crtc->state->event = NULL;
19361503ca47SThierry Reding 	}
19374aa3df71SThierry Reding }
19384aa3df71SThierry Reding 
1939613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1940613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
19414aa3df71SThierry Reding {
194247802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
194347802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
194447307954SThierry Reding 	u32 value;
194547802b09SThierry Reding 
194647307954SThierry Reding 	value = state->planes << 8 | GENERAL_UPDATE;
194747307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
194847307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
194947307954SThierry Reding 
195047307954SThierry Reding 	value = state->planes | GENERAL_ACT_REQ;
195147307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
195247307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
19534aa3df71SThierry Reding }
19544aa3df71SThierry Reding 
1955dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
19564aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
19574aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
19580b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
195964581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1960dee8268fSThierry Reding };
1961dee8268fSThierry Reding 
1962dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1963dee8268fSThierry Reding {
1964dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1965dee8268fSThierry Reding 	unsigned long status;
1966dee8268fSThierry Reding 
1967dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1968dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1969dee8268fSThierry Reding 
1970dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1971dee8268fSThierry Reding 		/*
1972dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1973dee8268fSThierry Reding 		*/
1974791ddb1eSThierry Reding 		dc->stats.frames++;
1975dee8268fSThierry Reding 	}
1976dee8268fSThierry Reding 
1977dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1978dee8268fSThierry Reding 		/*
1979dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1980dee8268fSThierry Reding 		*/
1981ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1982791ddb1eSThierry Reding 		dc->stats.vblank++;
1983dee8268fSThierry Reding 	}
1984dee8268fSThierry Reding 
1985dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1986dee8268fSThierry Reding 		/*
1987dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1988dee8268fSThierry Reding 		*/
1989791ddb1eSThierry Reding 		dc->stats.underflow++;
1990791ddb1eSThierry Reding 	}
1991791ddb1eSThierry Reding 
1992791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1993791ddb1eSThierry Reding 		/*
1994791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1995791ddb1eSThierry Reding 		*/
1996791ddb1eSThierry Reding 		dc->stats.overflow++;
1997dee8268fSThierry Reding 	}
1998dee8268fSThierry Reding 
199947307954SThierry Reding 	if (status & HEAD_UF_INT) {
200047307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
200147307954SThierry Reding 		dc->stats.underflow++;
200247307954SThierry Reding 	}
200347307954SThierry Reding 
2004dee8268fSThierry Reding 	return IRQ_HANDLED;
2005dee8268fSThierry Reding }
2006dee8268fSThierry Reding 
2007e75d0477SThierry Reding static bool tegra_dc_has_window_groups(struct tegra_dc *dc)
2008e75d0477SThierry Reding {
2009e75d0477SThierry Reding 	unsigned int i;
2010e75d0477SThierry Reding 
2011e75d0477SThierry Reding 	if (!dc->soc->wgrps)
2012e75d0477SThierry Reding 		return true;
2013e75d0477SThierry Reding 
2014e75d0477SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
2015e75d0477SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
2016e75d0477SThierry Reding 
2017e75d0477SThierry Reding 		if (wgrp->dc == dc->pipe && wgrp->num_windows > 0)
2018e75d0477SThierry Reding 			return true;
2019e75d0477SThierry Reding 	}
2020e75d0477SThierry Reding 
2021e75d0477SThierry Reding 	return false;
2022e75d0477SThierry Reding }
2023e75d0477SThierry Reding 
2024dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
2025dee8268fSThierry Reding {
2026608f43adSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
20272bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
2028dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2029d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
2030c7679306SThierry Reding 	struct drm_plane *primary = NULL;
2031c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
2032dee8268fSThierry Reding 	int err;
2033dee8268fSThierry Reding 
2034759d706fSThierry Reding 	/*
2035759d706fSThierry Reding 	 * XXX do not register DCs with no window groups because we cannot
2036759d706fSThierry Reding 	 * assign a primary plane to them, which in turn will cause KMS to
2037759d706fSThierry Reding 	 * crash.
2038759d706fSThierry Reding 	 */
2039e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2040759d706fSThierry Reding 		return 0;
2041759d706fSThierry Reding 
2042fd67e9c6SThierry Reding 	/*
2043fd67e9c6SThierry Reding 	 * Set the display hub as the host1x client parent for the display
2044fd67e9c6SThierry Reding 	 * controller. This is needed for the runtime reference counting that
2045fd67e9c6SThierry Reding 	 * ensures the display hub is always powered when any of the display
2046fd67e9c6SThierry Reding 	 * controllers are.
2047fd67e9c6SThierry Reding 	 */
2048fd67e9c6SThierry Reding 	if (dc->soc->has_nvdisplay)
2049fd67e9c6SThierry Reding 		client->parent = &tegra->hub->client;
2050fd67e9c6SThierry Reding 
2051617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
20522bcdcbfaSThierry Reding 	if (!dc->syncpt)
20532bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
20542bcdcbfaSThierry Reding 
20557edd7961SThierry Reding 	err = host1x_client_iommu_attach(client);
2056a8817489SThierry Reding 	if (err < 0 && err != -ENODEV) {
20570c407de5SThierry Reding 		dev_err(client->dev, "failed to attach to domain: %d\n", err);
2058df06b759SThierry Reding 		return err;
2059df06b759SThierry Reding 	}
2060df06b759SThierry Reding 
206147307954SThierry Reding 	if (dc->soc->wgrps)
206247307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
206347307954SThierry Reding 	else
206447307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
206547307954SThierry Reding 
2066c7679306SThierry Reding 	if (IS_ERR(primary)) {
2067c7679306SThierry Reding 		err = PTR_ERR(primary);
2068c7679306SThierry Reding 		goto cleanup;
2069c7679306SThierry Reding 	}
2070c7679306SThierry Reding 
2071c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
2072c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
2073c7679306SThierry Reding 		if (IS_ERR(cursor)) {
2074c7679306SThierry Reding 			err = PTR_ERR(cursor);
2075c7679306SThierry Reding 			goto cleanup;
2076c7679306SThierry Reding 		}
20779f446d83SDmitry Osipenko 	} else {
20789f446d83SDmitry Osipenko 		/* dedicate one overlay to mouse cursor */
20799f446d83SDmitry Osipenko 		cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
20809f446d83SDmitry Osipenko 		if (IS_ERR(cursor)) {
20819f446d83SDmitry Osipenko 			err = PTR_ERR(cursor);
20829f446d83SDmitry Osipenko 			goto cleanup;
20839f446d83SDmitry Osipenko 		}
2084c7679306SThierry Reding 	}
2085c7679306SThierry Reding 
2086c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
2087f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
2088c7679306SThierry Reding 	if (err < 0)
2089c7679306SThierry Reding 		goto cleanup;
2090c7679306SThierry Reding 
2091dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
2092dee8268fSThierry Reding 
2093d1f3e1e0SThierry Reding 	/*
2094d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
2095d1f3e1e0SThierry Reding 	 * controllers.
2096d1f3e1e0SThierry Reding 	 */
2097d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
2098d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
2099d1f3e1e0SThierry Reding 
21009910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
2101dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2102dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
2103c7679306SThierry Reding 		goto cleanup;
2104dee8268fSThierry Reding 	}
2105dee8268fSThierry Reding 
2106dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
2107dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
2108dee8268fSThierry Reding 	if (err < 0) {
2109dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
2110dee8268fSThierry Reding 			err);
2111c7679306SThierry Reding 		goto cleanup;
2112dee8268fSThierry Reding 	}
2113dee8268fSThierry Reding 
211447b15779SThierry Reding 	/*
211547b15779SThierry Reding 	 * Inherit the DMA parameters (such as maximum segment size) from the
2116608f43adSThierry Reding 	 * parent host1x device.
211747b15779SThierry Reding 	 */
2118608f43adSThierry Reding 	client->dev->dma_parms = client->host->dma_parms;
211947b15779SThierry Reding 
2120dee8268fSThierry Reding 	return 0;
2121c7679306SThierry Reding 
2122c7679306SThierry Reding cleanup:
212347307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
2124c7679306SThierry Reding 		drm_plane_cleanup(cursor);
2125c7679306SThierry Reding 
212647307954SThierry Reding 	if (!IS_ERR(primary))
2127c7679306SThierry Reding 		drm_plane_cleanup(primary);
2128c7679306SThierry Reding 
2129aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
2130fd5ec0dcSThierry Reding 	host1x_syncpt_free(dc->syncpt);
2131fd5ec0dcSThierry Reding 
2132c7679306SThierry Reding 	return err;
2133dee8268fSThierry Reding }
2134dee8268fSThierry Reding 
2135dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
2136dee8268fSThierry Reding {
2137dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2138dee8268fSThierry Reding 	int err;
2139dee8268fSThierry Reding 
2140e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2141e75d0477SThierry Reding 		return 0;
2142e75d0477SThierry Reding 
214347b15779SThierry Reding 	/* avoid a dangling pointer just in case this disappears */
214447b15779SThierry Reding 	client->dev->dma_parms = NULL;
214547b15779SThierry Reding 
2146dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
2147dee8268fSThierry Reding 
2148dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
2149dee8268fSThierry Reding 	if (err) {
2150dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
2151dee8268fSThierry Reding 		return err;
2152dee8268fSThierry Reding 	}
2153dee8268fSThierry Reding 
2154aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
21552bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
21562bcdcbfaSThierry Reding 
2157dee8268fSThierry Reding 	return 0;
2158dee8268fSThierry Reding }
2159dee8268fSThierry Reding 
2160fd67e9c6SThierry Reding static int tegra_dc_runtime_suspend(struct host1x_client *client)
2161fd67e9c6SThierry Reding {
2162fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2163fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2164fd67e9c6SThierry Reding 	int err;
2165fd67e9c6SThierry Reding 
2166fd67e9c6SThierry Reding 	err = reset_control_assert(dc->rst);
2167fd67e9c6SThierry Reding 	if (err < 0) {
2168fd67e9c6SThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
2169fd67e9c6SThierry Reding 		return err;
2170fd67e9c6SThierry Reding 	}
2171fd67e9c6SThierry Reding 
2172fd67e9c6SThierry Reding 	if (dc->soc->has_powergate)
2173fd67e9c6SThierry Reding 		tegra_powergate_power_off(dc->powergate);
2174fd67e9c6SThierry Reding 
2175fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2176fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2177fd67e9c6SThierry Reding 
2178fd67e9c6SThierry Reding 	return 0;
2179fd67e9c6SThierry Reding }
2180fd67e9c6SThierry Reding 
2181fd67e9c6SThierry Reding static int tegra_dc_runtime_resume(struct host1x_client *client)
2182fd67e9c6SThierry Reding {
2183fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2184fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2185fd67e9c6SThierry Reding 	int err;
2186fd67e9c6SThierry Reding 
2187fd67e9c6SThierry Reding 	err = pm_runtime_get_sync(dev);
2188fd67e9c6SThierry Reding 	if (err < 0) {
2189fd67e9c6SThierry Reding 		dev_err(dev, "failed to get runtime PM: %d\n", err);
2190fd67e9c6SThierry Reding 		return err;
2191fd67e9c6SThierry Reding 	}
2192fd67e9c6SThierry Reding 
2193fd67e9c6SThierry Reding 	if (dc->soc->has_powergate) {
2194fd67e9c6SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2195fd67e9c6SThierry Reding 							dc->rst);
2196fd67e9c6SThierry Reding 		if (err < 0) {
2197fd67e9c6SThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
2198fd67e9c6SThierry Reding 			goto put_rpm;
2199fd67e9c6SThierry Reding 		}
2200fd67e9c6SThierry Reding 	} else {
2201fd67e9c6SThierry Reding 		err = clk_prepare_enable(dc->clk);
2202fd67e9c6SThierry Reding 		if (err < 0) {
2203fd67e9c6SThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
2204fd67e9c6SThierry Reding 			goto put_rpm;
2205fd67e9c6SThierry Reding 		}
2206fd67e9c6SThierry Reding 
2207fd67e9c6SThierry Reding 		err = reset_control_deassert(dc->rst);
2208fd67e9c6SThierry Reding 		if (err < 0) {
2209fd67e9c6SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
2210fd67e9c6SThierry Reding 			goto disable_clk;
2211fd67e9c6SThierry Reding 		}
2212fd67e9c6SThierry Reding 	}
2213fd67e9c6SThierry Reding 
2214fd67e9c6SThierry Reding 	return 0;
2215fd67e9c6SThierry Reding 
2216fd67e9c6SThierry Reding disable_clk:
2217fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2218fd67e9c6SThierry Reding put_rpm:
2219fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2220fd67e9c6SThierry Reding 	return err;
2221fd67e9c6SThierry Reding }
2222fd67e9c6SThierry Reding 
2223dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
2224dee8268fSThierry Reding 	.init = tegra_dc_init,
2225dee8268fSThierry Reding 	.exit = tegra_dc_exit,
2226fd67e9c6SThierry Reding 	.suspend = tegra_dc_runtime_suspend,
2227fd67e9c6SThierry Reding 	.resume = tegra_dc_runtime_resume,
2228dee8268fSThierry Reding };
2229dee8268fSThierry Reding 
22308620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
22317116e9a8SThierry Reding 	.supports_background_color = false,
22328620fc62SThierry Reding 	.supports_interlacing = false,
2233e687651bSThierry Reding 	.supports_cursor = false,
2234c134f019SThierry Reding 	.supports_block_linear = false,
2235a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2236d1f3e1e0SThierry Reding 	.pitch_align = 8,
22379c012700SThierry Reding 	.has_powergate = false,
2238f68ba691SDmitry Osipenko 	.coupled_pm = true,
223947307954SThierry Reding 	.has_nvdisplay = false,
2240511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2241511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2242511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2243511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2244e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2245acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = true,
2246acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = true,
22478620fc62SThierry Reding };
22488620fc62SThierry Reding 
22498620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
22507116e9a8SThierry Reding 	.supports_background_color = false,
22518620fc62SThierry Reding 	.supports_interlacing = false,
2252e687651bSThierry Reding 	.supports_cursor = false,
2253c134f019SThierry Reding 	.supports_block_linear = false,
2254a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2255d1f3e1e0SThierry Reding 	.pitch_align = 8,
22569c012700SThierry Reding 	.has_powergate = false,
2257f68ba691SDmitry Osipenko 	.coupled_pm = false,
225847307954SThierry Reding 	.has_nvdisplay = false,
2259511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2260511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2261511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2262511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2263e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2264acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2265acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2266d1f3e1e0SThierry Reding };
2267d1f3e1e0SThierry Reding 
2268d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
22697116e9a8SThierry Reding 	.supports_background_color = false,
2270d1f3e1e0SThierry Reding 	.supports_interlacing = false,
2271d1f3e1e0SThierry Reding 	.supports_cursor = false,
2272d1f3e1e0SThierry Reding 	.supports_block_linear = false,
2273a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2274d1f3e1e0SThierry Reding 	.pitch_align = 64,
22759c012700SThierry Reding 	.has_powergate = true,
2276f68ba691SDmitry Osipenko 	.coupled_pm = false,
227747307954SThierry Reding 	.has_nvdisplay = false,
2278511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2279511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2280511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2281511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2282e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2283acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2284acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
22858620fc62SThierry Reding };
22868620fc62SThierry Reding 
22878620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
22887116e9a8SThierry Reding 	.supports_background_color = true,
22898620fc62SThierry Reding 	.supports_interlacing = true,
2290e687651bSThierry Reding 	.supports_cursor = true,
2291c134f019SThierry Reding 	.supports_block_linear = true,
2292a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
2293d1f3e1e0SThierry Reding 	.pitch_align = 64,
22949c012700SThierry Reding 	.has_powergate = true,
2295f68ba691SDmitry Osipenko 	.coupled_pm = false,
229647307954SThierry Reding 	.has_nvdisplay = false,
2297511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
22989a02d3afSStefan Agner 	.primary_formats = tegra124_primary_formats,
2299511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
23009a02d3afSStefan Agner 	.overlay_formats = tegra124_overlay_formats,
2301e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2302acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2303acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
23048620fc62SThierry Reding };
23058620fc62SThierry Reding 
23065b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
23077116e9a8SThierry Reding 	.supports_background_color = true,
23085b4f516fSThierry Reding 	.supports_interlacing = true,
23095b4f516fSThierry Reding 	.supports_cursor = true,
23105b4f516fSThierry Reding 	.supports_block_linear = true,
2311a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
23125b4f516fSThierry Reding 	.pitch_align = 64,
23135b4f516fSThierry Reding 	.has_powergate = true,
2314f68ba691SDmitry Osipenko 	.coupled_pm = false,
231547307954SThierry Reding 	.has_nvdisplay = false,
2316511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2317511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2318511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2319511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2320e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2321acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2322acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
232347307954SThierry Reding };
232447307954SThierry Reding 
232547307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
232647307954SThierry Reding 	{
232747307954SThierry Reding 		.index = 0,
232847307954SThierry Reding 		.dc = 0,
232947307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
233047307954SThierry Reding 		.num_windows = 1,
233147307954SThierry Reding 	}, {
233247307954SThierry Reding 		.index = 1,
233347307954SThierry Reding 		.dc = 1,
233447307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
233547307954SThierry Reding 		.num_windows = 1,
233647307954SThierry Reding 	}, {
233747307954SThierry Reding 		.index = 2,
233847307954SThierry Reding 		.dc = 1,
233947307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
234047307954SThierry Reding 		.num_windows = 1,
234147307954SThierry Reding 	}, {
234247307954SThierry Reding 		.index = 3,
234347307954SThierry Reding 		.dc = 2,
234447307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
234547307954SThierry Reding 		.num_windows = 1,
234647307954SThierry Reding 	}, {
234747307954SThierry Reding 		.index = 4,
234847307954SThierry Reding 		.dc = 2,
234947307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
235047307954SThierry Reding 		.num_windows = 1,
235147307954SThierry Reding 	}, {
235247307954SThierry Reding 		.index = 5,
235347307954SThierry Reding 		.dc = 2,
235447307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
235547307954SThierry Reding 		.num_windows = 1,
235647307954SThierry Reding 	},
235747307954SThierry Reding };
235847307954SThierry Reding 
235947307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
236047307954SThierry Reding 	.supports_background_color = true,
236147307954SThierry Reding 	.supports_interlacing = true,
236247307954SThierry Reding 	.supports_cursor = true,
236347307954SThierry Reding 	.supports_block_linear = true,
2364a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
236547307954SThierry Reding 	.pitch_align = 64,
236647307954SThierry Reding 	.has_powergate = false,
2367f68ba691SDmitry Osipenko 	.coupled_pm = false,
236847307954SThierry Reding 	.has_nvdisplay = true,
236947307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
237047307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
23715b4f516fSThierry Reding };
23725b4f516fSThierry Reding 
237347443196SThierry Reding static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
237447443196SThierry Reding 	{
237547443196SThierry Reding 		.index = 0,
237647443196SThierry Reding 		.dc = 0,
237747443196SThierry Reding 		.windows = (const unsigned int[]) { 0 },
237847443196SThierry Reding 		.num_windows = 1,
237947443196SThierry Reding 	}, {
238047443196SThierry Reding 		.index = 1,
238147443196SThierry Reding 		.dc = 1,
238247443196SThierry Reding 		.windows = (const unsigned int[]) { 1 },
238347443196SThierry Reding 		.num_windows = 1,
238447443196SThierry Reding 	}, {
238547443196SThierry Reding 		.index = 2,
238647443196SThierry Reding 		.dc = 1,
238747443196SThierry Reding 		.windows = (const unsigned int[]) { 2 },
238847443196SThierry Reding 		.num_windows = 1,
238947443196SThierry Reding 	}, {
239047443196SThierry Reding 		.index = 3,
239147443196SThierry Reding 		.dc = 2,
239247443196SThierry Reding 		.windows = (const unsigned int[]) { 3 },
239347443196SThierry Reding 		.num_windows = 1,
239447443196SThierry Reding 	}, {
239547443196SThierry Reding 		.index = 4,
239647443196SThierry Reding 		.dc = 2,
239747443196SThierry Reding 		.windows = (const unsigned int[]) { 4 },
239847443196SThierry Reding 		.num_windows = 1,
239947443196SThierry Reding 	}, {
240047443196SThierry Reding 		.index = 5,
240147443196SThierry Reding 		.dc = 2,
240247443196SThierry Reding 		.windows = (const unsigned int[]) { 5 },
240347443196SThierry Reding 		.num_windows = 1,
240447443196SThierry Reding 	},
240547443196SThierry Reding };
240647443196SThierry Reding 
240747443196SThierry Reding static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
240847443196SThierry Reding 	.supports_background_color = true,
240947443196SThierry Reding 	.supports_interlacing = true,
241047443196SThierry Reding 	.supports_cursor = true,
241147443196SThierry Reding 	.supports_block_linear = true,
241247443196SThierry Reding 	.has_legacy_blending = false,
241347443196SThierry Reding 	.pitch_align = 64,
241447443196SThierry Reding 	.has_powergate = false,
241547443196SThierry Reding 	.coupled_pm = false,
241647443196SThierry Reding 	.has_nvdisplay = true,
241747443196SThierry Reding 	.wgrps = tegra194_dc_wgrps,
241847443196SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
241947443196SThierry Reding };
242047443196SThierry Reding 
24218620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
24228620fc62SThierry Reding 	{
242347443196SThierry Reding 		.compatible = "nvidia,tegra194-dc",
242447443196SThierry Reding 		.data = &tegra194_dc_soc_info,
242547443196SThierry Reding 	}, {
242647307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
242747307954SThierry Reding 		.data = &tegra186_dc_soc_info,
242847307954SThierry Reding 	}, {
24295b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
24305b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
24315b4f516fSThierry Reding 	}, {
24328620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
24338620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
24348620fc62SThierry Reding 	}, {
24359c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
24369c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
24379c012700SThierry Reding 	}, {
24388620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
24398620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
24408620fc62SThierry Reding 	}, {
24418620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
24428620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
24438620fc62SThierry Reding 	}, {
24448620fc62SThierry Reding 		/* sentinel */
24458620fc62SThierry Reding 	}
24468620fc62SThierry Reding };
2447ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
24488620fc62SThierry Reding 
244913411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
245013411dddSThierry Reding {
245113411dddSThierry Reding 	struct device_node *np;
245213411dddSThierry Reding 	u32 value = 0;
245313411dddSThierry Reding 	int err;
245413411dddSThierry Reding 
245513411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
245613411dddSThierry Reding 	if (err < 0) {
245713411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
245813411dddSThierry Reding 
245913411dddSThierry Reding 		/*
246013411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
246113411dddSThierry Reding 		 * correct head number by looking up the position of this
246213411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
246313411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
246413411dddSThierry Reding 		 * that the translation into a flattened device tree blob
246513411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
246613411dddSThierry Reding 		 * head number.
246713411dddSThierry Reding 		 *
246813411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
246913411dddSThierry Reding 		 * cases where only a single display controller is used.
247013411dddSThierry Reding 		 */
247113411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
2472cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
2473cf6b1744SJulia Lawall 				of_node_put(np);
247413411dddSThierry Reding 				break;
2475cf6b1744SJulia Lawall 			}
247613411dddSThierry Reding 
247713411dddSThierry Reding 			value++;
247813411dddSThierry Reding 		}
247913411dddSThierry Reding 	}
248013411dddSThierry Reding 
248113411dddSThierry Reding 	dc->pipe = value;
248213411dddSThierry Reding 
248313411dddSThierry Reding 	return 0;
248413411dddSThierry Reding }
248513411dddSThierry Reding 
248692ce7e83SSuzuki K Poulose static int tegra_dc_match_by_pipe(struct device *dev, const void *data)
2487f68ba691SDmitry Osipenko {
2488f68ba691SDmitry Osipenko 	struct tegra_dc *dc = dev_get_drvdata(dev);
248992ce7e83SSuzuki K Poulose 	unsigned int pipe = (unsigned long)(void *)data;
2490f68ba691SDmitry Osipenko 
2491f68ba691SDmitry Osipenko 	return dc->pipe == pipe;
2492f68ba691SDmitry Osipenko }
2493f68ba691SDmitry Osipenko 
2494f68ba691SDmitry Osipenko static int tegra_dc_couple(struct tegra_dc *dc)
2495f68ba691SDmitry Osipenko {
2496f68ba691SDmitry Osipenko 	/*
2497f68ba691SDmitry Osipenko 	 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2498f68ba691SDmitry Osipenko 	 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2499f68ba691SDmitry Osipenko 	 * POWER_CONTROL registers during CRTC enabling.
2500f68ba691SDmitry Osipenko 	 */
2501f68ba691SDmitry Osipenko 	if (dc->soc->coupled_pm && dc->pipe == 1) {
2502e88728f4SVivek Gautam 		u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
2503f68ba691SDmitry Osipenko 		struct device_link *link;
2504f68ba691SDmitry Osipenko 		struct device *partner;
2505f68ba691SDmitry Osipenko 
2506ef1b204aSWei Yongjun 		partner = driver_find_device(dc->dev->driver, NULL, NULL,
2507f68ba691SDmitry Osipenko 					     tegra_dc_match_by_pipe);
2508f68ba691SDmitry Osipenko 		if (!partner)
2509f68ba691SDmitry Osipenko 			return -EPROBE_DEFER;
2510f68ba691SDmitry Osipenko 
2511f68ba691SDmitry Osipenko 		link = device_link_add(dc->dev, partner, flags);
2512f68ba691SDmitry Osipenko 		if (!link) {
2513f68ba691SDmitry Osipenko 			dev_err(dc->dev, "failed to link controllers\n");
2514f68ba691SDmitry Osipenko 			return -EINVAL;
2515f68ba691SDmitry Osipenko 		}
2516f68ba691SDmitry Osipenko 
2517f68ba691SDmitry Osipenko 		dev_dbg(dc->dev, "coupled to %s\n", dev_name(partner));
2518f68ba691SDmitry Osipenko 	}
2519f68ba691SDmitry Osipenko 
2520f68ba691SDmitry Osipenko 	return 0;
2521f68ba691SDmitry Osipenko }
2522f68ba691SDmitry Osipenko 
2523dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
2524dee8268fSThierry Reding {
2525dee8268fSThierry Reding 	struct tegra_dc *dc;
2526dee8268fSThierry Reding 	int err;
2527dee8268fSThierry Reding 
2528dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2529dee8268fSThierry Reding 	if (!dc)
2530dee8268fSThierry Reding 		return -ENOMEM;
2531dee8268fSThierry Reding 
2532b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
25338620fc62SThierry Reding 
2534dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2535dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2536dee8268fSThierry Reding 
253713411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
253813411dddSThierry Reding 	if (err < 0)
253913411dddSThierry Reding 		return err;
254013411dddSThierry Reding 
2541f68ba691SDmitry Osipenko 	err = tegra_dc_couple(dc);
2542f68ba691SDmitry Osipenko 	if (err < 0)
2543f68ba691SDmitry Osipenko 		return err;
2544f68ba691SDmitry Osipenko 
2545dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2546dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
2547dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
2548dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
2549dee8268fSThierry Reding 	}
2550dee8268fSThierry Reding 
2551ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2552ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2553ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2554ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2555ca48080aSStephen Warren 	}
2556ca48080aSStephen Warren 
2557a2f2f740SThierry Reding 	/* assert reset and disable clock */
2558a2f2f740SThierry Reding 	err = clk_prepare_enable(dc->clk);
2559a2f2f740SThierry Reding 	if (err < 0)
2560a2f2f740SThierry Reding 		return err;
2561a2f2f740SThierry Reding 
2562a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2563a2f2f740SThierry Reding 
2564a2f2f740SThierry Reding 	err = reset_control_assert(dc->rst);
2565a2f2f740SThierry Reding 	if (err < 0)
2566a2f2f740SThierry Reding 		return err;
2567a2f2f740SThierry Reding 
2568a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2569a2f2f740SThierry Reding 
2570a2f2f740SThierry Reding 	clk_disable_unprepare(dc->clk);
257133a8eb8dSThierry Reding 
25729c012700SThierry Reding 	if (dc->soc->has_powergate) {
25739c012700SThierry Reding 		if (dc->pipe == 0)
25749c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
25759c012700SThierry Reding 		else
25769c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
25779c012700SThierry Reding 
257833a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
25799c012700SThierry Reding 	}
2580dee8268fSThierry Reding 
2581a858ac8fSDmitry Osipenko 	dc->regs = devm_platform_ioremap_resource(pdev, 0);
2582dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2583dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2584dee8268fSThierry Reding 
2585dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
25865f1df70fSTang Bin 	if (dc->irq < 0)
2587dee8268fSThierry Reding 		return -ENXIO;
2588dee8268fSThierry Reding 
2589dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2590dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
25918f839fb6SDmitry Osipenko 		const char *level = KERN_ERR;
25928f839fb6SDmitry Osipenko 
25938f839fb6SDmitry Osipenko 		if (err == -EPROBE_DEFER)
25948f839fb6SDmitry Osipenko 			level = KERN_DEBUG;
25958f839fb6SDmitry Osipenko 
25968f839fb6SDmitry Osipenko 		dev_printk(level, dc->dev, "failed to probe RGB output: %d\n",
25978f839fb6SDmitry Osipenko 			   err);
2598dee8268fSThierry Reding 		return err;
2599dee8268fSThierry Reding 	}
2600dee8268fSThierry Reding 
260133a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
260233a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
260333a8eb8dSThierry Reding 
260433a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
260533a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
260633a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
260733a8eb8dSThierry Reding 
2608dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2609dee8268fSThierry Reding 	if (err < 0) {
2610dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2611dee8268fSThierry Reding 			err);
26120411ea89SDmitry Osipenko 		goto disable_pm;
2613dee8268fSThierry Reding 	}
2614dee8268fSThierry Reding 
2615dee8268fSThierry Reding 	return 0;
26160411ea89SDmitry Osipenko 
26170411ea89SDmitry Osipenko disable_pm:
26180411ea89SDmitry Osipenko 	pm_runtime_disable(&pdev->dev);
26190411ea89SDmitry Osipenko 	tegra_dc_rgb_remove(dc);
26200411ea89SDmitry Osipenko 
26210411ea89SDmitry Osipenko 	return err;
2622dee8268fSThierry Reding }
2623dee8268fSThierry Reding 
2624dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2625dee8268fSThierry Reding {
2626dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2627dee8268fSThierry Reding 	int err;
2628dee8268fSThierry Reding 
2629dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2630dee8268fSThierry Reding 	if (err < 0) {
2631dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2632dee8268fSThierry Reding 			err);
2633dee8268fSThierry Reding 		return err;
2634dee8268fSThierry Reding 	}
2635dee8268fSThierry Reding 
263659d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
263759d29c0eSThierry Reding 	if (err < 0) {
263859d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
263959d29c0eSThierry Reding 		return err;
264059d29c0eSThierry Reding 	}
264159d29c0eSThierry Reding 
264233a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
264333a8eb8dSThierry Reding 
264433a8eb8dSThierry Reding 	return 0;
264533a8eb8dSThierry Reding }
264633a8eb8dSThierry Reding 
2647dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2648dee8268fSThierry Reding 	.driver = {
2649dee8268fSThierry Reding 		.name = "tegra-dc",
2650dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
2651dee8268fSThierry Reding 	},
2652dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2653dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2654dee8268fSThierry Reding };
2655