xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 042c0bd76d7b053ea6bc47e4db471d5cfc7f19c5)
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);
138df561f66SGustavo 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,
6077c11b99aSMaxime Ripard 				    struct drm_atomic_state *state)
6084aa3df71SThierry Reding {
6097c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
6107c11b99aSMaxime Ripard 										 plane);
611ba5c1649SMaxime Ripard 	struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
612cd740777SDmitry Osipenko 	unsigned int supported_rotation = DRM_MODE_ROTATE_0 |
613cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_X |
614cd740777SDmitry Osipenko 					  DRM_MODE_REFLECT_Y;
615ba5c1649SMaxime Ripard 	unsigned int rotation = new_plane_state->rotation;
6168f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
61747802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
618ba5c1649SMaxime Ripard 	struct tegra_dc *dc = to_tegra_dc(new_plane_state->crtc);
619c7679306SThierry Reding 	int err;
620c7679306SThierry Reding 
6214aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
622ba5c1649SMaxime Ripard 	if (!new_plane_state->crtc)
6234aa3df71SThierry Reding 		return 0;
6244aa3df71SThierry Reding 
625ba5c1649SMaxime Ripard 	err = tegra_plane_format(new_plane_state->fb->format->format,
6263dae08bcSDmitry Osipenko 				 &plane_state->format,
6278f604f8cSThierry Reding 				 &plane_state->swap);
6284aa3df71SThierry Reding 	if (err < 0)
6294aa3df71SThierry Reding 		return err;
6304aa3df71SThierry Reding 
631ebae8d07SThierry Reding 	/*
632ebae8d07SThierry Reding 	 * Tegra20 and Tegra30 are special cases here because they support
633ebae8d07SThierry Reding 	 * only variants of specific formats with an alpha component, but not
634ebae8d07SThierry Reding 	 * the corresponding opaque formats. However, the opaque formats can
635ebae8d07SThierry Reding 	 * be emulated by disabling alpha blending for the plane.
636ebae8d07SThierry Reding 	 */
637a43d0a00SDmitry Osipenko 	if (dc->soc->has_legacy_blending) {
6383dae08bcSDmitry Osipenko 		err = tegra_plane_setup_legacy_state(tegra, plane_state);
639ebae8d07SThierry Reding 		if (err < 0)
640ebae8d07SThierry Reding 			return err;
641ebae8d07SThierry Reding 	}
642ebae8d07SThierry Reding 
643ba5c1649SMaxime Ripard 	err = tegra_fb_get_tiling(new_plane_state->fb, tiling);
6448f604f8cSThierry Reding 	if (err < 0)
6458f604f8cSThierry Reding 		return err;
6468f604f8cSThierry Reding 
6478f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
6484aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
6494aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
6504aa3df71SThierry Reding 		return -EINVAL;
6514aa3df71SThierry Reding 	}
6524aa3df71SThierry Reding 
653cd740777SDmitry Osipenko 	/*
654cd740777SDmitry Osipenko 	 * Older userspace used custom BO flag in order to specify the Y
655cd740777SDmitry Osipenko 	 * reflection, while modern userspace uses the generic DRM rotation
656cd740777SDmitry Osipenko 	 * property in order to achieve the same result.  The legacy BO flag
657cd740777SDmitry Osipenko 	 * duplicates the DRM rotation property when both are set.
658cd740777SDmitry Osipenko 	 */
659ba5c1649SMaxime Ripard 	if (tegra_fb_is_bottom_up(new_plane_state->fb))
660cd740777SDmitry Osipenko 		rotation |= DRM_MODE_REFLECT_Y;
661cd740777SDmitry Osipenko 
662cd740777SDmitry Osipenko 	rotation = drm_rotation_simplify(rotation, supported_rotation);
663cd740777SDmitry Osipenko 
664cd740777SDmitry Osipenko 	if (rotation & DRM_MODE_REFLECT_X)
665cd740777SDmitry Osipenko 		plane_state->reflect_x = true;
666cd740777SDmitry Osipenko 	else
667cd740777SDmitry Osipenko 		plane_state->reflect_x = false;
668995c5a50SThierry Reding 
669995c5a50SThierry Reding 	if (rotation & DRM_MODE_REFLECT_Y)
670e9e476f7SDmitry Osipenko 		plane_state->reflect_y = true;
671995c5a50SThierry Reding 	else
672e9e476f7SDmitry Osipenko 		plane_state->reflect_y = false;
673995c5a50SThierry Reding 
6744aa3df71SThierry Reding 	/*
6754aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
6764aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
6774aa3df71SThierry Reding 	 * configuration.
6784aa3df71SThierry Reding 	 */
679ba5c1649SMaxime Ripard 	if (new_plane_state->fb->format->num_planes > 2) {
680ba5c1649SMaxime Ripard 		if (new_plane_state->fb->pitches[2] != new_plane_state->fb->pitches[1]) {
6814aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
6824aa3df71SThierry Reding 			return -EINVAL;
6834aa3df71SThierry Reding 		}
6844aa3df71SThierry Reding 	}
6854aa3df71SThierry Reding 
686ba5c1649SMaxime Ripard 	err = tegra_plane_state_add(tegra, new_plane_state);
68747802b09SThierry Reding 	if (err < 0)
68847802b09SThierry Reding 		return err;
68947802b09SThierry Reding 
6904aa3df71SThierry Reding 	return 0;
6914aa3df71SThierry Reding }
6924aa3df71SThierry Reding 
693a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
694977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
69580d3eef1SDmitry Osipenko {
696977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
697977697e2SMaxime Ripard 									   plane);
698a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
69980d3eef1SDmitry Osipenko 	u32 value;
70080d3eef1SDmitry Osipenko 
701a4bfa096SThierry Reding 	/* rien ne va plus */
702a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
703a4bfa096SThierry Reding 		return;
704a4bfa096SThierry Reding 
7051087fac1SThierry Reding 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
70680d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
7071087fac1SThierry Reding 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
70880d3eef1SDmitry Osipenko }
70980d3eef1SDmitry Osipenko 
7104aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
711977697e2SMaxime Ripard 				      struct drm_atomic_state *state)
7124aa3df71SThierry Reding {
71337418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
71437418bf1SMaxime Ripard 									   plane);
71541016fe1SMaxime Ripard 	struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
716e05162c0SMaxime Ripard 	struct drm_framebuffer *fb = new_state->fb;
7174aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
7184aa3df71SThierry Reding 	struct tegra_dc_window window;
7194aa3df71SThierry Reding 	unsigned int i;
7204aa3df71SThierry Reding 
7214aa3df71SThierry Reding 	/* rien ne va plus */
722e05162c0SMaxime Ripard 	if (!new_state->crtc || !new_state->fb)
7234aa3df71SThierry Reding 		return;
7244aa3df71SThierry Reding 
725e05162c0SMaxime Ripard 	if (!new_state->visible)
726977697e2SMaxime Ripard 		return tegra_plane_atomic_disable(plane, state);
72780d3eef1SDmitry Osipenko 
728c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
729e05162c0SMaxime Ripard 	window.src.x = new_state->src.x1 >> 16;
730e05162c0SMaxime Ripard 	window.src.y = new_state->src.y1 >> 16;
731e05162c0SMaxime Ripard 	window.src.w = drm_rect_width(&new_state->src) >> 16;
732e05162c0SMaxime Ripard 	window.src.h = drm_rect_height(&new_state->src) >> 16;
733e05162c0SMaxime Ripard 	window.dst.x = new_state->dst.x1;
734e05162c0SMaxime Ripard 	window.dst.y = new_state->dst.y1;
735e05162c0SMaxime Ripard 	window.dst.w = drm_rect_width(&new_state->dst);
736e05162c0SMaxime Ripard 	window.dst.h = drm_rect_height(&new_state->dst);
737272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
73841016fe1SMaxime Ripard 	window.reflect_x = tegra_plane_state->reflect_x;
73941016fe1SMaxime Ripard 	window.reflect_y = tegra_plane_state->reflect_y;
740c7679306SThierry Reding 
7418f604f8cSThierry Reding 	/* copy from state */
742e05162c0SMaxime Ripard 	window.zpos = new_state->normalized_zpos;
74341016fe1SMaxime Ripard 	window.tiling = tegra_plane_state->tiling;
74441016fe1SMaxime Ripard 	window.format = tegra_plane_state->format;
74541016fe1SMaxime Ripard 	window.swap = tegra_plane_state->swap;
746c7679306SThierry Reding 
747bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
74841016fe1SMaxime Ripard 		window.base[i] = tegra_plane_state->iova[i] + fb->offsets[i];
74908ee0178SDmitry Osipenko 
75008ee0178SDmitry Osipenko 		/*
75108ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
75208ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
75308ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
75408ee0178SDmitry Osipenko 		 */
75508ee0178SDmitry Osipenko 		if (i < 2)
7564aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
757c7679306SThierry Reding 	}
758c7679306SThierry Reding 
7591087fac1SThierry Reding 	tegra_dc_setup_window(p, &window);
7604aa3df71SThierry Reding }
7614aa3df71SThierry Reding 
762a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
7632e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
7642e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
7654aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
7664aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
767a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
768c7679306SThierry Reding };
769c7679306SThierry Reding 
77089f65018SThierry Reding static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
771c7679306SThierry Reding {
772518e6227SThierry Reding 	/*
773518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
774518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
775518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
776518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
777518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
778518e6227SThierry Reding 	 * here.
779518e6227SThierry Reding 	 *
780518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
781518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
782518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
783518e6227SThierry Reding 	 */
78489f65018SThierry Reding 	return 1 << drm->mode_config.num_crtc;
78589f65018SThierry Reding }
78689f65018SThierry Reding 
78789f65018SThierry Reding static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
78889f65018SThierry Reding 						    struct tegra_dc *dc)
78989f65018SThierry Reding {
79089f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
79147307954SThierry Reding 	enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
792c7679306SThierry Reding 	struct tegra_plane *plane;
793c7679306SThierry Reding 	unsigned int num_formats;
794e90124cbSThierry Reding 	const u64 *modifiers;
795c7679306SThierry Reding 	const u32 *formats;
796c7679306SThierry Reding 	int err;
797c7679306SThierry Reding 
798c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
799c7679306SThierry Reding 	if (!plane)
800c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
801c7679306SThierry Reding 
8021087fac1SThierry Reding 	/* Always use window A as primary window */
8031087fac1SThierry Reding 	plane->offset = 0xa00;
804c4755fb9SThierry Reding 	plane->index = 0;
8051087fac1SThierry Reding 	plane->dc = dc;
8061087fac1SThierry Reding 
8071087fac1SThierry Reding 	num_formats = dc->soc->num_primary_formats;
8081087fac1SThierry Reding 	formats = dc->soc->primary_formats;
809e90124cbSThierry Reding 	modifiers = dc->soc->modifiers;
810c4755fb9SThierry Reding 
811518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
812c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
813e90124cbSThierry Reding 				       num_formats, modifiers, type, NULL);
814c7679306SThierry Reding 	if (err < 0) {
815c7679306SThierry Reding 		kfree(plane);
816c7679306SThierry Reding 		return ERR_PTR(err);
817c7679306SThierry Reding 	}
818c7679306SThierry Reding 
819a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
8203dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
821ab7d3f58SThierry Reding 
822995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
823995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
824995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
8254fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
826cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
827995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
828995c5a50SThierry Reding 	if (err < 0)
829995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
830995c5a50SThierry Reding 			err);
831995c5a50SThierry Reding 
832c7679306SThierry Reding 	return &plane->base;
833c7679306SThierry Reding }
834c7679306SThierry Reding 
835c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
836c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
837c7679306SThierry Reding };
838c7679306SThierry Reding 
8394aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
8407c11b99aSMaxime Ripard 				     struct drm_atomic_state *state)
841c7679306SThierry Reding {
8427c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
8437c11b99aSMaxime Ripard 										 plane);
84447802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
84547802b09SThierry Reding 	int err;
84647802b09SThierry Reding 
8474aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
848ba5c1649SMaxime Ripard 	if (!new_plane_state->crtc)
8494aa3df71SThierry Reding 		return 0;
850c7679306SThierry Reding 
851c7679306SThierry Reding 	/* scaling not supported for cursor */
852ba5c1649SMaxime Ripard 	if ((new_plane_state->src_w >> 16 != new_plane_state->crtc_w) ||
853ba5c1649SMaxime Ripard 	    (new_plane_state->src_h >> 16 != new_plane_state->crtc_h))
854c7679306SThierry Reding 		return -EINVAL;
855c7679306SThierry Reding 
856c7679306SThierry Reding 	/* only square cursors supported */
857ba5c1649SMaxime Ripard 	if (new_plane_state->src_w != new_plane_state->src_h)
858c7679306SThierry Reding 		return -EINVAL;
859c7679306SThierry Reding 
860ba5c1649SMaxime Ripard 	if (new_plane_state->crtc_w != 32 && new_plane_state->crtc_w != 64 &&
861ba5c1649SMaxime Ripard 	    new_plane_state->crtc_w != 128 && new_plane_state->crtc_w != 256)
8624aa3df71SThierry Reding 		return -EINVAL;
8634aa3df71SThierry Reding 
864ba5c1649SMaxime Ripard 	err = tegra_plane_state_add(tegra, new_plane_state);
86547802b09SThierry Reding 	if (err < 0)
86647802b09SThierry Reding 		return err;
86747802b09SThierry Reding 
8684aa3df71SThierry Reding 	return 0;
8694aa3df71SThierry Reding }
8704aa3df71SThierry Reding 
8714aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
872977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
8734aa3df71SThierry Reding {
87437418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
87537418bf1SMaxime Ripard 									   plane);
87641016fe1SMaxime Ripard 	struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
877e05162c0SMaxime Ripard 	struct tegra_dc *dc = to_tegra_dc(new_state->crtc);
8784aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
8794aa3df71SThierry Reding 
8804aa3df71SThierry Reding 	/* rien ne va plus */
881e05162c0SMaxime Ripard 	if (!new_state->crtc || !new_state->fb)
8824aa3df71SThierry Reding 		return;
8834aa3df71SThierry Reding 
884e05162c0SMaxime Ripard 	switch (new_state->crtc_w) {
885c7679306SThierry Reding 	case 32:
886c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
887c7679306SThierry Reding 		break;
888c7679306SThierry Reding 
889c7679306SThierry Reding 	case 64:
890c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
891c7679306SThierry Reding 		break;
892c7679306SThierry Reding 
893c7679306SThierry Reding 	case 128:
894c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
895c7679306SThierry Reding 		break;
896c7679306SThierry Reding 
897c7679306SThierry Reding 	case 256:
898c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
899c7679306SThierry Reding 		break;
900c7679306SThierry Reding 
901c7679306SThierry Reding 	default:
902c52e167bSThierry Reding 		WARN(1, "cursor size %ux%u not supported\n",
903e05162c0SMaxime Ripard 		     new_state->crtc_w, new_state->crtc_h);
9044aa3df71SThierry Reding 		return;
905c7679306SThierry Reding 	}
906c7679306SThierry Reding 
90741016fe1SMaxime Ripard 	value |= (tegra_plane_state->iova[0] >> 10) & 0x3fffff;
908c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
909c7679306SThierry Reding 
910c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
91141016fe1SMaxime Ripard 	value = (tegra_plane_state->iova[0] >> 32) & 0x3;
912c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
913c7679306SThierry Reding #endif
914c7679306SThierry Reding 
915c7679306SThierry Reding 	/* enable cursor and set blend mode */
916c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
917c7679306SThierry Reding 	value |= CURSOR_ENABLE;
918c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
919c7679306SThierry Reding 
920c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
921c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
922c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
923c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
924c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
925c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
926c7679306SThierry Reding 	value |= CURSOR_ALPHA;
927c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
928c7679306SThierry Reding 
929c7679306SThierry Reding 	/* position the cursor */
930e05162c0SMaxime Ripard 	value = (new_state->crtc_y & 0x3fff) << 16 |
931e05162c0SMaxime Ripard 		(new_state->crtc_x & 0x3fff);
932c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
933c7679306SThierry Reding }
934c7679306SThierry Reding 
9354aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
936977697e2SMaxime Ripard 					struct drm_atomic_state *state)
937c7679306SThierry Reding {
938977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
939977697e2SMaxime Ripard 									   plane);
9404aa3df71SThierry Reding 	struct tegra_dc *dc;
941c7679306SThierry Reding 	u32 value;
942c7679306SThierry Reding 
9434aa3df71SThierry Reding 	/* rien ne va plus */
9444aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
9454aa3df71SThierry Reding 		return;
9464aa3df71SThierry Reding 
9474aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
948c7679306SThierry Reding 
949c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
950c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
951c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
952c7679306SThierry Reding }
953c7679306SThierry Reding 
9544aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
9552e8d8749SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
9562e8d8749SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
9574aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
9584aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
9594aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
960c7679306SThierry Reding };
961c7679306SThierry Reding 
962c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
963c7679306SThierry Reding 						      struct tegra_dc *dc)
964c7679306SThierry Reding {
96589f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
966c7679306SThierry Reding 	struct tegra_plane *plane;
967c7679306SThierry Reding 	unsigned int num_formats;
968c7679306SThierry Reding 	const u32 *formats;
969c7679306SThierry Reding 	int err;
970c7679306SThierry Reding 
971c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
972c7679306SThierry Reding 	if (!plane)
973c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
974c7679306SThierry Reding 
97547802b09SThierry Reding 	/*
976a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
977a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
978a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
979a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
980a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
98147802b09SThierry Reding 	 */
98247802b09SThierry Reding 	plane->index = 6;
9831087fac1SThierry Reding 	plane->dc = dc;
98447802b09SThierry Reding 
985c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
986c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
987c7679306SThierry Reding 
98889f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
989c1cb4b61SThierry Reding 				       &tegra_plane_funcs, formats,
990e6fc3b68SBen Widawsky 				       num_formats, NULL,
991e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
992c7679306SThierry Reding 	if (err < 0) {
993c7679306SThierry Reding 		kfree(plane);
994c7679306SThierry Reding 		return ERR_PTR(err);
995c7679306SThierry Reding 	}
996c7679306SThierry Reding 
9974aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
998fce3a51dSThierry Reding 	drm_plane_create_zpos_immutable_property(&plane->base, 255);
9994aa3df71SThierry Reding 
1000c7679306SThierry Reding 	return &plane->base;
1001c7679306SThierry Reding }
1002c7679306SThierry Reding 
1003511c7023SThierry Reding static const u32 tegra20_overlay_formats[] = {
1004511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1005511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1006dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
1007511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1008511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1009511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1010ebae8d07SThierry Reding 	/* non-native formats */
1011ebae8d07SThierry Reding 	DRM_FORMAT_XRGB1555,
1012ebae8d07SThierry Reding 	DRM_FORMAT_RGBX5551,
1013ebae8d07SThierry Reding 	DRM_FORMAT_XBGR8888,
1014ebae8d07SThierry Reding 	DRM_FORMAT_XRGB8888,
1015511c7023SThierry Reding 	/* planar formats */
1016511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1017511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1018511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1019511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1020511c7023SThierry Reding };
1021511c7023SThierry Reding 
1022511c7023SThierry Reding static const u32 tegra114_overlay_formats[] = {
1023511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1024511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1025511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1026511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1027511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1028511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1029511c7023SThierry Reding 	/* new on Tegra114 */
1030511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1031511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1032511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1033511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1034511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1035511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1036511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1037511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1038511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1039511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1040511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1041511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1042511c7023SThierry Reding 	/* planar formats */
1043511c7023SThierry Reding 	DRM_FORMAT_UYVY,
1044511c7023SThierry Reding 	DRM_FORMAT_YUYV,
1045511c7023SThierry Reding 	DRM_FORMAT_YUV420,
1046511c7023SThierry Reding 	DRM_FORMAT_YUV422,
1047511c7023SThierry Reding };
1048511c7023SThierry Reding 
1049511c7023SThierry Reding static const u32 tegra124_overlay_formats[] = {
1050511c7023SThierry Reding 	DRM_FORMAT_ARGB4444,
1051511c7023SThierry Reding 	DRM_FORMAT_ARGB1555,
1052511c7023SThierry Reding 	DRM_FORMAT_RGB565,
1053511c7023SThierry Reding 	DRM_FORMAT_RGBA5551,
1054511c7023SThierry Reding 	DRM_FORMAT_ABGR8888,
1055511c7023SThierry Reding 	DRM_FORMAT_ARGB8888,
1056511c7023SThierry Reding 	/* new on Tegra114 */
1057511c7023SThierry Reding 	DRM_FORMAT_ABGR4444,
1058511c7023SThierry Reding 	DRM_FORMAT_ABGR1555,
1059511c7023SThierry Reding 	DRM_FORMAT_BGRA5551,
1060511c7023SThierry Reding 	DRM_FORMAT_XRGB1555,
1061511c7023SThierry Reding 	DRM_FORMAT_RGBX5551,
1062511c7023SThierry Reding 	DRM_FORMAT_XBGR1555,
1063511c7023SThierry Reding 	DRM_FORMAT_BGRX5551,
1064511c7023SThierry Reding 	DRM_FORMAT_BGR565,
1065511c7023SThierry Reding 	DRM_FORMAT_BGRA8888,
1066511c7023SThierry Reding 	DRM_FORMAT_RGBA8888,
1067511c7023SThierry Reding 	DRM_FORMAT_XRGB8888,
1068511c7023SThierry Reding 	DRM_FORMAT_XBGR8888,
1069511c7023SThierry Reding 	/* new on Tegra124 */
1070511c7023SThierry Reding 	DRM_FORMAT_RGBX8888,
1071511c7023SThierry Reding 	DRM_FORMAT_BGRX8888,
1072511c7023SThierry Reding 	/* planar formats */
1073dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
1074f925390eSThierry Reding 	DRM_FORMAT_YUYV,
1075dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
1076dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
1077dee8268fSThierry Reding };
1078dee8268fSThierry Reding 
1079c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
1080c7679306SThierry Reding 						       struct tegra_dc *dc,
10819f446d83SDmitry Osipenko 						       unsigned int index,
10829f446d83SDmitry Osipenko 						       bool cursor)
1083dee8268fSThierry Reding {
108489f65018SThierry Reding 	unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1085dee8268fSThierry Reding 	struct tegra_plane *plane;
1086c7679306SThierry Reding 	unsigned int num_formats;
10879f446d83SDmitry Osipenko 	enum drm_plane_type type;
1088c7679306SThierry Reding 	const u32 *formats;
1089c7679306SThierry Reding 	int err;
1090dee8268fSThierry Reding 
1091f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1092dee8268fSThierry Reding 	if (!plane)
1093c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
1094dee8268fSThierry Reding 
10951087fac1SThierry Reding 	plane->offset = 0xa00 + 0x200 * index;
1096c7679306SThierry Reding 	plane->index = index;
10971087fac1SThierry Reding 	plane->dc = dc;
1098dee8268fSThierry Reding 
1099511c7023SThierry Reding 	num_formats = dc->soc->num_overlay_formats;
1100511c7023SThierry Reding 	formats = dc->soc->overlay_formats;
1101c7679306SThierry Reding 
11029f446d83SDmitry Osipenko 	if (!cursor)
11039f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_OVERLAY;
11049f446d83SDmitry Osipenko 	else
11059f446d83SDmitry Osipenko 		type = DRM_PLANE_TYPE_CURSOR;
11069f446d83SDmitry Osipenko 
110789f65018SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1108301e0ddbSThierry Reding 				       &tegra_plane_funcs, formats,
11099f446d83SDmitry Osipenko 				       num_formats, NULL, type, NULL);
1110f002abc1SThierry Reding 	if (err < 0) {
1111f002abc1SThierry Reding 		kfree(plane);
1112c7679306SThierry Reding 		return ERR_PTR(err);
1113dee8268fSThierry Reding 	}
1114c7679306SThierry Reding 
1115a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
11163dae08bcSDmitry Osipenko 	drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
1117ab7d3f58SThierry Reding 
1118995c5a50SThierry Reding 	err = drm_plane_create_rotation_property(&plane->base,
1119995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0,
1120995c5a50SThierry Reding 						 DRM_MODE_ROTATE_0 |
11214fba6d22SDmitry Osipenko 						 DRM_MODE_ROTATE_180 |
1122cd740777SDmitry Osipenko 						 DRM_MODE_REFLECT_X |
1123995c5a50SThierry Reding 						 DRM_MODE_REFLECT_Y);
1124995c5a50SThierry Reding 	if (err < 0)
1125995c5a50SThierry Reding 		dev_err(dc->dev, "failed to create rotation property: %d\n",
1126995c5a50SThierry Reding 			err);
1127995c5a50SThierry Reding 
1128c7679306SThierry Reding 	return &plane->base;
1129c7679306SThierry Reding }
1130c7679306SThierry Reding 
113147307954SThierry Reding static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
113247307954SThierry Reding 						    struct tegra_dc *dc)
1133c7679306SThierry Reding {
113447307954SThierry Reding 	struct drm_plane *plane, *primary = NULL;
113547307954SThierry Reding 	unsigned int i, j;
113647307954SThierry Reding 
113747307954SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
113847307954SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
113947307954SThierry Reding 
114047307954SThierry Reding 		if (wgrp->dc == dc->pipe) {
114147307954SThierry Reding 			for (j = 0; j < wgrp->num_windows; j++) {
114247307954SThierry Reding 				unsigned int index = wgrp->windows[j];
114347307954SThierry Reding 
114447307954SThierry Reding 				plane = tegra_shared_plane_create(drm, dc,
114547307954SThierry Reding 								  wgrp->index,
114647307954SThierry Reding 								  index);
114747307954SThierry Reding 				if (IS_ERR(plane))
114847307954SThierry Reding 					return plane;
114947307954SThierry Reding 
115047307954SThierry Reding 				/*
115147307954SThierry Reding 				 * Choose the first shared plane owned by this
115247307954SThierry Reding 				 * head as the primary plane.
115347307954SThierry Reding 				 */
115447307954SThierry Reding 				if (!primary) {
115547307954SThierry Reding 					plane->type = DRM_PLANE_TYPE_PRIMARY;
115647307954SThierry Reding 					primary = plane;
115747307954SThierry Reding 				}
115847307954SThierry Reding 			}
115947307954SThierry Reding 		}
116047307954SThierry Reding 	}
116147307954SThierry Reding 
116247307954SThierry Reding 	return primary;
116347307954SThierry Reding }
116447307954SThierry Reding 
116547307954SThierry Reding static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
116647307954SThierry Reding 					     struct tegra_dc *dc)
116747307954SThierry Reding {
11688f62142eSThierry Reding 	struct drm_plane *planes[2], *primary;
11699f446d83SDmitry Osipenko 	unsigned int planes_num;
1170c7679306SThierry Reding 	unsigned int i;
11718f62142eSThierry Reding 	int err;
1172c7679306SThierry Reding 
117347307954SThierry Reding 	primary = tegra_primary_plane_create(drm, dc);
117447307954SThierry Reding 	if (IS_ERR(primary))
117547307954SThierry Reding 		return primary;
117647307954SThierry Reding 
11779f446d83SDmitry Osipenko 	if (dc->soc->supports_cursor)
11789f446d83SDmitry Osipenko 		planes_num = 2;
11799f446d83SDmitry Osipenko 	else
11809f446d83SDmitry Osipenko 		planes_num = 1;
11819f446d83SDmitry Osipenko 
11829f446d83SDmitry Osipenko 	for (i = 0; i < planes_num; i++) {
11839f446d83SDmitry Osipenko 		planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
11849f446d83SDmitry Osipenko 							  false);
11858f62142eSThierry Reding 		if (IS_ERR(planes[i])) {
11868f62142eSThierry Reding 			err = PTR_ERR(planes[i]);
11878f62142eSThierry Reding 
11888f62142eSThierry Reding 			while (i--)
11898f62142eSThierry Reding 				tegra_plane_funcs.destroy(planes[i]);
11908f62142eSThierry Reding 
11918f62142eSThierry Reding 			tegra_plane_funcs.destroy(primary);
11928f62142eSThierry Reding 			return ERR_PTR(err);
119347307954SThierry Reding 		}
1194f002abc1SThierry Reding 	}
1195dee8268fSThierry Reding 
119647307954SThierry Reding 	return primary;
1197dee8268fSThierry Reding }
1198dee8268fSThierry Reding 
1199f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1200f002abc1SThierry Reding {
1201f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1202f002abc1SThierry Reding }
1203f002abc1SThierry Reding 
1204ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1205ca915b10SThierry Reding {
1206b7e0b04aSMaarten Lankhorst 	struct tegra_dc_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
1207ca915b10SThierry Reding 
12083b59b7acSThierry Reding 	if (crtc->state)
1209b7e0b04aSMaarten Lankhorst 		tegra_crtc_atomic_destroy_state(crtc, crtc->state);
12103b59b7acSThierry Reding 
1211b7e0b04aSMaarten Lankhorst 	__drm_atomic_helper_crtc_reset(crtc, &state->base);
1212ca915b10SThierry Reding }
1213ca915b10SThierry Reding 
1214ca915b10SThierry Reding static struct drm_crtc_state *
1215ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1216ca915b10SThierry Reding {
1217ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1218ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1219ca915b10SThierry Reding 
12203b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1221ca915b10SThierry Reding 	if (!copy)
1222ca915b10SThierry Reding 		return NULL;
1223ca915b10SThierry Reding 
12243b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
12253b59b7acSThierry Reding 	copy->clk = state->clk;
12263b59b7acSThierry Reding 	copy->pclk = state->pclk;
12273b59b7acSThierry Reding 	copy->div = state->div;
12283b59b7acSThierry Reding 	copy->planes = state->planes;
1229ca915b10SThierry Reding 
1230ca915b10SThierry Reding 	return &copy->base;
1231ca915b10SThierry Reding }
1232ca915b10SThierry Reding 
1233ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1234ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1235ca915b10SThierry Reding {
1236ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1237ca915b10SThierry Reding 	kfree(state);
1238ca915b10SThierry Reding }
1239ca915b10SThierry Reding 
1240b95800eeSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1241b95800eeSThierry Reding 
1242b95800eeSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1243b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1244b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1245b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1246b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1247b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1248b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1249b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1250b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1251b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1252b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1253b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1254b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1255b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1256b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1257b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1258b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1259b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1260b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1261b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1262b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1263b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1264b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1265b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1266b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1267b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1268b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1269b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1270b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1271b95800eeSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1272b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1273b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1274b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1275b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1276b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1277b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1278b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1279b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1280b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1281b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1282b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1283b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1284b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1285b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1286b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1287b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1288b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1289b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1290b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1291b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1292b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1293b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1294b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1295b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1296b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1297b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1298b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1299b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1300b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1301b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1302b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1303b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1304b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1305b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1306b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1307b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1308b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1309b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1310b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1311b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1312b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1313b95800eeSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1314b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1315b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1316b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1317b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1318b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1319b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1320b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1321b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1322b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1323b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1324b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1325b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1326b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1327b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1328b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1329b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1330b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1331b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1332b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1333b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1334b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1335b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1336b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1337b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1338b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1339b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1340b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1341b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1342b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1343b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1344b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1345b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1346b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1347b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1348b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1349b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1350b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1351b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1352b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1353b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1354b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1355b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1356b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1357b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1358b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1359b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1360b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1361b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1362b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1363b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1364b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1365b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1366b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1367b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1368b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1369b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1370b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1371b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1372b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1373b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1374b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1375b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1376b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1377b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1378b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1379b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1380b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1381b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1382b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1383b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1384b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1385b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1386b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1387b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1388b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1389b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1390b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1391b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1392b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1393b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1394b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1395b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1396b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1397b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1398b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1399b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1400b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1401b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1402b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1403b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1404b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1405b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1406b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1407b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1408b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1409b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1410b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1411b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1412b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1413b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1414b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1415b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1416b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1417b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1418b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1419b95800eeSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1420b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1421b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1422b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1423b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1424b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1425b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1426b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1427b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1428b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1429b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1430b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1431b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1432b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1433b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1434b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1435b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1436b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1437b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1438b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1439b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1440b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1441b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1442b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1443b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1444b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1445b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1446b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1447b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1448b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1449b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1450b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1451b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1452b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1453b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1454b95800eeSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1455b95800eeSThierry Reding };
1456b95800eeSThierry Reding 
1457b95800eeSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1458b95800eeSThierry Reding {
1459b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1460b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1461b95800eeSThierry Reding 	unsigned int i;
1462b95800eeSThierry Reding 	int err = 0;
1463b95800eeSThierry Reding 
1464b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1465b95800eeSThierry Reding 
1466b95800eeSThierry Reding 	if (!dc->base.state->active) {
1467b95800eeSThierry Reding 		err = -EBUSY;
1468b95800eeSThierry Reding 		goto unlock;
1469b95800eeSThierry Reding 	}
1470b95800eeSThierry Reding 
1471b95800eeSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1472b95800eeSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1473b95800eeSThierry Reding 
1474b95800eeSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1475b95800eeSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1476b95800eeSThierry Reding 	}
1477b95800eeSThierry Reding 
1478b95800eeSThierry Reding unlock:
1479b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1480b95800eeSThierry Reding 	return err;
1481b95800eeSThierry Reding }
1482b95800eeSThierry Reding 
1483b95800eeSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
1484b95800eeSThierry Reding {
1485b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1486b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1487b95800eeSThierry Reding 	int err = 0;
1488b95800eeSThierry Reding 	u32 value;
1489b95800eeSThierry Reding 
1490b95800eeSThierry Reding 	drm_modeset_lock(&dc->base.mutex, NULL);
1491b95800eeSThierry Reding 
1492b95800eeSThierry Reding 	if (!dc->base.state->active) {
1493b95800eeSThierry Reding 		err = -EBUSY;
1494b95800eeSThierry Reding 		goto unlock;
1495b95800eeSThierry Reding 	}
1496b95800eeSThierry Reding 
1497b95800eeSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1498b95800eeSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1499b95800eeSThierry Reding 	tegra_dc_commit(dc);
1500b95800eeSThierry Reding 
1501b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1502b95800eeSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
1503b95800eeSThierry Reding 
1504b95800eeSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1505b95800eeSThierry Reding 	seq_printf(s, "%08x\n", value);
1506b95800eeSThierry Reding 
1507b95800eeSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1508b95800eeSThierry Reding 
1509b95800eeSThierry Reding unlock:
1510b95800eeSThierry Reding 	drm_modeset_unlock(&dc->base.mutex);
1511b95800eeSThierry Reding 	return err;
1512b95800eeSThierry Reding }
1513b95800eeSThierry Reding 
1514b95800eeSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1515b95800eeSThierry Reding {
1516b95800eeSThierry Reding 	struct drm_info_node *node = s->private;
1517b95800eeSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1518b95800eeSThierry Reding 
1519b95800eeSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1520b95800eeSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1521b95800eeSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1522b95800eeSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1523b95800eeSThierry Reding 
1524b95800eeSThierry Reding 	return 0;
1525b95800eeSThierry Reding }
1526b95800eeSThierry Reding 
1527b95800eeSThierry Reding static struct drm_info_list debugfs_files[] = {
1528b95800eeSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1529b95800eeSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1530b95800eeSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1531b95800eeSThierry Reding };
1532b95800eeSThierry Reding 
1533b95800eeSThierry Reding static int tegra_dc_late_register(struct drm_crtc *crtc)
1534b95800eeSThierry Reding {
1535b95800eeSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
1536b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
153739f55c61SArnd Bergmann 	struct dentry *root;
1538b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1539b95800eeSThierry Reding 
154039f55c61SArnd Bergmann #ifdef CONFIG_DEBUG_FS
154139f55c61SArnd Bergmann 	root = crtc->debugfs_entry;
154239f55c61SArnd Bergmann #else
154339f55c61SArnd Bergmann 	root = NULL;
154439f55c61SArnd Bergmann #endif
154539f55c61SArnd Bergmann 
1546b95800eeSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1547b95800eeSThierry Reding 				    GFP_KERNEL);
1548b95800eeSThierry Reding 	if (!dc->debugfs_files)
1549b95800eeSThierry Reding 		return -ENOMEM;
1550b95800eeSThierry Reding 
1551b95800eeSThierry Reding 	for (i = 0; i < count; i++)
1552b95800eeSThierry Reding 		dc->debugfs_files[i].data = dc;
1553b95800eeSThierry Reding 
1554ad6d94f2SWambui Karuga 	drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1555b95800eeSThierry Reding 
1556b95800eeSThierry Reding 	return 0;
1557b95800eeSThierry Reding }
1558b95800eeSThierry Reding 
1559b95800eeSThierry Reding static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1560b95800eeSThierry Reding {
1561b95800eeSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
1562b95800eeSThierry Reding 	struct drm_minor *minor = crtc->dev->primary;
1563b95800eeSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1564b95800eeSThierry Reding 
1565b95800eeSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1566b95800eeSThierry Reding 	kfree(dc->debugfs_files);
1567b95800eeSThierry Reding 	dc->debugfs_files = NULL;
1568b95800eeSThierry Reding }
1569b95800eeSThierry Reding 
1570c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1571c49c81e2SThierry Reding {
1572c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1573c49c81e2SThierry Reding 
157447307954SThierry Reding 	/* XXX vblank syncpoints don't work with nvdisplay yet */
157547307954SThierry Reding 	if (dc->syncpt && !dc->soc->has_nvdisplay)
1576c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
1577c49c81e2SThierry Reding 
1578c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
15793abe2413SDhinakaran Pandiyan 	return (u32)drm_crtc_vblank_count(&dc->base);
1580c49c81e2SThierry Reding }
1581c49c81e2SThierry Reding 
1582c49c81e2SThierry Reding static int tegra_dc_enable_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 	return 0;
1592c49c81e2SThierry Reding }
1593c49c81e2SThierry Reding 
1594c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1595c49c81e2SThierry Reding {
1596c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1597363541e8SThierry Reding 	u32 value;
1598c49c81e2SThierry Reding 
1599c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1600c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
1601c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1602c49c81e2SThierry Reding }
1603c49c81e2SThierry Reding 
1604dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
16051503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
160674f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1607f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1608ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1609ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1610ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1611b95800eeSThierry Reding 	.late_register = tegra_dc_late_register,
1612b95800eeSThierry Reding 	.early_unregister = tegra_dc_early_unregister,
161310437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
161410437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
161510437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1616dee8268fSThierry Reding };
1617dee8268fSThierry Reding 
1618dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1619dee8268fSThierry Reding 				struct drm_display_mode *mode)
1620dee8268fSThierry Reding {
16210444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
16220444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1623dee8268fSThierry Reding 	unsigned long value;
1624dee8268fSThierry Reding 
162547307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1626dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1627dee8268fSThierry Reding 
1628dee8268fSThierry Reding 		value = (v_ref_to_sync << 16) | h_ref_to_sync;
1629dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
163047307954SThierry Reding 	}
1631dee8268fSThierry Reding 
1632dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1633dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1634dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1635dee8268fSThierry Reding 
1636dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1637dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1638dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1639dee8268fSThierry Reding 
1640dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1641dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1642dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1643dee8268fSThierry Reding 
1644dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1645dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1646dee8268fSThierry Reding 
1647dee8268fSThierry Reding 	return 0;
1648dee8268fSThierry Reding }
1649dee8268fSThierry Reding 
16509d910b60SThierry Reding /**
16519d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
16529d910b60SThierry Reding  *     state
16539d910b60SThierry Reding  * @dc: display controller
16549d910b60SThierry Reding  * @crtc_state: CRTC atomic state
16559d910b60SThierry Reding  * @clk: parent clock for display controller
16569d910b60SThierry Reding  * @pclk: pixel clock
16579d910b60SThierry Reding  * @div: shift clock divider
16589d910b60SThierry Reding  *
16599d910b60SThierry Reding  * Returns:
16609d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
16619d910b60SThierry Reding  */
1662ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1663ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1664ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1665ca915b10SThierry Reding 			       unsigned int div)
1666ca915b10SThierry Reding {
1667ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1668ca915b10SThierry Reding 
1669d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1670d2982748SThierry Reding 		return -EINVAL;
1671d2982748SThierry Reding 
1672ca915b10SThierry Reding 	state->clk = clk;
1673ca915b10SThierry Reding 	state->pclk = pclk;
1674ca915b10SThierry Reding 	state->div = div;
1675ca915b10SThierry Reding 
1676ca915b10SThierry Reding 	return 0;
1677ca915b10SThierry Reding }
1678ca915b10SThierry Reding 
167976d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
168076d59ed0SThierry Reding 				  struct tegra_dc_state *state)
168176d59ed0SThierry Reding {
168276d59ed0SThierry Reding 	u32 value;
168376d59ed0SThierry Reding 	int err;
168476d59ed0SThierry Reding 
168576d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
168676d59ed0SThierry Reding 	if (err < 0)
168776d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
168876d59ed0SThierry Reding 
168976d59ed0SThierry Reding 	/*
169076d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
169176d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
169276d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
169376d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
169476d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
169576d59ed0SThierry Reding 	 * should therefore be avoided.
169676d59ed0SThierry Reding 	 */
169776d59ed0SThierry Reding 	if (state->pclk > 0) {
169876d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
169976d59ed0SThierry Reding 		if (err < 0)
170076d59ed0SThierry Reding 			dev_err(dc->dev,
170176d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
170276d59ed0SThierry Reding 				state->pclk);
1703f8fb97c9SDmitry Osipenko 
1704f8fb97c9SDmitry Osipenko 		err = clk_set_rate(dc->clk, state->pclk);
1705f8fb97c9SDmitry Osipenko 		if (err < 0)
1706f8fb97c9SDmitry Osipenko 			dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1707f8fb97c9SDmitry Osipenko 				dc->clk, state->pclk, err);
170876d59ed0SThierry Reding 	}
170976d59ed0SThierry Reding 
171076d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
171176d59ed0SThierry Reding 		      state->div);
171276d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
171376d59ed0SThierry Reding 
171447307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
171576d59ed0SThierry Reding 		value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
171676d59ed0SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
171747307954SThierry Reding 	}
171876d59ed0SThierry Reding }
171976d59ed0SThierry Reding 
1720003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1721003fc848SThierry Reding {
1722003fc848SThierry Reding 	u32 value;
1723003fc848SThierry Reding 
1724003fc848SThierry Reding 	/* stop the display controller */
1725003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1726003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1727003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1728003fc848SThierry Reding 
1729003fc848SThierry Reding 	tegra_dc_commit(dc);
1730003fc848SThierry Reding }
1731003fc848SThierry Reding 
1732003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1733003fc848SThierry Reding {
1734003fc848SThierry Reding 	u32 value;
1735003fc848SThierry Reding 
1736003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1737003fc848SThierry Reding 
1738003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1739003fc848SThierry Reding }
1740003fc848SThierry Reding 
1741003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1742003fc848SThierry Reding {
1743003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1744003fc848SThierry Reding 
1745003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1746003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1747003fc848SThierry Reding 			return 0;
1748003fc848SThierry Reding 
1749003fc848SThierry Reding 		usleep_range(1000, 2000);
1750003fc848SThierry Reding 	}
1751003fc848SThierry Reding 
1752003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1753003fc848SThierry Reding 	return -ETIMEDOUT;
1754003fc848SThierry Reding }
1755003fc848SThierry Reding 
175664581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1757351f950dSMaxime Ripard 				      struct drm_atomic_state *state)
1758003fc848SThierry Reding {
1759003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1760003fc848SThierry Reding 	u32 value;
1761fd67e9c6SThierry Reding 	int err;
1762003fc848SThierry Reding 
1763003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1764003fc848SThierry Reding 		tegra_dc_stop(dc);
1765003fc848SThierry Reding 
1766003fc848SThierry Reding 		/*
1767003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1768003fc848SThierry Reding 		 * in case this fails.
1769003fc848SThierry Reding 		 */
1770003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1771003fc848SThierry Reding 	}
1772003fc848SThierry Reding 
1773003fc848SThierry Reding 	/*
1774003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1775003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1776003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1777003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1778003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1779003fc848SThierry Reding 	 * to go idle.
1780003fc848SThierry Reding 	 *
1781003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1782003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1783003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1784003fc848SThierry Reding 	 *
1785003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1786003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1787003fc848SThierry Reding 	 * the RGB encoder?
1788003fc848SThierry Reding 	 */
1789003fc848SThierry Reding 	if (dc->rgb) {
1790003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1791003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1792003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1793003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1794003fc848SThierry Reding 	}
1795003fc848SThierry Reding 
1796003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1797003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
179833a8eb8dSThierry Reding 
17999d99ab6eSThierry Reding 	spin_lock_irq(&crtc->dev->event_lock);
18009d99ab6eSThierry Reding 
18019d99ab6eSThierry Reding 	if (crtc->state->event) {
18029d99ab6eSThierry Reding 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
18039d99ab6eSThierry Reding 		crtc->state->event = NULL;
18049d99ab6eSThierry Reding 	}
18059d99ab6eSThierry Reding 
18069d99ab6eSThierry Reding 	spin_unlock_irq(&crtc->dev->event_lock);
18079d99ab6eSThierry Reding 
1808fd67e9c6SThierry Reding 	err = host1x_client_suspend(&dc->client);
1809fd67e9c6SThierry Reding 	if (err < 0)
1810fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to suspend: %d\n", err);
1811003fc848SThierry Reding }
1812003fc848SThierry Reding 
18130b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1814351f950dSMaxime Ripard 				     struct drm_atomic_state *state)
1815dee8268fSThierry Reding {
18164aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1817351f950dSMaxime Ripard 	struct tegra_dc_state *crtc_state = to_dc_state(crtc->state);
1818dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1819dbb3f2f7SThierry Reding 	u32 value;
1820fd67e9c6SThierry Reding 	int err;
1821dee8268fSThierry Reding 
1822fd67e9c6SThierry Reding 	err = host1x_client_resume(&dc->client);
1823fd67e9c6SThierry Reding 	if (err < 0) {
1824fd67e9c6SThierry Reding 		dev_err(dc->dev, "failed to resume: %d\n", err);
1825fd67e9c6SThierry Reding 		return;
1826fd67e9c6SThierry Reding 	}
182733a8eb8dSThierry Reding 
182833a8eb8dSThierry Reding 	/* initialize display controller */
182933a8eb8dSThierry Reding 	if (dc->syncpt) {
183047307954SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
183147307954SThierry Reding 
183247307954SThierry Reding 		if (dc->soc->has_nvdisplay)
183347307954SThierry Reding 			enable = 1 << 31;
183447307954SThierry Reding 		else
183547307954SThierry Reding 			enable = 1 << 8;
183633a8eb8dSThierry Reding 
183733a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
183833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
183933a8eb8dSThierry Reding 
184047307954SThierry Reding 		value = enable | syncpt;
184133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
184233a8eb8dSThierry Reding 	}
184333a8eb8dSThierry Reding 
184447307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
184547307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
184647307954SThierry Reding 			DSC_OBUF_UF_INT;
184747307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
184847307954SThierry Reding 
184947307954SThierry Reding 		value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
185047307954SThierry Reding 			DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
185147307954SThierry Reding 			HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
185247307954SThierry Reding 			REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
185347307954SThierry Reding 			VBLANK_INT | FRAME_END_INT;
185447307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
185547307954SThierry Reding 
185647307954SThierry Reding 		value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
185747307954SThierry Reding 			FRAME_END_INT;
185847307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
185947307954SThierry Reding 
186047307954SThierry Reding 		value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
186147307954SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
186247307954SThierry Reding 
186347307954SThierry Reding 		tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
186447307954SThierry Reding 	} else {
186533a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
186633a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
186733a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
186833a8eb8dSThierry Reding 
186933a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
187033a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
187133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
187233a8eb8dSThierry Reding 
187333a8eb8dSThierry Reding 		/* initialize timer */
187433a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
187533a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
187633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
187733a8eb8dSThierry Reding 
187833a8eb8dSThierry Reding 		value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
187933a8eb8dSThierry Reding 			WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
188033a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
188133a8eb8dSThierry Reding 
188233a8eb8dSThierry Reding 		value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
188333a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
188433a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
188533a8eb8dSThierry Reding 
188633a8eb8dSThierry Reding 		value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
188733a8eb8dSThierry Reding 			WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
188833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
188947307954SThierry Reding 	}
189033a8eb8dSThierry Reding 
18917116e9a8SThierry Reding 	if (dc->soc->supports_background_color)
18927116e9a8SThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
18937116e9a8SThierry Reding 	else
189433a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
189533a8eb8dSThierry Reding 
189633a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
1897351f950dSMaxime Ripard 	tegra_dc_commit_state(dc, crtc_state);
189876d59ed0SThierry Reding 
1899dee8268fSThierry Reding 	/* program display mode */
1900dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1901dee8268fSThierry Reding 
19028620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
19038620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
19048620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
19058620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
19068620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
19078620fc62SThierry Reding 	}
1908666cb873SThierry Reding 
1909666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1910666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1911666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1912666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1913666cb873SThierry Reding 
191447307954SThierry Reding 	if (!dc->soc->has_nvdisplay) {
1915666cb873SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1916666cb873SThierry Reding 		value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1917666cb873SThierry Reding 			 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1918666cb873SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
191947307954SThierry Reding 	}
192047307954SThierry Reding 
192147307954SThierry Reding 	/* enable underflow reporting and display red for missing pixels */
192247307954SThierry Reding 	if (dc->soc->has_nvdisplay) {
192347307954SThierry Reding 		value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
192447307954SThierry Reding 		tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
192547307954SThierry Reding 	}
1926666cb873SThierry Reding 
1927666cb873SThierry Reding 	tegra_dc_commit(dc);
1928dee8268fSThierry Reding 
19298ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1930dee8268fSThierry Reding }
1931dee8268fSThierry Reding 
1932613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1933f6ebe9f9SMaxime Ripard 				    struct drm_atomic_state *state)
19344aa3df71SThierry Reding {
19359d99ab6eSThierry Reding 	unsigned long flags;
19361503ca47SThierry Reding 
19371503ca47SThierry Reding 	if (crtc->state->event) {
19389d99ab6eSThierry Reding 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
19391503ca47SThierry Reding 
19409d99ab6eSThierry Reding 		if (drm_crtc_vblank_get(crtc) != 0)
19419d99ab6eSThierry Reding 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
19429d99ab6eSThierry Reding 		else
19439d99ab6eSThierry Reding 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
19441503ca47SThierry Reding 
19459d99ab6eSThierry Reding 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
19469d99ab6eSThierry Reding 
19471503ca47SThierry Reding 		crtc->state->event = NULL;
19481503ca47SThierry Reding 	}
19494aa3df71SThierry Reding }
19504aa3df71SThierry Reding 
1951613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1952f6ebe9f9SMaxime Ripard 				    struct drm_atomic_state *state)
19534aa3df71SThierry Reding {
1954253f28b6SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1955253f28b6SMaxime Ripard 									  crtc);
1956253f28b6SMaxime Ripard 	struct tegra_dc_state *dc_state = to_dc_state(crtc_state);
195747802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
195847307954SThierry Reding 	u32 value;
195947802b09SThierry Reding 
1960253f28b6SMaxime Ripard 	value = dc_state->planes << 8 | GENERAL_UPDATE;
196147307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
196247307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
196347307954SThierry Reding 
1964253f28b6SMaxime Ripard 	value = dc_state->planes | GENERAL_ACT_REQ;
196547307954SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
196647307954SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
19674aa3df71SThierry Reding }
19684aa3df71SThierry Reding 
1969dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
19704aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
19714aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
19720b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
197364581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1974dee8268fSThierry Reding };
1975dee8268fSThierry Reding 
1976dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1977dee8268fSThierry Reding {
1978dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1979dee8268fSThierry Reding 	unsigned long status;
1980dee8268fSThierry Reding 
1981dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1982dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1983dee8268fSThierry Reding 
1984dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1985dee8268fSThierry Reding 		/*
1986dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1987dee8268fSThierry Reding 		*/
1988791ddb1eSThierry Reding 		dc->stats.frames++;
1989dee8268fSThierry Reding 	}
1990dee8268fSThierry Reding 
1991dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1992dee8268fSThierry Reding 		/*
1993dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1994dee8268fSThierry Reding 		*/
1995ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1996791ddb1eSThierry Reding 		dc->stats.vblank++;
1997dee8268fSThierry Reding 	}
1998dee8268fSThierry Reding 
1999dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
2000dee8268fSThierry Reding 		/*
2001dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
2002dee8268fSThierry Reding 		*/
2003791ddb1eSThierry Reding 		dc->stats.underflow++;
2004791ddb1eSThierry Reding 	}
2005791ddb1eSThierry Reding 
2006791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
2007791ddb1eSThierry Reding 		/*
2008791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
2009791ddb1eSThierry Reding 		*/
2010791ddb1eSThierry Reding 		dc->stats.overflow++;
2011dee8268fSThierry Reding 	}
2012dee8268fSThierry Reding 
201347307954SThierry Reding 	if (status & HEAD_UF_INT) {
201447307954SThierry Reding 		dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
201547307954SThierry Reding 		dc->stats.underflow++;
201647307954SThierry Reding 	}
201747307954SThierry Reding 
2018dee8268fSThierry Reding 	return IRQ_HANDLED;
2019dee8268fSThierry Reding }
2020dee8268fSThierry Reding 
2021e75d0477SThierry Reding static bool tegra_dc_has_window_groups(struct tegra_dc *dc)
2022e75d0477SThierry Reding {
2023e75d0477SThierry Reding 	unsigned int i;
2024e75d0477SThierry Reding 
2025e75d0477SThierry Reding 	if (!dc->soc->wgrps)
2026e75d0477SThierry Reding 		return true;
2027e75d0477SThierry Reding 
2028e75d0477SThierry Reding 	for (i = 0; i < dc->soc->num_wgrps; i++) {
2029e75d0477SThierry Reding 		const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
2030e75d0477SThierry Reding 
2031e75d0477SThierry Reding 		if (wgrp->dc == dc->pipe && wgrp->num_windows > 0)
2032e75d0477SThierry Reding 			return true;
2033e75d0477SThierry Reding 	}
2034e75d0477SThierry Reding 
2035e75d0477SThierry Reding 	return false;
2036e75d0477SThierry Reding }
2037e75d0477SThierry Reding 
2038dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
2039dee8268fSThierry Reding {
2040608f43adSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
20412bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
2042dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2043d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
2044c7679306SThierry Reding 	struct drm_plane *primary = NULL;
2045c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
2046dee8268fSThierry Reding 	int err;
2047dee8268fSThierry Reding 
2048759d706fSThierry Reding 	/*
2049f5ba33fbSMikko Perttunen 	 * DC has been reset by now, so VBLANK syncpoint can be released
2050f5ba33fbSMikko Perttunen 	 * for general use.
2051f5ba33fbSMikko Perttunen 	 */
2052f5ba33fbSMikko Perttunen 	host1x_syncpt_release_vblank_reservation(client, 26 + dc->pipe);
2053f5ba33fbSMikko Perttunen 
2054f5ba33fbSMikko Perttunen 	/*
2055759d706fSThierry Reding 	 * XXX do not register DCs with no window groups because we cannot
2056759d706fSThierry Reding 	 * assign a primary plane to them, which in turn will cause KMS to
2057759d706fSThierry Reding 	 * crash.
2058759d706fSThierry Reding 	 */
2059e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2060759d706fSThierry Reding 		return 0;
2061759d706fSThierry Reding 
2062fd67e9c6SThierry Reding 	/*
2063fd67e9c6SThierry Reding 	 * Set the display hub as the host1x client parent for the display
2064fd67e9c6SThierry Reding 	 * controller. This is needed for the runtime reference counting that
2065fd67e9c6SThierry Reding 	 * ensures the display hub is always powered when any of the display
2066fd67e9c6SThierry Reding 	 * controllers are.
2067fd67e9c6SThierry Reding 	 */
2068fd67e9c6SThierry Reding 	if (dc->soc->has_nvdisplay)
2069fd67e9c6SThierry Reding 		client->parent = &tegra->hub->client;
2070fd67e9c6SThierry Reding 
2071617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
20722bcdcbfaSThierry Reding 	if (!dc->syncpt)
20732bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
20742bcdcbfaSThierry Reding 
20757edd7961SThierry Reding 	err = host1x_client_iommu_attach(client);
2076a8817489SThierry Reding 	if (err < 0 && err != -ENODEV) {
20770c407de5SThierry Reding 		dev_err(client->dev, "failed to attach to domain: %d\n", err);
2078df06b759SThierry Reding 		return err;
2079df06b759SThierry Reding 	}
2080df06b759SThierry Reding 
208147307954SThierry Reding 	if (dc->soc->wgrps)
208247307954SThierry Reding 		primary = tegra_dc_add_shared_planes(drm, dc);
208347307954SThierry Reding 	else
208447307954SThierry Reding 		primary = tegra_dc_add_planes(drm, dc);
208547307954SThierry Reding 
2086c7679306SThierry Reding 	if (IS_ERR(primary)) {
2087c7679306SThierry Reding 		err = PTR_ERR(primary);
2088c7679306SThierry Reding 		goto cleanup;
2089c7679306SThierry Reding 	}
2090c7679306SThierry Reding 
2091c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
2092c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
2093c7679306SThierry Reding 		if (IS_ERR(cursor)) {
2094c7679306SThierry Reding 			err = PTR_ERR(cursor);
2095c7679306SThierry Reding 			goto cleanup;
2096c7679306SThierry Reding 		}
20979f446d83SDmitry Osipenko 	} else {
20989f446d83SDmitry Osipenko 		/* dedicate one overlay to mouse cursor */
20999f446d83SDmitry Osipenko 		cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
21009f446d83SDmitry Osipenko 		if (IS_ERR(cursor)) {
21019f446d83SDmitry Osipenko 			err = PTR_ERR(cursor);
21029f446d83SDmitry Osipenko 			goto cleanup;
21039f446d83SDmitry Osipenko 		}
2104c7679306SThierry Reding 	}
2105c7679306SThierry Reding 
2106c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
2107f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
2108c7679306SThierry Reding 	if (err < 0)
2109c7679306SThierry Reding 		goto cleanup;
2110c7679306SThierry Reding 
2111dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
2112dee8268fSThierry Reding 
2113d1f3e1e0SThierry Reding 	/*
2114d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
2115d1f3e1e0SThierry Reding 	 * controllers.
2116d1f3e1e0SThierry Reding 	 */
2117d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
2118d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
2119d1f3e1e0SThierry Reding 
2120*042c0bd7SThierry Reding 	/* track maximum resolution */
2121*042c0bd7SThierry Reding 	if (dc->soc->has_nvdisplay)
2122*042c0bd7SThierry Reding 		drm->mode_config.max_width = drm->mode_config.max_height = 16384;
2123*042c0bd7SThierry Reding 	else
2124*042c0bd7SThierry Reding 		drm->mode_config.max_width = drm->mode_config.max_height = 4096;
2125*042c0bd7SThierry Reding 
21269910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
2127dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2128dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
2129c7679306SThierry Reding 		goto cleanup;
2130dee8268fSThierry Reding 	}
2131dee8268fSThierry Reding 
2132dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
2133dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
2134dee8268fSThierry Reding 	if (err < 0) {
2135dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
2136dee8268fSThierry Reding 			err);
2137c7679306SThierry Reding 		goto cleanup;
2138dee8268fSThierry Reding 	}
2139dee8268fSThierry Reding 
214047b15779SThierry Reding 	/*
214147b15779SThierry Reding 	 * Inherit the DMA parameters (such as maximum segment size) from the
2142608f43adSThierry Reding 	 * parent host1x device.
214347b15779SThierry Reding 	 */
2144608f43adSThierry Reding 	client->dev->dma_parms = client->host->dma_parms;
214547b15779SThierry Reding 
2146dee8268fSThierry Reding 	return 0;
2147c7679306SThierry Reding 
2148c7679306SThierry Reding cleanup:
214947307954SThierry Reding 	if (!IS_ERR_OR_NULL(cursor))
2150c7679306SThierry Reding 		drm_plane_cleanup(cursor);
2151c7679306SThierry Reding 
215247307954SThierry Reding 	if (!IS_ERR(primary))
2153c7679306SThierry Reding 		drm_plane_cleanup(primary);
2154c7679306SThierry Reding 
2155aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
21562aed4f5aSMikko Perttunen 	host1x_syncpt_put(dc->syncpt);
2157fd5ec0dcSThierry Reding 
2158c7679306SThierry Reding 	return err;
2159dee8268fSThierry Reding }
2160dee8268fSThierry Reding 
2161dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
2162dee8268fSThierry Reding {
2163dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2164dee8268fSThierry Reding 	int err;
2165dee8268fSThierry Reding 
2166e75d0477SThierry Reding 	if (!tegra_dc_has_window_groups(dc))
2167e75d0477SThierry Reding 		return 0;
2168e75d0477SThierry Reding 
216947b15779SThierry Reding 	/* avoid a dangling pointer just in case this disappears */
217047b15779SThierry Reding 	client->dev->dma_parms = NULL;
217147b15779SThierry Reding 
2172dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
2173dee8268fSThierry Reding 
2174dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
2175dee8268fSThierry Reding 	if (err) {
2176dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
2177dee8268fSThierry Reding 		return err;
2178dee8268fSThierry Reding 	}
2179dee8268fSThierry Reding 
2180aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
21812aed4f5aSMikko Perttunen 	host1x_syncpt_put(dc->syncpt);
21822bcdcbfaSThierry Reding 
2183dee8268fSThierry Reding 	return 0;
2184dee8268fSThierry Reding }
2185dee8268fSThierry Reding 
2186fd67e9c6SThierry Reding static int tegra_dc_runtime_suspend(struct host1x_client *client)
2187fd67e9c6SThierry Reding {
2188fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2189fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2190fd67e9c6SThierry Reding 	int err;
2191fd67e9c6SThierry Reding 
2192fd67e9c6SThierry Reding 	err = reset_control_assert(dc->rst);
2193fd67e9c6SThierry Reding 	if (err < 0) {
2194fd67e9c6SThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
2195fd67e9c6SThierry Reding 		return err;
2196fd67e9c6SThierry Reding 	}
2197fd67e9c6SThierry Reding 
2198fd67e9c6SThierry Reding 	if (dc->soc->has_powergate)
2199fd67e9c6SThierry Reding 		tegra_powergate_power_off(dc->powergate);
2200fd67e9c6SThierry Reding 
2201fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2202fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2203fd67e9c6SThierry Reding 
2204fd67e9c6SThierry Reding 	return 0;
2205fd67e9c6SThierry Reding }
2206fd67e9c6SThierry Reding 
2207fd67e9c6SThierry Reding static int tegra_dc_runtime_resume(struct host1x_client *client)
2208fd67e9c6SThierry Reding {
2209fd67e9c6SThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
2210fd67e9c6SThierry Reding 	struct device *dev = client->dev;
2211fd67e9c6SThierry Reding 	int err;
2212fd67e9c6SThierry Reding 
2213dcdfe271SQinglang Miao 	err = pm_runtime_resume_and_get(dev);
2214fd67e9c6SThierry Reding 	if (err < 0) {
2215fd67e9c6SThierry Reding 		dev_err(dev, "failed to get runtime PM: %d\n", err);
2216fd67e9c6SThierry Reding 		return err;
2217fd67e9c6SThierry Reding 	}
2218fd67e9c6SThierry Reding 
2219fd67e9c6SThierry Reding 	if (dc->soc->has_powergate) {
2220fd67e9c6SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2221fd67e9c6SThierry Reding 							dc->rst);
2222fd67e9c6SThierry Reding 		if (err < 0) {
2223fd67e9c6SThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
2224fd67e9c6SThierry Reding 			goto put_rpm;
2225fd67e9c6SThierry Reding 		}
2226fd67e9c6SThierry Reding 	} else {
2227fd67e9c6SThierry Reding 		err = clk_prepare_enable(dc->clk);
2228fd67e9c6SThierry Reding 		if (err < 0) {
2229fd67e9c6SThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
2230fd67e9c6SThierry Reding 			goto put_rpm;
2231fd67e9c6SThierry Reding 		}
2232fd67e9c6SThierry Reding 
2233fd67e9c6SThierry Reding 		err = reset_control_deassert(dc->rst);
2234fd67e9c6SThierry Reding 		if (err < 0) {
2235fd67e9c6SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
2236fd67e9c6SThierry Reding 			goto disable_clk;
2237fd67e9c6SThierry Reding 		}
2238fd67e9c6SThierry Reding 	}
2239fd67e9c6SThierry Reding 
2240fd67e9c6SThierry Reding 	return 0;
2241fd67e9c6SThierry Reding 
2242fd67e9c6SThierry Reding disable_clk:
2243fd67e9c6SThierry Reding 	clk_disable_unprepare(dc->clk);
2244fd67e9c6SThierry Reding put_rpm:
2245fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
2246fd67e9c6SThierry Reding 	return err;
2247fd67e9c6SThierry Reding }
2248fd67e9c6SThierry Reding 
2249dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
2250dee8268fSThierry Reding 	.init = tegra_dc_init,
2251dee8268fSThierry Reding 	.exit = tegra_dc_exit,
2252fd67e9c6SThierry Reding 	.suspend = tegra_dc_runtime_suspend,
2253fd67e9c6SThierry Reding 	.resume = tegra_dc_runtime_resume,
2254dee8268fSThierry Reding };
2255dee8268fSThierry Reding 
22568620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
22577116e9a8SThierry Reding 	.supports_background_color = false,
22588620fc62SThierry Reding 	.supports_interlacing = false,
2259e687651bSThierry Reding 	.supports_cursor = false,
2260c134f019SThierry Reding 	.supports_block_linear = false,
2261a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2262d1f3e1e0SThierry Reding 	.pitch_align = 8,
22639c012700SThierry Reding 	.has_powergate = false,
2264f68ba691SDmitry Osipenko 	.coupled_pm = true,
226547307954SThierry Reding 	.has_nvdisplay = false,
2266511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2267511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2268511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2269511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2270e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2271acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = true,
2272acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = true,
22738620fc62SThierry Reding };
22748620fc62SThierry Reding 
22758620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
22767116e9a8SThierry Reding 	.supports_background_color = false,
22778620fc62SThierry Reding 	.supports_interlacing = false,
2278e687651bSThierry Reding 	.supports_cursor = false,
2279c134f019SThierry Reding 	.supports_block_linear = false,
2280a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2281d1f3e1e0SThierry Reding 	.pitch_align = 8,
22829c012700SThierry Reding 	.has_powergate = false,
2283f68ba691SDmitry Osipenko 	.coupled_pm = false,
228447307954SThierry Reding 	.has_nvdisplay = false,
2285511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2286511c7023SThierry Reding 	.primary_formats = tegra20_primary_formats,
2287511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2288511c7023SThierry Reding 	.overlay_formats = tegra20_overlay_formats,
2289e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2290acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2291acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
2292d1f3e1e0SThierry Reding };
2293d1f3e1e0SThierry Reding 
2294d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
22957116e9a8SThierry Reding 	.supports_background_color = false,
2296d1f3e1e0SThierry Reding 	.supports_interlacing = false,
2297d1f3e1e0SThierry Reding 	.supports_cursor = false,
2298d1f3e1e0SThierry Reding 	.supports_block_linear = false,
2299a43d0a00SDmitry Osipenko 	.has_legacy_blending = true,
2300d1f3e1e0SThierry Reding 	.pitch_align = 64,
23019c012700SThierry Reding 	.has_powergate = true,
2302f68ba691SDmitry Osipenko 	.coupled_pm = false,
230347307954SThierry Reding 	.has_nvdisplay = false,
2304511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2305511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2306511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2307511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2308e90124cbSThierry Reding 	.modifiers = tegra20_modifiers,
2309acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2310acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
23118620fc62SThierry Reding };
23128620fc62SThierry Reding 
23138620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
23147116e9a8SThierry Reding 	.supports_background_color = true,
23158620fc62SThierry Reding 	.supports_interlacing = true,
2316e687651bSThierry Reding 	.supports_cursor = true,
2317c134f019SThierry Reding 	.supports_block_linear = true,
2318a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
2319d1f3e1e0SThierry Reding 	.pitch_align = 64,
23209c012700SThierry Reding 	.has_powergate = true,
2321f68ba691SDmitry Osipenko 	.coupled_pm = false,
232247307954SThierry Reding 	.has_nvdisplay = false,
2323511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
23249a02d3afSStefan Agner 	.primary_formats = tegra124_primary_formats,
2325511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
23269a02d3afSStefan Agner 	.overlay_formats = tegra124_overlay_formats,
2327e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2328acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2329acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
23308620fc62SThierry Reding };
23318620fc62SThierry Reding 
23325b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
23337116e9a8SThierry Reding 	.supports_background_color = true,
23345b4f516fSThierry Reding 	.supports_interlacing = true,
23355b4f516fSThierry Reding 	.supports_cursor = true,
23365b4f516fSThierry Reding 	.supports_block_linear = true,
2337a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
23385b4f516fSThierry Reding 	.pitch_align = 64,
23395b4f516fSThierry Reding 	.has_powergate = true,
2340f68ba691SDmitry Osipenko 	.coupled_pm = false,
234147307954SThierry Reding 	.has_nvdisplay = false,
2342511c7023SThierry Reding 	.num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2343511c7023SThierry Reding 	.primary_formats = tegra114_primary_formats,
2344511c7023SThierry Reding 	.num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2345511c7023SThierry Reding 	.overlay_formats = tegra114_overlay_formats,
2346e90124cbSThierry Reding 	.modifiers = tegra124_modifiers,
2347acc6a3a9SDmitry Osipenko 	.has_win_a_without_filters = false,
2348acc6a3a9SDmitry Osipenko 	.has_win_c_without_vert_filter = false,
234947307954SThierry Reding };
235047307954SThierry Reding 
235147307954SThierry Reding static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
235247307954SThierry Reding 	{
235347307954SThierry Reding 		.index = 0,
235447307954SThierry Reding 		.dc = 0,
235547307954SThierry Reding 		.windows = (const unsigned int[]) { 0 },
235647307954SThierry Reding 		.num_windows = 1,
235747307954SThierry Reding 	}, {
235847307954SThierry Reding 		.index = 1,
235947307954SThierry Reding 		.dc = 1,
236047307954SThierry Reding 		.windows = (const unsigned int[]) { 1 },
236147307954SThierry Reding 		.num_windows = 1,
236247307954SThierry Reding 	}, {
236347307954SThierry Reding 		.index = 2,
236447307954SThierry Reding 		.dc = 1,
236547307954SThierry Reding 		.windows = (const unsigned int[]) { 2 },
236647307954SThierry Reding 		.num_windows = 1,
236747307954SThierry Reding 	}, {
236847307954SThierry Reding 		.index = 3,
236947307954SThierry Reding 		.dc = 2,
237047307954SThierry Reding 		.windows = (const unsigned int[]) { 3 },
237147307954SThierry Reding 		.num_windows = 1,
237247307954SThierry Reding 	}, {
237347307954SThierry Reding 		.index = 4,
237447307954SThierry Reding 		.dc = 2,
237547307954SThierry Reding 		.windows = (const unsigned int[]) { 4 },
237647307954SThierry Reding 		.num_windows = 1,
237747307954SThierry Reding 	}, {
237847307954SThierry Reding 		.index = 5,
237947307954SThierry Reding 		.dc = 2,
238047307954SThierry Reding 		.windows = (const unsigned int[]) { 5 },
238147307954SThierry Reding 		.num_windows = 1,
238247307954SThierry Reding 	},
238347307954SThierry Reding };
238447307954SThierry Reding 
238547307954SThierry Reding static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
238647307954SThierry Reding 	.supports_background_color = true,
238747307954SThierry Reding 	.supports_interlacing = true,
238847307954SThierry Reding 	.supports_cursor = true,
238947307954SThierry Reding 	.supports_block_linear = true,
2390a43d0a00SDmitry Osipenko 	.has_legacy_blending = false,
239147307954SThierry Reding 	.pitch_align = 64,
239247307954SThierry Reding 	.has_powergate = false,
2393f68ba691SDmitry Osipenko 	.coupled_pm = false,
239447307954SThierry Reding 	.has_nvdisplay = true,
239547307954SThierry Reding 	.wgrps = tegra186_dc_wgrps,
239647307954SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
23975b4f516fSThierry Reding };
23985b4f516fSThierry Reding 
239947443196SThierry Reding static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
240047443196SThierry Reding 	{
240147443196SThierry Reding 		.index = 0,
240247443196SThierry Reding 		.dc = 0,
240347443196SThierry Reding 		.windows = (const unsigned int[]) { 0 },
240447443196SThierry Reding 		.num_windows = 1,
240547443196SThierry Reding 	}, {
240647443196SThierry Reding 		.index = 1,
240747443196SThierry Reding 		.dc = 1,
240847443196SThierry Reding 		.windows = (const unsigned int[]) { 1 },
240947443196SThierry Reding 		.num_windows = 1,
241047443196SThierry Reding 	}, {
241147443196SThierry Reding 		.index = 2,
241247443196SThierry Reding 		.dc = 1,
241347443196SThierry Reding 		.windows = (const unsigned int[]) { 2 },
241447443196SThierry Reding 		.num_windows = 1,
241547443196SThierry Reding 	}, {
241647443196SThierry Reding 		.index = 3,
241747443196SThierry Reding 		.dc = 2,
241847443196SThierry Reding 		.windows = (const unsigned int[]) { 3 },
241947443196SThierry Reding 		.num_windows = 1,
242047443196SThierry Reding 	}, {
242147443196SThierry Reding 		.index = 4,
242247443196SThierry Reding 		.dc = 2,
242347443196SThierry Reding 		.windows = (const unsigned int[]) { 4 },
242447443196SThierry Reding 		.num_windows = 1,
242547443196SThierry Reding 	}, {
242647443196SThierry Reding 		.index = 5,
242747443196SThierry Reding 		.dc = 2,
242847443196SThierry Reding 		.windows = (const unsigned int[]) { 5 },
242947443196SThierry Reding 		.num_windows = 1,
243047443196SThierry Reding 	},
243147443196SThierry Reding };
243247443196SThierry Reding 
243347443196SThierry Reding static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
243447443196SThierry Reding 	.supports_background_color = true,
243547443196SThierry Reding 	.supports_interlacing = true,
243647443196SThierry Reding 	.supports_cursor = true,
243747443196SThierry Reding 	.supports_block_linear = true,
243847443196SThierry Reding 	.has_legacy_blending = false,
243947443196SThierry Reding 	.pitch_align = 64,
244047443196SThierry Reding 	.has_powergate = false,
244147443196SThierry Reding 	.coupled_pm = false,
244247443196SThierry Reding 	.has_nvdisplay = true,
244347443196SThierry Reding 	.wgrps = tegra194_dc_wgrps,
244447443196SThierry Reding 	.num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
244547443196SThierry Reding };
244647443196SThierry Reding 
24478620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
24488620fc62SThierry Reding 	{
244947443196SThierry Reding 		.compatible = "nvidia,tegra194-dc",
245047443196SThierry Reding 		.data = &tegra194_dc_soc_info,
245147443196SThierry Reding 	}, {
245247307954SThierry Reding 		.compatible = "nvidia,tegra186-dc",
245347307954SThierry Reding 		.data = &tegra186_dc_soc_info,
245447307954SThierry Reding 	}, {
24555b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
24565b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
24575b4f516fSThierry Reding 	}, {
24588620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
24598620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
24608620fc62SThierry Reding 	}, {
24619c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
24629c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
24639c012700SThierry Reding 	}, {
24648620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
24658620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
24668620fc62SThierry Reding 	}, {
24678620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
24688620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
24698620fc62SThierry Reding 	}, {
24708620fc62SThierry Reding 		/* sentinel */
24718620fc62SThierry Reding 	}
24728620fc62SThierry Reding };
2473ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
24748620fc62SThierry Reding 
247513411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
247613411dddSThierry Reding {
247713411dddSThierry Reding 	struct device_node *np;
247813411dddSThierry Reding 	u32 value = 0;
247913411dddSThierry Reding 	int err;
248013411dddSThierry Reding 
248113411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
248213411dddSThierry Reding 	if (err < 0) {
248313411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
248413411dddSThierry Reding 
248513411dddSThierry Reding 		/*
248613411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
248713411dddSThierry Reding 		 * correct head number by looking up the position of this
248813411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
248913411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
249013411dddSThierry Reding 		 * that the translation into a flattened device tree blob
249113411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
249213411dddSThierry Reding 		 * head number.
249313411dddSThierry Reding 		 *
249413411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
249513411dddSThierry Reding 		 * cases where only a single display controller is used.
249613411dddSThierry Reding 		 */
249713411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
2498cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
2499cf6b1744SJulia Lawall 				of_node_put(np);
250013411dddSThierry Reding 				break;
2501cf6b1744SJulia Lawall 			}
250213411dddSThierry Reding 
250313411dddSThierry Reding 			value++;
250413411dddSThierry Reding 		}
250513411dddSThierry Reding 	}
250613411dddSThierry Reding 
250713411dddSThierry Reding 	dc->pipe = value;
250813411dddSThierry Reding 
250913411dddSThierry Reding 	return 0;
251013411dddSThierry Reding }
251113411dddSThierry Reding 
251292ce7e83SSuzuki K Poulose static int tegra_dc_match_by_pipe(struct device *dev, const void *data)
2513f68ba691SDmitry Osipenko {
2514f68ba691SDmitry Osipenko 	struct tegra_dc *dc = dev_get_drvdata(dev);
251592ce7e83SSuzuki K Poulose 	unsigned int pipe = (unsigned long)(void *)data;
2516f68ba691SDmitry Osipenko 
2517f68ba691SDmitry Osipenko 	return dc->pipe == pipe;
2518f68ba691SDmitry Osipenko }
2519f68ba691SDmitry Osipenko 
2520f68ba691SDmitry Osipenko static int tegra_dc_couple(struct tegra_dc *dc)
2521f68ba691SDmitry Osipenko {
2522f68ba691SDmitry Osipenko 	/*
2523f68ba691SDmitry Osipenko 	 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2524f68ba691SDmitry Osipenko 	 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2525f68ba691SDmitry Osipenko 	 * POWER_CONTROL registers during CRTC enabling.
2526f68ba691SDmitry Osipenko 	 */
2527f68ba691SDmitry Osipenko 	if (dc->soc->coupled_pm && dc->pipe == 1) {
2528a31500feSThierry Reding 		struct device *companion;
2529a31500feSThierry Reding 		struct tegra_dc *parent;
2530f68ba691SDmitry Osipenko 
2531a31500feSThierry Reding 		companion = driver_find_device(dc->dev->driver, NULL, (const void *)0,
2532f68ba691SDmitry Osipenko 					       tegra_dc_match_by_pipe);
2533a31500feSThierry Reding 		if (!companion)
2534f68ba691SDmitry Osipenko 			return -EPROBE_DEFER;
2535f68ba691SDmitry Osipenko 
2536a31500feSThierry Reding 		parent = dev_get_drvdata(companion);
2537a31500feSThierry Reding 		dc->client.parent = &parent->client;
2538f68ba691SDmitry Osipenko 
2539a31500feSThierry Reding 		dev_dbg(dc->dev, "coupled to %s\n", dev_name(companion));
2540f68ba691SDmitry Osipenko 	}
2541f68ba691SDmitry Osipenko 
2542f68ba691SDmitry Osipenko 	return 0;
2543f68ba691SDmitry Osipenko }
2544f68ba691SDmitry Osipenko 
2545dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
2546dee8268fSThierry Reding {
254786044e74SThierry Reding 	u64 dma_mask = dma_get_mask(pdev->dev.parent);
2548dee8268fSThierry Reding 	struct tegra_dc *dc;
2549dee8268fSThierry Reding 	int err;
2550dee8268fSThierry Reding 
255186044e74SThierry Reding 	err = dma_coerce_mask_and_coherent(&pdev->dev, dma_mask);
255286044e74SThierry Reding 	if (err < 0) {
255386044e74SThierry Reding 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
255486044e74SThierry Reding 		return err;
255586044e74SThierry Reding 	}
255686044e74SThierry Reding 
2557dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2558dee8268fSThierry Reding 	if (!dc)
2559dee8268fSThierry Reding 		return -ENOMEM;
2560dee8268fSThierry Reding 
2561b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
25628620fc62SThierry Reding 
2563dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2564dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2565dee8268fSThierry Reding 
256613411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
256713411dddSThierry Reding 	if (err < 0)
256813411dddSThierry Reding 		return err;
256913411dddSThierry Reding 
2570f68ba691SDmitry Osipenko 	err = tegra_dc_couple(dc);
2571f68ba691SDmitry Osipenko 	if (err < 0)
2572f68ba691SDmitry Osipenko 		return err;
2573f68ba691SDmitry Osipenko 
2574dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2575dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
2576dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
2577dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
2578dee8268fSThierry Reding 	}
2579dee8268fSThierry Reding 
2580ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2581ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2582ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2583ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2584ca48080aSStephen Warren 	}
2585ca48080aSStephen Warren 
2586a2f2f740SThierry Reding 	/* assert reset and disable clock */
2587a2f2f740SThierry Reding 	err = clk_prepare_enable(dc->clk);
2588a2f2f740SThierry Reding 	if (err < 0)
2589a2f2f740SThierry Reding 		return err;
2590a2f2f740SThierry Reding 
2591a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2592a2f2f740SThierry Reding 
2593a2f2f740SThierry Reding 	err = reset_control_assert(dc->rst);
2594a2f2f740SThierry Reding 	if (err < 0)
2595a2f2f740SThierry Reding 		return err;
2596a2f2f740SThierry Reding 
2597a2f2f740SThierry Reding 	usleep_range(2000, 4000);
2598a2f2f740SThierry Reding 
2599a2f2f740SThierry Reding 	clk_disable_unprepare(dc->clk);
260033a8eb8dSThierry Reding 
26019c012700SThierry Reding 	if (dc->soc->has_powergate) {
26029c012700SThierry Reding 		if (dc->pipe == 0)
26039c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
26049c012700SThierry Reding 		else
26059c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
26069c012700SThierry Reding 
260733a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
26089c012700SThierry Reding 	}
2609dee8268fSThierry Reding 
2610a858ac8fSDmitry Osipenko 	dc->regs = devm_platform_ioremap_resource(pdev, 0);
2611dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2612dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2613dee8268fSThierry Reding 
2614dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
26155f1df70fSTang Bin 	if (dc->irq < 0)
2616dee8268fSThierry Reding 		return -ENXIO;
2617dee8268fSThierry Reding 
2618dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2619dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
26208f839fb6SDmitry Osipenko 		const char *level = KERN_ERR;
26218f839fb6SDmitry Osipenko 
26228f839fb6SDmitry Osipenko 		if (err == -EPROBE_DEFER)
26238f839fb6SDmitry Osipenko 			level = KERN_DEBUG;
26248f839fb6SDmitry Osipenko 
26258f839fb6SDmitry Osipenko 		dev_printk(level, dc->dev, "failed to probe RGB output: %d\n",
26268f839fb6SDmitry Osipenko 			   err);
2627dee8268fSThierry Reding 		return err;
2628dee8268fSThierry Reding 	}
2629dee8268fSThierry Reding 
263033a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
263133a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
263233a8eb8dSThierry Reding 
263333a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
263433a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
263533a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
263633a8eb8dSThierry Reding 
2637dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2638dee8268fSThierry Reding 	if (err < 0) {
2639dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2640dee8268fSThierry Reding 			err);
26410411ea89SDmitry Osipenko 		goto disable_pm;
2642dee8268fSThierry Reding 	}
2643dee8268fSThierry Reding 
2644dee8268fSThierry Reding 	return 0;
26450411ea89SDmitry Osipenko 
26460411ea89SDmitry Osipenko disable_pm:
26470411ea89SDmitry Osipenko 	pm_runtime_disable(&pdev->dev);
26480411ea89SDmitry Osipenko 	tegra_dc_rgb_remove(dc);
26490411ea89SDmitry Osipenko 
26500411ea89SDmitry Osipenko 	return err;
2651dee8268fSThierry Reding }
2652dee8268fSThierry Reding 
2653dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2654dee8268fSThierry Reding {
2655dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2656dee8268fSThierry Reding 	int err;
2657dee8268fSThierry Reding 
2658dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2659dee8268fSThierry Reding 	if (err < 0) {
2660dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2661dee8268fSThierry Reding 			err);
2662dee8268fSThierry Reding 		return err;
2663dee8268fSThierry Reding 	}
2664dee8268fSThierry Reding 
266559d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
266659d29c0eSThierry Reding 	if (err < 0) {
266759d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
266859d29c0eSThierry Reding 		return err;
266959d29c0eSThierry Reding 	}
267059d29c0eSThierry Reding 
267133a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
267233a8eb8dSThierry Reding 
267333a8eb8dSThierry Reding 	return 0;
267433a8eb8dSThierry Reding }
267533a8eb8dSThierry Reding 
2676dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2677dee8268fSThierry Reding 	.driver = {
2678dee8268fSThierry Reding 		.name = "tegra-dc",
2679dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
2680dee8268fSThierry Reding 	},
2681dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2682dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2683dee8268fSThierry Reding };
2684