xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 4aa3df7149a00cb061d2ba74e2136cd14a6d885a)
1dee8268fSThierry Reding /*
2dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
3dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
4dee8268fSThierry Reding  *
5dee8268fSThierry Reding  * This program is free software; you can redistribute it and/or modify
6dee8268fSThierry Reding  * it under the terms of the GNU General Public License version 2 as
7dee8268fSThierry Reding  * published by the Free Software Foundation.
8dee8268fSThierry Reding  */
9dee8268fSThierry Reding 
10dee8268fSThierry Reding #include <linux/clk.h>
11dee8268fSThierry Reding #include <linux/debugfs.h>
12df06b759SThierry Reding #include <linux/iommu.h>
13ca48080aSStephen Warren #include <linux/reset.h>
14dee8268fSThierry Reding 
159c012700SThierry Reding #include <soc/tegra/pmc.h>
169c012700SThierry Reding 
17dee8268fSThierry Reding #include "dc.h"
18dee8268fSThierry Reding #include "drm.h"
19dee8268fSThierry Reding #include "gem.h"
20dee8268fSThierry Reding 
21*4aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
223cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
233cb9ae4fSDaniel Vetter 
248620fc62SThierry Reding struct tegra_dc_soc_info {
2542d0659bSThierry Reding 	bool supports_border_color;
268620fc62SThierry Reding 	bool supports_interlacing;
27e687651bSThierry Reding 	bool supports_cursor;
28c134f019SThierry Reding 	bool supports_block_linear;
29d1f3e1e0SThierry Reding 	unsigned int pitch_align;
309c012700SThierry Reding 	bool has_powergate;
318620fc62SThierry Reding };
328620fc62SThierry Reding 
33dee8268fSThierry Reding struct tegra_plane {
34dee8268fSThierry Reding 	struct drm_plane base;
35dee8268fSThierry Reding 	unsigned int index;
36dee8268fSThierry Reding };
37dee8268fSThierry Reding 
38dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
39dee8268fSThierry Reding {
40dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
41dee8268fSThierry Reding }
42dee8268fSThierry Reding 
43205d48edSThierry Reding static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
44205d48edSThierry Reding {
45205d48edSThierry Reding 	u32 value = WIN_A_ACT_REQ << index;
46205d48edSThierry Reding 
47205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
48205d48edSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
49205d48edSThierry Reding }
50205d48edSThierry Reding 
51205d48edSThierry Reding static void tegra_dc_cursor_commit(struct tegra_dc *dc)
52205d48edSThierry Reding {
53205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
54205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
55205d48edSThierry Reding }
56205d48edSThierry Reding 
57d700ba7aSThierry Reding /*
5886df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
5986df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
6086df256fSThierry Reding  * active copy of some registers.
6186df256fSThierry Reding  */
6286df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
6386df256fSThierry Reding {
6486df256fSThierry Reding 	unsigned long flags;
6586df256fSThierry Reding 	u32 value;
6686df256fSThierry Reding 
6786df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
6886df256fSThierry Reding 
6986df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
7086df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
7186df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
7286df256fSThierry Reding 
7386df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
7486df256fSThierry Reding 	return value;
7586df256fSThierry Reding }
7686df256fSThierry Reding 
7786df256fSThierry Reding /*
78d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
79d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
80d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
81d700ba7aSThierry Reding  * on the next frame boundary otherwise.
82d700ba7aSThierry Reding  *
83d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
84d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
85d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
86d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
87d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
88d700ba7aSThierry Reding  */
8962b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
90205d48edSThierry Reding {
91205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
92205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
93205d48edSThierry Reding }
94205d48edSThierry Reding 
9510288eeaSThierry Reding static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
9610288eeaSThierry Reding {
9710288eeaSThierry Reding 	/* assume no swapping of fetched data */
9810288eeaSThierry Reding 	if (swap)
9910288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
10010288eeaSThierry Reding 
10110288eeaSThierry Reding 	switch (format) {
10210288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
10310288eeaSThierry Reding 		return WIN_COLOR_DEPTH_R8G8B8A8;
10410288eeaSThierry Reding 
10510288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
10610288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B8G8R8A8;
10710288eeaSThierry Reding 
10810288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
10910288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B5G6R5;
11010288eeaSThierry Reding 
11110288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
11210288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
11310288eeaSThierry Reding 
11410288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
11510288eeaSThierry Reding 		if (swap)
11610288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
11710288eeaSThierry Reding 
11810288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
11910288eeaSThierry Reding 
12010288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
12110288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr420P;
12210288eeaSThierry Reding 
12310288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
12410288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422P;
12510288eeaSThierry Reding 
12610288eeaSThierry Reding 	default:
12710288eeaSThierry Reding 		break;
12810288eeaSThierry Reding 	}
12910288eeaSThierry Reding 
13010288eeaSThierry Reding 	WARN(1, "unsupported pixel format %u, using default\n", format);
13110288eeaSThierry Reding 	return WIN_COLOR_DEPTH_B8G8R8A8;
13210288eeaSThierry Reding }
13310288eeaSThierry Reding 
13410288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
13510288eeaSThierry Reding {
13610288eeaSThierry Reding 	switch (format) {
13710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
13810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
13910288eeaSThierry Reding 		if (planar)
14010288eeaSThierry Reding 			*planar = false;
14110288eeaSThierry Reding 
14210288eeaSThierry Reding 		return true;
14310288eeaSThierry Reding 
14410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
14510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
14610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
14710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
14810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
14910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
15010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
15110288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
15210288eeaSThierry Reding 		if (planar)
15310288eeaSThierry Reding 			*planar = true;
15410288eeaSThierry Reding 
15510288eeaSThierry Reding 		return true;
15610288eeaSThierry Reding 	}
15710288eeaSThierry Reding 
158fb35c6b6SThierry Reding 	if (planar)
159fb35c6b6SThierry Reding 		*planar = false;
160fb35c6b6SThierry Reding 
16110288eeaSThierry Reding 	return false;
16210288eeaSThierry Reding }
16310288eeaSThierry Reding 
16410288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
16510288eeaSThierry Reding 				  unsigned int bpp)
16610288eeaSThierry Reding {
16710288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
16810288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
16910288eeaSThierry Reding 	u32 dda_inc;
17010288eeaSThierry Reding 	int max;
17110288eeaSThierry Reding 
17210288eeaSThierry Reding 	if (v)
17310288eeaSThierry Reding 		max = 15;
17410288eeaSThierry Reding 	else {
17510288eeaSThierry Reding 		switch (bpp) {
17610288eeaSThierry Reding 		case 2:
17710288eeaSThierry Reding 			max = 8;
17810288eeaSThierry Reding 			break;
17910288eeaSThierry Reding 
18010288eeaSThierry Reding 		default:
18110288eeaSThierry Reding 			WARN_ON_ONCE(1);
18210288eeaSThierry Reding 			/* fallthrough */
18310288eeaSThierry Reding 		case 4:
18410288eeaSThierry Reding 			max = 4;
18510288eeaSThierry Reding 			break;
18610288eeaSThierry Reding 		}
18710288eeaSThierry Reding 	}
18810288eeaSThierry Reding 
18910288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
19010288eeaSThierry Reding 	inf.full -= dfixed_const(1);
19110288eeaSThierry Reding 
19210288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
19310288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
19410288eeaSThierry Reding 
19510288eeaSThierry Reding 	return dda_inc;
19610288eeaSThierry Reding }
19710288eeaSThierry Reding 
19810288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
19910288eeaSThierry Reding {
20010288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20110288eeaSThierry Reding 	return dfixed_frac(inf);
20210288eeaSThierry Reding }
20310288eeaSThierry Reding 
204*4aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
20510288eeaSThierry Reding 				  const struct tegra_dc_window *window)
20610288eeaSThierry Reding {
20710288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
20893396d0fSSean Paul 	unsigned long value, flags;
20910288eeaSThierry Reding 	bool yuv, planar;
21010288eeaSThierry Reding 
21110288eeaSThierry Reding 	/*
21210288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
21310288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
21410288eeaSThierry Reding 	 */
21510288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
21610288eeaSThierry Reding 	if (!yuv)
21710288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
21810288eeaSThierry Reding 	else
21910288eeaSThierry Reding 		bpp = planar ? 1 : 2;
22010288eeaSThierry Reding 
22193396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
22293396d0fSSean Paul 
22310288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
22410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
22510288eeaSThierry Reding 
22610288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
22710288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
22810288eeaSThierry Reding 
22910288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
23010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
23110288eeaSThierry Reding 
23210288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
23310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
23410288eeaSThierry Reding 
23510288eeaSThierry Reding 	h_offset = window->src.x * bpp;
23610288eeaSThierry Reding 	v_offset = window->src.y;
23710288eeaSThierry Reding 	h_size = window->src.w * bpp;
23810288eeaSThierry Reding 	v_size = window->src.h;
23910288eeaSThierry Reding 
24010288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
24110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
24210288eeaSThierry Reding 
24310288eeaSThierry Reding 	/*
24410288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
24510288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
24610288eeaSThierry Reding 	 */
24710288eeaSThierry Reding 	if (yuv && planar)
24810288eeaSThierry Reding 		bpp = 2;
24910288eeaSThierry Reding 
25010288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
25110288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
25210288eeaSThierry Reding 
25310288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
25410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
25510288eeaSThierry Reding 
25610288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
25710288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
25810288eeaSThierry Reding 
25910288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
26010288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
26110288eeaSThierry Reding 
26210288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
26310288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
26410288eeaSThierry Reding 
26510288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
26610288eeaSThierry Reding 
26710288eeaSThierry Reding 	if (yuv && planar) {
26810288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
26910288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
27010288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
27110288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
27210288eeaSThierry Reding 	} else {
27310288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
27410288eeaSThierry Reding 	}
27510288eeaSThierry Reding 
27610288eeaSThierry Reding 	if (window->bottom_up)
27710288eeaSThierry Reding 		v_offset += window->src.h - 1;
27810288eeaSThierry Reding 
27910288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
28010288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
28110288eeaSThierry Reding 
282c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
283c134f019SThierry Reding 		unsigned long height = window->tiling.value;
284c134f019SThierry Reding 
285c134f019SThierry Reding 		switch (window->tiling.mode) {
286c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
287c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
288c134f019SThierry Reding 			break;
289c134f019SThierry Reding 
290c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
291c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
292c134f019SThierry Reding 			break;
293c134f019SThierry Reding 
294c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
295c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
296c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
297c134f019SThierry Reding 			break;
298c134f019SThierry Reding 		}
299c134f019SThierry Reding 
300c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
30110288eeaSThierry Reding 	} else {
302c134f019SThierry Reding 		switch (window->tiling.mode) {
303c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
30410288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
30510288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
306c134f019SThierry Reding 			break;
307c134f019SThierry Reding 
308c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
309c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
310c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
311c134f019SThierry Reding 			break;
312c134f019SThierry Reding 
313c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
314*4aa3df71SThierry Reding 			/*
315*4aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
316*4aa3df71SThierry Reding 			 * will already have filtered it out.
317*4aa3df71SThierry Reding 			 */
318*4aa3df71SThierry Reding 			break;
31910288eeaSThierry Reding 		}
32010288eeaSThierry Reding 
32110288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
322c134f019SThierry Reding 	}
32310288eeaSThierry Reding 
32410288eeaSThierry Reding 	value = WIN_ENABLE;
32510288eeaSThierry Reding 
32610288eeaSThierry Reding 	if (yuv) {
32710288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
32810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
32910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
33010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
33110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
33210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
33310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
33410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
33510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
33610288eeaSThierry Reding 
33710288eeaSThierry Reding 		value |= CSC_ENABLE;
33810288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
33910288eeaSThierry Reding 		value |= COLOR_EXPAND;
34010288eeaSThierry Reding 	}
34110288eeaSThierry Reding 
34210288eeaSThierry Reding 	if (window->bottom_up)
34310288eeaSThierry Reding 		value |= V_DIRECTION;
34410288eeaSThierry Reding 
34510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
34610288eeaSThierry Reding 
34710288eeaSThierry Reding 	/*
34810288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
34910288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
35010288eeaSThierry Reding 	 */
35110288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
35210288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
35310288eeaSThierry Reding 
35410288eeaSThierry Reding 	switch (index) {
35510288eeaSThierry Reding 	case 0:
35610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
35710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
35810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
35910288eeaSThierry Reding 		break;
36010288eeaSThierry Reding 
36110288eeaSThierry Reding 	case 1:
36210288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
36310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
36410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
36510288eeaSThierry Reding 		break;
36610288eeaSThierry Reding 
36710288eeaSThierry Reding 	case 2:
36810288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
36910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
37010288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
37110288eeaSThierry Reding 		break;
37210288eeaSThierry Reding 	}
37310288eeaSThierry Reding 
374205d48edSThierry Reding 	tegra_dc_window_commit(dc, index);
37510288eeaSThierry Reding 
37693396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
377c7679306SThierry Reding }
378c7679306SThierry Reding 
379c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
380c7679306SThierry Reding {
381c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
382c7679306SThierry Reding 
383c7679306SThierry Reding 	drm_plane_cleanup(plane);
384c7679306SThierry Reding 	kfree(p);
385c7679306SThierry Reding }
386c7679306SThierry Reding 
387c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
388c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
389c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
390c7679306SThierry Reding 	DRM_FORMAT_RGB565,
391c7679306SThierry Reding };
392c7679306SThierry Reding 
393*4aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
394c7679306SThierry Reding {
395*4aa3df71SThierry Reding 	tegra_plane_destroy(plane);
396*4aa3df71SThierry Reding }
397*4aa3df71SThierry Reding 
398*4aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
399*4aa3df71SThierry Reding 	.update_plane = drm_plane_helper_update,
400*4aa3df71SThierry Reding 	.disable_plane = drm_plane_helper_disable,
401*4aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
402*4aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
403*4aa3df71SThierry Reding };
404*4aa3df71SThierry Reding 
405*4aa3df71SThierry Reding static int tegra_plane_prepare_fb(struct drm_plane *plane,
406*4aa3df71SThierry Reding 				  struct drm_framebuffer *fb)
407*4aa3df71SThierry Reding {
408*4aa3df71SThierry Reding 	return 0;
409*4aa3df71SThierry Reding }
410*4aa3df71SThierry Reding 
411*4aa3df71SThierry Reding static void tegra_plane_cleanup_fb(struct drm_plane *plane,
412*4aa3df71SThierry Reding 				   struct drm_framebuffer *fb)
413*4aa3df71SThierry Reding {
414*4aa3df71SThierry Reding }
415*4aa3df71SThierry Reding 
416*4aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
417*4aa3df71SThierry Reding 				    struct drm_plane_state *state)
418*4aa3df71SThierry Reding {
419*4aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
420*4aa3df71SThierry Reding 	struct tegra_bo_tiling tiling;
421c7679306SThierry Reding 	int err;
422c7679306SThierry Reding 
423*4aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
424*4aa3df71SThierry Reding 	if (!state->crtc)
425*4aa3df71SThierry Reding 		return 0;
426*4aa3df71SThierry Reding 
427*4aa3df71SThierry Reding 	err = tegra_fb_get_tiling(state->fb, &tiling);
428*4aa3df71SThierry Reding 	if (err < 0)
429*4aa3df71SThierry Reding 		return err;
430*4aa3df71SThierry Reding 
431*4aa3df71SThierry Reding 	if (tiling.mode == TEGRA_BO_TILING_MODE_BLOCK &&
432*4aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
433*4aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
434*4aa3df71SThierry Reding 		return -EINVAL;
435*4aa3df71SThierry Reding 	}
436*4aa3df71SThierry Reding 
437*4aa3df71SThierry Reding 	/*
438*4aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
439*4aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
440*4aa3df71SThierry Reding 	 * configuration.
441*4aa3df71SThierry Reding 	 */
442*4aa3df71SThierry Reding 	if (drm_format_num_planes(state->fb->pixel_format) > 2) {
443*4aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
444*4aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
445*4aa3df71SThierry Reding 			return -EINVAL;
446*4aa3df71SThierry Reding 		}
447*4aa3df71SThierry Reding 	}
448*4aa3df71SThierry Reding 
449*4aa3df71SThierry Reding 	return 0;
450*4aa3df71SThierry Reding }
451*4aa3df71SThierry Reding 
452*4aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
453*4aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
454*4aa3df71SThierry Reding {
455*4aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
456*4aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
457*4aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
458*4aa3df71SThierry Reding 	struct tegra_dc_window window;
459*4aa3df71SThierry Reding 	unsigned int i;
460*4aa3df71SThierry Reding 	int err;
461*4aa3df71SThierry Reding 
462*4aa3df71SThierry Reding 	/* rien ne va plus */
463*4aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
464*4aa3df71SThierry Reding 		return;
465*4aa3df71SThierry Reding 
466c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
467*4aa3df71SThierry Reding 	window.src.x = plane->state->src_x >> 16;
468*4aa3df71SThierry Reding 	window.src.y = plane->state->src_y >> 16;
469*4aa3df71SThierry Reding 	window.src.w = plane->state->src_w >> 16;
470*4aa3df71SThierry Reding 	window.src.h = plane->state->src_h >> 16;
471*4aa3df71SThierry Reding 	window.dst.x = plane->state->crtc_x;
472*4aa3df71SThierry Reding 	window.dst.y = plane->state->crtc_y;
473*4aa3df71SThierry Reding 	window.dst.w = plane->state->crtc_w;
474*4aa3df71SThierry Reding 	window.dst.h = plane->state->crtc_h;
475c7679306SThierry Reding 	window.format = tegra_dc_format(fb->pixel_format, &window.swap);
476c7679306SThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
477c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
478c7679306SThierry Reding 
479c7679306SThierry Reding 	err = tegra_fb_get_tiling(fb, &window.tiling);
480*4aa3df71SThierry Reding 	WARN_ON(err < 0);
481c7679306SThierry Reding 
482*4aa3df71SThierry Reding 	for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
483*4aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
484c7679306SThierry Reding 
485*4aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
486*4aa3df71SThierry Reding 		window.stride[i] = fb->pitches[i];
487c7679306SThierry Reding 	}
488c7679306SThierry Reding 
489*4aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
490*4aa3df71SThierry Reding }
491*4aa3df71SThierry Reding 
492*4aa3df71SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
493*4aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
494c7679306SThierry Reding {
495*4aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
496*4aa3df71SThierry Reding 	struct tegra_dc *dc;
497*4aa3df71SThierry Reding 	unsigned long flags;
498*4aa3df71SThierry Reding 	u32 value;
499*4aa3df71SThierry Reding 
500*4aa3df71SThierry Reding 	/* rien ne va plus */
501*4aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
502*4aa3df71SThierry Reding 		return;
503*4aa3df71SThierry Reding 
504*4aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
505*4aa3df71SThierry Reding 
506*4aa3df71SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
507*4aa3df71SThierry Reding 
508*4aa3df71SThierry Reding 	value = WINDOW_A_SELECT << p->index;
509*4aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
510*4aa3df71SThierry Reding 
511*4aa3df71SThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
512*4aa3df71SThierry Reding 	value &= ~WIN_ENABLE;
513*4aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
514*4aa3df71SThierry Reding 
515*4aa3df71SThierry Reding 	tegra_dc_window_commit(dc, p->index);
516*4aa3df71SThierry Reding 
517*4aa3df71SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
518c7679306SThierry Reding }
519c7679306SThierry Reding 
520*4aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
521*4aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
522*4aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
523*4aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
524*4aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
525*4aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
526c7679306SThierry Reding };
527c7679306SThierry Reding 
528c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
529c7679306SThierry Reding 						       struct tegra_dc *dc)
530c7679306SThierry Reding {
531518e6227SThierry Reding 	/*
532518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
533518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
534518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
535518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
536518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
537518e6227SThierry Reding 	 * here.
538518e6227SThierry Reding 	 *
539518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
540518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
541518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
542518e6227SThierry Reding 	 */
543518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
544c7679306SThierry Reding 	struct tegra_plane *plane;
545c7679306SThierry Reding 	unsigned int num_formats;
546c7679306SThierry Reding 	const u32 *formats;
547c7679306SThierry Reding 	int err;
548c7679306SThierry Reding 
549c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
550c7679306SThierry Reding 	if (!plane)
551c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
552c7679306SThierry Reding 
553c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
554c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
555c7679306SThierry Reding 
556518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
557c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
558c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_PRIMARY);
559c7679306SThierry Reding 	if (err < 0) {
560c7679306SThierry Reding 		kfree(plane);
561c7679306SThierry Reding 		return ERR_PTR(err);
562c7679306SThierry Reding 	}
563c7679306SThierry Reding 
564*4aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
565*4aa3df71SThierry Reding 
566c7679306SThierry Reding 	return &plane->base;
567c7679306SThierry Reding }
568c7679306SThierry Reding 
569c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
570c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
571c7679306SThierry Reding };
572c7679306SThierry Reding 
573*4aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
574*4aa3df71SThierry Reding 				     struct drm_plane_state *state)
575c7679306SThierry Reding {
576*4aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
577*4aa3df71SThierry Reding 	if (!state->crtc)
578*4aa3df71SThierry Reding 		return 0;
579c7679306SThierry Reding 
580c7679306SThierry Reding 	/* scaling not supported for cursor */
581*4aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
582*4aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
583c7679306SThierry Reding 		return -EINVAL;
584c7679306SThierry Reding 
585c7679306SThierry Reding 	/* only square cursors supported */
586*4aa3df71SThierry Reding 	if (state->src_w != state->src_h)
587c7679306SThierry Reding 		return -EINVAL;
588c7679306SThierry Reding 
589*4aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
590*4aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
591*4aa3df71SThierry Reding 		return -EINVAL;
592*4aa3df71SThierry Reding 
593*4aa3df71SThierry Reding 	return 0;
594*4aa3df71SThierry Reding }
595*4aa3df71SThierry Reding 
596*4aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
597*4aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
598*4aa3df71SThierry Reding {
599*4aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
600*4aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
601*4aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
602*4aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
603*4aa3df71SThierry Reding 
604*4aa3df71SThierry Reding 	/* rien ne va plus */
605*4aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
606*4aa3df71SThierry Reding 		return;
607*4aa3df71SThierry Reding 
608*4aa3df71SThierry Reding 	switch (state->crtc_w) {
609c7679306SThierry Reding 	case 32:
610c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
611c7679306SThierry Reding 		break;
612c7679306SThierry Reding 
613c7679306SThierry Reding 	case 64:
614c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
615c7679306SThierry Reding 		break;
616c7679306SThierry Reding 
617c7679306SThierry Reding 	case 128:
618c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
619c7679306SThierry Reding 		break;
620c7679306SThierry Reding 
621c7679306SThierry Reding 	case 256:
622c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
623c7679306SThierry Reding 		break;
624c7679306SThierry Reding 
625c7679306SThierry Reding 	default:
626*4aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
627*4aa3df71SThierry Reding 		     state->crtc_h);
628*4aa3df71SThierry Reding 		return;
629c7679306SThierry Reding 	}
630c7679306SThierry Reding 
631c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
632c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
633c7679306SThierry Reding 
634c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
635c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
636c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
637c7679306SThierry Reding #endif
638c7679306SThierry Reding 
639c7679306SThierry Reding 	/* enable cursor and set blend mode */
640c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
641c7679306SThierry Reding 	value |= CURSOR_ENABLE;
642c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
643c7679306SThierry Reding 
644c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
645c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
646c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
647c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
648c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
649c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
650c7679306SThierry Reding 	value |= CURSOR_ALPHA;
651c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
652c7679306SThierry Reding 
653c7679306SThierry Reding 	/* position the cursor */
654*4aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
655c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
656c7679306SThierry Reding 
657c7679306SThierry Reding 	/* apply changes */
658c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
659c7679306SThierry Reding 	tegra_dc_commit(dc);
660c7679306SThierry Reding }
661c7679306SThierry Reding 
662*4aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
663*4aa3df71SThierry Reding 					struct drm_plane_state *old_state)
664c7679306SThierry Reding {
665*4aa3df71SThierry Reding 	struct tegra_dc *dc;
666c7679306SThierry Reding 	u32 value;
667c7679306SThierry Reding 
668*4aa3df71SThierry Reding 	/* rien ne va plus */
669*4aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
670*4aa3df71SThierry Reding 		return;
671*4aa3df71SThierry Reding 
672*4aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
673c7679306SThierry Reding 
674c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
675c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
676c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
677c7679306SThierry Reding 
678c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
679c7679306SThierry Reding 	tegra_dc_commit(dc);
680c7679306SThierry Reding }
681c7679306SThierry Reding 
682c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
683*4aa3df71SThierry Reding 	.update_plane = drm_plane_helper_update,
684*4aa3df71SThierry Reding 	.disable_plane = drm_plane_helper_disable,
685c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
686*4aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
687*4aa3df71SThierry Reding };
688*4aa3df71SThierry Reding 
689*4aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
690*4aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
691*4aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
692*4aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
693*4aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
694*4aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
695c7679306SThierry Reding };
696c7679306SThierry Reding 
697c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
698c7679306SThierry Reding 						      struct tegra_dc *dc)
699c7679306SThierry Reding {
700c7679306SThierry Reding 	struct tegra_plane *plane;
701c7679306SThierry Reding 	unsigned int num_formats;
702c7679306SThierry Reding 	const u32 *formats;
703c7679306SThierry Reding 	int err;
704c7679306SThierry Reding 
705c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
706c7679306SThierry Reding 	if (!plane)
707c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
708c7679306SThierry Reding 
709c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
710c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
711c7679306SThierry Reding 
712c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
713c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
714c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_CURSOR);
715c7679306SThierry Reding 	if (err < 0) {
716c7679306SThierry Reding 		kfree(plane);
717c7679306SThierry Reding 		return ERR_PTR(err);
718c7679306SThierry Reding 	}
719c7679306SThierry Reding 
720*4aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
721*4aa3df71SThierry Reding 
722c7679306SThierry Reding 	return &plane->base;
723c7679306SThierry Reding }
724c7679306SThierry Reding 
725c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
726dee8268fSThierry Reding {
727c7679306SThierry Reding 	tegra_plane_destroy(plane);
728dee8268fSThierry Reding }
729dee8268fSThierry Reding 
730c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
731*4aa3df71SThierry Reding 	.update_plane = drm_plane_helper_update,
732*4aa3df71SThierry Reding 	.disable_plane = drm_plane_helper_disable,
733c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
734*4aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
735dee8268fSThierry Reding };
736dee8268fSThierry Reding 
737c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
738dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
739dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
740dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
741dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
742f925390eSThierry Reding 	DRM_FORMAT_YUYV,
743dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
744dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
745dee8268fSThierry Reding };
746dee8268fSThierry Reding 
747*4aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
748*4aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
749*4aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
750*4aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
751*4aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
752*4aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
753*4aa3df71SThierry Reding };
754*4aa3df71SThierry Reding 
755c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
756c7679306SThierry Reding 						       struct tegra_dc *dc,
757c7679306SThierry Reding 						       unsigned int index)
758dee8268fSThierry Reding {
759dee8268fSThierry Reding 	struct tegra_plane *plane;
760c7679306SThierry Reding 	unsigned int num_formats;
761c7679306SThierry Reding 	const u32 *formats;
762c7679306SThierry Reding 	int err;
763dee8268fSThierry Reding 
764f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
765dee8268fSThierry Reding 	if (!plane)
766c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
767dee8268fSThierry Reding 
768c7679306SThierry Reding 	plane->index = index;
769dee8268fSThierry Reding 
770c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
771c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
772c7679306SThierry Reding 
773c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
774c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
775c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_OVERLAY);
776f002abc1SThierry Reding 	if (err < 0) {
777f002abc1SThierry Reding 		kfree(plane);
778c7679306SThierry Reding 		return ERR_PTR(err);
779dee8268fSThierry Reding 	}
780c7679306SThierry Reding 
781*4aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
782*4aa3df71SThierry Reding 
783c7679306SThierry Reding 	return &plane->base;
784c7679306SThierry Reding }
785c7679306SThierry Reding 
786c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
787c7679306SThierry Reding {
788c7679306SThierry Reding 	struct drm_plane *plane;
789c7679306SThierry Reding 	unsigned int i;
790c7679306SThierry Reding 
791c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
792c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
793c7679306SThierry Reding 		if (IS_ERR(plane))
794c7679306SThierry Reding 			return PTR_ERR(plane);
795f002abc1SThierry Reding 	}
796dee8268fSThierry Reding 
797dee8268fSThierry Reding 	return 0;
798dee8268fSThierry Reding }
799dee8268fSThierry Reding 
800dee8268fSThierry Reding static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
801dee8268fSThierry Reding 			     struct drm_framebuffer *fb)
802dee8268fSThierry Reding {
803dee8268fSThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
804db7fbdfdSThierry Reding 	unsigned int h_offset = 0, v_offset = 0;
805c134f019SThierry Reding 	struct tegra_bo_tiling tiling;
80693396d0fSSean Paul 	unsigned long value, flags;
807f925390eSThierry Reding 	unsigned int format, swap;
808c134f019SThierry Reding 	int err;
809c134f019SThierry Reding 
810c134f019SThierry Reding 	err = tegra_fb_get_tiling(fb, &tiling);
811c134f019SThierry Reding 	if (err < 0)
812c134f019SThierry Reding 		return err;
813dee8268fSThierry Reding 
81493396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
81593396d0fSSean Paul 
816dee8268fSThierry Reding 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
817dee8268fSThierry Reding 
818dee8268fSThierry Reding 	value = fb->offsets[0] + y * fb->pitches[0] +
819dee8268fSThierry Reding 		x * fb->bits_per_pixel / 8;
820dee8268fSThierry Reding 
821dee8268fSThierry Reding 	tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
822dee8268fSThierry Reding 	tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
823f925390eSThierry Reding 
824f925390eSThierry Reding 	format = tegra_dc_format(fb->pixel_format, &swap);
825dee8268fSThierry Reding 	tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
826f925390eSThierry Reding 	tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
827dee8268fSThierry Reding 
828c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
829c134f019SThierry Reding 		unsigned long height = tiling.value;
830c134f019SThierry Reding 
831c134f019SThierry Reding 		switch (tiling.mode) {
832c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
833c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
834c134f019SThierry Reding 			break;
835c134f019SThierry Reding 
836c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
837c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
838c134f019SThierry Reding 			break;
839c134f019SThierry Reding 
840c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
841c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
842c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
843c134f019SThierry Reding 			break;
844c134f019SThierry Reding 		}
845c134f019SThierry Reding 
846c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
847773af77fSThierry Reding 	} else {
848c134f019SThierry Reding 		switch (tiling.mode) {
849c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
850773af77fSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
851773af77fSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
852c134f019SThierry Reding 			break;
853c134f019SThierry Reding 
854c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
855c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
856c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
857c134f019SThierry Reding 			break;
858c134f019SThierry Reding 
859c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
860c134f019SThierry Reding 			DRM_ERROR("hardware doesn't support block linear mode\n");
86193396d0fSSean Paul 			spin_unlock_irqrestore(&dc->lock, flags);
862c134f019SThierry Reding 			return -EINVAL;
863773af77fSThierry Reding 		}
864773af77fSThierry Reding 
865773af77fSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
866c134f019SThierry Reding 	}
867773af77fSThierry Reding 
868db7fbdfdSThierry Reding 	/* make sure bottom-up buffers are properly displayed */
869db7fbdfdSThierry Reding 	if (tegra_fb_is_bottom_up(fb)) {
870db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
871eba66501SThierry Reding 		value |= V_DIRECTION;
872db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
873db7fbdfdSThierry Reding 
874db7fbdfdSThierry Reding 		v_offset += fb->height - 1;
875db7fbdfdSThierry Reding 	} else {
876db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
877eba66501SThierry Reding 		value &= ~V_DIRECTION;
878db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
879db7fbdfdSThierry Reding 	}
880db7fbdfdSThierry Reding 
881db7fbdfdSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
882db7fbdfdSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
883db7fbdfdSThierry Reding 
884dee8268fSThierry Reding 	value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
885205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
886dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
887dee8268fSThierry Reding 
88893396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
88993396d0fSSean Paul 
890dee8268fSThierry Reding 	return 0;
891dee8268fSThierry Reding }
892dee8268fSThierry Reding 
893dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
894dee8268fSThierry Reding {
895dee8268fSThierry Reding 	unsigned long value, flags;
896dee8268fSThierry Reding 
897dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
898dee8268fSThierry Reding 
899dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
900dee8268fSThierry Reding 	value |= VBLANK_INT;
901dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
902dee8268fSThierry Reding 
903dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
904dee8268fSThierry Reding }
905dee8268fSThierry Reding 
906dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
907dee8268fSThierry Reding {
908dee8268fSThierry Reding 	unsigned long value, flags;
909dee8268fSThierry Reding 
910dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
911dee8268fSThierry Reding 
912dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
913dee8268fSThierry Reding 	value &= ~VBLANK_INT;
914dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
915dee8268fSThierry Reding 
916dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
917dee8268fSThierry Reding }
918dee8268fSThierry Reding 
919dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
920dee8268fSThierry Reding {
921dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
922dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
923dee8268fSThierry Reding 	unsigned long flags, base;
924dee8268fSThierry Reding 	struct tegra_bo *bo;
925dee8268fSThierry Reding 
9266b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
9276b59cc1cSThierry Reding 
9286b59cc1cSThierry Reding 	if (!dc->event) {
9296b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
930dee8268fSThierry Reding 		return;
9316b59cc1cSThierry Reding 	}
932dee8268fSThierry Reding 
933f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
934dee8268fSThierry Reding 
9358643bc6dSDan Carpenter 	spin_lock(&dc->lock);
93693396d0fSSean Paul 
937dee8268fSThierry Reding 	/* check if new start address has been latched */
93893396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
939dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
940dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
941dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
942dee8268fSThierry Reding 
9438643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
94493396d0fSSean Paul 
945f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
946ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
947ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
948dee8268fSThierry Reding 		dc->event = NULL;
949dee8268fSThierry Reding 	}
9506b59cc1cSThierry Reding 
9516b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
952dee8268fSThierry Reding }
953dee8268fSThierry Reding 
954dee8268fSThierry Reding void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
955dee8268fSThierry Reding {
956dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
957dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
958dee8268fSThierry Reding 	unsigned long flags;
959dee8268fSThierry Reding 
960dee8268fSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
961dee8268fSThierry Reding 
962dee8268fSThierry Reding 	if (dc->event && dc->event->base.file_priv == file) {
963dee8268fSThierry Reding 		dc->event->base.destroy(&dc->event->base);
964ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
965dee8268fSThierry Reding 		dc->event = NULL;
966dee8268fSThierry Reding 	}
967dee8268fSThierry Reding 
968dee8268fSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
969dee8268fSThierry Reding }
970dee8268fSThierry Reding 
971dee8268fSThierry Reding static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
972dee8268fSThierry Reding 			      struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
973dee8268fSThierry Reding {
974ed7dae58SThierry Reding 	unsigned int pipe = drm_crtc_index(crtc);
975dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
976dee8268fSThierry Reding 
977dee8268fSThierry Reding 	if (dc->event)
978dee8268fSThierry Reding 		return -EBUSY;
979dee8268fSThierry Reding 
980dee8268fSThierry Reding 	if (event) {
981ed7dae58SThierry Reding 		event->pipe = pipe;
982dee8268fSThierry Reding 		dc->event = event;
983ed7dae58SThierry Reding 		drm_crtc_vblank_get(crtc);
984dee8268fSThierry Reding 	}
985dee8268fSThierry Reding 
986dee8268fSThierry Reding 	tegra_dc_set_base(dc, 0, 0, fb);
987f4510a27SMatt Roper 	crtc->primary->fb = fb;
988dee8268fSThierry Reding 
989dee8268fSThierry Reding 	return 0;
990dee8268fSThierry Reding }
991dee8268fSThierry Reding 
992f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
993f002abc1SThierry Reding {
994f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
995f002abc1SThierry Reding }
996f002abc1SThierry Reding 
997dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
998dee8268fSThierry Reding 	.page_flip = tegra_dc_page_flip,
999dee8268fSThierry Reding 	.set_config = drm_crtc_helper_set_config,
1000f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1001*4aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
1002dee8268fSThierry Reding };
1003dee8268fSThierry Reding 
100486df256fSThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
100586df256fSThierry Reding {
100686df256fSThierry Reding 	u32 value;
100786df256fSThierry Reding 
100886df256fSThierry Reding 	/* stop the display controller */
100986df256fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
101086df256fSThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
101186df256fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
101286df256fSThierry Reding 
101386df256fSThierry Reding 	tegra_dc_commit(dc);
101486df256fSThierry Reding }
101586df256fSThierry Reding 
101686df256fSThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
101786df256fSThierry Reding {
101886df256fSThierry Reding 	u32 value;
101986df256fSThierry Reding 
102086df256fSThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
102186df256fSThierry Reding 
102286df256fSThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
102386df256fSThierry Reding }
102486df256fSThierry Reding 
102586df256fSThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
102686df256fSThierry Reding {
102786df256fSThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
102886df256fSThierry Reding 
102986df256fSThierry Reding 	while (time_before(jiffies, timeout)) {
103086df256fSThierry Reding 		if (tegra_dc_idle(dc))
103186df256fSThierry Reding 			return 0;
103286df256fSThierry Reding 
103386df256fSThierry Reding 		usleep_range(1000, 2000);
103486df256fSThierry Reding 	}
103586df256fSThierry Reding 
103686df256fSThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
103786df256fSThierry Reding 	return -ETIMEDOUT;
103886df256fSThierry Reding }
103986df256fSThierry Reding 
1040dee8268fSThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
1041dee8268fSThierry Reding {
1042f002abc1SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
10433b0e5855SThierry Reding 	u32 value;
1044f002abc1SThierry Reding 
104586df256fSThierry Reding 	if (!tegra_dc_idle(dc)) {
104686df256fSThierry Reding 		tegra_dc_stop(dc);
104786df256fSThierry Reding 
104886df256fSThierry Reding 		/*
104986df256fSThierry Reding 		 * Ignore the return value, there isn't anything useful to do
105086df256fSThierry Reding 		 * in case this fails.
105186df256fSThierry Reding 		 */
105286df256fSThierry Reding 		tegra_dc_wait_idle(dc, 100);
105386df256fSThierry Reding 	}
105436904adfSThierry Reding 
10553b0e5855SThierry Reding 	/*
10563b0e5855SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
10573b0e5855SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
10583b0e5855SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
10593b0e5855SThierry Reding 	 * time the encoder is disabled before the display controller, so the
10603b0e5855SThierry Reding 	 * above code is always going to timeout waiting for the controller
10613b0e5855SThierry Reding 	 * to go idle.
10623b0e5855SThierry Reding 	 *
10633b0e5855SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
10643b0e5855SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
10653b0e5855SThierry Reding 	 * encoder drivers require these bits to be cleared.
10663b0e5855SThierry Reding 	 *
10673b0e5855SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
10683b0e5855SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
10693b0e5855SThierry Reding 	 * the RGB encoder?
10703b0e5855SThierry Reding 	 */
10713b0e5855SThierry Reding 	if (dc->rgb) {
10723b0e5855SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
10733b0e5855SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
10743b0e5855SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
10753b0e5855SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
10763b0e5855SThierry Reding 	}
10773b0e5855SThierry Reding 
10788ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
1079c7679306SThierry Reding 	tegra_dc_commit(dc);
1080dee8268fSThierry Reding }
1081dee8268fSThierry Reding 
1082dee8268fSThierry Reding static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
1083dee8268fSThierry Reding 				  const struct drm_display_mode *mode,
1084dee8268fSThierry Reding 				  struct drm_display_mode *adjusted)
1085dee8268fSThierry Reding {
1086dee8268fSThierry Reding 	return true;
1087dee8268fSThierry Reding }
1088dee8268fSThierry Reding 
1089dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1090dee8268fSThierry Reding 				struct drm_display_mode *mode)
1091dee8268fSThierry Reding {
10920444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10930444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1094dee8268fSThierry Reding 	unsigned long value;
1095dee8268fSThierry Reding 
1096dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1097dee8268fSThierry Reding 
1098dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1099dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1100dee8268fSThierry Reding 
1101dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1102dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1103dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1104dee8268fSThierry Reding 
1105dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1106dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1107dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1108dee8268fSThierry Reding 
1109dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1110dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1111dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1112dee8268fSThierry Reding 
1113dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1114dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1115dee8268fSThierry Reding 
1116dee8268fSThierry Reding 	return 0;
1117dee8268fSThierry Reding }
1118dee8268fSThierry Reding 
1119c5a107d3SThierry Reding int tegra_dc_setup_clock(struct tegra_dc *dc, struct clk *parent,
1120c5a107d3SThierry Reding 			 unsigned long pclk, unsigned int div)
1121c5a107d3SThierry Reding {
1122c5a107d3SThierry Reding 	u32 value;
1123c5a107d3SThierry Reding 	int err;
1124c5a107d3SThierry Reding 
1125c5a107d3SThierry Reding 	err = clk_set_parent(dc->clk, parent);
1126c5a107d3SThierry Reding 	if (err < 0) {
1127c5a107d3SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1128c5a107d3SThierry Reding 		return err;
1129c5a107d3SThierry Reding 	}
1130c5a107d3SThierry Reding 
1131c5a107d3SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
1132c5a107d3SThierry Reding 
1133c5a107d3SThierry Reding 	value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1134c5a107d3SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1135c5a107d3SThierry Reding 
1136c5a107d3SThierry Reding 	return 0;
1137c5a107d3SThierry Reding }
1138c5a107d3SThierry Reding 
1139*4aa3df71SThierry Reding static void tegra_crtc_mode_set_nofb(struct drm_crtc *crtc)
1140dee8268fSThierry Reding {
1141*4aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1142dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1143dbb3f2f7SThierry Reding 	u32 value;
1144dee8268fSThierry Reding 
1145dee8268fSThierry Reding 	/* program display mode */
1146dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1147dee8268fSThierry Reding 
114842d0659bSThierry Reding 	if (dc->soc->supports_border_color)
114942d0659bSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
115042d0659bSThierry Reding 
11518620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
11528620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
11538620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
11548620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
11558620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
11568620fc62SThierry Reding 	}
1157dee8268fSThierry Reding }
1158dee8268fSThierry Reding 
1159dee8268fSThierry Reding static void tegra_crtc_prepare(struct drm_crtc *crtc)
1160dee8268fSThierry Reding {
1161dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1162dee8268fSThierry Reding 	unsigned int syncpt;
1163dee8268fSThierry Reding 	unsigned long value;
1164dee8268fSThierry Reding 
11658ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
11668ff64c17SThierry Reding 
1167dee8268fSThierry Reding 	if (dc->pipe)
1168dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK1;
1169dee8268fSThierry Reding 	else
1170dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK0;
1171dee8268fSThierry Reding 
1172dee8268fSThierry Reding 	/* initialize display controller */
1173dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1174dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1175dee8268fSThierry Reding 
1176dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1177dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1178dee8268fSThierry Reding 
1179dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1180dee8268fSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1181dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1182dee8268fSThierry Reding 
1183dee8268fSThierry Reding 	/* initialize timer */
1184dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1185dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1186dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1187dee8268fSThierry Reding 
1188dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1189dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1190dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1191dee8268fSThierry Reding 
1192dee8268fSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1193dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1194dee8268fSThierry Reding 
1195dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1196dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1197dee8268fSThierry Reding }
1198dee8268fSThierry Reding 
1199dee8268fSThierry Reding static void tegra_crtc_commit(struct drm_crtc *crtc)
1200dee8268fSThierry Reding {
1201dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1202dee8268fSThierry Reding 
12038ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1204205d48edSThierry Reding 	tegra_dc_commit(dc);
1205dee8268fSThierry Reding }
1206dee8268fSThierry Reding 
1207*4aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1208*4aa3df71SThierry Reding 				   struct drm_crtc_state *state)
1209*4aa3df71SThierry Reding {
1210*4aa3df71SThierry Reding 	return 0;
1211*4aa3df71SThierry Reding }
1212*4aa3df71SThierry Reding 
1213*4aa3df71SThierry Reding static void tegra_crtc_atomic_begin(struct drm_crtc *crtc)
1214*4aa3df71SThierry Reding {
1215*4aa3df71SThierry Reding }
1216*4aa3df71SThierry Reding 
1217*4aa3df71SThierry Reding static void tegra_crtc_atomic_flush(struct drm_crtc *crtc)
1218*4aa3df71SThierry Reding {
1219*4aa3df71SThierry Reding }
1220*4aa3df71SThierry Reding 
1221dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
1222dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
1223dee8268fSThierry Reding 	.mode_fixup = tegra_crtc_mode_fixup,
1224*4aa3df71SThierry Reding 	.mode_set = drm_helper_crtc_mode_set,
1225*4aa3df71SThierry Reding 	.mode_set_nofb = tegra_crtc_mode_set_nofb,
1226*4aa3df71SThierry Reding 	.mode_set_base = drm_helper_crtc_mode_set_base,
1227dee8268fSThierry Reding 	.prepare = tegra_crtc_prepare,
1228dee8268fSThierry Reding 	.commit = tegra_crtc_commit,
1229*4aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
1230*4aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
1231*4aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
1232dee8268fSThierry Reding };
1233dee8268fSThierry Reding 
1234dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1235dee8268fSThierry Reding {
1236dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1237dee8268fSThierry Reding 	unsigned long status;
1238dee8268fSThierry Reding 
1239dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1240dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1241dee8268fSThierry Reding 
1242dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1243dee8268fSThierry Reding 		/*
1244dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1245dee8268fSThierry Reding 		*/
1246dee8268fSThierry Reding 	}
1247dee8268fSThierry Reding 
1248dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1249dee8268fSThierry Reding 		/*
1250dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1251dee8268fSThierry Reding 		*/
1252ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1253dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1254dee8268fSThierry Reding 	}
1255dee8268fSThierry Reding 
1256dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1257dee8268fSThierry Reding 		/*
1258dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1259dee8268fSThierry Reding 		*/
1260dee8268fSThierry Reding 	}
1261dee8268fSThierry Reding 
1262dee8268fSThierry Reding 	return IRQ_HANDLED;
1263dee8268fSThierry Reding }
1264dee8268fSThierry Reding 
1265dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1266dee8268fSThierry Reding {
1267dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1268dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1269dee8268fSThierry Reding 
1270dee8268fSThierry Reding #define DUMP_REG(name)						\
127103a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1272dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1273dee8268fSThierry Reding 
1274dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1275dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1276dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1277dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1278dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1279dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1280dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1281dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1282dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1283dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1284dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1285dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1286dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1287dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1288dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1289dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1290dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1291dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1292dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1293dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1294dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1295dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1296dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1297dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1298dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1299dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1300dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1301dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1302dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1303dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1304dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1305dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1306dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1307dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1308dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1309dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1310dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1311dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1312dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1313dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1314dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1315dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1316dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1317dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1318dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1319dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1320dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1321dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1322dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1323dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1324dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1325dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1326dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1327dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1328dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1329dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1330dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1331dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1332dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1333dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1334dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1335dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1336dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1337dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1338dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1339dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1340dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1341dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1342dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1343dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1344dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1345dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1346dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1347dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1348dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1349dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1350dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1351dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1352dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1353dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1354dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1355dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1356dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1357dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1358dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1359dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1360dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1361dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1362dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1363dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1364dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1365dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1366dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1367dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1368dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1369dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1370dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1371dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1372dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1373dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1374dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1375dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1376dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1377dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1378dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1379dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1380dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1381dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1382dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1383dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1384dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1385dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1386dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1387dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1388dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1389dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1390dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1391dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1392dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1393dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1394dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1395dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1396dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1397dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1398dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1399dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1400dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1401dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1402dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1403dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1404dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1405dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1406dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1407dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1408dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1409dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1410dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1411dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1412dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1413dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1414dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1415dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1416dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1417dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1418dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1419dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1420dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1421dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1422dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1423dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1424dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1425dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1426dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1427dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1428dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1429dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1430dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1431dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1432dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1433dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1434dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1435dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1436dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1437dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1438dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1439dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1440dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1441dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1442dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1443dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1444dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1445dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1446dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1447dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1448dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1449e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1450e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1451dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1452dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1453dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1454dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1455dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1456dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1457dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1458dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1459dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1460dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1461dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1462dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1463dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1464dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1465dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1466dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1467dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1468dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1469dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1470dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1471dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1472dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1473dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1474dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1475dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1476dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1477dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1478dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1479dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1480dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1481dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1482dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1483dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1484dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1485dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1486dee8268fSThierry Reding 
1487dee8268fSThierry Reding #undef DUMP_REG
1488dee8268fSThierry Reding 
1489dee8268fSThierry Reding 	return 0;
1490dee8268fSThierry Reding }
1491dee8268fSThierry Reding 
1492dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1493dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1494dee8268fSThierry Reding };
1495dee8268fSThierry Reding 
1496dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1497dee8268fSThierry Reding {
1498dee8268fSThierry Reding 	unsigned int i;
1499dee8268fSThierry Reding 	char *name;
1500dee8268fSThierry Reding 	int err;
1501dee8268fSThierry Reding 
1502dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1503dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1504dee8268fSThierry Reding 	kfree(name);
1505dee8268fSThierry Reding 
1506dee8268fSThierry Reding 	if (!dc->debugfs)
1507dee8268fSThierry Reding 		return -ENOMEM;
1508dee8268fSThierry Reding 
1509dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1510dee8268fSThierry Reding 				    GFP_KERNEL);
1511dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1512dee8268fSThierry Reding 		err = -ENOMEM;
1513dee8268fSThierry Reding 		goto remove;
1514dee8268fSThierry Reding 	}
1515dee8268fSThierry Reding 
1516dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1517dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1518dee8268fSThierry Reding 
1519dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1520dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1521dee8268fSThierry Reding 				       dc->debugfs, minor);
1522dee8268fSThierry Reding 	if (err < 0)
1523dee8268fSThierry Reding 		goto free;
1524dee8268fSThierry Reding 
1525dee8268fSThierry Reding 	dc->minor = minor;
1526dee8268fSThierry Reding 
1527dee8268fSThierry Reding 	return 0;
1528dee8268fSThierry Reding 
1529dee8268fSThierry Reding free:
1530dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1531dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1532dee8268fSThierry Reding remove:
1533dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1534dee8268fSThierry Reding 	dc->debugfs = NULL;
1535dee8268fSThierry Reding 
1536dee8268fSThierry Reding 	return err;
1537dee8268fSThierry Reding }
1538dee8268fSThierry Reding 
1539dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1540dee8268fSThierry Reding {
1541dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1542dee8268fSThierry Reding 				 dc->minor);
1543dee8268fSThierry Reding 	dc->minor = NULL;
1544dee8268fSThierry Reding 
1545dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1546dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1547dee8268fSThierry Reding 
1548dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1549dee8268fSThierry Reding 	dc->debugfs = NULL;
1550dee8268fSThierry Reding 
1551dee8268fSThierry Reding 	return 0;
1552dee8268fSThierry Reding }
1553dee8268fSThierry Reding 
1554dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1555dee8268fSThierry Reding {
15569910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
1557dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1558d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1559c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1560c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1561dee8268fSThierry Reding 	int err;
1562dee8268fSThierry Reding 
1563df06b759SThierry Reding 	if (tegra->domain) {
1564df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1565df06b759SThierry Reding 		if (err < 0) {
1566df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1567df06b759SThierry Reding 				err);
1568df06b759SThierry Reding 			return err;
1569df06b759SThierry Reding 		}
1570df06b759SThierry Reding 
1571df06b759SThierry Reding 		dc->domain = tegra->domain;
1572df06b759SThierry Reding 	}
1573df06b759SThierry Reding 
1574c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1575c7679306SThierry Reding 	if (IS_ERR(primary)) {
1576c7679306SThierry Reding 		err = PTR_ERR(primary);
1577c7679306SThierry Reding 		goto cleanup;
1578c7679306SThierry Reding 	}
1579c7679306SThierry Reding 
1580c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1581c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1582c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1583c7679306SThierry Reding 			err = PTR_ERR(cursor);
1584c7679306SThierry Reding 			goto cleanup;
1585c7679306SThierry Reding 		}
1586c7679306SThierry Reding 	}
1587c7679306SThierry Reding 
1588c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1589c7679306SThierry Reding 					&tegra_crtc_funcs);
1590c7679306SThierry Reding 	if (err < 0)
1591c7679306SThierry Reding 		goto cleanup;
1592c7679306SThierry Reding 
1593dee8268fSThierry Reding 	drm_mode_crtc_set_gamma_size(&dc->base, 256);
1594dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1595dee8268fSThierry Reding 
1596d1f3e1e0SThierry Reding 	/*
1597d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1598d1f3e1e0SThierry Reding 	 * controllers.
1599d1f3e1e0SThierry Reding 	 */
1600d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1601d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1602d1f3e1e0SThierry Reding 
16039910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1604dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1605dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1606c7679306SThierry Reding 		goto cleanup;
1607dee8268fSThierry Reding 	}
1608dee8268fSThierry Reding 
16099910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1610dee8268fSThierry Reding 	if (err < 0)
1611c7679306SThierry Reding 		goto cleanup;
1612dee8268fSThierry Reding 
1613dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
16149910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1615dee8268fSThierry Reding 		if (err < 0)
1616dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1617dee8268fSThierry Reding 	}
1618dee8268fSThierry Reding 
1619dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1620dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1621dee8268fSThierry Reding 	if (err < 0) {
1622dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1623dee8268fSThierry Reding 			err);
1624c7679306SThierry Reding 		goto cleanup;
1625dee8268fSThierry Reding 	}
1626dee8268fSThierry Reding 
1627dee8268fSThierry Reding 	return 0;
1628c7679306SThierry Reding 
1629c7679306SThierry Reding cleanup:
1630c7679306SThierry Reding 	if (cursor)
1631c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1632c7679306SThierry Reding 
1633c7679306SThierry Reding 	if (primary)
1634c7679306SThierry Reding 		drm_plane_cleanup(primary);
1635c7679306SThierry Reding 
1636c7679306SThierry Reding 	if (tegra->domain) {
1637c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1638c7679306SThierry Reding 		dc->domain = NULL;
1639c7679306SThierry Reding 	}
1640c7679306SThierry Reding 
1641c7679306SThierry Reding 	return err;
1642dee8268fSThierry Reding }
1643dee8268fSThierry Reding 
1644dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1645dee8268fSThierry Reding {
1646dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1647dee8268fSThierry Reding 	int err;
1648dee8268fSThierry Reding 
1649dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1650dee8268fSThierry Reding 
1651dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1652dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1653dee8268fSThierry Reding 		if (err < 0)
1654dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1655dee8268fSThierry Reding 	}
1656dee8268fSThierry Reding 
1657dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1658dee8268fSThierry Reding 	if (err) {
1659dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1660dee8268fSThierry Reding 		return err;
1661dee8268fSThierry Reding 	}
1662dee8268fSThierry Reding 
1663df06b759SThierry Reding 	if (dc->domain) {
1664df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1665df06b759SThierry Reding 		dc->domain = NULL;
1666df06b759SThierry Reding 	}
1667df06b759SThierry Reding 
1668dee8268fSThierry Reding 	return 0;
1669dee8268fSThierry Reding }
1670dee8268fSThierry Reding 
1671dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1672dee8268fSThierry Reding 	.init = tegra_dc_init,
1673dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1674dee8268fSThierry Reding };
1675dee8268fSThierry Reding 
16768620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
167742d0659bSThierry Reding 	.supports_border_color = true,
16788620fc62SThierry Reding 	.supports_interlacing = false,
1679e687651bSThierry Reding 	.supports_cursor = false,
1680c134f019SThierry Reding 	.supports_block_linear = false,
1681d1f3e1e0SThierry Reding 	.pitch_align = 8,
16829c012700SThierry Reding 	.has_powergate = false,
16838620fc62SThierry Reding };
16848620fc62SThierry Reding 
16858620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
168642d0659bSThierry Reding 	.supports_border_color = true,
16878620fc62SThierry Reding 	.supports_interlacing = false,
1688e687651bSThierry Reding 	.supports_cursor = false,
1689c134f019SThierry Reding 	.supports_block_linear = false,
1690d1f3e1e0SThierry Reding 	.pitch_align = 8,
16919c012700SThierry Reding 	.has_powergate = false,
1692d1f3e1e0SThierry Reding };
1693d1f3e1e0SThierry Reding 
1694d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
169542d0659bSThierry Reding 	.supports_border_color = true,
1696d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1697d1f3e1e0SThierry Reding 	.supports_cursor = false,
1698d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1699d1f3e1e0SThierry Reding 	.pitch_align = 64,
17009c012700SThierry Reding 	.has_powergate = true,
17018620fc62SThierry Reding };
17028620fc62SThierry Reding 
17038620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
170442d0659bSThierry Reding 	.supports_border_color = false,
17058620fc62SThierry Reding 	.supports_interlacing = true,
1706e687651bSThierry Reding 	.supports_cursor = true,
1707c134f019SThierry Reding 	.supports_block_linear = true,
1708d1f3e1e0SThierry Reding 	.pitch_align = 64,
17099c012700SThierry Reding 	.has_powergate = true,
17108620fc62SThierry Reding };
17118620fc62SThierry Reding 
17128620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
17138620fc62SThierry Reding 	{
17148620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
17158620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
17168620fc62SThierry Reding 	}, {
17179c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
17189c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
17199c012700SThierry Reding 	}, {
17208620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
17218620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
17228620fc62SThierry Reding 	}, {
17238620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
17248620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
17258620fc62SThierry Reding 	}, {
17268620fc62SThierry Reding 		/* sentinel */
17278620fc62SThierry Reding 	}
17288620fc62SThierry Reding };
1729ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
17308620fc62SThierry Reding 
173113411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
173213411dddSThierry Reding {
173313411dddSThierry Reding 	struct device_node *np;
173413411dddSThierry Reding 	u32 value = 0;
173513411dddSThierry Reding 	int err;
173613411dddSThierry Reding 
173713411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
173813411dddSThierry Reding 	if (err < 0) {
173913411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
174013411dddSThierry Reding 
174113411dddSThierry Reding 		/*
174213411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
174313411dddSThierry Reding 		 * correct head number by looking up the position of this
174413411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
174513411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
174613411dddSThierry Reding 		 * that the translation into a flattened device tree blob
174713411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
174813411dddSThierry Reding 		 * head number.
174913411dddSThierry Reding 		 *
175013411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
175113411dddSThierry Reding 		 * cases where only a single display controller is used.
175213411dddSThierry Reding 		 */
175313411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
175413411dddSThierry Reding 			if (np == dc->dev->of_node)
175513411dddSThierry Reding 				break;
175613411dddSThierry Reding 
175713411dddSThierry Reding 			value++;
175813411dddSThierry Reding 		}
175913411dddSThierry Reding 	}
176013411dddSThierry Reding 
176113411dddSThierry Reding 	dc->pipe = value;
176213411dddSThierry Reding 
176313411dddSThierry Reding 	return 0;
176413411dddSThierry Reding }
176513411dddSThierry Reding 
1766dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1767dee8268fSThierry Reding {
17688620fc62SThierry Reding 	const struct of_device_id *id;
1769dee8268fSThierry Reding 	struct resource *regs;
1770dee8268fSThierry Reding 	struct tegra_dc *dc;
1771dee8268fSThierry Reding 	int err;
1772dee8268fSThierry Reding 
1773dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1774dee8268fSThierry Reding 	if (!dc)
1775dee8268fSThierry Reding 		return -ENOMEM;
1776dee8268fSThierry Reding 
17778620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
17788620fc62SThierry Reding 	if (!id)
17798620fc62SThierry Reding 		return -ENODEV;
17808620fc62SThierry Reding 
1781dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1782dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1783dee8268fSThierry Reding 	dc->dev = &pdev->dev;
17848620fc62SThierry Reding 	dc->soc = id->data;
1785dee8268fSThierry Reding 
178613411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
178713411dddSThierry Reding 	if (err < 0)
178813411dddSThierry Reding 		return err;
178913411dddSThierry Reding 
1790dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1791dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1792dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1793dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1794dee8268fSThierry Reding 	}
1795dee8268fSThierry Reding 
1796ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1797ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1798ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1799ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1800ca48080aSStephen Warren 	}
1801ca48080aSStephen Warren 
18029c012700SThierry Reding 	if (dc->soc->has_powergate) {
18039c012700SThierry Reding 		if (dc->pipe == 0)
18049c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
18059c012700SThierry Reding 		else
18069c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
18079c012700SThierry Reding 
18089c012700SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
18099c012700SThierry Reding 							dc->rst);
18109c012700SThierry Reding 		if (err < 0) {
18119c012700SThierry Reding 			dev_err(&pdev->dev, "failed to power partition: %d\n",
18129c012700SThierry Reding 				err);
1813dee8268fSThierry Reding 			return err;
18149c012700SThierry Reding 		}
18159c012700SThierry Reding 	} else {
18169c012700SThierry Reding 		err = clk_prepare_enable(dc->clk);
18179c012700SThierry Reding 		if (err < 0) {
18189c012700SThierry Reding 			dev_err(&pdev->dev, "failed to enable clock: %d\n",
18199c012700SThierry Reding 				err);
18209c012700SThierry Reding 			return err;
18219c012700SThierry Reding 		}
18229c012700SThierry Reding 
18239c012700SThierry Reding 		err = reset_control_deassert(dc->rst);
18249c012700SThierry Reding 		if (err < 0) {
18259c012700SThierry Reding 			dev_err(&pdev->dev, "failed to deassert reset: %d\n",
18269c012700SThierry Reding 				err);
18279c012700SThierry Reding 			return err;
18289c012700SThierry Reding 		}
18299c012700SThierry Reding 	}
1830dee8268fSThierry Reding 
1831dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1832dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1833dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1834dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1835dee8268fSThierry Reding 
1836dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1837dee8268fSThierry Reding 	if (dc->irq < 0) {
1838dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1839dee8268fSThierry Reding 		return -ENXIO;
1840dee8268fSThierry Reding 	}
1841dee8268fSThierry Reding 
1842dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
1843dee8268fSThierry Reding 	dc->client.ops = &dc_client_ops;
1844dee8268fSThierry Reding 	dc->client.dev = &pdev->dev;
1845dee8268fSThierry Reding 
1846dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1847dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1848dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1849dee8268fSThierry Reding 		return err;
1850dee8268fSThierry Reding 	}
1851dee8268fSThierry Reding 
1852dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1853dee8268fSThierry Reding 	if (err < 0) {
1854dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1855dee8268fSThierry Reding 			err);
1856dee8268fSThierry Reding 		return err;
1857dee8268fSThierry Reding 	}
1858dee8268fSThierry Reding 
1859dee8268fSThierry Reding 	platform_set_drvdata(pdev, dc);
1860dee8268fSThierry Reding 
1861dee8268fSThierry Reding 	return 0;
1862dee8268fSThierry Reding }
1863dee8268fSThierry Reding 
1864dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1865dee8268fSThierry Reding {
1866dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1867dee8268fSThierry Reding 	int err;
1868dee8268fSThierry Reding 
1869dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1870dee8268fSThierry Reding 	if (err < 0) {
1871dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1872dee8268fSThierry Reding 			err);
1873dee8268fSThierry Reding 		return err;
1874dee8268fSThierry Reding 	}
1875dee8268fSThierry Reding 
187659d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
187759d29c0eSThierry Reding 	if (err < 0) {
187859d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
187959d29c0eSThierry Reding 		return err;
188059d29c0eSThierry Reding 	}
188159d29c0eSThierry Reding 
18825482d75aSThierry Reding 	reset_control_assert(dc->rst);
18839c012700SThierry Reding 
18849c012700SThierry Reding 	if (dc->soc->has_powergate)
18859c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
18869c012700SThierry Reding 
1887dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
1888dee8268fSThierry Reding 
1889dee8268fSThierry Reding 	return 0;
1890dee8268fSThierry Reding }
1891dee8268fSThierry Reding 
1892dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
1893dee8268fSThierry Reding 	.driver = {
1894dee8268fSThierry Reding 		.name = "tegra-dc",
1895dee8268fSThierry Reding 		.owner = THIS_MODULE,
1896dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
1897dee8268fSThierry Reding 	},
1898dee8268fSThierry Reding 	.probe = tegra_dc_probe,
1899dee8268fSThierry Reding 	.remove = tegra_dc_remove,
1900dee8268fSThierry Reding };
1901