xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 3b0e58554873d1034beef737f15c7aa46492ff98)
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 
213cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
223cb9ae4fSDaniel Vetter 
238620fc62SThierry Reding struct tegra_dc_soc_info {
2442d0659bSThierry Reding 	bool supports_border_color;
258620fc62SThierry Reding 	bool supports_interlacing;
26e687651bSThierry Reding 	bool supports_cursor;
27c134f019SThierry Reding 	bool supports_block_linear;
28d1f3e1e0SThierry Reding 	unsigned int pitch_align;
299c012700SThierry Reding 	bool has_powergate;
308620fc62SThierry Reding };
318620fc62SThierry Reding 
32dee8268fSThierry Reding struct tegra_plane {
33dee8268fSThierry Reding 	struct drm_plane base;
34dee8268fSThierry Reding 	unsigned int index;
35dee8268fSThierry Reding };
36dee8268fSThierry Reding 
37dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
38dee8268fSThierry Reding {
39dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
40dee8268fSThierry Reding }
41dee8268fSThierry Reding 
42205d48edSThierry Reding static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
43205d48edSThierry Reding {
44205d48edSThierry Reding 	u32 value = WIN_A_ACT_REQ << index;
45205d48edSThierry Reding 
46205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
47205d48edSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
48205d48edSThierry Reding }
49205d48edSThierry Reding 
50205d48edSThierry Reding static void tegra_dc_cursor_commit(struct tegra_dc *dc)
51205d48edSThierry Reding {
52205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
53205d48edSThierry Reding 	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
54205d48edSThierry Reding }
55205d48edSThierry Reding 
56d700ba7aSThierry Reding /*
5786df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
5886df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
5986df256fSThierry Reding  * active copy of some registers.
6086df256fSThierry Reding  */
6186df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
6286df256fSThierry Reding {
6386df256fSThierry Reding 	unsigned long flags;
6486df256fSThierry Reding 	u32 value;
6586df256fSThierry Reding 
6686df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
6786df256fSThierry Reding 
6886df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
6986df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
7086df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
7186df256fSThierry Reding 
7286df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
7386df256fSThierry Reding 	return value;
7486df256fSThierry Reding }
7586df256fSThierry Reding 
7686df256fSThierry Reding /*
77d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
78d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
79d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
80d700ba7aSThierry Reding  * on the next frame boundary otherwise.
81d700ba7aSThierry Reding  *
82d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
83d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
84d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
85d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
86d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
87d700ba7aSThierry Reding  */
8862b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
89205d48edSThierry Reding {
90205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
91205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
92205d48edSThierry Reding }
93205d48edSThierry Reding 
9410288eeaSThierry Reding static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
9510288eeaSThierry Reding {
9610288eeaSThierry Reding 	/* assume no swapping of fetched data */
9710288eeaSThierry Reding 	if (swap)
9810288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
9910288eeaSThierry Reding 
10010288eeaSThierry Reding 	switch (format) {
10110288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
10210288eeaSThierry Reding 		return WIN_COLOR_DEPTH_R8G8B8A8;
10310288eeaSThierry Reding 
10410288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
10510288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B8G8R8A8;
10610288eeaSThierry Reding 
10710288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
10810288eeaSThierry Reding 		return WIN_COLOR_DEPTH_B5G6R5;
10910288eeaSThierry Reding 
11010288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
11110288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
11210288eeaSThierry Reding 
11310288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
11410288eeaSThierry Reding 		if (swap)
11510288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
11610288eeaSThierry Reding 
11710288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
11810288eeaSThierry Reding 
11910288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
12010288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr420P;
12110288eeaSThierry Reding 
12210288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
12310288eeaSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422P;
12410288eeaSThierry Reding 
12510288eeaSThierry Reding 	default:
12610288eeaSThierry Reding 		break;
12710288eeaSThierry Reding 	}
12810288eeaSThierry Reding 
12910288eeaSThierry Reding 	WARN(1, "unsupported pixel format %u, using default\n", format);
13010288eeaSThierry Reding 	return WIN_COLOR_DEPTH_B8G8R8A8;
13110288eeaSThierry Reding }
13210288eeaSThierry Reding 
13310288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
13410288eeaSThierry Reding {
13510288eeaSThierry Reding 	switch (format) {
13610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
13710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
13810288eeaSThierry Reding 		if (planar)
13910288eeaSThierry Reding 			*planar = false;
14010288eeaSThierry Reding 
14110288eeaSThierry Reding 		return true;
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
14410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
14510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
14610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
14710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
14810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
14910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
15010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
15110288eeaSThierry Reding 		if (planar)
15210288eeaSThierry Reding 			*planar = true;
15310288eeaSThierry Reding 
15410288eeaSThierry Reding 		return true;
15510288eeaSThierry Reding 	}
15610288eeaSThierry Reding 
157fb35c6b6SThierry Reding 	if (planar)
158fb35c6b6SThierry Reding 		*planar = false;
159fb35c6b6SThierry Reding 
16010288eeaSThierry Reding 	return false;
16110288eeaSThierry Reding }
16210288eeaSThierry Reding 
16310288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
16410288eeaSThierry Reding 				  unsigned int bpp)
16510288eeaSThierry Reding {
16610288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
16710288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
16810288eeaSThierry Reding 	u32 dda_inc;
16910288eeaSThierry Reding 	int max;
17010288eeaSThierry Reding 
17110288eeaSThierry Reding 	if (v)
17210288eeaSThierry Reding 		max = 15;
17310288eeaSThierry Reding 	else {
17410288eeaSThierry Reding 		switch (bpp) {
17510288eeaSThierry Reding 		case 2:
17610288eeaSThierry Reding 			max = 8;
17710288eeaSThierry Reding 			break;
17810288eeaSThierry Reding 
17910288eeaSThierry Reding 		default:
18010288eeaSThierry Reding 			WARN_ON_ONCE(1);
18110288eeaSThierry Reding 			/* fallthrough */
18210288eeaSThierry Reding 		case 4:
18310288eeaSThierry Reding 			max = 4;
18410288eeaSThierry Reding 			break;
18510288eeaSThierry Reding 		}
18610288eeaSThierry Reding 	}
18710288eeaSThierry Reding 
18810288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
18910288eeaSThierry Reding 	inf.full -= dfixed_const(1);
19010288eeaSThierry Reding 
19110288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
19210288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
19310288eeaSThierry Reding 
19410288eeaSThierry Reding 	return dda_inc;
19510288eeaSThierry Reding }
19610288eeaSThierry Reding 
19710288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
19810288eeaSThierry Reding {
19910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20010288eeaSThierry Reding 	return dfixed_frac(inf);
20110288eeaSThierry Reding }
20210288eeaSThierry Reding 
20310288eeaSThierry Reding static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
20410288eeaSThierry Reding 				 const struct tegra_dc_window *window)
20510288eeaSThierry Reding {
20610288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
20793396d0fSSean Paul 	unsigned long value, flags;
20810288eeaSThierry Reding 	bool yuv, planar;
20910288eeaSThierry Reding 
21010288eeaSThierry Reding 	/*
21110288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
21210288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
21310288eeaSThierry Reding 	 */
21410288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
21510288eeaSThierry Reding 	if (!yuv)
21610288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
21710288eeaSThierry Reding 	else
21810288eeaSThierry Reding 		bpp = planar ? 1 : 2;
21910288eeaSThierry Reding 
22093396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
22193396d0fSSean Paul 
22210288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
22310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
22410288eeaSThierry Reding 
22510288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
22610288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
22710288eeaSThierry Reding 
22810288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
22910288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
23010288eeaSThierry Reding 
23110288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
23210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
23310288eeaSThierry Reding 
23410288eeaSThierry Reding 	h_offset = window->src.x * bpp;
23510288eeaSThierry Reding 	v_offset = window->src.y;
23610288eeaSThierry Reding 	h_size = window->src.w * bpp;
23710288eeaSThierry Reding 	v_size = window->src.h;
23810288eeaSThierry Reding 
23910288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
24010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
24110288eeaSThierry Reding 
24210288eeaSThierry Reding 	/*
24310288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
24410288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
24510288eeaSThierry Reding 	 */
24610288eeaSThierry Reding 	if (yuv && planar)
24710288eeaSThierry Reding 		bpp = 2;
24810288eeaSThierry Reding 
24910288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
25010288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
25110288eeaSThierry Reding 
25210288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
25310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
25410288eeaSThierry Reding 
25510288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
25610288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
25710288eeaSThierry Reding 
25810288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
25910288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
26010288eeaSThierry Reding 
26110288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
26210288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
26310288eeaSThierry Reding 
26410288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
26510288eeaSThierry Reding 
26610288eeaSThierry Reding 	if (yuv && planar) {
26710288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
26810288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
26910288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
27010288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
27110288eeaSThierry Reding 	} else {
27210288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
27310288eeaSThierry Reding 	}
27410288eeaSThierry Reding 
27510288eeaSThierry Reding 	if (window->bottom_up)
27610288eeaSThierry Reding 		v_offset += window->src.h - 1;
27710288eeaSThierry Reding 
27810288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
27910288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
28010288eeaSThierry Reding 
281c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
282c134f019SThierry Reding 		unsigned long height = window->tiling.value;
283c134f019SThierry Reding 
284c134f019SThierry Reding 		switch (window->tiling.mode) {
285c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
286c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
287c134f019SThierry Reding 			break;
288c134f019SThierry Reding 
289c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
290c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
291c134f019SThierry Reding 			break;
292c134f019SThierry Reding 
293c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
294c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
295c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
296c134f019SThierry Reding 			break;
297c134f019SThierry Reding 		}
298c134f019SThierry Reding 
299c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
30010288eeaSThierry Reding 	} else {
301c134f019SThierry Reding 		switch (window->tiling.mode) {
302c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
30310288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
30410288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
305c134f019SThierry Reding 			break;
306c134f019SThierry Reding 
307c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
308c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
309c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
310c134f019SThierry Reding 			break;
311c134f019SThierry Reding 
312c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
313c134f019SThierry Reding 			DRM_ERROR("hardware doesn't support block linear mode\n");
31493396d0fSSean Paul 			spin_unlock_irqrestore(&dc->lock, flags);
315c134f019SThierry Reding 			return -EINVAL;
31610288eeaSThierry Reding 		}
31710288eeaSThierry Reding 
31810288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
319c134f019SThierry Reding 	}
32010288eeaSThierry Reding 
32110288eeaSThierry Reding 	value = WIN_ENABLE;
32210288eeaSThierry Reding 
32310288eeaSThierry Reding 	if (yuv) {
32410288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
32510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
32610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
32710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
32810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
32910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
33010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
33110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
33210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
33310288eeaSThierry Reding 
33410288eeaSThierry Reding 		value |= CSC_ENABLE;
33510288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
33610288eeaSThierry Reding 		value |= COLOR_EXPAND;
33710288eeaSThierry Reding 	}
33810288eeaSThierry Reding 
33910288eeaSThierry Reding 	if (window->bottom_up)
34010288eeaSThierry Reding 		value |= V_DIRECTION;
34110288eeaSThierry Reding 
34210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
34310288eeaSThierry Reding 
34410288eeaSThierry Reding 	/*
34510288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
34610288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
34710288eeaSThierry Reding 	 */
34810288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
34910288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
35010288eeaSThierry Reding 
35110288eeaSThierry Reding 	switch (index) {
35210288eeaSThierry Reding 	case 0:
35310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
35410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
35510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
35610288eeaSThierry Reding 		break;
35710288eeaSThierry Reding 
35810288eeaSThierry Reding 	case 1:
35910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
36010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
36110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
36210288eeaSThierry Reding 		break;
36310288eeaSThierry Reding 
36410288eeaSThierry Reding 	case 2:
36510288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
36610288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
36710288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
36810288eeaSThierry Reding 		break;
36910288eeaSThierry Reding 	}
37010288eeaSThierry Reding 
371205d48edSThierry Reding 	tegra_dc_window_commit(dc, index);
37210288eeaSThierry Reding 
37393396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
37493396d0fSSean Paul 
37510288eeaSThierry Reding 	return 0;
37610288eeaSThierry Reding }
37710288eeaSThierry Reding 
378c7679306SThierry Reding static int tegra_window_plane_disable(struct drm_plane *plane)
379c7679306SThierry Reding {
380c7679306SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->crtc);
381c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
38293396d0fSSean Paul 	unsigned long flags;
383c7679306SThierry Reding 	u32 value;
384c7679306SThierry Reding 
385c7679306SThierry Reding 	if (!plane->crtc)
386c7679306SThierry Reding 		return 0;
387c7679306SThierry Reding 
38893396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
38993396d0fSSean Paul 
390c7679306SThierry Reding 	value = WINDOW_A_SELECT << p->index;
391c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
392c7679306SThierry Reding 
393c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
394c7679306SThierry Reding 	value &= ~WIN_ENABLE;
395c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
396c7679306SThierry Reding 
397c7679306SThierry Reding 	tegra_dc_window_commit(dc, p->index);
398c7679306SThierry Reding 
39993396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
40093396d0fSSean Paul 
401c7679306SThierry Reding 	return 0;
402c7679306SThierry Reding }
403c7679306SThierry Reding 
404c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
405c7679306SThierry Reding {
406c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
407c7679306SThierry Reding 
408c7679306SThierry Reding 	drm_plane_cleanup(plane);
409c7679306SThierry Reding 	kfree(p);
410c7679306SThierry Reding }
411c7679306SThierry Reding 
412c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
413c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
414c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
415c7679306SThierry Reding 	DRM_FORMAT_RGB565,
416c7679306SThierry Reding };
417c7679306SThierry Reding 
418c7679306SThierry Reding static int tegra_primary_plane_update(struct drm_plane *plane,
419c7679306SThierry Reding 				      struct drm_crtc *crtc,
420dee8268fSThierry Reding 				      struct drm_framebuffer *fb, int crtc_x,
421dee8268fSThierry Reding 				      int crtc_y, unsigned int crtc_w,
422dee8268fSThierry Reding 				      unsigned int crtc_h, uint32_t src_x,
423c7679306SThierry Reding 				      uint32_t src_y, uint32_t src_w,
424c7679306SThierry Reding 				      uint32_t src_h)
425c7679306SThierry Reding {
426c7679306SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
427c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
428c7679306SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
429c7679306SThierry Reding 	struct tegra_dc_window window;
430c7679306SThierry Reding 	int err;
431c7679306SThierry Reding 
432c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
433c7679306SThierry Reding 	window.src.x = src_x >> 16;
434c7679306SThierry Reding 	window.src.y = src_y >> 16;
435c7679306SThierry Reding 	window.src.w = src_w >> 16;
436c7679306SThierry Reding 	window.src.h = src_h >> 16;
437c7679306SThierry Reding 	window.dst.x = crtc_x;
438c7679306SThierry Reding 	window.dst.y = crtc_y;
439c7679306SThierry Reding 	window.dst.w = crtc_w;
440c7679306SThierry Reding 	window.dst.h = crtc_h;
441c7679306SThierry Reding 	window.format = tegra_dc_format(fb->pixel_format, &window.swap);
442c7679306SThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
443c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
444c7679306SThierry Reding 
445c7679306SThierry Reding 	err = tegra_fb_get_tiling(fb, &window.tiling);
446c7679306SThierry Reding 	if (err < 0)
447c7679306SThierry Reding 		return err;
448c7679306SThierry Reding 
449c7679306SThierry Reding 	window.base[0] = bo->paddr + fb->offsets[0];
450c7679306SThierry Reding 	window.stride[0] = fb->pitches[0];
451c7679306SThierry Reding 
452c7679306SThierry Reding 	err = tegra_dc_setup_window(dc, p->index, &window);
453c7679306SThierry Reding 	if (err < 0)
454c7679306SThierry Reding 		return err;
455c7679306SThierry Reding 
456c7679306SThierry Reding 	return 0;
457c7679306SThierry Reding }
458c7679306SThierry Reding 
459c7679306SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
460c7679306SThierry Reding {
461c7679306SThierry Reding 	tegra_window_plane_disable(plane);
462c7679306SThierry Reding 	tegra_plane_destroy(plane);
463c7679306SThierry Reding }
464c7679306SThierry Reding 
465c7679306SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
466c7679306SThierry Reding 	.update_plane = tegra_primary_plane_update,
467c7679306SThierry Reding 	.disable_plane = tegra_window_plane_disable,
468c7679306SThierry Reding 	.destroy = tegra_primary_plane_destroy,
469c7679306SThierry Reding };
470c7679306SThierry Reding 
471c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
472c7679306SThierry Reding 						       struct tegra_dc *dc)
473c7679306SThierry Reding {
474518e6227SThierry Reding 	/*
475518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
476518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
477518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
478518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
479518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
480518e6227SThierry Reding 	 * here.
481518e6227SThierry Reding 	 *
482518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
483518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
484518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
485518e6227SThierry Reding 	 */
486518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
487c7679306SThierry Reding 	struct tegra_plane *plane;
488c7679306SThierry Reding 	unsigned int num_formats;
489c7679306SThierry Reding 	const u32 *formats;
490c7679306SThierry Reding 	int err;
491c7679306SThierry Reding 
492c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
493c7679306SThierry Reding 	if (!plane)
494c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
495c7679306SThierry Reding 
496c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
497c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
498c7679306SThierry Reding 
499518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
500c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
501c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_PRIMARY);
502c7679306SThierry Reding 	if (err < 0) {
503c7679306SThierry Reding 		kfree(plane);
504c7679306SThierry Reding 		return ERR_PTR(err);
505c7679306SThierry Reding 	}
506c7679306SThierry Reding 
507c7679306SThierry Reding 	return &plane->base;
508c7679306SThierry Reding }
509c7679306SThierry Reding 
510c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
511c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
512c7679306SThierry Reding };
513c7679306SThierry Reding 
514c7679306SThierry Reding static int tegra_cursor_plane_update(struct drm_plane *plane,
515c7679306SThierry Reding 				     struct drm_crtc *crtc,
516c7679306SThierry Reding 				     struct drm_framebuffer *fb, int crtc_x,
517c7679306SThierry Reding 				     int crtc_y, unsigned int crtc_w,
518c7679306SThierry Reding 				     unsigned int crtc_h, uint32_t src_x,
519c7679306SThierry Reding 				     uint32_t src_y, uint32_t src_w,
520c7679306SThierry Reding 				     uint32_t src_h)
521c7679306SThierry Reding {
522c7679306SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
523c7679306SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
524c7679306SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
525c7679306SThierry Reding 
526c7679306SThierry Reding 	/* scaling not supported for cursor */
527c7679306SThierry Reding 	if ((src_w >> 16 != crtc_w) || (src_h >> 16 != crtc_h))
528c7679306SThierry Reding 		return -EINVAL;
529c7679306SThierry Reding 
530c7679306SThierry Reding 	/* only square cursors supported */
531c7679306SThierry Reding 	if (src_w != src_h)
532c7679306SThierry Reding 		return -EINVAL;
533c7679306SThierry Reding 
534c7679306SThierry Reding 	switch (crtc_w) {
535c7679306SThierry Reding 	case 32:
536c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
537c7679306SThierry Reding 		break;
538c7679306SThierry Reding 
539c7679306SThierry Reding 	case 64:
540c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
541c7679306SThierry Reding 		break;
542c7679306SThierry Reding 
543c7679306SThierry Reding 	case 128:
544c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
545c7679306SThierry Reding 		break;
546c7679306SThierry Reding 
547c7679306SThierry Reding 	case 256:
548c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
549c7679306SThierry Reding 		break;
550c7679306SThierry Reding 
551c7679306SThierry Reding 	default:
552c7679306SThierry Reding 		return -EINVAL;
553c7679306SThierry Reding 	}
554c7679306SThierry Reding 
555c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
556c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
557c7679306SThierry Reding 
558c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
559c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
560c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
561c7679306SThierry Reding #endif
562c7679306SThierry Reding 
563c7679306SThierry Reding 	/* enable cursor and set blend mode */
564c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
565c7679306SThierry Reding 	value |= CURSOR_ENABLE;
566c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
567c7679306SThierry Reding 
568c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
569c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
570c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
571c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
572c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
573c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
574c7679306SThierry Reding 	value |= CURSOR_ALPHA;
575c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
576c7679306SThierry Reding 
577c7679306SThierry Reding 	/* position the cursor */
578c7679306SThierry Reding 	value = (crtc_y & 0x3fff) << 16 | (crtc_x & 0x3fff);
579c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
580c7679306SThierry Reding 
581c7679306SThierry Reding 	/* apply changes */
582c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
583c7679306SThierry Reding 	tegra_dc_commit(dc);
584c7679306SThierry Reding 
585c7679306SThierry Reding 	return 0;
586c7679306SThierry Reding }
587c7679306SThierry Reding 
588c7679306SThierry Reding static int tegra_cursor_plane_disable(struct drm_plane *plane)
589c7679306SThierry Reding {
590c7679306SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->crtc);
591c7679306SThierry Reding 	u32 value;
592c7679306SThierry Reding 
593c7679306SThierry Reding 	if (!plane->crtc)
594c7679306SThierry Reding 		return 0;
595c7679306SThierry Reding 
596c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
597c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
598c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
599c7679306SThierry Reding 
600c7679306SThierry Reding 	tegra_dc_cursor_commit(dc);
601c7679306SThierry Reding 	tegra_dc_commit(dc);
602c7679306SThierry Reding 
603c7679306SThierry Reding 	return 0;
604c7679306SThierry Reding }
605c7679306SThierry Reding 
606c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
607c7679306SThierry Reding 	.update_plane = tegra_cursor_plane_update,
608c7679306SThierry Reding 	.disable_plane = tegra_cursor_plane_disable,
609c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
610c7679306SThierry Reding };
611c7679306SThierry Reding 
612c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
613c7679306SThierry Reding 						      struct tegra_dc *dc)
614c7679306SThierry Reding {
615c7679306SThierry Reding 	struct tegra_plane *plane;
616c7679306SThierry Reding 	unsigned int num_formats;
617c7679306SThierry Reding 	const u32 *formats;
618c7679306SThierry Reding 	int err;
619c7679306SThierry Reding 
620c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
621c7679306SThierry Reding 	if (!plane)
622c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
623c7679306SThierry Reding 
624c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
625c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
626c7679306SThierry Reding 
627c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
628c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
629c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_CURSOR);
630c7679306SThierry Reding 	if (err < 0) {
631c7679306SThierry Reding 		kfree(plane);
632c7679306SThierry Reding 		return ERR_PTR(err);
633c7679306SThierry Reding 	}
634c7679306SThierry Reding 
635c7679306SThierry Reding 	return &plane->base;
636c7679306SThierry Reding }
637c7679306SThierry Reding 
638c7679306SThierry Reding static int tegra_overlay_plane_update(struct drm_plane *plane,
639c7679306SThierry Reding 				      struct drm_crtc *crtc,
640c7679306SThierry Reding 				      struct drm_framebuffer *fb, int crtc_x,
641c7679306SThierry Reding 				      int crtc_y, unsigned int crtc_w,
642c7679306SThierry Reding 				      unsigned int crtc_h, uint32_t src_x,
643c7679306SThierry Reding 				      uint32_t src_y, uint32_t src_w,
644c7679306SThierry Reding 				      uint32_t src_h)
645dee8268fSThierry Reding {
646dee8268fSThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
647dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
648dee8268fSThierry Reding 	struct tegra_dc_window window;
649dee8268fSThierry Reding 	unsigned int i;
650c134f019SThierry Reding 	int err;
651dee8268fSThierry Reding 
652dee8268fSThierry Reding 	memset(&window, 0, sizeof(window));
653dee8268fSThierry Reding 	window.src.x = src_x >> 16;
654dee8268fSThierry Reding 	window.src.y = src_y >> 16;
655dee8268fSThierry Reding 	window.src.w = src_w >> 16;
656dee8268fSThierry Reding 	window.src.h = src_h >> 16;
657dee8268fSThierry Reding 	window.dst.x = crtc_x;
658dee8268fSThierry Reding 	window.dst.y = crtc_y;
659dee8268fSThierry Reding 	window.dst.w = crtc_w;
660dee8268fSThierry Reding 	window.dst.h = crtc_h;
661f925390eSThierry Reding 	window.format = tegra_dc_format(fb->pixel_format, &window.swap);
662dee8268fSThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
663db7fbdfdSThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
664c134f019SThierry Reding 
665c134f019SThierry Reding 	err = tegra_fb_get_tiling(fb, &window.tiling);
666c134f019SThierry Reding 	if (err < 0)
667c134f019SThierry Reding 		return err;
668dee8268fSThierry Reding 
669dee8268fSThierry Reding 	for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
670dee8268fSThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
671dee8268fSThierry Reding 
672dee8268fSThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
673dee8268fSThierry Reding 
674dee8268fSThierry Reding 		/*
675dee8268fSThierry Reding 		 * Tegra doesn't support different strides for U and V planes
676dee8268fSThierry Reding 		 * so we display a warning if the user tries to display a
677dee8268fSThierry Reding 		 * framebuffer with such a configuration.
678dee8268fSThierry Reding 		 */
679dee8268fSThierry Reding 		if (i >= 2) {
680dee8268fSThierry Reding 			if (fb->pitches[i] != window.stride[1])
681dee8268fSThierry Reding 				DRM_ERROR("unsupported UV-plane configuration\n");
682dee8268fSThierry Reding 		} else {
683dee8268fSThierry Reding 			window.stride[i] = fb->pitches[i];
684dee8268fSThierry Reding 		}
685dee8268fSThierry Reding 	}
686dee8268fSThierry Reding 
687dee8268fSThierry Reding 	return tegra_dc_setup_window(dc, p->index, &window);
688dee8268fSThierry Reding }
689dee8268fSThierry Reding 
690c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
691dee8268fSThierry Reding {
692c7679306SThierry Reding 	tegra_window_plane_disable(plane);
693c7679306SThierry Reding 	tegra_plane_destroy(plane);
694dee8268fSThierry Reding }
695dee8268fSThierry Reding 
696c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
697c7679306SThierry Reding 	.update_plane = tegra_overlay_plane_update,
698c7679306SThierry Reding 	.disable_plane = tegra_window_plane_disable,
699c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
700dee8268fSThierry Reding };
701dee8268fSThierry Reding 
702c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
703dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
704dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
705dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
706dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
707f925390eSThierry Reding 	DRM_FORMAT_YUYV,
708dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
709dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
710dee8268fSThierry Reding };
711dee8268fSThierry Reding 
712c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
713c7679306SThierry Reding 						       struct tegra_dc *dc,
714c7679306SThierry Reding 						       unsigned int index)
715dee8268fSThierry Reding {
716dee8268fSThierry Reding 	struct tegra_plane *plane;
717c7679306SThierry Reding 	unsigned int num_formats;
718c7679306SThierry Reding 	const u32 *formats;
719c7679306SThierry Reding 	int err;
720dee8268fSThierry Reding 
721f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
722dee8268fSThierry Reding 	if (!plane)
723c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
724dee8268fSThierry Reding 
725c7679306SThierry Reding 	plane->index = index;
726dee8268fSThierry Reding 
727c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
728c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
729c7679306SThierry Reding 
730c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
731c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
732c7679306SThierry Reding 				       num_formats, DRM_PLANE_TYPE_OVERLAY);
733f002abc1SThierry Reding 	if (err < 0) {
734f002abc1SThierry Reding 		kfree(plane);
735c7679306SThierry Reding 		return ERR_PTR(err);
736dee8268fSThierry Reding 	}
737c7679306SThierry Reding 
738c7679306SThierry Reding 	return &plane->base;
739c7679306SThierry Reding }
740c7679306SThierry Reding 
741c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
742c7679306SThierry Reding {
743c7679306SThierry Reding 	struct drm_plane *plane;
744c7679306SThierry Reding 	unsigned int i;
745c7679306SThierry Reding 
746c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
747c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
748c7679306SThierry Reding 		if (IS_ERR(plane))
749c7679306SThierry Reding 			return PTR_ERR(plane);
750f002abc1SThierry Reding 	}
751dee8268fSThierry Reding 
752dee8268fSThierry Reding 	return 0;
753dee8268fSThierry Reding }
754dee8268fSThierry Reding 
755dee8268fSThierry Reding static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
756dee8268fSThierry Reding 			     struct drm_framebuffer *fb)
757dee8268fSThierry Reding {
758dee8268fSThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
759db7fbdfdSThierry Reding 	unsigned int h_offset = 0, v_offset = 0;
760c134f019SThierry Reding 	struct tegra_bo_tiling tiling;
76193396d0fSSean Paul 	unsigned long value, flags;
762f925390eSThierry Reding 	unsigned int format, swap;
763c134f019SThierry Reding 	int err;
764c134f019SThierry Reding 
765c134f019SThierry Reding 	err = tegra_fb_get_tiling(fb, &tiling);
766c134f019SThierry Reding 	if (err < 0)
767c134f019SThierry Reding 		return err;
768dee8268fSThierry Reding 
76993396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
77093396d0fSSean Paul 
771dee8268fSThierry Reding 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
772dee8268fSThierry Reding 
773dee8268fSThierry Reding 	value = fb->offsets[0] + y * fb->pitches[0] +
774dee8268fSThierry Reding 		x * fb->bits_per_pixel / 8;
775dee8268fSThierry Reding 
776dee8268fSThierry Reding 	tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
777dee8268fSThierry Reding 	tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
778f925390eSThierry Reding 
779f925390eSThierry Reding 	format = tegra_dc_format(fb->pixel_format, &swap);
780dee8268fSThierry Reding 	tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
781f925390eSThierry Reding 	tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
782dee8268fSThierry Reding 
783c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
784c134f019SThierry Reding 		unsigned long height = tiling.value;
785c134f019SThierry Reding 
786c134f019SThierry Reding 		switch (tiling.mode) {
787c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
788c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
789c134f019SThierry Reding 			break;
790c134f019SThierry Reding 
791c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
792c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
793c134f019SThierry Reding 			break;
794c134f019SThierry Reding 
795c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
796c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
797c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
798c134f019SThierry Reding 			break;
799c134f019SThierry Reding 		}
800c134f019SThierry Reding 
801c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
802773af77fSThierry Reding 	} else {
803c134f019SThierry Reding 		switch (tiling.mode) {
804c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
805773af77fSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
806773af77fSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
807c134f019SThierry Reding 			break;
808c134f019SThierry Reding 
809c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
810c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
811c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
812c134f019SThierry Reding 			break;
813c134f019SThierry Reding 
814c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
815c134f019SThierry Reding 			DRM_ERROR("hardware doesn't support block linear mode\n");
81693396d0fSSean Paul 			spin_unlock_irqrestore(&dc->lock, flags);
817c134f019SThierry Reding 			return -EINVAL;
818773af77fSThierry Reding 		}
819773af77fSThierry Reding 
820773af77fSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
821c134f019SThierry Reding 	}
822773af77fSThierry Reding 
823db7fbdfdSThierry Reding 	/* make sure bottom-up buffers are properly displayed */
824db7fbdfdSThierry Reding 	if (tegra_fb_is_bottom_up(fb)) {
825db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
826eba66501SThierry Reding 		value |= V_DIRECTION;
827db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
828db7fbdfdSThierry Reding 
829db7fbdfdSThierry Reding 		v_offset += fb->height - 1;
830db7fbdfdSThierry Reding 	} else {
831db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
832eba66501SThierry Reding 		value &= ~V_DIRECTION;
833db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
834db7fbdfdSThierry Reding 	}
835db7fbdfdSThierry Reding 
836db7fbdfdSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
837db7fbdfdSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
838db7fbdfdSThierry Reding 
839dee8268fSThierry Reding 	value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
840205d48edSThierry Reding 	tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
841dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
842dee8268fSThierry Reding 
84393396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
84493396d0fSSean Paul 
845dee8268fSThierry Reding 	return 0;
846dee8268fSThierry Reding }
847dee8268fSThierry Reding 
848dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
849dee8268fSThierry Reding {
850dee8268fSThierry Reding 	unsigned long value, flags;
851dee8268fSThierry Reding 
852dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
853dee8268fSThierry Reding 
854dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
855dee8268fSThierry Reding 	value |= VBLANK_INT;
856dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
857dee8268fSThierry Reding 
858dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
859dee8268fSThierry Reding }
860dee8268fSThierry Reding 
861dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
862dee8268fSThierry Reding {
863dee8268fSThierry Reding 	unsigned long value, flags;
864dee8268fSThierry Reding 
865dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
866dee8268fSThierry Reding 
867dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
868dee8268fSThierry Reding 	value &= ~VBLANK_INT;
869dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
870dee8268fSThierry Reding 
871dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
872dee8268fSThierry Reding }
873dee8268fSThierry Reding 
874dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
875dee8268fSThierry Reding {
876dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
877dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
878dee8268fSThierry Reding 	unsigned long flags, base;
879dee8268fSThierry Reding 	struct tegra_bo *bo;
880dee8268fSThierry Reding 
8816b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
8826b59cc1cSThierry Reding 
8836b59cc1cSThierry Reding 	if (!dc->event) {
8846b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
885dee8268fSThierry Reding 		return;
8866b59cc1cSThierry Reding 	}
887dee8268fSThierry Reding 
888f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
889dee8268fSThierry Reding 
8908643bc6dSDan Carpenter 	spin_lock(&dc->lock);
89193396d0fSSean Paul 
892dee8268fSThierry Reding 	/* check if new start address has been latched */
89393396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
894dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
895dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
896dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
897dee8268fSThierry Reding 
8988643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
89993396d0fSSean Paul 
900f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
901ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
902ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
903dee8268fSThierry Reding 		dc->event = NULL;
904dee8268fSThierry Reding 	}
9056b59cc1cSThierry Reding 
9066b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
907dee8268fSThierry Reding }
908dee8268fSThierry Reding 
909dee8268fSThierry Reding void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
910dee8268fSThierry Reding {
911dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
912dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
913dee8268fSThierry Reding 	unsigned long flags;
914dee8268fSThierry Reding 
915dee8268fSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
916dee8268fSThierry Reding 
917dee8268fSThierry Reding 	if (dc->event && dc->event->base.file_priv == file) {
918dee8268fSThierry Reding 		dc->event->base.destroy(&dc->event->base);
919ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
920dee8268fSThierry Reding 		dc->event = NULL;
921dee8268fSThierry Reding 	}
922dee8268fSThierry Reding 
923dee8268fSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
924dee8268fSThierry Reding }
925dee8268fSThierry Reding 
926dee8268fSThierry Reding static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
927dee8268fSThierry Reding 			      struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
928dee8268fSThierry Reding {
929ed7dae58SThierry Reding 	unsigned int pipe = drm_crtc_index(crtc);
930dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
931dee8268fSThierry Reding 
932dee8268fSThierry Reding 	if (dc->event)
933dee8268fSThierry Reding 		return -EBUSY;
934dee8268fSThierry Reding 
935dee8268fSThierry Reding 	if (event) {
936ed7dae58SThierry Reding 		event->pipe = pipe;
937dee8268fSThierry Reding 		dc->event = event;
938ed7dae58SThierry Reding 		drm_crtc_vblank_get(crtc);
939dee8268fSThierry Reding 	}
940dee8268fSThierry Reding 
941dee8268fSThierry Reding 	tegra_dc_set_base(dc, 0, 0, fb);
942f4510a27SMatt Roper 	crtc->primary->fb = fb;
943dee8268fSThierry Reding 
944dee8268fSThierry Reding 	return 0;
945dee8268fSThierry Reding }
946dee8268fSThierry Reding 
947f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
948f002abc1SThierry Reding {
949f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
950f002abc1SThierry Reding }
951f002abc1SThierry Reding 
952dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
953dee8268fSThierry Reding 	.page_flip = tegra_dc_page_flip,
954dee8268fSThierry Reding 	.set_config = drm_crtc_helper_set_config,
955f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
956dee8268fSThierry Reding };
957dee8268fSThierry Reding 
95886df256fSThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
95986df256fSThierry Reding {
96086df256fSThierry Reding 	u32 value;
96186df256fSThierry Reding 
96286df256fSThierry Reding 	/* stop the display controller */
96386df256fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
96486df256fSThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
96586df256fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
96686df256fSThierry Reding 
96786df256fSThierry Reding 	tegra_dc_commit(dc);
96886df256fSThierry Reding }
96986df256fSThierry Reding 
97086df256fSThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
97186df256fSThierry Reding {
97286df256fSThierry Reding 	u32 value;
97386df256fSThierry Reding 
97486df256fSThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
97586df256fSThierry Reding 
97686df256fSThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
97786df256fSThierry Reding }
97886df256fSThierry Reding 
97986df256fSThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
98086df256fSThierry Reding {
98186df256fSThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
98286df256fSThierry Reding 
98386df256fSThierry Reding 	while (time_before(jiffies, timeout)) {
98486df256fSThierry Reding 		if (tegra_dc_idle(dc))
98586df256fSThierry Reding 			return 0;
98686df256fSThierry Reding 
98786df256fSThierry Reding 		usleep_range(1000, 2000);
98886df256fSThierry Reding 	}
98986df256fSThierry Reding 
99086df256fSThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
99186df256fSThierry Reding 	return -ETIMEDOUT;
99286df256fSThierry Reding }
99386df256fSThierry Reding 
994dee8268fSThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
995dee8268fSThierry Reding {
996f002abc1SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
997*3b0e5855SThierry Reding 	u32 value;
998f002abc1SThierry Reding 
99986df256fSThierry Reding 	if (!tegra_dc_idle(dc)) {
100086df256fSThierry Reding 		tegra_dc_stop(dc);
100186df256fSThierry Reding 
100286df256fSThierry Reding 		/*
100386df256fSThierry Reding 		 * Ignore the return value, there isn't anything useful to do
100486df256fSThierry Reding 		 * in case this fails.
100586df256fSThierry Reding 		 */
100686df256fSThierry Reding 		tegra_dc_wait_idle(dc, 100);
100786df256fSThierry Reding 	}
100836904adfSThierry Reding 
1009*3b0e5855SThierry Reding 	/*
1010*3b0e5855SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1011*3b0e5855SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1012*3b0e5855SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1013*3b0e5855SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1014*3b0e5855SThierry Reding 	 * above code is always going to timeout waiting for the controller
1015*3b0e5855SThierry Reding 	 * to go idle.
1016*3b0e5855SThierry Reding 	 *
1017*3b0e5855SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1018*3b0e5855SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1019*3b0e5855SThierry Reding 	 * encoder drivers require these bits to be cleared.
1020*3b0e5855SThierry Reding 	 *
1021*3b0e5855SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1022*3b0e5855SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1023*3b0e5855SThierry Reding 	 * the RGB encoder?
1024*3b0e5855SThierry Reding 	 */
1025*3b0e5855SThierry Reding 	if (dc->rgb) {
1026*3b0e5855SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1027*3b0e5855SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1028*3b0e5855SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1029*3b0e5855SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1030*3b0e5855SThierry Reding 	}
1031*3b0e5855SThierry Reding 
10328ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
1033c7679306SThierry Reding 	tegra_dc_commit(dc);
1034dee8268fSThierry Reding }
1035dee8268fSThierry Reding 
1036dee8268fSThierry Reding static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
1037dee8268fSThierry Reding 				  const struct drm_display_mode *mode,
1038dee8268fSThierry Reding 				  struct drm_display_mode *adjusted)
1039dee8268fSThierry Reding {
1040dee8268fSThierry Reding 	return true;
1041dee8268fSThierry Reding }
1042dee8268fSThierry Reding 
1043dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1044dee8268fSThierry Reding 				struct drm_display_mode *mode)
1045dee8268fSThierry Reding {
10460444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10470444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1048dee8268fSThierry Reding 	unsigned long value;
1049dee8268fSThierry Reding 
1050dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1051dee8268fSThierry Reding 
1052dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1053dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1054dee8268fSThierry Reding 
1055dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1056dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1057dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1058dee8268fSThierry Reding 
1059dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1060dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1061dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1062dee8268fSThierry Reding 
1063dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1064dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1065dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1066dee8268fSThierry Reding 
1067dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1068dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1069dee8268fSThierry Reding 
1070dee8268fSThierry Reding 	return 0;
1071dee8268fSThierry Reding }
1072dee8268fSThierry Reding 
1073dee8268fSThierry Reding static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
1074dbb3f2f7SThierry Reding 				struct drm_display_mode *mode)
1075dee8268fSThierry Reding {
107691eded9bSThierry Reding 	unsigned long pclk = mode->clock * 1000;
1077dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1078dee8268fSThierry Reding 	struct tegra_output *output = NULL;
1079dee8268fSThierry Reding 	struct drm_encoder *encoder;
1080dbb3f2f7SThierry Reding 	unsigned int div;
1081dbb3f2f7SThierry Reding 	u32 value;
1082dee8268fSThierry Reding 	long err;
1083dee8268fSThierry Reding 
1084dee8268fSThierry Reding 	list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
1085dee8268fSThierry Reding 		if (encoder->crtc == crtc) {
1086dee8268fSThierry Reding 			output = encoder_to_output(encoder);
1087dee8268fSThierry Reding 			break;
1088dee8268fSThierry Reding 		}
1089dee8268fSThierry Reding 
1090dee8268fSThierry Reding 	if (!output)
1091dee8268fSThierry Reding 		return -ENODEV;
1092dee8268fSThierry Reding 
1093dee8268fSThierry Reding 	/*
1094d5bae6f3SThierry Reding 	 * The ->setup_clock() callback is optional, but if encoders don't
1095d5bae6f3SThierry Reding 	 * implement it they most likely need to do the equivalent within the
1096d5bae6f3SThierry Reding 	 * ->mode_fixup() callback.
1097d5bae6f3SThierry Reding 	 */
1098d5bae6f3SThierry Reding 	if (!output->ops || !output->ops->setup_clock)
1099d5bae6f3SThierry Reding 		return 0;
1100d5bae6f3SThierry Reding 
1101d5bae6f3SThierry Reding 	/*
110291eded9bSThierry Reding 	 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
110391eded9bSThierry Reding 	 * respectively, each of which divides the base pll_d by 2.
1104dee8268fSThierry Reding 	 */
1105d5bae6f3SThierry Reding 	err = output->ops->setup_clock(output, dc->clk, pclk, &div);
1106dee8268fSThierry Reding 	if (err < 0) {
1107dee8268fSThierry Reding 		dev_err(dc->dev, "failed to setup clock: %ld\n", err);
1108dee8268fSThierry Reding 		return err;
1109dee8268fSThierry Reding 	}
1110dee8268fSThierry Reding 
111191eded9bSThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
1112dbb3f2f7SThierry Reding 
1113dbb3f2f7SThierry Reding 	value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1114dbb3f2f7SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1115dee8268fSThierry Reding 
1116dee8268fSThierry Reding 	return 0;
1117dee8268fSThierry Reding }
1118dee8268fSThierry Reding 
1119c5a107d3SThierry Reding int tegra_dc_setup_clock(struct tegra_dc *dc, struct clk *parent,
1120c5a107d3SThierry Reding 			 unsigned long pclk, unsigned int div)
1121c5a107d3SThierry Reding {
1122c5a107d3SThierry Reding 	u32 value;
1123c5a107d3SThierry Reding 	int err;
1124c5a107d3SThierry Reding 
1125c5a107d3SThierry Reding 	err = clk_set_parent(dc->clk, parent);
1126c5a107d3SThierry Reding 	if (err < 0) {
1127c5a107d3SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1128c5a107d3SThierry Reding 		return err;
1129c5a107d3SThierry Reding 	}
1130c5a107d3SThierry Reding 
1131c5a107d3SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
1132c5a107d3SThierry Reding 
1133c5a107d3SThierry Reding 	value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1134c5a107d3SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1135c5a107d3SThierry Reding 
1136c5a107d3SThierry Reding 	return 0;
1137c5a107d3SThierry Reding }
1138c5a107d3SThierry Reding 
1139dee8268fSThierry Reding static int tegra_crtc_mode_set(struct drm_crtc *crtc,
1140dee8268fSThierry Reding 			       struct drm_display_mode *mode,
1141dee8268fSThierry Reding 			       struct drm_display_mode *adjusted,
1142dee8268fSThierry Reding 			       int x, int y, struct drm_framebuffer *old_fb)
1143dee8268fSThierry Reding {
1144f4510a27SMatt Roper 	struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
1145dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1146dee8268fSThierry Reding 	struct tegra_dc_window window;
1147dbb3f2f7SThierry Reding 	u32 value;
1148dee8268fSThierry Reding 	int err;
1149dee8268fSThierry Reding 
1150dbb3f2f7SThierry Reding 	err = tegra_crtc_setup_clk(crtc, mode);
1151dee8268fSThierry Reding 	if (err) {
1152dee8268fSThierry Reding 		dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
1153dee8268fSThierry Reding 		return err;
1154dee8268fSThierry Reding 	}
1155dee8268fSThierry Reding 
1156dee8268fSThierry Reding 	/* program display mode */
1157dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1158dee8268fSThierry Reding 
115942d0659bSThierry Reding 	if (dc->soc->supports_border_color)
116042d0659bSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
116142d0659bSThierry Reding 
11628620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
11638620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
11648620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
11658620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
11668620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
11678620fc62SThierry Reding 	}
11688620fc62SThierry Reding 
1169dee8268fSThierry Reding 	/* setup window parameters */
1170dee8268fSThierry Reding 	memset(&window, 0, sizeof(window));
1171dee8268fSThierry Reding 	window.src.x = 0;
1172dee8268fSThierry Reding 	window.src.y = 0;
1173dee8268fSThierry Reding 	window.src.w = mode->hdisplay;
1174dee8268fSThierry Reding 	window.src.h = mode->vdisplay;
1175dee8268fSThierry Reding 	window.dst.x = 0;
1176dee8268fSThierry Reding 	window.dst.y = 0;
1177dee8268fSThierry Reding 	window.dst.w = mode->hdisplay;
1178dee8268fSThierry Reding 	window.dst.h = mode->vdisplay;
1179f925390eSThierry Reding 	window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
1180f925390eSThierry Reding 					&window.swap);
1181f4510a27SMatt Roper 	window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
1182f4510a27SMatt Roper 	window.stride[0] = crtc->primary->fb->pitches[0];
1183dee8268fSThierry Reding 	window.base[0] = bo->paddr;
1184dee8268fSThierry Reding 
1185dee8268fSThierry Reding 	err = tegra_dc_setup_window(dc, 0, &window);
1186dee8268fSThierry Reding 	if (err < 0)
1187dee8268fSThierry Reding 		dev_err(dc->dev, "failed to enable root plane\n");
1188dee8268fSThierry Reding 
1189dee8268fSThierry Reding 	return 0;
1190dee8268fSThierry Reding }
1191dee8268fSThierry Reding 
1192dee8268fSThierry Reding static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1193dee8268fSThierry Reding 				    struct drm_framebuffer *old_fb)
1194dee8268fSThierry Reding {
1195dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1196dee8268fSThierry Reding 
1197f4510a27SMatt Roper 	return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
1198dee8268fSThierry Reding }
1199dee8268fSThierry Reding 
1200dee8268fSThierry Reding static void tegra_crtc_prepare(struct drm_crtc *crtc)
1201dee8268fSThierry Reding {
1202dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1203dee8268fSThierry Reding 	unsigned int syncpt;
1204dee8268fSThierry Reding 	unsigned long value;
1205dee8268fSThierry Reding 
12068ff64c17SThierry Reding 	drm_crtc_vblank_off(crtc);
12078ff64c17SThierry Reding 
1208dee8268fSThierry Reding 	/* hardware initialization */
1209ca48080aSStephen Warren 	reset_control_deassert(dc->rst);
1210dee8268fSThierry Reding 	usleep_range(10000, 20000);
1211dee8268fSThierry Reding 
1212dee8268fSThierry Reding 	if (dc->pipe)
1213dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK1;
1214dee8268fSThierry Reding 	else
1215dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK0;
1216dee8268fSThierry Reding 
1217dee8268fSThierry Reding 	/* initialize display controller */
1218dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1219dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1220dee8268fSThierry Reding 
1221dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1222dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1223dee8268fSThierry Reding 
1224dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1225dee8268fSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1226dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1227dee8268fSThierry Reding 
1228dee8268fSThierry Reding 	/* initialize timer */
1229dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1230dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1231dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1232dee8268fSThierry Reding 
1233dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1234dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1235dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1236dee8268fSThierry Reding 
1237dee8268fSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1238dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1239dee8268fSThierry Reding 
1240dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1241dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1242dee8268fSThierry Reding }
1243dee8268fSThierry Reding 
1244dee8268fSThierry Reding static void tegra_crtc_commit(struct drm_crtc *crtc)
1245dee8268fSThierry Reding {
1246dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1247dee8268fSThierry Reding 
12488ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1249205d48edSThierry Reding 	tegra_dc_commit(dc);
1250dee8268fSThierry Reding }
1251dee8268fSThierry Reding 
1252dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
1253dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
1254dee8268fSThierry Reding 	.mode_fixup = tegra_crtc_mode_fixup,
1255dee8268fSThierry Reding 	.mode_set = tegra_crtc_mode_set,
1256dee8268fSThierry Reding 	.mode_set_base = tegra_crtc_mode_set_base,
1257dee8268fSThierry Reding 	.prepare = tegra_crtc_prepare,
1258dee8268fSThierry Reding 	.commit = tegra_crtc_commit,
1259dee8268fSThierry Reding };
1260dee8268fSThierry Reding 
1261dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1262dee8268fSThierry Reding {
1263dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1264dee8268fSThierry Reding 	unsigned long status;
1265dee8268fSThierry Reding 
1266dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1267dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1268dee8268fSThierry Reding 
1269dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1270dee8268fSThierry Reding 		/*
1271dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1272dee8268fSThierry Reding 		*/
1273dee8268fSThierry Reding 	}
1274dee8268fSThierry Reding 
1275dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1276dee8268fSThierry Reding 		/*
1277dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1278dee8268fSThierry Reding 		*/
1279ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1280dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1281dee8268fSThierry Reding 	}
1282dee8268fSThierry Reding 
1283dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1284dee8268fSThierry Reding 		/*
1285dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1286dee8268fSThierry Reding 		*/
1287dee8268fSThierry Reding 	}
1288dee8268fSThierry Reding 
1289dee8268fSThierry Reding 	return IRQ_HANDLED;
1290dee8268fSThierry Reding }
1291dee8268fSThierry Reding 
1292dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1293dee8268fSThierry Reding {
1294dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1295dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1296dee8268fSThierry Reding 
1297dee8268fSThierry Reding #define DUMP_REG(name)						\
129803a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1299dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1300dee8268fSThierry Reding 
1301dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1302dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1303dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1304dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1305dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1306dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1307dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1308dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1309dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1310dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1311dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1312dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1313dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1314dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1315dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1316dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1317dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1318dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1319dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1320dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1321dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1322dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1323dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1324dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1325dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1326dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1327dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1328dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1329dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1330dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1331dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1332dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1333dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1334dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1335dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1336dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1337dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1338dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1339dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1340dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1341dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1342dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1343dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1344dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1345dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1346dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1347dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1348dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1349dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1350dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1351dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1352dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1353dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1354dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1355dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1356dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1357dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1358dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1359dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1360dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1361dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1362dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1363dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1364dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1365dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1366dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1367dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1368dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1369dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1370dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1371dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1372dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1373dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1374dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1375dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1376dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1377dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1378dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1379dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1380dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1381dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1382dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1383dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1384dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1385dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1386dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1387dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1388dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1389dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1390dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1391dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1392dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1393dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1394dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1395dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1396dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1397dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1398dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1399dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1400dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1401dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1402dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1403dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1404dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1405dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1406dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1407dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1408dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1409dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1410dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1411dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1412dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1413dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1414dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1415dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1416dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1417dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1418dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1419dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1420dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1421dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1422dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1423dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1424dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1425dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1426dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1427dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1428dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1429dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1430dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1431dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1432dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1433dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1434dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1435dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1436dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1437dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1438dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1439dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1440dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1441dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1442dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1443dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1444dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1445dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1446dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1447dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1448dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1449dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1450dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1451dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1452dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1453dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1454dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1455dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1456dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1457dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1458dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1459dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1460dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1461dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1462dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1463dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1464dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1465dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1466dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1467dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1468dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1469dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1470dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1471dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1472dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1473dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1474dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1475dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1476e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1477e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1478dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1479dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1480dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1481dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1482dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1483dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1484dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1485dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1486dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1487dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1488dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1489dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1490dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1491dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1492dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1493dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1494dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1495dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1496dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1497dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1498dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1499dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1500dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1501dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1502dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1503dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1504dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1505dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1506dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1507dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1508dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1509dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1510dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1511dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1512dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1513dee8268fSThierry Reding 
1514dee8268fSThierry Reding #undef DUMP_REG
1515dee8268fSThierry Reding 
1516dee8268fSThierry Reding 	return 0;
1517dee8268fSThierry Reding }
1518dee8268fSThierry Reding 
1519dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1520dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1521dee8268fSThierry Reding };
1522dee8268fSThierry Reding 
1523dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1524dee8268fSThierry Reding {
1525dee8268fSThierry Reding 	unsigned int i;
1526dee8268fSThierry Reding 	char *name;
1527dee8268fSThierry Reding 	int err;
1528dee8268fSThierry Reding 
1529dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1530dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1531dee8268fSThierry Reding 	kfree(name);
1532dee8268fSThierry Reding 
1533dee8268fSThierry Reding 	if (!dc->debugfs)
1534dee8268fSThierry Reding 		return -ENOMEM;
1535dee8268fSThierry Reding 
1536dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1537dee8268fSThierry Reding 				    GFP_KERNEL);
1538dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1539dee8268fSThierry Reding 		err = -ENOMEM;
1540dee8268fSThierry Reding 		goto remove;
1541dee8268fSThierry Reding 	}
1542dee8268fSThierry Reding 
1543dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1544dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1545dee8268fSThierry Reding 
1546dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1547dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1548dee8268fSThierry Reding 				       dc->debugfs, minor);
1549dee8268fSThierry Reding 	if (err < 0)
1550dee8268fSThierry Reding 		goto free;
1551dee8268fSThierry Reding 
1552dee8268fSThierry Reding 	dc->minor = minor;
1553dee8268fSThierry Reding 
1554dee8268fSThierry Reding 	return 0;
1555dee8268fSThierry Reding 
1556dee8268fSThierry Reding free:
1557dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1558dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1559dee8268fSThierry Reding remove:
1560dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1561dee8268fSThierry Reding 	dc->debugfs = NULL;
1562dee8268fSThierry Reding 
1563dee8268fSThierry Reding 	return err;
1564dee8268fSThierry Reding }
1565dee8268fSThierry Reding 
1566dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1567dee8268fSThierry Reding {
1568dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1569dee8268fSThierry Reding 				 dc->minor);
1570dee8268fSThierry Reding 	dc->minor = NULL;
1571dee8268fSThierry Reding 
1572dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1573dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1574dee8268fSThierry Reding 
1575dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1576dee8268fSThierry Reding 	dc->debugfs = NULL;
1577dee8268fSThierry Reding 
1578dee8268fSThierry Reding 	return 0;
1579dee8268fSThierry Reding }
1580dee8268fSThierry Reding 
1581dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1582dee8268fSThierry Reding {
15839910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
1584dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1585d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1586c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1587c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1588dee8268fSThierry Reding 	int err;
1589dee8268fSThierry Reding 
1590df06b759SThierry Reding 	if (tegra->domain) {
1591df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1592df06b759SThierry Reding 		if (err < 0) {
1593df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1594df06b759SThierry Reding 				err);
1595df06b759SThierry Reding 			return err;
1596df06b759SThierry Reding 		}
1597df06b759SThierry Reding 
1598df06b759SThierry Reding 		dc->domain = tegra->domain;
1599df06b759SThierry Reding 	}
1600df06b759SThierry Reding 
1601c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1602c7679306SThierry Reding 	if (IS_ERR(primary)) {
1603c7679306SThierry Reding 		err = PTR_ERR(primary);
1604c7679306SThierry Reding 		goto cleanup;
1605c7679306SThierry Reding 	}
1606c7679306SThierry Reding 
1607c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1608c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1609c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1610c7679306SThierry Reding 			err = PTR_ERR(cursor);
1611c7679306SThierry Reding 			goto cleanup;
1612c7679306SThierry Reding 		}
1613c7679306SThierry Reding 	}
1614c7679306SThierry Reding 
1615c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1616c7679306SThierry Reding 					&tegra_crtc_funcs);
1617c7679306SThierry Reding 	if (err < 0)
1618c7679306SThierry Reding 		goto cleanup;
1619c7679306SThierry Reding 
1620dee8268fSThierry Reding 	drm_mode_crtc_set_gamma_size(&dc->base, 256);
1621dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1622dee8268fSThierry Reding 
1623d1f3e1e0SThierry Reding 	/*
1624d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1625d1f3e1e0SThierry Reding 	 * controllers.
1626d1f3e1e0SThierry Reding 	 */
1627d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1628d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1629d1f3e1e0SThierry Reding 
16309910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1631dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1632dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1633c7679306SThierry Reding 		goto cleanup;
1634dee8268fSThierry Reding 	}
1635dee8268fSThierry Reding 
16369910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1637dee8268fSThierry Reding 	if (err < 0)
1638c7679306SThierry Reding 		goto cleanup;
1639dee8268fSThierry Reding 
1640dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
16419910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1642dee8268fSThierry Reding 		if (err < 0)
1643dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1644dee8268fSThierry Reding 	}
1645dee8268fSThierry Reding 
1646dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1647dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1648dee8268fSThierry Reding 	if (err < 0) {
1649dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1650dee8268fSThierry Reding 			err);
1651c7679306SThierry Reding 		goto cleanup;
1652dee8268fSThierry Reding 	}
1653dee8268fSThierry Reding 
1654dee8268fSThierry Reding 	return 0;
1655c7679306SThierry Reding 
1656c7679306SThierry Reding cleanup:
1657c7679306SThierry Reding 	if (cursor)
1658c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1659c7679306SThierry Reding 
1660c7679306SThierry Reding 	if (primary)
1661c7679306SThierry Reding 		drm_plane_cleanup(primary);
1662c7679306SThierry Reding 
1663c7679306SThierry Reding 	if (tegra->domain) {
1664c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1665c7679306SThierry Reding 		dc->domain = NULL;
1666c7679306SThierry Reding 	}
1667c7679306SThierry Reding 
1668c7679306SThierry Reding 	return err;
1669dee8268fSThierry Reding }
1670dee8268fSThierry Reding 
1671dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1672dee8268fSThierry Reding {
1673dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1674dee8268fSThierry Reding 	int err;
1675dee8268fSThierry Reding 
1676dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1677dee8268fSThierry Reding 
1678dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1679dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1680dee8268fSThierry Reding 		if (err < 0)
1681dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1682dee8268fSThierry Reding 	}
1683dee8268fSThierry Reding 
1684dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1685dee8268fSThierry Reding 	if (err) {
1686dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1687dee8268fSThierry Reding 		return err;
1688dee8268fSThierry Reding 	}
1689dee8268fSThierry Reding 
1690df06b759SThierry Reding 	if (dc->domain) {
1691df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1692df06b759SThierry Reding 		dc->domain = NULL;
1693df06b759SThierry Reding 	}
1694df06b759SThierry Reding 
1695dee8268fSThierry Reding 	return 0;
1696dee8268fSThierry Reding }
1697dee8268fSThierry Reding 
1698dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1699dee8268fSThierry Reding 	.init = tegra_dc_init,
1700dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1701dee8268fSThierry Reding };
1702dee8268fSThierry Reding 
17038620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
170442d0659bSThierry Reding 	.supports_border_color = true,
17058620fc62SThierry Reding 	.supports_interlacing = false,
1706e687651bSThierry Reding 	.supports_cursor = false,
1707c134f019SThierry Reding 	.supports_block_linear = false,
1708d1f3e1e0SThierry Reding 	.pitch_align = 8,
17099c012700SThierry Reding 	.has_powergate = false,
17108620fc62SThierry Reding };
17118620fc62SThierry Reding 
17128620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
171342d0659bSThierry Reding 	.supports_border_color = true,
17148620fc62SThierry Reding 	.supports_interlacing = false,
1715e687651bSThierry Reding 	.supports_cursor = false,
1716c134f019SThierry Reding 	.supports_block_linear = false,
1717d1f3e1e0SThierry Reding 	.pitch_align = 8,
17189c012700SThierry Reding 	.has_powergate = false,
1719d1f3e1e0SThierry Reding };
1720d1f3e1e0SThierry Reding 
1721d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
172242d0659bSThierry Reding 	.supports_border_color = true,
1723d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1724d1f3e1e0SThierry Reding 	.supports_cursor = false,
1725d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1726d1f3e1e0SThierry Reding 	.pitch_align = 64,
17279c012700SThierry Reding 	.has_powergate = true,
17288620fc62SThierry Reding };
17298620fc62SThierry Reding 
17308620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
173142d0659bSThierry Reding 	.supports_border_color = false,
17328620fc62SThierry Reding 	.supports_interlacing = true,
1733e687651bSThierry Reding 	.supports_cursor = true,
1734c134f019SThierry Reding 	.supports_block_linear = true,
1735d1f3e1e0SThierry Reding 	.pitch_align = 64,
17369c012700SThierry Reding 	.has_powergate = true,
17378620fc62SThierry Reding };
17388620fc62SThierry Reding 
17398620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
17408620fc62SThierry Reding 	{
17418620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
17428620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
17438620fc62SThierry Reding 	}, {
17449c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
17459c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
17469c012700SThierry Reding 	}, {
17478620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
17488620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
17498620fc62SThierry Reding 	}, {
17508620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
17518620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
17528620fc62SThierry Reding 	}, {
17538620fc62SThierry Reding 		/* sentinel */
17548620fc62SThierry Reding 	}
17558620fc62SThierry Reding };
1756ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
17578620fc62SThierry Reding 
175813411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
175913411dddSThierry Reding {
176013411dddSThierry Reding 	struct device_node *np;
176113411dddSThierry Reding 	u32 value = 0;
176213411dddSThierry Reding 	int err;
176313411dddSThierry Reding 
176413411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
176513411dddSThierry Reding 	if (err < 0) {
176613411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
176713411dddSThierry Reding 
176813411dddSThierry Reding 		/*
176913411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
177013411dddSThierry Reding 		 * correct head number by looking up the position of this
177113411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
177213411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
177313411dddSThierry Reding 		 * that the translation into a flattened device tree blob
177413411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
177513411dddSThierry Reding 		 * head number.
177613411dddSThierry Reding 		 *
177713411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
177813411dddSThierry Reding 		 * cases where only a single display controller is used.
177913411dddSThierry Reding 		 */
178013411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
178113411dddSThierry Reding 			if (np == dc->dev->of_node)
178213411dddSThierry Reding 				break;
178313411dddSThierry Reding 
178413411dddSThierry Reding 			value++;
178513411dddSThierry Reding 		}
178613411dddSThierry Reding 	}
178713411dddSThierry Reding 
178813411dddSThierry Reding 	dc->pipe = value;
178913411dddSThierry Reding 
179013411dddSThierry Reding 	return 0;
179113411dddSThierry Reding }
179213411dddSThierry Reding 
1793dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1794dee8268fSThierry Reding {
17958620fc62SThierry Reding 	const struct of_device_id *id;
1796dee8268fSThierry Reding 	struct resource *regs;
1797dee8268fSThierry Reding 	struct tegra_dc *dc;
1798dee8268fSThierry Reding 	int err;
1799dee8268fSThierry Reding 
1800dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1801dee8268fSThierry Reding 	if (!dc)
1802dee8268fSThierry Reding 		return -ENOMEM;
1803dee8268fSThierry Reding 
18048620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
18058620fc62SThierry Reding 	if (!id)
18068620fc62SThierry Reding 		return -ENODEV;
18078620fc62SThierry Reding 
1808dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1809dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1810dee8268fSThierry Reding 	dc->dev = &pdev->dev;
18118620fc62SThierry Reding 	dc->soc = id->data;
1812dee8268fSThierry Reding 
181313411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
181413411dddSThierry Reding 	if (err < 0)
181513411dddSThierry Reding 		return err;
181613411dddSThierry Reding 
1817dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1818dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1819dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1820dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1821dee8268fSThierry Reding 	}
1822dee8268fSThierry Reding 
1823ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1824ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1825ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1826ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1827ca48080aSStephen Warren 	}
1828ca48080aSStephen Warren 
18299c012700SThierry Reding 	if (dc->soc->has_powergate) {
18309c012700SThierry Reding 		if (dc->pipe == 0)
18319c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
18329c012700SThierry Reding 		else
18339c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
18349c012700SThierry Reding 
18359c012700SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
18369c012700SThierry Reding 							dc->rst);
18379c012700SThierry Reding 		if (err < 0) {
18389c012700SThierry Reding 			dev_err(&pdev->dev, "failed to power partition: %d\n",
18399c012700SThierry Reding 				err);
1840dee8268fSThierry Reding 			return err;
18419c012700SThierry Reding 		}
18429c012700SThierry Reding 	} else {
18439c012700SThierry Reding 		err = clk_prepare_enable(dc->clk);
18449c012700SThierry Reding 		if (err < 0) {
18459c012700SThierry Reding 			dev_err(&pdev->dev, "failed to enable clock: %d\n",
18469c012700SThierry Reding 				err);
18479c012700SThierry Reding 			return err;
18489c012700SThierry Reding 		}
18499c012700SThierry Reding 
18509c012700SThierry Reding 		err = reset_control_deassert(dc->rst);
18519c012700SThierry Reding 		if (err < 0) {
18529c012700SThierry Reding 			dev_err(&pdev->dev, "failed to deassert reset: %d\n",
18539c012700SThierry Reding 				err);
18549c012700SThierry Reding 			return err;
18559c012700SThierry Reding 		}
18569c012700SThierry Reding 	}
1857dee8268fSThierry Reding 
1858dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1859dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1860dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1861dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1862dee8268fSThierry Reding 
1863dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1864dee8268fSThierry Reding 	if (dc->irq < 0) {
1865dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1866dee8268fSThierry Reding 		return -ENXIO;
1867dee8268fSThierry Reding 	}
1868dee8268fSThierry Reding 
1869dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
1870dee8268fSThierry Reding 	dc->client.ops = &dc_client_ops;
1871dee8268fSThierry Reding 	dc->client.dev = &pdev->dev;
1872dee8268fSThierry Reding 
1873dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1874dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1875dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1876dee8268fSThierry Reding 		return err;
1877dee8268fSThierry Reding 	}
1878dee8268fSThierry Reding 
1879dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1880dee8268fSThierry Reding 	if (err < 0) {
1881dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1882dee8268fSThierry Reding 			err);
1883dee8268fSThierry Reding 		return err;
1884dee8268fSThierry Reding 	}
1885dee8268fSThierry Reding 
1886dee8268fSThierry Reding 	platform_set_drvdata(pdev, dc);
1887dee8268fSThierry Reding 
1888dee8268fSThierry Reding 	return 0;
1889dee8268fSThierry Reding }
1890dee8268fSThierry Reding 
1891dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1892dee8268fSThierry Reding {
1893dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1894dee8268fSThierry Reding 	int err;
1895dee8268fSThierry Reding 
1896dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1897dee8268fSThierry Reding 	if (err < 0) {
1898dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1899dee8268fSThierry Reding 			err);
1900dee8268fSThierry Reding 		return err;
1901dee8268fSThierry Reding 	}
1902dee8268fSThierry Reding 
190359d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
190459d29c0eSThierry Reding 	if (err < 0) {
190559d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
190659d29c0eSThierry Reding 		return err;
190759d29c0eSThierry Reding 	}
190859d29c0eSThierry Reding 
19095482d75aSThierry Reding 	reset_control_assert(dc->rst);
19109c012700SThierry Reding 
19119c012700SThierry Reding 	if (dc->soc->has_powergate)
19129c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
19139c012700SThierry Reding 
1914dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
1915dee8268fSThierry Reding 
1916dee8268fSThierry Reding 	return 0;
1917dee8268fSThierry Reding }
1918dee8268fSThierry Reding 
1919dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
1920dee8268fSThierry Reding 	.driver = {
1921dee8268fSThierry Reding 		.name = "tegra-dc",
1922dee8268fSThierry Reding 		.owner = THIS_MODULE,
1923dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
1924dee8268fSThierry Reding 	},
1925dee8268fSThierry Reding 	.probe = tegra_dc_probe,
1926dee8268fSThierry Reding 	.remove = tegra_dc_remove,
1927dee8268fSThierry Reding };
1928