xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 07866963b675b358d82baf8df73dba545d967a1d)
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 
219d44189fSThierry Reding #include <drm/drm_atomic.h>
224aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
233cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
243cb9ae4fSDaniel Vetter 
258620fc62SThierry Reding struct tegra_dc_soc_info {
2642d0659bSThierry Reding 	bool supports_border_color;
278620fc62SThierry Reding 	bool supports_interlacing;
28e687651bSThierry Reding 	bool supports_cursor;
29c134f019SThierry Reding 	bool supports_block_linear;
30d1f3e1e0SThierry Reding 	unsigned int pitch_align;
319c012700SThierry Reding 	bool has_powergate;
328620fc62SThierry Reding };
338620fc62SThierry Reding 
34dee8268fSThierry Reding struct tegra_plane {
35dee8268fSThierry Reding 	struct drm_plane base;
36dee8268fSThierry Reding 	unsigned int index;
37dee8268fSThierry Reding };
38dee8268fSThierry Reding 
39dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
40dee8268fSThierry Reding {
41dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
42dee8268fSThierry Reding }
43dee8268fSThierry Reding 
44205d48edSThierry Reding static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
45205d48edSThierry Reding {
46205d48edSThierry Reding 	u32 value = WIN_A_ACT_REQ << index;
47205d48edSThierry Reding 
48205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
49205d48edSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
50205d48edSThierry Reding }
51205d48edSThierry Reding 
52205d48edSThierry Reding static void tegra_dc_cursor_commit(struct tegra_dc *dc)
53205d48edSThierry Reding {
54205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
55205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
56205d48edSThierry Reding }
57205d48edSThierry Reding 
58d700ba7aSThierry Reding /*
5986df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
6086df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
6186df256fSThierry Reding  * active copy of some registers.
6286df256fSThierry Reding  */
6386df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
6486df256fSThierry Reding {
6586df256fSThierry Reding 	unsigned long flags;
6686df256fSThierry Reding 	u32 value;
6786df256fSThierry Reding 
6886df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
6986df256fSThierry Reding 
7086df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
7186df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
7286df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
7386df256fSThierry Reding 
7486df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
7586df256fSThierry Reding 	return value;
7686df256fSThierry Reding }
7786df256fSThierry Reding 
7886df256fSThierry Reding /*
79d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
80d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
81d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
82d700ba7aSThierry Reding  * on the next frame boundary otherwise.
83d700ba7aSThierry Reding  *
84d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
85d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
86d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
87d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
88d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
89d700ba7aSThierry Reding  */
9062b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
91205d48edSThierry Reding {
92205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
93205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
94205d48edSThierry Reding }
95205d48edSThierry Reding 
9610288eeaSThierry Reding static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
9710288eeaSThierry Reding {
9810288eeaSThierry Reding 	/* assume no swapping of fetched data */
9910288eeaSThierry Reding 	if (swap)
10010288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
10110288eeaSThierry Reding 
10210288eeaSThierry Reding 	switch (format) {
10310288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
10410288eeaSThierry Reding 		return WIN_COLOR_DEPTH_R8G8B8A8;
10510288eeaSThierry Reding 
10610288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
10710288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B8G8R8A8;
10810288eeaSThierry Reding 
10910288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
11010288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B5G6R5;
11110288eeaSThierry Reding 
11210288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
11310288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
11410288eeaSThierry Reding 
11510288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
11610288eeaSThierry Reding 		if (swap)
11710288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
11810288eeaSThierry Reding 
11910288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
12010288eeaSThierry Reding 
12110288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
12210288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr420P;
12310288eeaSThierry Reding 
12410288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
12510288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422P;
12610288eeaSThierry Reding 
12710288eeaSThierry Reding 	default:
12810288eeaSThierry Reding 		break;
12910288eeaSThierry Reding 	}
13010288eeaSThierry Reding 
13110288eeaSThierry Reding 	WARN(1, "unsupported pixel format %u, using default\n", format);
13210288eeaSThierry Reding 	return WIN_COLOR_DEPTH_B8G8R8A8;
13310288eeaSThierry Reding }
13410288eeaSThierry Reding 
13510288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
13610288eeaSThierry Reding {
13710288eeaSThierry Reding 	switch (format) {
13810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
13910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
14010288eeaSThierry Reding 		if (planar)
14110288eeaSThierry Reding 			*planar = false;
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 		return true;
14410288eeaSThierry Reding 
14510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
14610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
14710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
14810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
14910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
15010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
15110288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
15210288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
15310288eeaSThierry Reding 		if (planar)
15410288eeaSThierry Reding 			*planar = true;
15510288eeaSThierry Reding 
15610288eeaSThierry Reding 		return true;
15710288eeaSThierry Reding 	}
15810288eeaSThierry Reding 
159fb35c6b6SThierry Reding 	if (planar)
160fb35c6b6SThierry Reding 		*planar = false;
161fb35c6b6SThierry Reding 
16210288eeaSThierry Reding 	return false;
16310288eeaSThierry Reding }
16410288eeaSThierry Reding 
16510288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
16610288eeaSThierry Reding 				  unsigned int bpp)
16710288eeaSThierry Reding {
16810288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
16910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
17010288eeaSThierry Reding 	u32 dda_inc;
17110288eeaSThierry Reding 	int max;
17210288eeaSThierry Reding 
17310288eeaSThierry Reding 	if (v)
17410288eeaSThierry Reding 		max = 15;
17510288eeaSThierry Reding 	else {
17610288eeaSThierry Reding 		switch (bpp) {
17710288eeaSThierry Reding 		case 2:
17810288eeaSThierry Reding 			max = 8;
17910288eeaSThierry Reding 			break;
18010288eeaSThierry Reding 
18110288eeaSThierry Reding 		default:
18210288eeaSThierry Reding 			WARN_ON_ONCE(1);
18310288eeaSThierry Reding 			/* fallthrough */
18410288eeaSThierry Reding 		case 4:
18510288eeaSThierry Reding 			max = 4;
18610288eeaSThierry Reding 			break;
18710288eeaSThierry Reding 		}
18810288eeaSThierry Reding 	}
18910288eeaSThierry Reding 
19010288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
19110288eeaSThierry Reding 	inf.full -= dfixed_const(1);
19210288eeaSThierry Reding 
19310288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
19410288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
19510288eeaSThierry Reding 
19610288eeaSThierry Reding 	return dda_inc;
19710288eeaSThierry Reding }
19810288eeaSThierry Reding 
19910288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
20010288eeaSThierry Reding {
20110288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20210288eeaSThierry Reding 	return dfixed_frac(inf);
20310288eeaSThierry Reding }
20410288eeaSThierry Reding 
2054aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
20610288eeaSThierry Reding 				  const struct tegra_dc_window *window)
20710288eeaSThierry Reding {
20810288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
20993396d0fSSean Paul 	unsigned long value, flags;
21010288eeaSThierry Reding 	bool yuv, planar;
21110288eeaSThierry Reding 
21210288eeaSThierry Reding 	/*
21310288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
21410288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
21510288eeaSThierry Reding 	 */
21610288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
21710288eeaSThierry Reding 	if (!yuv)
21810288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
21910288eeaSThierry Reding 	else
22010288eeaSThierry Reding 		bpp = planar ? 1 : 2;
22110288eeaSThierry Reding 
22293396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
22393396d0fSSean Paul 
22410288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
22510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
22610288eeaSThierry Reding 
22710288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
22810288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
22910288eeaSThierry Reding 
23010288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
23110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
23210288eeaSThierry Reding 
23310288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
23410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
23510288eeaSThierry Reding 
23610288eeaSThierry Reding 	h_offset = window->src.x * bpp;
23710288eeaSThierry Reding 	v_offset = window->src.y;
23810288eeaSThierry Reding 	h_size = window->src.w * bpp;
23910288eeaSThierry Reding 	v_size = window->src.h;
24010288eeaSThierry Reding 
24110288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
24210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
24310288eeaSThierry Reding 
24410288eeaSThierry Reding 	/*
24510288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
24610288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
24710288eeaSThierry Reding 	 */
24810288eeaSThierry Reding 	if (yuv && planar)
24910288eeaSThierry Reding 		bpp = 2;
25010288eeaSThierry Reding 
25110288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
25210288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
25310288eeaSThierry Reding 
25410288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
25510288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
25610288eeaSThierry Reding 
25710288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
25810288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
25910288eeaSThierry Reding 
26010288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
26110288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
26210288eeaSThierry Reding 
26310288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
26410288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
26510288eeaSThierry Reding 
26610288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
26710288eeaSThierry Reding 
26810288eeaSThierry Reding 	if (yuv && planar) {
26910288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
27010288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
27110288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
27210288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
27310288eeaSThierry Reding 	} else {
27410288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
27510288eeaSThierry Reding 	}
27610288eeaSThierry Reding 
27710288eeaSThierry Reding 	if (window->bottom_up)
27810288eeaSThierry Reding 		v_offset += window->src.h - 1;
27910288eeaSThierry Reding 
28010288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
28110288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
28210288eeaSThierry Reding 
283c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
284c134f019SThierry Reding 		unsigned long height = window->tiling.value;
285c134f019SThierry Reding 
286c134f019SThierry Reding 		switch (window->tiling.mode) {
287c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
288c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
289c134f019SThierry Reding 			break;
290c134f019SThierry Reding 
291c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
292c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
293c134f019SThierry Reding 			break;
294c134f019SThierry Reding 
295c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
296c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
297c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
298c134f019SThierry Reding 			break;
299c134f019SThierry Reding 		}
300c134f019SThierry Reding 
301c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
30210288eeaSThierry Reding 	} else {
303c134f019SThierry Reding 		switch (window->tiling.mode) {
304c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
30510288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
30610288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
307c134f019SThierry Reding 			break;
308c134f019SThierry Reding 
309c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
310c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
311c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
312c134f019SThierry Reding 			break;
313c134f019SThierry Reding 
314c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3154aa3df71SThierry Reding 			/*
3164aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3174aa3df71SThierry Reding 			 * will already have filtered it out.
3184aa3df71SThierry Reding 			 */
3194aa3df71SThierry Reding 			break;
32010288eeaSThierry Reding 		}
32110288eeaSThierry Reding 
32210288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
323c134f019SThierry Reding 	}
32410288eeaSThierry Reding 
32510288eeaSThierry Reding 	value = WIN_ENABLE;
32610288eeaSThierry Reding 
32710288eeaSThierry Reding 	if (yuv) {
32810288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
32910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
33010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
33110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
33210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
33310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
33410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
33510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
33610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
33710288eeaSThierry Reding 
33810288eeaSThierry Reding 		value |= CSC_ENABLE;
33910288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
34010288eeaSThierry Reding 		value |= COLOR_EXPAND;
34110288eeaSThierry Reding 	}
34210288eeaSThierry Reding 
34310288eeaSThierry Reding 	if (window->bottom_up)
34410288eeaSThierry Reding 		value |= V_DIRECTION;
34510288eeaSThierry Reding 
34610288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
34710288eeaSThierry Reding 
34810288eeaSThierry Reding 	/*
34910288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
35010288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
35110288eeaSThierry Reding 	 */
35210288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
35310288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
35410288eeaSThierry Reding 
35510288eeaSThierry Reding 	switch (index) {
35610288eeaSThierry Reding 	case 0:
35710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
35810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
35910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
36010288eeaSThierry Reding 		break;
36110288eeaSThierry Reding 
36210288eeaSThierry Reding 	case 1:
36310288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
36410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
36510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
36610288eeaSThierry Reding 		break;
36710288eeaSThierry Reding 
36810288eeaSThierry Reding 	case 2:
36910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
37010288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
37110288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
37210288eeaSThierry Reding 		break;
37310288eeaSThierry Reding 	}
37410288eeaSThierry Reding 
375205d48edSThierry Reding 	tegra_dc_window_commit(dc, index);
37610288eeaSThierry Reding 
37793396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
378c7679306SThierry Reding }
379c7679306SThierry Reding 
380c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
381c7679306SThierry Reding {
382c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
383c7679306SThierry Reding 
384c7679306SThierry Reding 	drm_plane_cleanup(plane);
385c7679306SThierry Reding 	kfree(p);
386c7679306SThierry Reding }
387c7679306SThierry Reding 
388c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
389c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
390c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
391c7679306SThierry Reding 	DRM_FORMAT_RGB565,
392c7679306SThierry Reding };
393c7679306SThierry Reding 
3944aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
395c7679306SThierry Reding {
3964aa3df71SThierry Reding 	tegra_plane_destroy(plane);
3974aa3df71SThierry Reding }
3984aa3df71SThierry Reding 
3994aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
400*07866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
401*07866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
4024aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
4039d44189fSThierry Reding 	.reset = drm_atomic_helper_plane_reset,
4049d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
4054aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
4064aa3df71SThierry Reding };
4074aa3df71SThierry Reding 
4084aa3df71SThierry Reding static int tegra_plane_prepare_fb(struct drm_plane *plane,
4094aa3df71SThierry Reding 				  struct drm_framebuffer *fb)
4104aa3df71SThierry Reding {
4114aa3df71SThierry Reding 	return 0;
4124aa3df71SThierry Reding }
4134aa3df71SThierry Reding 
4144aa3df71SThierry Reding static void tegra_plane_cleanup_fb(struct drm_plane *plane,
4154aa3df71SThierry Reding 				   struct drm_framebuffer *fb)
4164aa3df71SThierry Reding {
4174aa3df71SThierry Reding }
4184aa3df71SThierry Reding 
4194aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
4204aa3df71SThierry Reding 				    struct drm_plane_state *state)
4214aa3df71SThierry Reding {
4224aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
4234aa3df71SThierry Reding 	struct tegra_bo_tiling tiling;
424c7679306SThierry Reding 	int err;
425c7679306SThierry Reding 
4264aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
4274aa3df71SThierry Reding 	if (!state->crtc)
4284aa3df71SThierry Reding 		return 0;
4294aa3df71SThierry Reding 
4304aa3df71SThierry Reding 	err = tegra_fb_get_tiling(state->fb, &tiling);
4314aa3df71SThierry Reding 	if (err < 0)
4324aa3df71SThierry Reding 		return err;
4334aa3df71SThierry Reding 
4344aa3df71SThierry Reding 	if (tiling.mode == TEGRA_BO_TILING_MODE_BLOCK &&
4354aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
4364aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
4374aa3df71SThierry Reding 		return -EINVAL;
4384aa3df71SThierry Reding 	}
4394aa3df71SThierry Reding 
4404aa3df71SThierry Reding 	/*
4414aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
4424aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
4434aa3df71SThierry Reding 	 * configuration.
4444aa3df71SThierry Reding 	 */
4454aa3df71SThierry Reding 	if (drm_format_num_planes(state->fb->pixel_format) > 2) {
4464aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
4474aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
4484aa3df71SThierry Reding 			return -EINVAL;
4494aa3df71SThierry Reding 		}
4504aa3df71SThierry Reding 	}
4514aa3df71SThierry Reding 
4524aa3df71SThierry Reding 	return 0;
4534aa3df71SThierry Reding }
4544aa3df71SThierry Reding 
4554aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
4564aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
4574aa3df71SThierry Reding {
4584aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
4594aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
4604aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
4614aa3df71SThierry Reding 	struct tegra_dc_window window;
4624aa3df71SThierry Reding 	unsigned int i;
4634aa3df71SThierry Reding 	int err;
4644aa3df71SThierry Reding 
4654aa3df71SThierry Reding 	/* rien ne va plus */
4664aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
4674aa3df71SThierry Reding 		return;
4684aa3df71SThierry Reding 
469c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
4704aa3df71SThierry Reding 	window.src.x = plane->state->src_x >> 16;
4714aa3df71SThierry Reding 	window.src.y = plane->state->src_y >> 16;
4724aa3df71SThierry Reding 	window.src.w = plane->state->src_w >> 16;
4734aa3df71SThierry Reding 	window.src.h = plane->state->src_h >> 16;
4744aa3df71SThierry Reding 	window.dst.x = plane->state->crtc_x;
4754aa3df71SThierry Reding 	window.dst.y = plane->state->crtc_y;
4764aa3df71SThierry Reding 	window.dst.w = plane->state->crtc_w;
4774aa3df71SThierry Reding 	window.dst.h = plane->state->crtc_h;
478c7679306SThierry Reding 	window.format = tegra_dc_format(fb->pixel_format, &window.swap);
479c7679306SThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
480c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
481c7679306SThierry Reding 
482c7679306SThierry Reding 	err = tegra_fb_get_tiling(fb, &window.tiling);
4834aa3df71SThierry Reding 	WARN_ON(err < 0);
484c7679306SThierry Reding 
4854aa3df71SThierry Reding 	for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
4864aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
487c7679306SThierry Reding 
4884aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
4894aa3df71SThierry Reding 		window.stride[i] = fb->pitches[i];
490c7679306SThierry Reding 	}
491c7679306SThierry Reding 
4924aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
4934aa3df71SThierry Reding }
4944aa3df71SThierry Reding 
4954aa3df71SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
4964aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
497c7679306SThierry Reding {
4984aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
4994aa3df71SThierry Reding 	struct tegra_dc *dc;
5004aa3df71SThierry Reding 	unsigned long flags;
5014aa3df71SThierry Reding 	u32 value;
5024aa3df71SThierry Reding 
5034aa3df71SThierry Reding 	/* rien ne va plus */
5044aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
5054aa3df71SThierry Reding 		return;
5064aa3df71SThierry Reding 
5074aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
5084aa3df71SThierry Reding 
5094aa3df71SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
5104aa3df71SThierry Reding 
5114aa3df71SThierry Reding 	value = WINDOW_A_SELECT << p->index;
5124aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
5134aa3df71SThierry Reding 
5144aa3df71SThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
5154aa3df71SThierry Reding 	value &= ~WIN_ENABLE;
5164aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
5174aa3df71SThierry Reding 
5184aa3df71SThierry Reding 	tegra_dc_window_commit(dc, p->index);
5194aa3df71SThierry Reding 
5204aa3df71SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
521c7679306SThierry Reding }
522c7679306SThierry Reding 
5234aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
5244aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
5254aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
5264aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
5274aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
5284aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
529c7679306SThierry Reding };
530c7679306SThierry Reding 
531c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
532c7679306SThierry Reding 						       struct tegra_dc *dc)
533c7679306SThierry Reding {
534518e6227SThierry Reding 	/*
535518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
536518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
537518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
538518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
539518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
540518e6227SThierry Reding 	 * here.
541518e6227SThierry Reding 	 *
542518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
543518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
544518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
545518e6227SThierry Reding 	 */
546518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
547c7679306SThierry Reding 	struct tegra_plane *plane;
548c7679306SThierry Reding 	unsigned int num_formats;
549c7679306SThierry Reding 	const u32 *formats;
550c7679306SThierry Reding 	int err;
551c7679306SThierry Reding 
552c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
553c7679306SThierry Reding 	if (!plane)
554c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
555c7679306SThierry Reding 
556c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
557c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
558c7679306SThierry Reding 
559518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
560c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
561c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_PRIMARY);
562c7679306SThierry Reding 	if (err < 0) {
563c7679306SThierry Reding 		kfree(plane);
564c7679306SThierry Reding 		return ERR_PTR(err);
565c7679306SThierry Reding 	}
566c7679306SThierry Reding 
5674aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
5684aa3df71SThierry Reding 
569c7679306SThierry Reding 	return &plane->base;
570c7679306SThierry Reding }
571c7679306SThierry Reding 
572c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
573c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
574c7679306SThierry Reding };
575c7679306SThierry Reding 
5764aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
5774aa3df71SThierry Reding 				     struct drm_plane_state *state)
578c7679306SThierry Reding {
5794aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
5804aa3df71SThierry Reding 	if (!state->crtc)
5814aa3df71SThierry Reding 		return 0;
582c7679306SThierry Reding 
583c7679306SThierry Reding 	/* scaling not supported for cursor */
5844aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
5854aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
586c7679306SThierry Reding 		return -EINVAL;
587c7679306SThierry Reding 
588c7679306SThierry Reding 	/* only square cursors supported */
5894aa3df71SThierry Reding 	if (state->src_w != state->src_h)
590c7679306SThierry Reding 		return -EINVAL;
591c7679306SThierry Reding 
5924aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
5934aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
5944aa3df71SThierry Reding 		return -EINVAL;
5954aa3df71SThierry Reding 
5964aa3df71SThierry Reding 	return 0;
5974aa3df71SThierry Reding }
5984aa3df71SThierry Reding 
5994aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
6004aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
6014aa3df71SThierry Reding {
6024aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
6034aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
6044aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
6054aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
6064aa3df71SThierry Reding 
6074aa3df71SThierry Reding 	/* rien ne va plus */
6084aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
6094aa3df71SThierry Reding 		return;
6104aa3df71SThierry Reding 
6114aa3df71SThierry Reding 	switch (state->crtc_w) {
612c7679306SThierry Reding 	case 32:
613c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
614c7679306SThierry Reding 		break;
615c7679306SThierry Reding 
616c7679306SThierry Reding 	case 64:
617c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
618c7679306SThierry Reding 		break;
619c7679306SThierry Reding 
620c7679306SThierry Reding 	case 128:
621c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
622c7679306SThierry Reding 		break;
623c7679306SThierry Reding 
624c7679306SThierry Reding 	case 256:
625c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
626c7679306SThierry Reding 		break;
627c7679306SThierry Reding 
628c7679306SThierry Reding 	default:
6294aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
6304aa3df71SThierry Reding 		     state->crtc_h);
6314aa3df71SThierry Reding 		return;
632c7679306SThierry Reding 	}
633c7679306SThierry Reding 
634c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
635c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
636c7679306SThierry Reding 
637c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
638c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
639c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
640c7679306SThierry Reding #endif
641c7679306SThierry Reding 
642c7679306SThierry Reding 	/* enable cursor and set blend mode */
643c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
644c7679306SThierry Reding 	value |= CURSOR_ENABLE;
645c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
646c7679306SThierry Reding 
647c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
648c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
649c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
650c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
651c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
652c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
653c7679306SThierry Reding 	value |= CURSOR_ALPHA;
654c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
655c7679306SThierry Reding 
656c7679306SThierry Reding 	/* position the cursor */
6574aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
658c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
659c7679306SThierry Reding 
660c7679306SThierry Reding 	/* apply changes */
661c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
662c7679306SThierry Reding 	tegra_dc_commit(dc);
663c7679306SThierry Reding }
664c7679306SThierry Reding 
6654aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
6664aa3df71SThierry Reding 					struct drm_plane_state *old_state)
667c7679306SThierry Reding {
6684aa3df71SThierry Reding 	struct tegra_dc *dc;
669c7679306SThierry Reding 	u32 value;
670c7679306SThierry Reding 
6714aa3df71SThierry Reding 	/* rien ne va plus */
6724aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
6734aa3df71SThierry Reding 		return;
6744aa3df71SThierry Reding 
6754aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
676c7679306SThierry Reding 
677c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
678c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
679c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
680c7679306SThierry Reding 
681c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
682c7679306SThierry Reding 	tegra_dc_commit(dc);
683c7679306SThierry Reding }
684c7679306SThierry Reding 
685c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
686*07866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
687*07866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
688c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
6899d44189fSThierry Reding 	.reset = drm_atomic_helper_plane_reset,
6909d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
6914aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
6924aa3df71SThierry Reding };
6934aa3df71SThierry Reding 
6944aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
6954aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
6964aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
6974aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
6984aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
6994aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
700c7679306SThierry Reding };
701c7679306SThierry Reding 
702c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
703c7679306SThierry Reding 						      struct tegra_dc *dc)
704c7679306SThierry Reding {
705c7679306SThierry Reding 	struct tegra_plane *plane;
706c7679306SThierry Reding 	unsigned int num_formats;
707c7679306SThierry Reding 	const u32 *formats;
708c7679306SThierry Reding 	int err;
709c7679306SThierry Reding 
710c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
711c7679306SThierry Reding 	if (!plane)
712c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
713c7679306SThierry Reding 
714c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
715c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
716c7679306SThierry Reding 
717c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
718c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
719c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_CURSOR);
720c7679306SThierry Reding 	if (err < 0) {
721c7679306SThierry Reding 		kfree(plane);
722c7679306SThierry Reding 		return ERR_PTR(err);
723c7679306SThierry Reding 	}
724c7679306SThierry Reding 
7254aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
7264aa3df71SThierry Reding 
727c7679306SThierry Reding 	return &plane->base;
728c7679306SThierry Reding }
729c7679306SThierry Reding 
730c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
731dee8268fSThierry Reding {
732c7679306SThierry Reding 	tegra_plane_destroy(plane);
733dee8268fSThierry Reding }
734dee8268fSThierry Reding 
735c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
736*07866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
737*07866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
738c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
7399d44189fSThierry Reding 	.reset = drm_atomic_helper_plane_reset,
7409d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
7414aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
742dee8268fSThierry Reding };
743dee8268fSThierry Reding 
744c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
745dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
746dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
747dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
748dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
749f925390eSThierry Reding 	DRM_FORMAT_YUYV,
750dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
751dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
752dee8268fSThierry Reding };
753dee8268fSThierry Reding 
7544aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
7554aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
7564aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
7574aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
7584aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
7594aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
7604aa3df71SThierry Reding };
7614aa3df71SThierry Reding 
762c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
763c7679306SThierry Reding 						       struct tegra_dc *dc,
764c7679306SThierry Reding 						       unsigned int index)
765dee8268fSThierry Reding {
766dee8268fSThierry Reding 	struct tegra_plane *plane;
767c7679306SThierry Reding 	unsigned int num_formats;
768c7679306SThierry Reding 	const u32 *formats;
769c7679306SThierry Reding 	int err;
770dee8268fSThierry Reding 
771f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
772dee8268fSThierry Reding 	if (!plane)
773c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
774dee8268fSThierry Reding 
775c7679306SThierry Reding 	plane->index = index;
776dee8268fSThierry Reding 
777c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
778c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
779c7679306SThierry Reding 
780c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
781c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
782c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_OVERLAY);
783f002abc1SThierry Reding 	if (err < 0) {
784f002abc1SThierry Reding 		kfree(plane);
785c7679306SThierry Reding 		return ERR_PTR(err);
786dee8268fSThierry Reding 	}
787c7679306SThierry Reding 
7884aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
7894aa3df71SThierry Reding 
790c7679306SThierry Reding 	return &plane->base;
791c7679306SThierry Reding }
792c7679306SThierry Reding 
793c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
794c7679306SThierry Reding {
795c7679306SThierry Reding 	struct drm_plane *plane;
796c7679306SThierry Reding 	unsigned int i;
797c7679306SThierry Reding 
798c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
799c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
800c7679306SThierry Reding 		if (IS_ERR(plane))
801c7679306SThierry Reding 			return PTR_ERR(plane);
802f002abc1SThierry Reding 	}
803dee8268fSThierry Reding 
804dee8268fSThierry Reding 	return 0;
805dee8268fSThierry Reding }
806dee8268fSThierry Reding 
807dee8268fSThierry Reding static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
808dee8268fSThierry Reding 			     struct drm_framebuffer *fb)
809dee8268fSThierry Reding {
810dee8268fSThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
811db7fbdfdSThierry Reding 	unsigned int h_offset = 0, v_offset = 0;
812c134f019SThierry Reding 	struct tegra_bo_tiling tiling;
81393396d0fSSean Paul 	unsigned long value, flags;
814f925390eSThierry Reding 	unsigned int format, swap;
815c134f019SThierry Reding 	int err;
816c134f019SThierry Reding 
817c134f019SThierry Reding 	err = tegra_fb_get_tiling(fb, &tiling);
818c134f019SThierry Reding 	if (err < 0)
819c134f019SThierry Reding 		return err;
820dee8268fSThierry Reding 
82193396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
82293396d0fSSean Paul 
823dee8268fSThierry Reding 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
824dee8268fSThierry Reding 
825dee8268fSThierry Reding 	value = fb->offsets[0] + y * fb->pitches[0] +
826dee8268fSThierry Reding 		x * fb->bits_per_pixel / 8;
827dee8268fSThierry Reding 
828dee8268fSThierry Reding 	tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
829dee8268fSThierry Reding 	tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
830f925390eSThierry Reding 
831f925390eSThierry Reding 	format = tegra_dc_format(fb->pixel_format, &swap);
832dee8268fSThierry Reding 	tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
833f925390eSThierry Reding 	tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
834dee8268fSThierry Reding 
835c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
836c134f019SThierry Reding 		unsigned long height = tiling.value;
837c134f019SThierry Reding 
838c134f019SThierry Reding 		switch (tiling.mode) {
839c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
840c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
841c134f019SThierry Reding 			break;
842c134f019SThierry Reding 
843c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
844c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
845c134f019SThierry Reding 			break;
846c134f019SThierry Reding 
847c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
848c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
849c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
850c134f019SThierry Reding 			break;
851c134f019SThierry Reding 		}
852c134f019SThierry Reding 
853c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
854773af77fSThierry Reding 	} else {
855c134f019SThierry Reding 		switch (tiling.mode) {
856c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
857773af77fSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
858773af77fSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
859c134f019SThierry Reding 			break;
860c134f019SThierry Reding 
861c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
862c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
863c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
864c134f019SThierry Reding 			break;
865c134f019SThierry Reding 
866c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
867c134f019SThierry Reding 			DRM_ERROR("hardware doesn't support block linear mode\n");
86893396d0fSSean Paul 			spin_unlock_irqrestore(&dc->lock, flags);
869c134f019SThierry Reding 			return -EINVAL;
870773af77fSThierry Reding 		}
871773af77fSThierry Reding 
872773af77fSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
873c134f019SThierry Reding 	}
874773af77fSThierry Reding 
875db7fbdfdSThierry Reding 	/* make sure bottom-up buffers are properly displayed */
876db7fbdfdSThierry Reding 	if (tegra_fb_is_bottom_up(fb)) {
877db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
878eba66501SThierry Reding 		value |= V_DIRECTION;
879db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
880db7fbdfdSThierry Reding 
881db7fbdfdSThierry Reding 		v_offset += fb->height - 1;
882db7fbdfdSThierry Reding 	} else {
883db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
884eba66501SThierry Reding 		value &= ~V_DIRECTION;
885db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
886db7fbdfdSThierry Reding 	}
887db7fbdfdSThierry Reding 
888db7fbdfdSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
889db7fbdfdSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
890db7fbdfdSThierry Reding 
891dee8268fSThierry Reding 	value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
892205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
893dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
894dee8268fSThierry Reding 
89593396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
89693396d0fSSean Paul 
897dee8268fSThierry Reding 	return 0;
898dee8268fSThierry Reding }
899dee8268fSThierry Reding 
900dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
901dee8268fSThierry Reding {
902dee8268fSThierry Reding 	unsigned long value, flags;
903dee8268fSThierry Reding 
904dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
905dee8268fSThierry Reding 
906dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
907dee8268fSThierry Reding 	value |= VBLANK_INT;
908dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
909dee8268fSThierry Reding 
910dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
911dee8268fSThierry Reding }
912dee8268fSThierry Reding 
913dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
914dee8268fSThierry Reding {
915dee8268fSThierry Reding 	unsigned long value, flags;
916dee8268fSThierry Reding 
917dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
918dee8268fSThierry Reding 
919dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
920dee8268fSThierry Reding 	value &= ~VBLANK_INT;
921dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
922dee8268fSThierry Reding 
923dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
924dee8268fSThierry Reding }
925dee8268fSThierry Reding 
926dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
927dee8268fSThierry Reding {
928dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
929dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
930dee8268fSThierry Reding 	unsigned long flags, base;
931dee8268fSThierry Reding 	struct tegra_bo *bo;
932dee8268fSThierry Reding 
9336b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
9346b59cc1cSThierry Reding 
9356b59cc1cSThierry Reding 	if (!dc->event) {
9366b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
937dee8268fSThierry Reding 		return;
9386b59cc1cSThierry Reding 	}
939dee8268fSThierry Reding 
940f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
941dee8268fSThierry Reding 
9428643bc6dSDan Carpenter 	spin_lock(&dc->lock);
94393396d0fSSean Paul 
944dee8268fSThierry Reding 	/* check if new start address has been latched */
94593396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
946dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
947dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
948dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
949dee8268fSThierry Reding 
9508643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
95193396d0fSSean Paul 
952f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
953ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
954ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
955dee8268fSThierry Reding 		dc->event = NULL;
956dee8268fSThierry Reding 	}
9576b59cc1cSThierry Reding 
9586b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
959dee8268fSThierry Reding }
960dee8268fSThierry Reding 
961dee8268fSThierry Reding void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
962dee8268fSThierry Reding {
963dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
964dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
965dee8268fSThierry Reding 	unsigned long flags;
966dee8268fSThierry Reding 
967dee8268fSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
968dee8268fSThierry Reding 
969dee8268fSThierry Reding 	if (dc->event && dc->event->base.file_priv == file) {
970dee8268fSThierry Reding 		dc->event->base.destroy(&dc->event->base);
971ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
972dee8268fSThierry Reding 		dc->event = NULL;
973dee8268fSThierry Reding 	}
974dee8268fSThierry Reding 
975dee8268fSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
976dee8268fSThierry Reding }
977dee8268fSThierry Reding 
978dee8268fSThierry Reding static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
979dee8268fSThierry Reding 			      struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
980dee8268fSThierry Reding {
981ed7dae58SThierry Reding 	unsigned int pipe = drm_crtc_index(crtc);
982dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
983dee8268fSThierry Reding 
984dee8268fSThierry Reding 	if (dc->event)
985dee8268fSThierry Reding 		return -EBUSY;
986dee8268fSThierry Reding 
987dee8268fSThierry Reding 	if (event) {
988ed7dae58SThierry Reding 		event->pipe = pipe;
989dee8268fSThierry Reding 		dc->event = event;
990ed7dae58SThierry Reding 		drm_crtc_vblank_get(crtc);
991dee8268fSThierry Reding 	}
992dee8268fSThierry Reding 
9939d44189fSThierry Reding 	if (crtc->primary->state)
9949d44189fSThierry Reding 		drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
9959d44189fSThierry Reding 
996dee8268fSThierry Reding 	tegra_dc_set_base(dc, 0, 0, fb);
997f4510a27SMatt Roper 	crtc->primary->fb = fb;
998dee8268fSThierry Reding 
999dee8268fSThierry Reding 	return 0;
1000dee8268fSThierry Reding }
1001dee8268fSThierry Reding 
1002f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1003f002abc1SThierry Reding {
1004f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1005f002abc1SThierry Reding }
1006f002abc1SThierry Reding 
1007dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
1008dee8268fSThierry Reding 	.page_flip = tegra_dc_page_flip,
1009dee8268fSThierry Reding 	.set_config = drm_crtc_helper_set_config,
1010f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
10119d44189fSThierry Reding 	.reset = drm_atomic_helper_crtc_reset,
10129d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
10134aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
1014dee8268fSThierry Reding };
1015dee8268fSThierry Reding 
101686df256fSThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
101786df256fSThierry Reding {
101886df256fSThierry Reding 	u32 value;
101986df256fSThierry Reding 
102086df256fSThierry Reding 	/* stop the display controller */
102186df256fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
102286df256fSThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
102386df256fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
102486df256fSThierry Reding 
102586df256fSThierry Reding 	tegra_dc_commit(dc);
102686df256fSThierry Reding }
102786df256fSThierry Reding 
102886df256fSThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
102986df256fSThierry Reding {
103086df256fSThierry Reding 	u32 value;
103186df256fSThierry Reding 
103286df256fSThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
103386df256fSThierry Reding 
103486df256fSThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
103586df256fSThierry Reding }
103686df256fSThierry Reding 
103786df256fSThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
103886df256fSThierry Reding {
103986df256fSThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
104086df256fSThierry Reding 
104186df256fSThierry Reding 	while (time_before(jiffies, timeout)) {
104286df256fSThierry Reding 		if (tegra_dc_idle(dc))
104386df256fSThierry Reding 			return 0;
104486df256fSThierry Reding 
104586df256fSThierry Reding 		usleep_range(1000, 2000);
104686df256fSThierry Reding 	}
104786df256fSThierry Reding 
104886df256fSThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
104986df256fSThierry Reding 	return -ETIMEDOUT;
105086df256fSThierry Reding }
105186df256fSThierry Reding 
1052dee8268fSThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
1053dee8268fSThierry Reding {
1054f002abc1SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
10553b0e5855SThierry Reding 	u32 value;
1056f002abc1SThierry Reding 
105786df256fSThierry Reding 	if (!tegra_dc_idle(dc)) {
105886df256fSThierry Reding 		tegra_dc_stop(dc);
105986df256fSThierry Reding 
106086df256fSThierry Reding 		/*
106186df256fSThierry Reding 		 * Ignore the return value, there isn't anything useful to do
106286df256fSThierry Reding 		 * in case this fails.
106386df256fSThierry Reding 		 */
106486df256fSThierry Reding 		tegra_dc_wait_idle(dc, 100);
106586df256fSThierry Reding 	}
106636904adfSThierry Reding 
10673b0e5855SThierry Reding 	/*
10683b0e5855SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
10693b0e5855SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
10703b0e5855SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
10713b0e5855SThierry Reding 	 * time the encoder is disabled before the display controller, so the
10723b0e5855SThierry Reding 	 * above code is always going to timeout waiting for the controller
10733b0e5855SThierry Reding 	 * to go idle.
10743b0e5855SThierry Reding 	 *
10753b0e5855SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
10763b0e5855SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
10773b0e5855SThierry Reding 	 * encoder drivers require these bits to be cleared.
10783b0e5855SThierry Reding 	 *
10793b0e5855SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
10803b0e5855SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
10813b0e5855SThierry Reding 	 * the RGB encoder?
10823b0e5855SThierry Reding 	 */
10833b0e5855SThierry Reding 	if (dc->rgb) {
10843b0e5855SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
10853b0e5855SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
10863b0e5855SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
10873b0e5855SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
10883b0e5855SThierry Reding 	}
10893b0e5855SThierry Reding 
10908ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
1091c7679306SThierry Reding 	tegra_dc_commit(dc);
1092dee8268fSThierry Reding }
1093dee8268fSThierry Reding 
1094dee8268fSThierry Reding static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
1095dee8268fSThierry Reding 				  const struct drm_display_mode *mode,
1096dee8268fSThierry Reding 				  struct drm_display_mode *adjusted)
1097dee8268fSThierry Reding {
1098dee8268fSThierry Reding 	return true;
1099dee8268fSThierry Reding }
1100dee8268fSThierry Reding 
1101dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1102dee8268fSThierry Reding 				struct drm_display_mode *mode)
1103dee8268fSThierry Reding {
11040444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
11050444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1106dee8268fSThierry Reding 	unsigned long value;
1107dee8268fSThierry Reding 
1108dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1109dee8268fSThierry Reding 
1110dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1111dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1112dee8268fSThierry Reding 
1113dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1114dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1115dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1116dee8268fSThierry Reding 
1117dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1118dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1119dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1120dee8268fSThierry Reding 
1121dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1122dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1123dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1124dee8268fSThierry Reding 
1125dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1126dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1127dee8268fSThierry Reding 
1128dee8268fSThierry Reding 	return 0;
1129dee8268fSThierry Reding }
1130dee8268fSThierry Reding 
1131c5a107d3SThierry Reding int tegra_dc_setup_clock(struct tegra_dc *dc, struct clk *parent,
1132c5a107d3SThierry Reding 			 unsigned long pclk, unsigned int div)
1133c5a107d3SThierry Reding {
1134c5a107d3SThierry Reding 	u32 value;
1135c5a107d3SThierry Reding 	int err;
1136c5a107d3SThierry Reding 
1137c5a107d3SThierry Reding 	err = clk_set_parent(dc->clk, parent);
1138c5a107d3SThierry Reding 	if (err < 0) {
1139c5a107d3SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1140c5a107d3SThierry Reding 		return err;
1141c5a107d3SThierry Reding 	}
1142c5a107d3SThierry Reding 
1143c5a107d3SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
1144c5a107d3SThierry Reding 
1145c5a107d3SThierry Reding 	value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1146c5a107d3SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1147c5a107d3SThierry Reding 
1148c5a107d3SThierry Reding 	return 0;
1149c5a107d3SThierry Reding }
1150c5a107d3SThierry Reding 
11514aa3df71SThierry Reding static void tegra_crtc_mode_set_nofb(struct drm_crtc *crtc)
1152dee8268fSThierry Reding {
11534aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1154dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1155dbb3f2f7SThierry Reding 	u32 value;
1156dee8268fSThierry Reding 
1157dee8268fSThierry Reding 	/* program display mode */
1158dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1159dee8268fSThierry Reding 
116042d0659bSThierry Reding 	if (dc->soc->supports_border_color)
116142d0659bSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
116242d0659bSThierry Reding 
11638620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
11648620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
11658620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
11668620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
11678620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
11688620fc62SThierry Reding 	}
1169dee8268fSThierry Reding }
1170dee8268fSThierry Reding 
1171dee8268fSThierry Reding static void tegra_crtc_prepare(struct drm_crtc *crtc)
1172dee8268fSThierry Reding {
1173dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1174dee8268fSThierry Reding 	unsigned int syncpt;
1175dee8268fSThierry Reding 	unsigned long value;
1176dee8268fSThierry Reding 
11778ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
11788ff64c17SThierry Reding 
1179dee8268fSThierry Reding 	if (dc->pipe)
1180dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK1;
1181dee8268fSThierry Reding 	else
1182dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK0;
1183dee8268fSThierry Reding 
1184dee8268fSThierry Reding 	/* initialize display controller */
1185dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1186dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1187dee8268fSThierry Reding 
1188dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1189dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1190dee8268fSThierry Reding 
1191dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1192dee8268fSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1193dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1194dee8268fSThierry Reding 
1195dee8268fSThierry Reding 	/* initialize timer */
1196dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1197dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1198dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1199dee8268fSThierry Reding 
1200dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1201dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1202dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1203dee8268fSThierry Reding 
1204dee8268fSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1205dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1206dee8268fSThierry Reding 
1207dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1208dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1209dee8268fSThierry Reding }
1210dee8268fSThierry Reding 
1211dee8268fSThierry Reding static void tegra_crtc_commit(struct drm_crtc *crtc)
1212dee8268fSThierry Reding {
1213dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1214dee8268fSThierry Reding 
12158ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1216205d48edSThierry Reding 	tegra_dc_commit(dc);
1217dee8268fSThierry Reding }
1218dee8268fSThierry Reding 
12194aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
12204aa3df71SThierry Reding 				   struct drm_crtc_state *state)
12214aa3df71SThierry Reding {
12224aa3df71SThierry Reding 	return 0;
12234aa3df71SThierry Reding }
12244aa3df71SThierry Reding 
12254aa3df71SThierry Reding static void tegra_crtc_atomic_begin(struct drm_crtc *crtc)
12264aa3df71SThierry Reding {
12274aa3df71SThierry Reding }
12284aa3df71SThierry Reding 
12294aa3df71SThierry Reding static void tegra_crtc_atomic_flush(struct drm_crtc *crtc)
12304aa3df71SThierry Reding {
12314aa3df71SThierry Reding }
12324aa3df71SThierry Reding 
1233dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
1234dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
1235dee8268fSThierry Reding 	.mode_fixup = tegra_crtc_mode_fixup,
12364aa3df71SThierry Reding 	.mode_set = drm_helper_crtc_mode_set,
12374aa3df71SThierry Reding 	.mode_set_nofb = tegra_crtc_mode_set_nofb,
12384aa3df71SThierry Reding 	.mode_set_base = drm_helper_crtc_mode_set_base,
1239dee8268fSThierry Reding 	.prepare = tegra_crtc_prepare,
1240dee8268fSThierry Reding 	.commit = tegra_crtc_commit,
12414aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
12424aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
12434aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
1244dee8268fSThierry Reding };
1245dee8268fSThierry Reding 
1246dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1247dee8268fSThierry Reding {
1248dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1249dee8268fSThierry Reding 	unsigned long status;
1250dee8268fSThierry Reding 
1251dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1252dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1253dee8268fSThierry Reding 
1254dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1255dee8268fSThierry Reding 		/*
1256dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1257dee8268fSThierry Reding 		*/
1258dee8268fSThierry Reding 	}
1259dee8268fSThierry Reding 
1260dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1261dee8268fSThierry Reding 		/*
1262dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1263dee8268fSThierry Reding 		*/
1264ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1265dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1266dee8268fSThierry Reding 	}
1267dee8268fSThierry Reding 
1268dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1269dee8268fSThierry Reding 		/*
1270dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1271dee8268fSThierry Reding 		*/
1272dee8268fSThierry Reding 	}
1273dee8268fSThierry Reding 
1274dee8268fSThierry Reding 	return IRQ_HANDLED;
1275dee8268fSThierry Reding }
1276dee8268fSThierry Reding 
1277dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1278dee8268fSThierry Reding {
1279dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1280dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1281dee8268fSThierry Reding 
1282dee8268fSThierry Reding #define DUMP_REG(name)						\
128303a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1284dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1285dee8268fSThierry Reding 
1286dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1287dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1288dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1289dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1290dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1291dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1292dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1293dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1294dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1295dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1296dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1297dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1298dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1299dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1300dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1301dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1302dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1303dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1304dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1305dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1306dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1307dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1308dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1309dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1310dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1311dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1312dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1313dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1314dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1315dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1316dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1317dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1318dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1319dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1320dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1321dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1322dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1323dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1324dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1325dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1326dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1327dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1328dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1329dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1330dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1331dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1332dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1333dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1334dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1335dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1336dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1337dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1338dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1339dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1340dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1341dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1342dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1343dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1344dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1345dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1346dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1347dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1348dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1349dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1350dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1351dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1352dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1353dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1354dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1355dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1356dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1357dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1358dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1359dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1360dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1361dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1362dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1363dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1364dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1365dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1366dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1367dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1368dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1369dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1370dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1371dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1372dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1373dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1374dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1375dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1376dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1377dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1378dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1379dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1380dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1381dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1382dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1383dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1384dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1385dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1386dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1387dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1388dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1389dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1390dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1391dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1392dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1393dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1394dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1395dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1396dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1397dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1398dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1399dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1400dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1401dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1402dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1403dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1404dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1405dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1406dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1407dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1408dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1409dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1410dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1411dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1412dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1413dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1414dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1415dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1416dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1417dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1418dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1419dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1420dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1421dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1422dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1423dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1424dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1425dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1426dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1427dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1428dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1429dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1430dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1431dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1432dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1433dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1434dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1435dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1436dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1437dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1438dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1439dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1440dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1441dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1442dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1443dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1444dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1445dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1446dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1447dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1448dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1449dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1450dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1451dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1452dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1453dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1454dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1455dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1456dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1457dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1458dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1459dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1460dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1461e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1462e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1463dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1464dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1465dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1466dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1467dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1468dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1469dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1470dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1471dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1472dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1473dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1474dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1475dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1476dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1477dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1478dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1479dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1480dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1481dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1482dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1483dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1484dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1485dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1486dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1487dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1488dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1489dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1490dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1491dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1492dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1493dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1494dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1495dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1496dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1497dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1498dee8268fSThierry Reding 
1499dee8268fSThierry Reding #undef DUMP_REG
1500dee8268fSThierry Reding 
1501dee8268fSThierry Reding 	return 0;
1502dee8268fSThierry Reding }
1503dee8268fSThierry Reding 
1504dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1505dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1506dee8268fSThierry Reding };
1507dee8268fSThierry Reding 
1508dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1509dee8268fSThierry Reding {
1510dee8268fSThierry Reding 	unsigned int i;
1511dee8268fSThierry Reding 	char *name;
1512dee8268fSThierry Reding 	int err;
1513dee8268fSThierry Reding 
1514dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1515dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1516dee8268fSThierry Reding 	kfree(name);
1517dee8268fSThierry Reding 
1518dee8268fSThierry Reding 	if (!dc->debugfs)
1519dee8268fSThierry Reding 		return -ENOMEM;
1520dee8268fSThierry Reding 
1521dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1522dee8268fSThierry Reding 				    GFP_KERNEL);
1523dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1524dee8268fSThierry Reding 		err = -ENOMEM;
1525dee8268fSThierry Reding 		goto remove;
1526dee8268fSThierry Reding 	}
1527dee8268fSThierry Reding 
1528dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1529dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1530dee8268fSThierry Reding 
1531dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1532dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1533dee8268fSThierry Reding 				       dc->debugfs, minor);
1534dee8268fSThierry Reding 	if (err < 0)
1535dee8268fSThierry Reding 		goto free;
1536dee8268fSThierry Reding 
1537dee8268fSThierry Reding 	dc->minor = minor;
1538dee8268fSThierry Reding 
1539dee8268fSThierry Reding 	return 0;
1540dee8268fSThierry Reding 
1541dee8268fSThierry Reding free:
1542dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1543dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1544dee8268fSThierry Reding remove:
1545dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1546dee8268fSThierry Reding 	dc->debugfs = NULL;
1547dee8268fSThierry Reding 
1548dee8268fSThierry Reding 	return err;
1549dee8268fSThierry Reding }
1550dee8268fSThierry Reding 
1551dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1552dee8268fSThierry Reding {
1553dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1554dee8268fSThierry Reding 				 dc->minor);
1555dee8268fSThierry Reding 	dc->minor = NULL;
1556dee8268fSThierry Reding 
1557dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1558dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1559dee8268fSThierry Reding 
1560dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1561dee8268fSThierry Reding 	dc->debugfs = NULL;
1562dee8268fSThierry Reding 
1563dee8268fSThierry Reding 	return 0;
1564dee8268fSThierry Reding }
1565dee8268fSThierry Reding 
1566dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1567dee8268fSThierry Reding {
15689910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
1569dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1570d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1571c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1572c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1573dee8268fSThierry Reding 	int err;
1574dee8268fSThierry Reding 
1575df06b759SThierry Reding 	if (tegra->domain) {
1576df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1577df06b759SThierry Reding 		if (err < 0) {
1578df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1579df06b759SThierry Reding 				err);
1580df06b759SThierry Reding 			return err;
1581df06b759SThierry Reding 		}
1582df06b759SThierry Reding 
1583df06b759SThierry Reding 		dc->domain = tegra->domain;
1584df06b759SThierry Reding 	}
1585df06b759SThierry Reding 
1586c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1587c7679306SThierry Reding 	if (IS_ERR(primary)) {
1588c7679306SThierry Reding 		err = PTR_ERR(primary);
1589c7679306SThierry Reding 		goto cleanup;
1590c7679306SThierry Reding 	}
1591c7679306SThierry Reding 
1592c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1593c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1594c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1595c7679306SThierry Reding 			err = PTR_ERR(cursor);
1596c7679306SThierry Reding 			goto cleanup;
1597c7679306SThierry Reding 		}
1598c7679306SThierry Reding 	}
1599c7679306SThierry Reding 
1600c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1601c7679306SThierry Reding 					&tegra_crtc_funcs);
1602c7679306SThierry Reding 	if (err < 0)
1603c7679306SThierry Reding 		goto cleanup;
1604c7679306SThierry Reding 
1605dee8268fSThierry Reding 	drm_mode_crtc_set_gamma_size(&dc->base, 256);
1606dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1607dee8268fSThierry Reding 
1608d1f3e1e0SThierry Reding 	/*
1609d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1610d1f3e1e0SThierry Reding 	 * controllers.
1611d1f3e1e0SThierry Reding 	 */
1612d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1613d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1614d1f3e1e0SThierry Reding 
16159910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1616dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1617dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1618c7679306SThierry Reding 		goto cleanup;
1619dee8268fSThierry Reding 	}
1620dee8268fSThierry Reding 
16219910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1622dee8268fSThierry Reding 	if (err < 0)
1623c7679306SThierry Reding 		goto cleanup;
1624dee8268fSThierry Reding 
1625dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
16269910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1627dee8268fSThierry Reding 		if (err < 0)
1628dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1629dee8268fSThierry Reding 	}
1630dee8268fSThierry Reding 
1631dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1632dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1633dee8268fSThierry Reding 	if (err < 0) {
1634dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1635dee8268fSThierry Reding 			err);
1636c7679306SThierry Reding 		goto cleanup;
1637dee8268fSThierry Reding 	}
1638dee8268fSThierry Reding 
1639dee8268fSThierry Reding 	return 0;
1640c7679306SThierry Reding 
1641c7679306SThierry Reding cleanup:
1642c7679306SThierry Reding 	if (cursor)
1643c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1644c7679306SThierry Reding 
1645c7679306SThierry Reding 	if (primary)
1646c7679306SThierry Reding 		drm_plane_cleanup(primary);
1647c7679306SThierry Reding 
1648c7679306SThierry Reding 	if (tegra->domain) {
1649c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1650c7679306SThierry Reding 		dc->domain = NULL;
1651c7679306SThierry Reding 	}
1652c7679306SThierry Reding 
1653c7679306SThierry Reding 	return err;
1654dee8268fSThierry Reding }
1655dee8268fSThierry Reding 
1656dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1657dee8268fSThierry Reding {
1658dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1659dee8268fSThierry Reding 	int err;
1660dee8268fSThierry Reding 
1661dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1662dee8268fSThierry Reding 
1663dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1664dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1665dee8268fSThierry Reding 		if (err < 0)
1666dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1667dee8268fSThierry Reding 	}
1668dee8268fSThierry Reding 
1669dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1670dee8268fSThierry Reding 	if (err) {
1671dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1672dee8268fSThierry Reding 		return err;
1673dee8268fSThierry Reding 	}
1674dee8268fSThierry Reding 
1675df06b759SThierry Reding 	if (dc->domain) {
1676df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1677df06b759SThierry Reding 		dc->domain = NULL;
1678df06b759SThierry Reding 	}
1679df06b759SThierry Reding 
1680dee8268fSThierry Reding 	return 0;
1681dee8268fSThierry Reding }
1682dee8268fSThierry Reding 
1683dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1684dee8268fSThierry Reding 	.init = tegra_dc_init,
1685dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1686dee8268fSThierry Reding };
1687dee8268fSThierry Reding 
16888620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
168942d0659bSThierry Reding 	.supports_border_color = true,
16908620fc62SThierry Reding 	.supports_interlacing = false,
1691e687651bSThierry Reding 	.supports_cursor = false,
1692c134f019SThierry Reding 	.supports_block_linear = false,
1693d1f3e1e0SThierry Reding 	.pitch_align = 8,
16949c012700SThierry Reding 	.has_powergate = false,
16958620fc62SThierry Reding };
16968620fc62SThierry Reding 
16978620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
169842d0659bSThierry Reding 	.supports_border_color = true,
16998620fc62SThierry Reding 	.supports_interlacing = false,
1700e687651bSThierry Reding 	.supports_cursor = false,
1701c134f019SThierry Reding 	.supports_block_linear = false,
1702d1f3e1e0SThierry Reding 	.pitch_align = 8,
17039c012700SThierry Reding 	.has_powergate = false,
1704d1f3e1e0SThierry Reding };
1705d1f3e1e0SThierry Reding 
1706d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
170742d0659bSThierry Reding 	.supports_border_color = true,
1708d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1709d1f3e1e0SThierry Reding 	.supports_cursor = false,
1710d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1711d1f3e1e0SThierry Reding 	.pitch_align = 64,
17129c012700SThierry Reding 	.has_powergate = true,
17138620fc62SThierry Reding };
17148620fc62SThierry Reding 
17158620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
171642d0659bSThierry Reding 	.supports_border_color = false,
17178620fc62SThierry Reding 	.supports_interlacing = true,
1718e687651bSThierry Reding 	.supports_cursor = true,
1719c134f019SThierry Reding 	.supports_block_linear = true,
1720d1f3e1e0SThierry Reding 	.pitch_align = 64,
17219c012700SThierry Reding 	.has_powergate = true,
17228620fc62SThierry Reding };
17238620fc62SThierry Reding 
17248620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
17258620fc62SThierry Reding 	{
17268620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
17278620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
17288620fc62SThierry Reding 	}, {
17299c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
17309c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
17319c012700SThierry Reding 	}, {
17328620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
17338620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
17348620fc62SThierry Reding 	}, {
17358620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
17368620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
17378620fc62SThierry Reding 	}, {
17388620fc62SThierry Reding 		/* sentinel */
17398620fc62SThierry Reding 	}
17408620fc62SThierry Reding };
1741ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
17428620fc62SThierry Reding 
174313411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
174413411dddSThierry Reding {
174513411dddSThierry Reding 	struct device_node *np;
174613411dddSThierry Reding 	u32 value = 0;
174713411dddSThierry Reding 	int err;
174813411dddSThierry Reding 
174913411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
175013411dddSThierry Reding 	if (err < 0) {
175113411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
175213411dddSThierry Reding 
175313411dddSThierry Reding 		/*
175413411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
175513411dddSThierry Reding 		 * correct head number by looking up the position of this
175613411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
175713411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
175813411dddSThierry Reding 		 * that the translation into a flattened device tree blob
175913411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
176013411dddSThierry Reding 		 * head number.
176113411dddSThierry Reding 		 *
176213411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
176313411dddSThierry Reding 		 * cases where only a single display controller is used.
176413411dddSThierry Reding 		 */
176513411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
176613411dddSThierry Reding 			if (np == dc->dev->of_node)
176713411dddSThierry Reding 				break;
176813411dddSThierry Reding 
176913411dddSThierry Reding 			value++;
177013411dddSThierry Reding 		}
177113411dddSThierry Reding 	}
177213411dddSThierry Reding 
177313411dddSThierry Reding 	dc->pipe = value;
177413411dddSThierry Reding 
177513411dddSThierry Reding 	return 0;
177613411dddSThierry Reding }
177713411dddSThierry Reding 
1778dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1779dee8268fSThierry Reding {
17808620fc62SThierry Reding 	const struct of_device_id *id;
1781dee8268fSThierry Reding 	struct resource *regs;
1782dee8268fSThierry Reding 	struct tegra_dc *dc;
1783dee8268fSThierry Reding 	int err;
1784dee8268fSThierry Reding 
1785dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1786dee8268fSThierry Reding 	if (!dc)
1787dee8268fSThierry Reding 		return -ENOMEM;
1788dee8268fSThierry Reding 
17898620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
17908620fc62SThierry Reding 	if (!id)
17918620fc62SThierry Reding 		return -ENODEV;
17928620fc62SThierry Reding 
1793dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1794dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1795dee8268fSThierry Reding 	dc->dev = &pdev->dev;
17968620fc62SThierry Reding 	dc->soc = id->data;
1797dee8268fSThierry Reding 
179813411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
179913411dddSThierry Reding 	if (err < 0)
180013411dddSThierry Reding 		return err;
180113411dddSThierry Reding 
1802dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1803dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1804dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1805dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1806dee8268fSThierry Reding 	}
1807dee8268fSThierry Reding 
1808ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1809ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1810ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1811ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1812ca48080aSStephen Warren 	}
1813ca48080aSStephen Warren 
18149c012700SThierry Reding 	if (dc->soc->has_powergate) {
18159c012700SThierry Reding 		if (dc->pipe == 0)
18169c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
18179c012700SThierry Reding 		else
18189c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
18199c012700SThierry Reding 
18209c012700SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
18219c012700SThierry Reding 							dc->rst);
18229c012700SThierry Reding 		if (err < 0) {
18239c012700SThierry Reding 			dev_err(&pdev->dev, "failed to power partition: %d\n",
18249c012700SThierry Reding 				err);
1825dee8268fSThierry Reding 			return err;
18269c012700SThierry Reding 		}
18279c012700SThierry Reding 	} else {
18289c012700SThierry Reding 		err = clk_prepare_enable(dc->clk);
18299c012700SThierry Reding 		if (err < 0) {
18309c012700SThierry Reding 			dev_err(&pdev->dev, "failed to enable clock: %d\n",
18319c012700SThierry Reding 				err);
18329c012700SThierry Reding 			return err;
18339c012700SThierry Reding 		}
18349c012700SThierry Reding 
18359c012700SThierry Reding 		err = reset_control_deassert(dc->rst);
18369c012700SThierry Reding 		if (err < 0) {
18379c012700SThierry Reding 			dev_err(&pdev->dev, "failed to deassert reset: %d\n",
18389c012700SThierry Reding 				err);
18399c012700SThierry Reding 			return err;
18409c012700SThierry Reding 		}
18419c012700SThierry Reding 	}
1842dee8268fSThierry Reding 
1843dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1844dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1845dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1846dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1847dee8268fSThierry Reding 
1848dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1849dee8268fSThierry Reding 	if (dc->irq < 0) {
1850dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1851dee8268fSThierry Reding 		return -ENXIO;
1852dee8268fSThierry Reding 	}
1853dee8268fSThierry Reding 
1854dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
1855dee8268fSThierry Reding 	dc->client.ops = &dc_client_ops;
1856dee8268fSThierry Reding 	dc->client.dev = &pdev->dev;
1857dee8268fSThierry Reding 
1858dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1859dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1860dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1861dee8268fSThierry Reding 		return err;
1862dee8268fSThierry Reding 	}
1863dee8268fSThierry Reding 
1864dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1865dee8268fSThierry Reding 	if (err < 0) {
1866dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1867dee8268fSThierry Reding 			err);
1868dee8268fSThierry Reding 		return err;
1869dee8268fSThierry Reding 	}
1870dee8268fSThierry Reding 
1871dee8268fSThierry Reding 	platform_set_drvdata(pdev, dc);
1872dee8268fSThierry Reding 
1873dee8268fSThierry Reding 	return 0;
1874dee8268fSThierry Reding }
1875dee8268fSThierry Reding 
1876dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1877dee8268fSThierry Reding {
1878dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1879dee8268fSThierry Reding 	int err;
1880dee8268fSThierry Reding 
1881dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1882dee8268fSThierry Reding 	if (err < 0) {
1883dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1884dee8268fSThierry Reding 			err);
1885dee8268fSThierry Reding 		return err;
1886dee8268fSThierry Reding 	}
1887dee8268fSThierry Reding 
188859d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
188959d29c0eSThierry Reding 	if (err < 0) {
189059d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
189159d29c0eSThierry Reding 		return err;
189259d29c0eSThierry Reding 	}
189359d29c0eSThierry Reding 
18945482d75aSThierry Reding 	reset_control_assert(dc->rst);
18959c012700SThierry Reding 
18969c012700SThierry Reding 	if (dc->soc->has_powergate)
18979c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
18989c012700SThierry Reding 
1899dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
1900dee8268fSThierry Reding 
1901dee8268fSThierry Reding 	return 0;
1902dee8268fSThierry Reding }
1903dee8268fSThierry Reding 
1904dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
1905dee8268fSThierry Reding 	.driver = {
1906dee8268fSThierry Reding 		.name = "tegra-dc",
1907dee8268fSThierry Reding 		.owner = THIS_MODULE,
1908dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
1909dee8268fSThierry Reding 	},
1910dee8268fSThierry Reding 	.probe = tegra_dc_probe,
1911dee8268fSThierry Reding 	.remove = tegra_dc_remove,
1912dee8268fSThierry Reding };
1913