xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision c49c81e21ce1a646c1402597d12137c679ac7f71)
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>
13b9ff7aeaSThierry Reding #include <linux/of_device.h>
1433a8eb8dSThierry Reding #include <linux/pm_runtime.h>
15ca48080aSStephen Warren #include <linux/reset.h>
16dee8268fSThierry Reding 
179c012700SThierry Reding #include <soc/tegra/pmc.h>
189c012700SThierry Reding 
19dee8268fSThierry Reding #include "dc.h"
20dee8268fSThierry Reding #include "drm.h"
21dee8268fSThierry Reding #include "gem.h"
22dee8268fSThierry Reding 
239d44189fSThierry Reding #include <drm/drm_atomic.h>
244aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
253cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
263cb9ae4fSDaniel Vetter 
27dee8268fSThierry Reding struct tegra_plane {
28dee8268fSThierry Reding 	struct drm_plane base;
29dee8268fSThierry Reding 	unsigned int index;
30dee8268fSThierry Reding };
31dee8268fSThierry Reding 
32dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
33dee8268fSThierry Reding {
34dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
35dee8268fSThierry Reding }
36dee8268fSThierry Reding 
37ca915b10SThierry Reding struct tegra_dc_state {
38ca915b10SThierry Reding 	struct drm_crtc_state base;
39ca915b10SThierry Reding 
40ca915b10SThierry Reding 	struct clk *clk;
41ca915b10SThierry Reding 	unsigned long pclk;
42ca915b10SThierry Reding 	unsigned int div;
4347802b09SThierry Reding 
4447802b09SThierry Reding 	u32 planes;
45ca915b10SThierry Reding };
46ca915b10SThierry Reding 
47ca915b10SThierry Reding static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
48ca915b10SThierry Reding {
49ca915b10SThierry Reding 	if (state)
50ca915b10SThierry Reding 		return container_of(state, struct tegra_dc_state, base);
51ca915b10SThierry Reding 
52ca915b10SThierry Reding 	return NULL;
53ca915b10SThierry Reding }
54ca915b10SThierry Reding 
558f604f8cSThierry Reding struct tegra_plane_state {
568f604f8cSThierry Reding 	struct drm_plane_state base;
578f604f8cSThierry Reding 
588f604f8cSThierry Reding 	struct tegra_bo_tiling tiling;
598f604f8cSThierry Reding 	u32 format;
608f604f8cSThierry Reding 	u32 swap;
618f604f8cSThierry Reding };
628f604f8cSThierry Reding 
638f604f8cSThierry Reding static inline struct tegra_plane_state *
648f604f8cSThierry Reding to_tegra_plane_state(struct drm_plane_state *state)
658f604f8cSThierry Reding {
668f604f8cSThierry Reding 	if (state)
678f604f8cSThierry Reding 		return container_of(state, struct tegra_plane_state, base);
688f604f8cSThierry Reding 
698f604f8cSThierry Reding 	return NULL;
708f604f8cSThierry Reding }
718f604f8cSThierry Reding 
72791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
73791ddb1eSThierry Reding {
74791ddb1eSThierry Reding 	stats->frames = 0;
75791ddb1eSThierry Reding 	stats->vblank = 0;
76791ddb1eSThierry Reding 	stats->underflow = 0;
77791ddb1eSThierry Reding 	stats->overflow = 0;
78791ddb1eSThierry Reding }
79791ddb1eSThierry Reding 
80d700ba7aSThierry Reding /*
8186df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
8286df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
8386df256fSThierry Reding  * active copy of some registers.
8486df256fSThierry Reding  */
8586df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
8686df256fSThierry Reding {
8786df256fSThierry Reding 	unsigned long flags;
8886df256fSThierry Reding 	u32 value;
8986df256fSThierry Reding 
9086df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
9186df256fSThierry Reding 
9286df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
9386df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
9486df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
9586df256fSThierry Reding 
9686df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
9786df256fSThierry Reding 	return value;
9886df256fSThierry Reding }
9986df256fSThierry Reding 
10086df256fSThierry Reding /*
101d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
102d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
103d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
104d700ba7aSThierry Reding  * on the next frame boundary otherwise.
105d700ba7aSThierry Reding  *
106d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
107d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
108d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
109d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
110d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
111d700ba7aSThierry Reding  */
11262b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
113205d48edSThierry Reding {
114205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
115205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
116205d48edSThierry Reding }
117205d48edSThierry Reding 
1188f604f8cSThierry Reding static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
11910288eeaSThierry Reding {
12010288eeaSThierry Reding 	/* assume no swapping of fetched data */
12110288eeaSThierry Reding 	if (swap)
12210288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
12310288eeaSThierry Reding 
1248f604f8cSThierry Reding 	switch (fourcc) {
12510288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
1268f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
1278f604f8cSThierry Reding 		break;
12810288eeaSThierry Reding 
12910288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
1308f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
1318f604f8cSThierry Reding 		break;
13210288eeaSThierry Reding 
13310288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
1348f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
1358f604f8cSThierry Reding 		break;
13610288eeaSThierry Reding 
13710288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
1388f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1398f604f8cSThierry Reding 		break;
14010288eeaSThierry Reding 
14110288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
14210288eeaSThierry Reding 		if (swap)
14310288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
14410288eeaSThierry Reding 
1458f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1468f604f8cSThierry Reding 		break;
14710288eeaSThierry Reding 
14810288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
1498f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
1508f604f8cSThierry Reding 		break;
15110288eeaSThierry Reding 
15210288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
1538f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
1548f604f8cSThierry Reding 		break;
15510288eeaSThierry Reding 
15610288eeaSThierry Reding 	default:
1578f604f8cSThierry Reding 		return -EINVAL;
15810288eeaSThierry Reding 	}
15910288eeaSThierry Reding 
1608f604f8cSThierry Reding 	return 0;
16110288eeaSThierry Reding }
16210288eeaSThierry Reding 
16310288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
16410288eeaSThierry Reding {
16510288eeaSThierry Reding 	switch (format) {
16610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
16710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
16810288eeaSThierry Reding 		if (planar)
16910288eeaSThierry Reding 			*planar = false;
17010288eeaSThierry Reding 
17110288eeaSThierry Reding 		return true;
17210288eeaSThierry Reding 
17310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
17410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
17510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
17610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
17710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
17810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
17910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
18010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
18110288eeaSThierry Reding 		if (planar)
18210288eeaSThierry Reding 			*planar = true;
18310288eeaSThierry Reding 
18410288eeaSThierry Reding 		return true;
18510288eeaSThierry Reding 	}
18610288eeaSThierry Reding 
187fb35c6b6SThierry Reding 	if (planar)
188fb35c6b6SThierry Reding 		*planar = false;
189fb35c6b6SThierry Reding 
19010288eeaSThierry Reding 	return false;
19110288eeaSThierry Reding }
19210288eeaSThierry Reding 
19310288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
19410288eeaSThierry Reding 				  unsigned int bpp)
19510288eeaSThierry Reding {
19610288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
19710288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
19810288eeaSThierry Reding 	u32 dda_inc;
19910288eeaSThierry Reding 	int max;
20010288eeaSThierry Reding 
20110288eeaSThierry Reding 	if (v)
20210288eeaSThierry Reding 		max = 15;
20310288eeaSThierry Reding 	else {
20410288eeaSThierry Reding 		switch (bpp) {
20510288eeaSThierry Reding 		case 2:
20610288eeaSThierry Reding 			max = 8;
20710288eeaSThierry Reding 			break;
20810288eeaSThierry Reding 
20910288eeaSThierry Reding 		default:
21010288eeaSThierry Reding 			WARN_ON_ONCE(1);
21110288eeaSThierry Reding 			/* fallthrough */
21210288eeaSThierry Reding 		case 4:
21310288eeaSThierry Reding 			max = 4;
21410288eeaSThierry Reding 			break;
21510288eeaSThierry Reding 		}
21610288eeaSThierry Reding 	}
21710288eeaSThierry Reding 
21810288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
21910288eeaSThierry Reding 	inf.full -= dfixed_const(1);
22010288eeaSThierry Reding 
22110288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
22210288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
22310288eeaSThierry Reding 
22410288eeaSThierry Reding 	return dda_inc;
22510288eeaSThierry Reding }
22610288eeaSThierry Reding 
22710288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
22810288eeaSThierry Reding {
22910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
23010288eeaSThierry Reding 	return dfixed_frac(inf);
23110288eeaSThierry Reding }
23210288eeaSThierry Reding 
2334aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
23410288eeaSThierry Reding 				  const struct tegra_dc_window *window)
23510288eeaSThierry Reding {
23610288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
23793396d0fSSean Paul 	unsigned long value, flags;
23810288eeaSThierry Reding 	bool yuv, planar;
23910288eeaSThierry Reding 
24010288eeaSThierry Reding 	/*
24110288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
24210288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
24310288eeaSThierry Reding 	 */
24410288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
24510288eeaSThierry Reding 	if (!yuv)
24610288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
24710288eeaSThierry Reding 	else
24810288eeaSThierry Reding 		bpp = planar ? 1 : 2;
24910288eeaSThierry Reding 
25093396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
25193396d0fSSean Paul 
25210288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
25310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
25410288eeaSThierry Reding 
25510288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
25610288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
25710288eeaSThierry Reding 
25810288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
25910288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
26010288eeaSThierry Reding 
26110288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
26210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
26310288eeaSThierry Reding 
26410288eeaSThierry Reding 	h_offset = window->src.x * bpp;
26510288eeaSThierry Reding 	v_offset = window->src.y;
26610288eeaSThierry Reding 	h_size = window->src.w * bpp;
26710288eeaSThierry Reding 	v_size = window->src.h;
26810288eeaSThierry Reding 
26910288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
27010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
27110288eeaSThierry Reding 
27210288eeaSThierry Reding 	/*
27310288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
27410288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
27510288eeaSThierry Reding 	 */
27610288eeaSThierry Reding 	if (yuv && planar)
27710288eeaSThierry Reding 		bpp = 2;
27810288eeaSThierry Reding 
27910288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
28010288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
28110288eeaSThierry Reding 
28210288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
28310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
28410288eeaSThierry Reding 
28510288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
28610288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
28710288eeaSThierry Reding 
28810288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
28910288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
29010288eeaSThierry Reding 
29110288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
29210288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
29310288eeaSThierry Reding 
29410288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
29510288eeaSThierry Reding 
29610288eeaSThierry Reding 	if (yuv && planar) {
29710288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
29810288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
29910288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
30010288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
30110288eeaSThierry Reding 	} else {
30210288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
30310288eeaSThierry Reding 	}
30410288eeaSThierry Reding 
30510288eeaSThierry Reding 	if (window->bottom_up)
30610288eeaSThierry Reding 		v_offset += window->src.h - 1;
30710288eeaSThierry Reding 
30810288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
30910288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
31010288eeaSThierry Reding 
311c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
312c134f019SThierry Reding 		unsigned long height = window->tiling.value;
313c134f019SThierry Reding 
314c134f019SThierry Reding 		switch (window->tiling.mode) {
315c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
316c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
317c134f019SThierry Reding 			break;
318c134f019SThierry Reding 
319c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
320c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
321c134f019SThierry Reding 			break;
322c134f019SThierry Reding 
323c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
324c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
325c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
326c134f019SThierry Reding 			break;
327c134f019SThierry Reding 		}
328c134f019SThierry Reding 
329c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
33010288eeaSThierry Reding 	} else {
331c134f019SThierry Reding 		switch (window->tiling.mode) {
332c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
33310288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
33410288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
335c134f019SThierry Reding 			break;
336c134f019SThierry Reding 
337c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
338c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
339c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
340c134f019SThierry Reding 			break;
341c134f019SThierry Reding 
342c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3434aa3df71SThierry Reding 			/*
3444aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3454aa3df71SThierry Reding 			 * will already have filtered it out.
3464aa3df71SThierry Reding 			 */
3474aa3df71SThierry Reding 			break;
34810288eeaSThierry Reding 		}
34910288eeaSThierry Reding 
35010288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
351c134f019SThierry Reding 	}
35210288eeaSThierry Reding 
35310288eeaSThierry Reding 	value = WIN_ENABLE;
35410288eeaSThierry Reding 
35510288eeaSThierry Reding 	if (yuv) {
35610288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
35710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
35810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
35910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
36010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
36110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
36210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
36310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
36410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
36510288eeaSThierry Reding 
36610288eeaSThierry Reding 		value |= CSC_ENABLE;
36710288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
36810288eeaSThierry Reding 		value |= COLOR_EXPAND;
36910288eeaSThierry Reding 	}
37010288eeaSThierry Reding 
37110288eeaSThierry Reding 	if (window->bottom_up)
37210288eeaSThierry Reding 		value |= V_DIRECTION;
37310288eeaSThierry Reding 
37410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
37510288eeaSThierry Reding 
37610288eeaSThierry Reding 	/*
37710288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
37810288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
37910288eeaSThierry Reding 	 */
38010288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
38110288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
38210288eeaSThierry Reding 
38310288eeaSThierry Reding 	switch (index) {
38410288eeaSThierry Reding 	case 0:
38510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
38610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
38710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
38810288eeaSThierry Reding 		break;
38910288eeaSThierry Reding 
39010288eeaSThierry Reding 	case 1:
39110288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
39210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
39310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
39410288eeaSThierry Reding 		break;
39510288eeaSThierry Reding 
39610288eeaSThierry Reding 	case 2:
39710288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
39810288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
39910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
40010288eeaSThierry Reding 		break;
40110288eeaSThierry Reding 	}
40210288eeaSThierry Reding 
40393396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
404c7679306SThierry Reding }
405c7679306SThierry Reding 
406c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
407c7679306SThierry Reding {
408c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
409c7679306SThierry Reding 
410c7679306SThierry Reding 	drm_plane_cleanup(plane);
411c7679306SThierry Reding 	kfree(p);
412c7679306SThierry Reding }
413c7679306SThierry Reding 
414c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
415c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
416c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
417c7679306SThierry Reding 	DRM_FORMAT_RGB565,
418c7679306SThierry Reding };
419c7679306SThierry Reding 
4204aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
421c7679306SThierry Reding {
4224aa3df71SThierry Reding 	tegra_plane_destroy(plane);
4234aa3df71SThierry Reding }
4244aa3df71SThierry Reding 
4258f604f8cSThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
4268f604f8cSThierry Reding {
4278f604f8cSThierry Reding 	struct tegra_plane_state *state;
4288f604f8cSThierry Reding 
4293b59b7acSThierry Reding 	if (plane->state)
4302f701695SDaniel Vetter 		__drm_atomic_helper_plane_destroy_state(plane->state);
4318f604f8cSThierry Reding 
4328f604f8cSThierry Reding 	kfree(plane->state);
4338f604f8cSThierry Reding 	plane->state = NULL;
4348f604f8cSThierry Reding 
4358f604f8cSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
4368f604f8cSThierry Reding 	if (state) {
4378f604f8cSThierry Reding 		plane->state = &state->base;
4388f604f8cSThierry Reding 		plane->state->plane = plane;
4398f604f8cSThierry Reding 	}
4408f604f8cSThierry Reding }
4418f604f8cSThierry Reding 
4428f604f8cSThierry Reding static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
4438f604f8cSThierry Reding {
4448f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
4458f604f8cSThierry Reding 	struct tegra_plane_state *copy;
4468f604f8cSThierry Reding 
4473b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
4488f604f8cSThierry Reding 	if (!copy)
4498f604f8cSThierry Reding 		return NULL;
4508f604f8cSThierry Reding 
4513b59b7acSThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
4523b59b7acSThierry Reding 	copy->tiling = state->tiling;
4533b59b7acSThierry Reding 	copy->format = state->format;
4543b59b7acSThierry Reding 	copy->swap = state->swap;
4558f604f8cSThierry Reding 
4568f604f8cSThierry Reding 	return &copy->base;
4578f604f8cSThierry Reding }
4588f604f8cSThierry Reding 
4598f604f8cSThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
4608f604f8cSThierry Reding 					     struct drm_plane_state *state)
4618f604f8cSThierry Reding {
4622f701695SDaniel Vetter 	__drm_atomic_helper_plane_destroy_state(state);
4638f604f8cSThierry Reding 	kfree(state);
4648f604f8cSThierry Reding }
4658f604f8cSThierry Reding 
4664aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
46707866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
46807866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
4694aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
4708f604f8cSThierry Reding 	.reset = tegra_plane_reset,
4718f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
4728f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
4734aa3df71SThierry Reding };
4744aa3df71SThierry Reding 
47547802b09SThierry Reding static int tegra_plane_state_add(struct tegra_plane *plane,
47647802b09SThierry Reding 				 struct drm_plane_state *state)
47747802b09SThierry Reding {
47847802b09SThierry Reding 	struct drm_crtc_state *crtc_state;
47947802b09SThierry Reding 	struct tegra_dc_state *tegra;
4807d205857SDmitry Osipenko 	struct drm_rect clip;
4817d205857SDmitry Osipenko 	int err;
48247802b09SThierry Reding 
48347802b09SThierry Reding 	/* Propagate errors from allocation or locking failures. */
48447802b09SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
48547802b09SThierry Reding 	if (IS_ERR(crtc_state))
48647802b09SThierry Reding 		return PTR_ERR(crtc_state);
48747802b09SThierry Reding 
4887d205857SDmitry Osipenko 	clip.x1 = 0;
4897d205857SDmitry Osipenko 	clip.y1 = 0;
4907d205857SDmitry Osipenko 	clip.x2 = crtc_state->mode.hdisplay;
4917d205857SDmitry Osipenko 	clip.y2 = crtc_state->mode.vdisplay;
4927d205857SDmitry Osipenko 
4937d205857SDmitry Osipenko 	/* Check plane state for visibility and calculate clipping bounds */
494a01cb8baSVille Syrjälä 	err = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
49510b47ee0SVille Syrjälä 						  0, INT_MAX, true, true);
4967d205857SDmitry Osipenko 	if (err < 0)
4977d205857SDmitry Osipenko 		return err;
4987d205857SDmitry Osipenko 
49947802b09SThierry Reding 	tegra = to_dc_state(crtc_state);
50047802b09SThierry Reding 
50147802b09SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
50247802b09SThierry Reding 
50347802b09SThierry Reding 	return 0;
50447802b09SThierry Reding }
50547802b09SThierry Reding 
5064aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
5074aa3df71SThierry Reding 				    struct drm_plane_state *state)
5084aa3df71SThierry Reding {
5098f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
5108f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
51147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
5124aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
513c7679306SThierry Reding 	int err;
514c7679306SThierry Reding 
5154aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
5164aa3df71SThierry Reding 	if (!state->crtc)
5174aa3df71SThierry Reding 		return 0;
5184aa3df71SThierry Reding 
519438b74a5SVille Syrjälä 	err = tegra_dc_format(state->fb->format->format, &plane_state->format,
5208f604f8cSThierry Reding 			      &plane_state->swap);
5214aa3df71SThierry Reding 	if (err < 0)
5224aa3df71SThierry Reding 		return err;
5234aa3df71SThierry Reding 
5248f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
5258f604f8cSThierry Reding 	if (err < 0)
5268f604f8cSThierry Reding 		return err;
5278f604f8cSThierry Reding 
5288f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
5294aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
5304aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
5314aa3df71SThierry Reding 		return -EINVAL;
5324aa3df71SThierry Reding 	}
5334aa3df71SThierry Reding 
5344aa3df71SThierry Reding 	/*
5354aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
5364aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
5374aa3df71SThierry Reding 	 * configuration.
5384aa3df71SThierry Reding 	 */
539bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
5404aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
5414aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
5424aa3df71SThierry Reding 			return -EINVAL;
5434aa3df71SThierry Reding 		}
5444aa3df71SThierry Reding 	}
5454aa3df71SThierry Reding 
54647802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
54747802b09SThierry Reding 	if (err < 0)
54847802b09SThierry Reding 		return err;
54947802b09SThierry Reding 
5504aa3df71SThierry Reding 	return 0;
5514aa3df71SThierry Reding }
5524aa3df71SThierry Reding 
553a4bfa096SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
554a4bfa096SThierry Reding 				       struct drm_plane_state *old_state)
55580d3eef1SDmitry Osipenko {
556a4bfa096SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
557a4bfa096SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
55880d3eef1SDmitry Osipenko 	unsigned long flags;
55980d3eef1SDmitry Osipenko 	u32 value;
56080d3eef1SDmitry Osipenko 
561a4bfa096SThierry Reding 	/* rien ne va plus */
562a4bfa096SThierry Reding 	if (!old_state || !old_state->crtc)
563a4bfa096SThierry Reding 		return;
564a4bfa096SThierry Reding 
56580d3eef1SDmitry Osipenko 	spin_lock_irqsave(&dc->lock, flags);
56680d3eef1SDmitry Osipenko 
567a4bfa096SThierry Reding 	value = WINDOW_A_SELECT << p->index;
56880d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
56980d3eef1SDmitry Osipenko 
57080d3eef1SDmitry Osipenko 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
57180d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
57280d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
57380d3eef1SDmitry Osipenko 
57480d3eef1SDmitry Osipenko 	spin_unlock_irqrestore(&dc->lock, flags);
57580d3eef1SDmitry Osipenko }
57680d3eef1SDmitry Osipenko 
5774aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
5784aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
5794aa3df71SThierry Reding {
5808f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
5814aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5824aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
5834aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
5844aa3df71SThierry Reding 	struct tegra_dc_window window;
5854aa3df71SThierry Reding 	unsigned int i;
5864aa3df71SThierry Reding 
5874aa3df71SThierry Reding 	/* rien ne va plus */
5884aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5894aa3df71SThierry Reding 		return;
5904aa3df71SThierry Reding 
59180d3eef1SDmitry Osipenko 	if (!plane->state->visible)
592a4bfa096SThierry Reding 		return tegra_plane_atomic_disable(plane, old_state);
59380d3eef1SDmitry Osipenko 
594c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
5957d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
5967d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
5977d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
5987d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
5997d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
6007d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
6017d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
6027d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
603272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
604c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
605c7679306SThierry Reding 
6068f604f8cSThierry Reding 	/* copy from state */
6078f604f8cSThierry Reding 	window.tiling = state->tiling;
6088f604f8cSThierry Reding 	window.format = state->format;
6098f604f8cSThierry Reding 	window.swap = state->swap;
610c7679306SThierry Reding 
611bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
6124aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
613c7679306SThierry Reding 
6144aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
61508ee0178SDmitry Osipenko 
61608ee0178SDmitry Osipenko 		/*
61708ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
61808ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
61908ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
62008ee0178SDmitry Osipenko 		 */
62108ee0178SDmitry Osipenko 		if (i < 2)
6224aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
623c7679306SThierry Reding 	}
624c7679306SThierry Reding 
6254aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
6264aa3df71SThierry Reding }
6274aa3df71SThierry Reding 
628a4bfa096SThierry Reding static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
6294aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
6304aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
631a4bfa096SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
632c7679306SThierry Reding };
633c7679306SThierry Reding 
634c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
635c7679306SThierry Reding 						       struct tegra_dc *dc)
636c7679306SThierry Reding {
637518e6227SThierry Reding 	/*
638518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
639518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
640518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
641518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
642518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
643518e6227SThierry Reding 	 * here.
644518e6227SThierry Reding 	 *
645518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
646518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
647518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
648518e6227SThierry Reding 	 */
649518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
650c7679306SThierry Reding 	struct tegra_plane *plane;
651c7679306SThierry Reding 	unsigned int num_formats;
652c7679306SThierry Reding 	const u32 *formats;
653c7679306SThierry Reding 	int err;
654c7679306SThierry Reding 
655c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
656c7679306SThierry Reding 	if (!plane)
657c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
658c7679306SThierry Reding 
659c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
660c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
661c7679306SThierry Reding 
662518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
663c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
664e6fc3b68SBen Widawsky 				       num_formats, NULL,
665e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_PRIMARY, NULL);
666c7679306SThierry Reding 	if (err < 0) {
667c7679306SThierry Reding 		kfree(plane);
668c7679306SThierry Reding 		return ERR_PTR(err);
669c7679306SThierry Reding 	}
670c7679306SThierry Reding 
671a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
6724aa3df71SThierry Reding 
673c7679306SThierry Reding 	return &plane->base;
674c7679306SThierry Reding }
675c7679306SThierry Reding 
676c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
677c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
678c7679306SThierry Reding };
679c7679306SThierry Reding 
6804aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
6814aa3df71SThierry Reding 				     struct drm_plane_state *state)
682c7679306SThierry Reding {
68347802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
68447802b09SThierry Reding 	int err;
68547802b09SThierry Reding 
6864aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6874aa3df71SThierry Reding 	if (!state->crtc)
6884aa3df71SThierry Reding 		return 0;
689c7679306SThierry Reding 
690c7679306SThierry Reding 	/* scaling not supported for cursor */
6914aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
6924aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
693c7679306SThierry Reding 		return -EINVAL;
694c7679306SThierry Reding 
695c7679306SThierry Reding 	/* only square cursors supported */
6964aa3df71SThierry Reding 	if (state->src_w != state->src_h)
697c7679306SThierry Reding 		return -EINVAL;
698c7679306SThierry Reding 
6994aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
7004aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
7014aa3df71SThierry Reding 		return -EINVAL;
7024aa3df71SThierry Reding 
70347802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
70447802b09SThierry Reding 	if (err < 0)
70547802b09SThierry Reding 		return err;
70647802b09SThierry Reding 
7074aa3df71SThierry Reding 	return 0;
7084aa3df71SThierry Reding }
7094aa3df71SThierry Reding 
7104aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
7114aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
7124aa3df71SThierry Reding {
7134aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
7144aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
7154aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
7164aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
7174aa3df71SThierry Reding 
7184aa3df71SThierry Reding 	/* rien ne va plus */
7194aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
7204aa3df71SThierry Reding 		return;
7214aa3df71SThierry Reding 
7224aa3df71SThierry Reding 	switch (state->crtc_w) {
723c7679306SThierry Reding 	case 32:
724c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
725c7679306SThierry Reding 		break;
726c7679306SThierry Reding 
727c7679306SThierry Reding 	case 64:
728c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
729c7679306SThierry Reding 		break;
730c7679306SThierry Reding 
731c7679306SThierry Reding 	case 128:
732c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
733c7679306SThierry Reding 		break;
734c7679306SThierry Reding 
735c7679306SThierry Reding 	case 256:
736c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
737c7679306SThierry Reding 		break;
738c7679306SThierry Reding 
739c7679306SThierry Reding 	default:
7404aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
7414aa3df71SThierry Reding 		     state->crtc_h);
7424aa3df71SThierry Reding 		return;
743c7679306SThierry Reding 	}
744c7679306SThierry Reding 
745c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
746c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
747c7679306SThierry Reding 
748c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
749c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
750c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
751c7679306SThierry Reding #endif
752c7679306SThierry Reding 
753c7679306SThierry Reding 	/* enable cursor and set blend mode */
754c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
755c7679306SThierry Reding 	value |= CURSOR_ENABLE;
756c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
757c7679306SThierry Reding 
758c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
759c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
760c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
761c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
762c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
763c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
764c7679306SThierry Reding 	value |= CURSOR_ALPHA;
765c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
766c7679306SThierry Reding 
767c7679306SThierry Reding 	/* position the cursor */
7684aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
769c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
770c7679306SThierry Reding }
771c7679306SThierry Reding 
7724aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
7734aa3df71SThierry Reding 					struct drm_plane_state *old_state)
774c7679306SThierry Reding {
7754aa3df71SThierry Reding 	struct tegra_dc *dc;
776c7679306SThierry Reding 	u32 value;
777c7679306SThierry Reding 
7784aa3df71SThierry Reding 	/* rien ne va plus */
7794aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
7804aa3df71SThierry Reding 		return;
7814aa3df71SThierry Reding 
7824aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
783c7679306SThierry Reding 
784c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
785c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
786c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
787c7679306SThierry Reding }
788c7679306SThierry Reding 
789c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
79007866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
79107866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
792c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
7938f604f8cSThierry Reding 	.reset = tegra_plane_reset,
7948f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
7958f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
7964aa3df71SThierry Reding };
7974aa3df71SThierry Reding 
7984aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
7994aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
8004aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
8014aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
802c7679306SThierry Reding };
803c7679306SThierry Reding 
804c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
805c7679306SThierry Reding 						      struct tegra_dc *dc)
806c7679306SThierry Reding {
807c7679306SThierry Reding 	struct tegra_plane *plane;
808c7679306SThierry Reding 	unsigned int num_formats;
809c7679306SThierry Reding 	const u32 *formats;
810c7679306SThierry Reding 	int err;
811c7679306SThierry Reding 
812c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
813c7679306SThierry Reding 	if (!plane)
814c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
815c7679306SThierry Reding 
81647802b09SThierry Reding 	/*
817a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
818a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
819a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
820a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
821a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
82247802b09SThierry Reding 	 */
82347802b09SThierry Reding 	plane->index = 6;
82447802b09SThierry Reding 
825c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
826c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
827c7679306SThierry Reding 
828c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
829c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
830e6fc3b68SBen Widawsky 				       num_formats, NULL,
831e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
832c7679306SThierry Reding 	if (err < 0) {
833c7679306SThierry Reding 		kfree(plane);
834c7679306SThierry Reding 		return ERR_PTR(err);
835c7679306SThierry Reding 	}
836c7679306SThierry Reding 
8374aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
8384aa3df71SThierry Reding 
839c7679306SThierry Reding 	return &plane->base;
840c7679306SThierry Reding }
841c7679306SThierry Reding 
842c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
843dee8268fSThierry Reding {
844c7679306SThierry Reding 	tegra_plane_destroy(plane);
845dee8268fSThierry Reding }
846dee8268fSThierry Reding 
847c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
84807866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
84907866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
850c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
8518f604f8cSThierry Reding 	.reset = tegra_plane_reset,
8528f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
8538f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
854dee8268fSThierry Reding };
855dee8268fSThierry Reding 
856c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
857dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
858dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
859dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
860dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
861f925390eSThierry Reding 	DRM_FORMAT_YUYV,
862dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
863dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
864dee8268fSThierry Reding };
865dee8268fSThierry Reding 
866c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
867c7679306SThierry Reding 						       struct tegra_dc *dc,
868c7679306SThierry Reding 						       unsigned int index)
869dee8268fSThierry Reding {
870dee8268fSThierry Reding 	struct tegra_plane *plane;
871c7679306SThierry Reding 	unsigned int num_formats;
872c7679306SThierry Reding 	const u32 *formats;
873c7679306SThierry Reding 	int err;
874dee8268fSThierry Reding 
875f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
876dee8268fSThierry Reding 	if (!plane)
877c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
878dee8268fSThierry Reding 
879c7679306SThierry Reding 	plane->index = index;
880dee8268fSThierry Reding 
881c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
882c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
883c7679306SThierry Reding 
884c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
885c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
886e6fc3b68SBen Widawsky 				       num_formats, NULL,
887e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_OVERLAY, NULL);
888f002abc1SThierry Reding 	if (err < 0) {
889f002abc1SThierry Reding 		kfree(plane);
890c7679306SThierry Reding 		return ERR_PTR(err);
891dee8268fSThierry Reding 	}
892c7679306SThierry Reding 
893a4bfa096SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
8944aa3df71SThierry Reding 
895c7679306SThierry Reding 	return &plane->base;
896c7679306SThierry Reding }
897c7679306SThierry Reding 
898c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
899c7679306SThierry Reding {
900c7679306SThierry Reding 	struct drm_plane *plane;
901c7679306SThierry Reding 	unsigned int i;
902c7679306SThierry Reding 
903c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
904c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
905c7679306SThierry Reding 		if (IS_ERR(plane))
906c7679306SThierry Reding 			return PTR_ERR(plane);
907f002abc1SThierry Reding 	}
908dee8268fSThierry Reding 
909dee8268fSThierry Reding 	return 0;
910dee8268fSThierry Reding }
911dee8268fSThierry Reding 
912f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
913f002abc1SThierry Reding {
914f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
915f002abc1SThierry Reding }
916f002abc1SThierry Reding 
917ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
918ca915b10SThierry Reding {
919ca915b10SThierry Reding 	struct tegra_dc_state *state;
920ca915b10SThierry Reding 
9213b59b7acSThierry Reding 	if (crtc->state)
922ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
9233b59b7acSThierry Reding 
924ca915b10SThierry Reding 	kfree(crtc->state);
925ca915b10SThierry Reding 	crtc->state = NULL;
926ca915b10SThierry Reding 
927ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
928332bbe70SThierry Reding 	if (state) {
929ca915b10SThierry Reding 		crtc->state = &state->base;
930332bbe70SThierry Reding 		crtc->state->crtc = crtc;
931332bbe70SThierry Reding 	}
93231930d4dSThierry Reding 
93331930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
934ca915b10SThierry Reding }
935ca915b10SThierry Reding 
936ca915b10SThierry Reding static struct drm_crtc_state *
937ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
938ca915b10SThierry Reding {
939ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
940ca915b10SThierry Reding 	struct tegra_dc_state *copy;
941ca915b10SThierry Reding 
9423b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
943ca915b10SThierry Reding 	if (!copy)
944ca915b10SThierry Reding 		return NULL;
945ca915b10SThierry Reding 
9463b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
9473b59b7acSThierry Reding 	copy->clk = state->clk;
9483b59b7acSThierry Reding 	copy->pclk = state->pclk;
9493b59b7acSThierry Reding 	copy->div = state->div;
9503b59b7acSThierry Reding 	copy->planes = state->planes;
951ca915b10SThierry Reding 
952ca915b10SThierry Reding 	return &copy->base;
953ca915b10SThierry Reding }
954ca915b10SThierry Reding 
955ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
956ca915b10SThierry Reding 					    struct drm_crtc_state *state)
957ca915b10SThierry Reding {
958ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
959ca915b10SThierry Reding 	kfree(state);
960ca915b10SThierry Reding }
961ca915b10SThierry Reding 
962*c49c81e2SThierry Reding static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
963*c49c81e2SThierry Reding {
964*c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
965*c49c81e2SThierry Reding 
966*c49c81e2SThierry Reding 	if (dc->syncpt)
967*c49c81e2SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
968*c49c81e2SThierry Reding 
969*c49c81e2SThierry Reding 	/* fallback to software emulated VBLANK counter */
970*c49c81e2SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
971*c49c81e2SThierry Reding }
972*c49c81e2SThierry Reding 
973*c49c81e2SThierry Reding static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
974*c49c81e2SThierry Reding {
975*c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
976*c49c81e2SThierry Reding 	unsigned long value, flags;
977*c49c81e2SThierry Reding 
978*c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
979*c49c81e2SThierry Reding 
980*c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
981*c49c81e2SThierry Reding 	value |= VBLANK_INT;
982*c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
983*c49c81e2SThierry Reding 
984*c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
985*c49c81e2SThierry Reding 
986*c49c81e2SThierry Reding 	return 0;
987*c49c81e2SThierry Reding }
988*c49c81e2SThierry Reding 
989*c49c81e2SThierry Reding static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
990*c49c81e2SThierry Reding {
991*c49c81e2SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
992*c49c81e2SThierry Reding 	unsigned long value, flags;
993*c49c81e2SThierry Reding 
994*c49c81e2SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
995*c49c81e2SThierry Reding 
996*c49c81e2SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
997*c49c81e2SThierry Reding 	value &= ~VBLANK_INT;
998*c49c81e2SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
999*c49c81e2SThierry Reding 
1000*c49c81e2SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
1001*c49c81e2SThierry Reding }
1002*c49c81e2SThierry Reding 
1003dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
10041503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
100574f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1006f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1007ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1008ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1009ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
101010437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
101110437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
101210437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1013dee8268fSThierry Reding };
1014dee8268fSThierry Reding 
1015dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1016dee8268fSThierry Reding 				struct drm_display_mode *mode)
1017dee8268fSThierry Reding {
10180444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10190444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1020dee8268fSThierry Reding 	unsigned long value;
1021dee8268fSThierry Reding 
1022dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1023dee8268fSThierry Reding 
1024dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1025dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1026dee8268fSThierry Reding 
1027dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1028dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1029dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1030dee8268fSThierry Reding 
1031dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1032dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1033dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1034dee8268fSThierry Reding 
1035dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1036dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1037dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1038dee8268fSThierry Reding 
1039dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1040dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1041dee8268fSThierry Reding 
1042dee8268fSThierry Reding 	return 0;
1043dee8268fSThierry Reding }
1044dee8268fSThierry Reding 
10459d910b60SThierry Reding /**
10469d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
10479d910b60SThierry Reding  *     state
10489d910b60SThierry Reding  * @dc: display controller
10499d910b60SThierry Reding  * @crtc_state: CRTC atomic state
10509d910b60SThierry Reding  * @clk: parent clock for display controller
10519d910b60SThierry Reding  * @pclk: pixel clock
10529d910b60SThierry Reding  * @div: shift clock divider
10539d910b60SThierry Reding  *
10549d910b60SThierry Reding  * Returns:
10559d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
10569d910b60SThierry Reding  */
1057ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1058ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1059ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1060ca915b10SThierry Reding 			       unsigned int div)
1061ca915b10SThierry Reding {
1062ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1063ca915b10SThierry Reding 
1064d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1065d2982748SThierry Reding 		return -EINVAL;
1066d2982748SThierry Reding 
1067ca915b10SThierry Reding 	state->clk = clk;
1068ca915b10SThierry Reding 	state->pclk = pclk;
1069ca915b10SThierry Reding 	state->div = div;
1070ca915b10SThierry Reding 
1071ca915b10SThierry Reding 	return 0;
1072ca915b10SThierry Reding }
1073ca915b10SThierry Reding 
107476d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
107576d59ed0SThierry Reding 				  struct tegra_dc_state *state)
107676d59ed0SThierry Reding {
107776d59ed0SThierry Reding 	u32 value;
107876d59ed0SThierry Reding 	int err;
107976d59ed0SThierry Reding 
108076d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
108176d59ed0SThierry Reding 	if (err < 0)
108276d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
108376d59ed0SThierry Reding 
108476d59ed0SThierry Reding 	/*
108576d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
108676d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
108776d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
108876d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
108976d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
109076d59ed0SThierry Reding 	 * should therefore be avoided.
109176d59ed0SThierry Reding 	 */
109276d59ed0SThierry Reding 	if (state->pclk > 0) {
109376d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
109476d59ed0SThierry Reding 		if (err < 0)
109576d59ed0SThierry Reding 			dev_err(dc->dev,
109676d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
109776d59ed0SThierry Reding 				state->pclk);
109876d59ed0SThierry Reding 	}
109976d59ed0SThierry Reding 
110076d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
110176d59ed0SThierry Reding 		      state->div);
110276d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
110376d59ed0SThierry Reding 
110476d59ed0SThierry Reding 	value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
110576d59ed0SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
110639e08affSThierry Reding 
110739e08affSThierry Reding 	err = clk_set_rate(dc->clk, state->pclk);
110839e08affSThierry Reding 	if (err < 0)
110939e08affSThierry Reding 		dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
111039e08affSThierry Reding 			dc->clk, state->pclk, err);
111176d59ed0SThierry Reding }
111276d59ed0SThierry Reding 
1113003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1114003fc848SThierry Reding {
1115003fc848SThierry Reding 	u32 value;
1116003fc848SThierry Reding 
1117003fc848SThierry Reding 	/* stop the display controller */
1118003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1119003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1120003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1121003fc848SThierry Reding 
1122003fc848SThierry Reding 	tegra_dc_commit(dc);
1123003fc848SThierry Reding }
1124003fc848SThierry Reding 
1125003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1126003fc848SThierry Reding {
1127003fc848SThierry Reding 	u32 value;
1128003fc848SThierry Reding 
1129003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1130003fc848SThierry Reding 
1131003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1132003fc848SThierry Reding }
1133003fc848SThierry Reding 
1134003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1135003fc848SThierry Reding {
1136003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1137003fc848SThierry Reding 
1138003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1139003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1140003fc848SThierry Reding 			return 0;
1141003fc848SThierry Reding 
1142003fc848SThierry Reding 		usleep_range(1000, 2000);
1143003fc848SThierry Reding 	}
1144003fc848SThierry Reding 
1145003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1146003fc848SThierry Reding 	return -ETIMEDOUT;
1147003fc848SThierry Reding }
1148003fc848SThierry Reding 
114964581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
115064581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1151003fc848SThierry Reding {
1152003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1153003fc848SThierry Reding 	u32 value;
1154003fc848SThierry Reding 
1155003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1156003fc848SThierry Reding 		tegra_dc_stop(dc);
1157003fc848SThierry Reding 
1158003fc848SThierry Reding 		/*
1159003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1160003fc848SThierry Reding 		 * in case this fails.
1161003fc848SThierry Reding 		 */
1162003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1163003fc848SThierry Reding 	}
1164003fc848SThierry Reding 
1165003fc848SThierry Reding 	/*
1166003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1167003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1168003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1169003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1170003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1171003fc848SThierry Reding 	 * to go idle.
1172003fc848SThierry Reding 	 *
1173003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1174003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1175003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1176003fc848SThierry Reding 	 *
1177003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1178003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1179003fc848SThierry Reding 	 * the RGB encoder?
1180003fc848SThierry Reding 	 */
1181003fc848SThierry Reding 	if (dc->rgb) {
1182003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1183003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1184003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1185003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1186003fc848SThierry Reding 	}
1187003fc848SThierry Reding 
1188003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1189003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
119033a8eb8dSThierry Reding 
119133a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1192003fc848SThierry Reding }
1193003fc848SThierry Reding 
11940b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
11950b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1196dee8268fSThierry Reding {
11974aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
119876d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1199dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1200dbb3f2f7SThierry Reding 	u32 value;
1201dee8268fSThierry Reding 
120233a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
120333a8eb8dSThierry Reding 
120433a8eb8dSThierry Reding 	/* initialize display controller */
120533a8eb8dSThierry Reding 	if (dc->syncpt) {
120633a8eb8dSThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt);
120733a8eb8dSThierry Reding 
120833a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
120933a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
121033a8eb8dSThierry Reding 
121133a8eb8dSThierry Reding 		value = SYNCPT_VSYNC_ENABLE | syncpt;
121233a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
121333a8eb8dSThierry Reding 	}
121433a8eb8dSThierry Reding 
121533a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
121633a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
121733a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
121833a8eb8dSThierry Reding 
121933a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
122033a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
122133a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
122233a8eb8dSThierry Reding 
122333a8eb8dSThierry Reding 	/* initialize timer */
122433a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
122533a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
122633a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
122733a8eb8dSThierry Reding 
122833a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
122933a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
123033a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
123133a8eb8dSThierry Reding 
123233a8eb8dSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
123333a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
123433a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
123533a8eb8dSThierry Reding 
123633a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
123733a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
123833a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
123933a8eb8dSThierry Reding 
124033a8eb8dSThierry Reding 	if (dc->soc->supports_border_color)
124133a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
124233a8eb8dSThierry Reding 
124333a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
124476d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
124576d59ed0SThierry Reding 
1246dee8268fSThierry Reding 	/* program display mode */
1247dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1248dee8268fSThierry Reding 
12498620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
12508620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
12518620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
12528620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
12538620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
12548620fc62SThierry Reding 	}
1255666cb873SThierry Reding 
1256666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1257666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1258666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1259666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1260666cb873SThierry Reding 
1261666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1262666cb873SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1263666cb873SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1264666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1265666cb873SThierry Reding 
1266666cb873SThierry Reding 	tegra_dc_commit(dc);
1267dee8268fSThierry Reding 
12688ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1269dee8268fSThierry Reding }
1270dee8268fSThierry Reding 
12714aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
12724aa3df71SThierry Reding 				   struct drm_crtc_state *state)
12734aa3df71SThierry Reding {
12744aa3df71SThierry Reding 	return 0;
12754aa3df71SThierry Reding }
12764aa3df71SThierry Reding 
1277613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1278613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
12794aa3df71SThierry Reding {
12801503ca47SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
12811503ca47SThierry Reding 
12821503ca47SThierry Reding 	if (crtc->state->event) {
12831503ca47SThierry Reding 		crtc->state->event->pipe = drm_crtc_index(crtc);
12841503ca47SThierry Reding 
12851503ca47SThierry Reding 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
12861503ca47SThierry Reding 
12871503ca47SThierry Reding 		dc->event = crtc->state->event;
12881503ca47SThierry Reding 		crtc->state->event = NULL;
12891503ca47SThierry Reding 	}
12904aa3df71SThierry Reding }
12914aa3df71SThierry Reding 
1292613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1293613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
12944aa3df71SThierry Reding {
129547802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
129647802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
129747802b09SThierry Reding 
129847802b09SThierry Reding 	tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
129947802b09SThierry Reding 	tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
13004aa3df71SThierry Reding }
13014aa3df71SThierry Reding 
1302dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
13034aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
13044aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
13054aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
13060b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
130764581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1308dee8268fSThierry Reding };
1309dee8268fSThierry Reding 
1310*c49c81e2SThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
1311*c49c81e2SThierry Reding {
1312*c49c81e2SThierry Reding 	struct drm_device *drm = dc->base.dev;
1313*c49c81e2SThierry Reding 	struct drm_crtc *crtc = &dc->base;
1314*c49c81e2SThierry Reding 	unsigned long flags, base;
1315*c49c81e2SThierry Reding 	struct tegra_bo *bo;
1316*c49c81e2SThierry Reding 
1317*c49c81e2SThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
1318*c49c81e2SThierry Reding 
1319*c49c81e2SThierry Reding 	if (!dc->event) {
1320*c49c81e2SThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
1321*c49c81e2SThierry Reding 		return;
1322*c49c81e2SThierry Reding 	}
1323*c49c81e2SThierry Reding 
1324*c49c81e2SThierry Reding 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
1325*c49c81e2SThierry Reding 
1326*c49c81e2SThierry Reding 	spin_lock(&dc->lock);
1327*c49c81e2SThierry Reding 
1328*c49c81e2SThierry Reding 	/* check if new start address has been latched */
1329*c49c81e2SThierry Reding 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
1330*c49c81e2SThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
1331*c49c81e2SThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
1332*c49c81e2SThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
1333*c49c81e2SThierry Reding 
1334*c49c81e2SThierry Reding 	spin_unlock(&dc->lock);
1335*c49c81e2SThierry Reding 
1336*c49c81e2SThierry Reding 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
1337*c49c81e2SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
1338*c49c81e2SThierry Reding 		drm_crtc_vblank_put(crtc);
1339*c49c81e2SThierry Reding 		dc->event = NULL;
1340*c49c81e2SThierry Reding 	}
1341*c49c81e2SThierry Reding 
1342*c49c81e2SThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
1343*c49c81e2SThierry Reding }
1344*c49c81e2SThierry Reding 
1345dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1346dee8268fSThierry Reding {
1347dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1348dee8268fSThierry Reding 	unsigned long status;
1349dee8268fSThierry Reding 
1350dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1351dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1352dee8268fSThierry Reding 
1353dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1354dee8268fSThierry Reding 		/*
1355dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1356dee8268fSThierry Reding 		*/
1357791ddb1eSThierry Reding 		dc->stats.frames++;
1358dee8268fSThierry Reding 	}
1359dee8268fSThierry Reding 
1360dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1361dee8268fSThierry Reding 		/*
1362dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1363dee8268fSThierry Reding 		*/
1364ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1365dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1366791ddb1eSThierry Reding 		dc->stats.vblank++;
1367dee8268fSThierry Reding 	}
1368dee8268fSThierry Reding 
1369dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1370dee8268fSThierry Reding 		/*
1371dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1372dee8268fSThierry Reding 		*/
1373791ddb1eSThierry Reding 		dc->stats.underflow++;
1374791ddb1eSThierry Reding 	}
1375791ddb1eSThierry Reding 
1376791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1377791ddb1eSThierry Reding 		/*
1378791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1379791ddb1eSThierry Reding 		*/
1380791ddb1eSThierry Reding 		dc->stats.overflow++;
1381dee8268fSThierry Reding 	}
1382dee8268fSThierry Reding 
1383dee8268fSThierry Reding 	return IRQ_HANDLED;
1384dee8268fSThierry Reding }
1385dee8268fSThierry Reding 
1386cf6824acSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1387cf6824acSThierry Reding 
1388cf6824acSThierry Reding static const struct debugfs_reg32 tegra_dc_regs[] = {
1389cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1390cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1391cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1392cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1393cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1394cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1395cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1396cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1397cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1398cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1399cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1400cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1401cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1402cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1403cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1404cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1405cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1406cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_STATUS),
1407cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_MASK),
1408cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1409cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_TYPE),
1410cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1411cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1412cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1413cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1414cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1415cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1416cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1417cf6824acSThierry Reding 	DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1418cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1419cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1420cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1421cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1422cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1423cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1424cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1425cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1426cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1427cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1428cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1429cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1430cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1431cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1432cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1433cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1434cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1435cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1436cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1437cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1438cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1439cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1440cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1441cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1442cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1443cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1444cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1445cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1446cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1447cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1448cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1449cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1450cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1451cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1452cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1453cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1454cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1455cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1456cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1457cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1458cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1459cf6824acSThierry Reding 	DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1460cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1461cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1462cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1463cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1464cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1465cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1466cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1467cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1468cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1469cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_ACTIVE),
1470cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1471cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1472cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1473cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1474cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1475cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1476cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1477cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1478cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1479cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1480cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1481cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1482cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1483cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1484cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1485cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1486cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1487cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1488cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1489cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1490cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1491cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1492cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1493cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1494cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1495cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1496cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1497cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1498cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1499cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1500cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1501cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1502cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1503cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1504cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1505cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1506cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1507cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1508cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1509cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1510cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1511cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1512cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1513cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1514cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1515cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1516cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1517cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1518cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1519cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1520cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1521cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1522cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1523cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1524cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1525cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1526cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1527cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1528cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1529cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1530cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1531cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1532cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1533cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1534cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1535cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1536cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1537cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1538cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1539cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1540cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1541cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1542cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1543cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1544cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1545cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1546cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1547cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1548cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1549cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1550cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1551cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1552cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1553cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1554cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1555cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1556cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1557cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1558cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1559cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1560cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1561cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1562cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1563cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1564cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1565cf6824acSThierry Reding 	DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1566cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1567cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1568cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1569cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1570cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_POSITION),
1571cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_SIZE),
1572cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1573cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1574cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1575cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_DDA_INC),
1576cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1577cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1578cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1579cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1580cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1581cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1582cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1583cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1584cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1585cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1586cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1587cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1588cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1589cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1590cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1591cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1592cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1593cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1594cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1595cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1596cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1597cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1598cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1599cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1600cf6824acSThierry Reding 	DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1601cf6824acSThierry Reding };
1602cf6824acSThierry Reding 
1603dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1604dee8268fSThierry Reding {
1605dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1606dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1607cf6824acSThierry Reding 	unsigned int i;
1608003fc848SThierry Reding 	int err = 0;
1609003fc848SThierry Reding 
161099612b27SDaniel Vetter 	drm_modeset_lock(&dc->base.mutex, NULL);
1611003fc848SThierry Reding 
1612003fc848SThierry Reding 	if (!dc->base.state->active) {
1613003fc848SThierry Reding 		err = -EBUSY;
1614003fc848SThierry Reding 		goto unlock;
1615003fc848SThierry Reding 	}
1616dee8268fSThierry Reding 
1617cf6824acSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1618cf6824acSThierry Reding 		unsigned int offset = tegra_dc_regs[i].offset;
1619dee8268fSThierry Reding 
1620cf6824acSThierry Reding 		seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1621cf6824acSThierry Reding 			   offset, tegra_dc_readl(dc, offset));
1622cf6824acSThierry Reding 	}
1623dee8268fSThierry Reding 
1624003fc848SThierry Reding unlock:
162599612b27SDaniel Vetter 	drm_modeset_unlock(&dc->base.mutex);
1626003fc848SThierry Reding 	return err;
1627dee8268fSThierry Reding }
1628dee8268fSThierry Reding 
16296ca1f62fSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
16306ca1f62fSThierry Reding {
16316ca1f62fSThierry Reding 	struct drm_info_node *node = s->private;
16326ca1f62fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1633003fc848SThierry Reding 	int err = 0;
16346ca1f62fSThierry Reding 	u32 value;
16356ca1f62fSThierry Reding 
163699612b27SDaniel Vetter 	drm_modeset_lock(&dc->base.mutex, NULL);
1637003fc848SThierry Reding 
1638003fc848SThierry Reding 	if (!dc->base.state->active) {
1639003fc848SThierry Reding 		err = -EBUSY;
1640003fc848SThierry Reding 		goto unlock;
1641003fc848SThierry Reding 	}
1642003fc848SThierry Reding 
16436ca1f62fSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
16446ca1f62fSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
16456ca1f62fSThierry Reding 	tegra_dc_commit(dc);
16466ca1f62fSThierry Reding 
16476ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16486ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16496ca1f62fSThierry Reding 
16506ca1f62fSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
16516ca1f62fSThierry Reding 	seq_printf(s, "%08x\n", value);
16526ca1f62fSThierry Reding 
16536ca1f62fSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
16546ca1f62fSThierry Reding 
1655003fc848SThierry Reding unlock:
165699612b27SDaniel Vetter 	drm_modeset_unlock(&dc->base.mutex);
1657003fc848SThierry Reding 	return err;
16586ca1f62fSThierry Reding }
16596ca1f62fSThierry Reding 
1660791ddb1eSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1661791ddb1eSThierry Reding {
1662791ddb1eSThierry Reding 	struct drm_info_node *node = s->private;
1663791ddb1eSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1664791ddb1eSThierry Reding 
1665791ddb1eSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1666791ddb1eSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1667791ddb1eSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1668791ddb1eSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1669791ddb1eSThierry Reding 
1670dee8268fSThierry Reding 	return 0;
1671dee8268fSThierry Reding }
1672dee8268fSThierry Reding 
1673dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1674dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
16756ca1f62fSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1676791ddb1eSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1677dee8268fSThierry Reding };
1678dee8268fSThierry Reding 
1679dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1680dee8268fSThierry Reding {
1681dee8268fSThierry Reding 	unsigned int i;
1682dee8268fSThierry Reding 	char *name;
1683dee8268fSThierry Reding 	int err;
1684dee8268fSThierry Reding 
1685dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1686dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1687dee8268fSThierry Reding 	kfree(name);
1688dee8268fSThierry Reding 
1689dee8268fSThierry Reding 	if (!dc->debugfs)
1690dee8268fSThierry Reding 		return -ENOMEM;
1691dee8268fSThierry Reding 
1692dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1693dee8268fSThierry Reding 				    GFP_KERNEL);
1694dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1695dee8268fSThierry Reding 		err = -ENOMEM;
1696dee8268fSThierry Reding 		goto remove;
1697dee8268fSThierry Reding 	}
1698dee8268fSThierry Reding 
1699dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1700dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1701dee8268fSThierry Reding 
1702dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1703dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1704dee8268fSThierry Reding 				       dc->debugfs, minor);
1705dee8268fSThierry Reding 	if (err < 0)
1706dee8268fSThierry Reding 		goto free;
1707dee8268fSThierry Reding 
1708dee8268fSThierry Reding 	dc->minor = minor;
1709dee8268fSThierry Reding 
1710dee8268fSThierry Reding 	return 0;
1711dee8268fSThierry Reding 
1712dee8268fSThierry Reding free:
1713dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1714dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1715dee8268fSThierry Reding remove:
1716dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1717dee8268fSThierry Reding 	dc->debugfs = NULL;
1718dee8268fSThierry Reding 
1719dee8268fSThierry Reding 	return err;
1720dee8268fSThierry Reding }
1721dee8268fSThierry Reding 
1722dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1723dee8268fSThierry Reding {
1724dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1725dee8268fSThierry Reding 				 dc->minor);
1726dee8268fSThierry Reding 	dc->minor = NULL;
1727dee8268fSThierry Reding 
1728dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1729dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1730dee8268fSThierry Reding 
1731dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1732dee8268fSThierry Reding 	dc->debugfs = NULL;
1733dee8268fSThierry Reding 
1734dee8268fSThierry Reding 	return 0;
1735dee8268fSThierry Reding }
1736dee8268fSThierry Reding 
1737dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1738dee8268fSThierry Reding {
17399910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
17402bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1741dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1742d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1743c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1744c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1745dee8268fSThierry Reding 	int err;
1746dee8268fSThierry Reding 
1747617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
17482bcdcbfaSThierry Reding 	if (!dc->syncpt)
17492bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
17502bcdcbfaSThierry Reding 
1751df06b759SThierry Reding 	if (tegra->domain) {
1752df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1753df06b759SThierry Reding 		if (err < 0) {
1754df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1755df06b759SThierry Reding 				err);
1756df06b759SThierry Reding 			return err;
1757df06b759SThierry Reding 		}
1758df06b759SThierry Reding 
1759df06b759SThierry Reding 		dc->domain = tegra->domain;
1760df06b759SThierry Reding 	}
1761df06b759SThierry Reding 
1762c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1763c7679306SThierry Reding 	if (IS_ERR(primary)) {
1764c7679306SThierry Reding 		err = PTR_ERR(primary);
1765c7679306SThierry Reding 		goto cleanup;
1766c7679306SThierry Reding 	}
1767c7679306SThierry Reding 
1768c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1769c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1770c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1771c7679306SThierry Reding 			err = PTR_ERR(cursor);
1772c7679306SThierry Reding 			goto cleanup;
1773c7679306SThierry Reding 		}
1774c7679306SThierry Reding 	}
1775c7679306SThierry Reding 
1776c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1777f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1778c7679306SThierry Reding 	if (err < 0)
1779c7679306SThierry Reding 		goto cleanup;
1780c7679306SThierry Reding 
1781dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1782dee8268fSThierry Reding 
1783d1f3e1e0SThierry Reding 	/*
1784d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1785d1f3e1e0SThierry Reding 	 * controllers.
1786d1f3e1e0SThierry Reding 	 */
1787d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1788d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1789d1f3e1e0SThierry Reding 
17909910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1791dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1792dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1793c7679306SThierry Reding 		goto cleanup;
1794dee8268fSThierry Reding 	}
1795dee8268fSThierry Reding 
17969910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1797dee8268fSThierry Reding 	if (err < 0)
1798c7679306SThierry Reding 		goto cleanup;
1799dee8268fSThierry Reding 
1800dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
18019910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1802dee8268fSThierry Reding 		if (err < 0)
1803dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1804dee8268fSThierry Reding 	}
1805dee8268fSThierry Reding 
1806dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1807dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1808dee8268fSThierry Reding 	if (err < 0) {
1809dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1810dee8268fSThierry Reding 			err);
1811c7679306SThierry Reding 		goto cleanup;
1812dee8268fSThierry Reding 	}
1813dee8268fSThierry Reding 
1814dee8268fSThierry Reding 	return 0;
1815c7679306SThierry Reding 
1816c7679306SThierry Reding cleanup:
1817c7679306SThierry Reding 	if (cursor)
1818c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1819c7679306SThierry Reding 
1820c7679306SThierry Reding 	if (primary)
1821c7679306SThierry Reding 		drm_plane_cleanup(primary);
1822c7679306SThierry Reding 
1823c7679306SThierry Reding 	if (tegra->domain) {
1824c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1825c7679306SThierry Reding 		dc->domain = NULL;
1826c7679306SThierry Reding 	}
1827c7679306SThierry Reding 
1828c7679306SThierry Reding 	return err;
1829dee8268fSThierry Reding }
1830dee8268fSThierry Reding 
1831dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1832dee8268fSThierry Reding {
1833dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1834dee8268fSThierry Reding 	int err;
1835dee8268fSThierry Reding 
1836dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1837dee8268fSThierry Reding 
1838dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1839dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1840dee8268fSThierry Reding 		if (err < 0)
1841dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1842dee8268fSThierry Reding 	}
1843dee8268fSThierry Reding 
1844dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1845dee8268fSThierry Reding 	if (err) {
1846dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1847dee8268fSThierry Reding 		return err;
1848dee8268fSThierry Reding 	}
1849dee8268fSThierry Reding 
1850df06b759SThierry Reding 	if (dc->domain) {
1851df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1852df06b759SThierry Reding 		dc->domain = NULL;
1853df06b759SThierry Reding 	}
1854df06b759SThierry Reding 
18552bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
18562bcdcbfaSThierry Reding 
1857dee8268fSThierry Reding 	return 0;
1858dee8268fSThierry Reding }
1859dee8268fSThierry Reding 
1860dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1861dee8268fSThierry Reding 	.init = tegra_dc_init,
1862dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1863dee8268fSThierry Reding };
1864dee8268fSThierry Reding 
18658620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
186642d0659bSThierry Reding 	.supports_border_color = true,
18678620fc62SThierry Reding 	.supports_interlacing = false,
1868e687651bSThierry Reding 	.supports_cursor = false,
1869c134f019SThierry Reding 	.supports_block_linear = false,
1870d1f3e1e0SThierry Reding 	.pitch_align = 8,
18719c012700SThierry Reding 	.has_powergate = false,
18726ac1571bSDmitry Osipenko 	.broken_reset = true,
18738620fc62SThierry Reding };
18748620fc62SThierry Reding 
18758620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
187642d0659bSThierry Reding 	.supports_border_color = true,
18778620fc62SThierry Reding 	.supports_interlacing = false,
1878e687651bSThierry Reding 	.supports_cursor = false,
1879c134f019SThierry Reding 	.supports_block_linear = false,
1880d1f3e1e0SThierry Reding 	.pitch_align = 8,
18819c012700SThierry Reding 	.has_powergate = false,
18826ac1571bSDmitry Osipenko 	.broken_reset = false,
1883d1f3e1e0SThierry Reding };
1884d1f3e1e0SThierry Reding 
1885d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
188642d0659bSThierry Reding 	.supports_border_color = true,
1887d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1888d1f3e1e0SThierry Reding 	.supports_cursor = false,
1889d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1890d1f3e1e0SThierry Reding 	.pitch_align = 64,
18919c012700SThierry Reding 	.has_powergate = true,
18926ac1571bSDmitry Osipenko 	.broken_reset = false,
18938620fc62SThierry Reding };
18948620fc62SThierry Reding 
18958620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
189642d0659bSThierry Reding 	.supports_border_color = false,
18978620fc62SThierry Reding 	.supports_interlacing = true,
1898e687651bSThierry Reding 	.supports_cursor = true,
1899c134f019SThierry Reding 	.supports_block_linear = true,
1900d1f3e1e0SThierry Reding 	.pitch_align = 64,
19019c012700SThierry Reding 	.has_powergate = true,
19026ac1571bSDmitry Osipenko 	.broken_reset = false,
19038620fc62SThierry Reding };
19048620fc62SThierry Reding 
19055b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
19065b4f516fSThierry Reding 	.supports_border_color = false,
19075b4f516fSThierry Reding 	.supports_interlacing = true,
19085b4f516fSThierry Reding 	.supports_cursor = true,
19095b4f516fSThierry Reding 	.supports_block_linear = true,
19105b4f516fSThierry Reding 	.pitch_align = 64,
19115b4f516fSThierry Reding 	.has_powergate = true,
19126ac1571bSDmitry Osipenko 	.broken_reset = false,
19135b4f516fSThierry Reding };
19145b4f516fSThierry Reding 
19158620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
19168620fc62SThierry Reding 	{
19175b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
19185b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
19195b4f516fSThierry Reding 	}, {
19208620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
19218620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
19228620fc62SThierry Reding 	}, {
19239c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
19249c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
19259c012700SThierry Reding 	}, {
19268620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
19278620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
19288620fc62SThierry Reding 	}, {
19298620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
19308620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
19318620fc62SThierry Reding 	}, {
19328620fc62SThierry Reding 		/* sentinel */
19338620fc62SThierry Reding 	}
19348620fc62SThierry Reding };
1935ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
19368620fc62SThierry Reding 
193713411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
193813411dddSThierry Reding {
193913411dddSThierry Reding 	struct device_node *np;
194013411dddSThierry Reding 	u32 value = 0;
194113411dddSThierry Reding 	int err;
194213411dddSThierry Reding 
194313411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
194413411dddSThierry Reding 	if (err < 0) {
194513411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
194613411dddSThierry Reding 
194713411dddSThierry Reding 		/*
194813411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
194913411dddSThierry Reding 		 * correct head number by looking up the position of this
195013411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
195113411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
195213411dddSThierry Reding 		 * that the translation into a flattened device tree blob
195313411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
195413411dddSThierry Reding 		 * head number.
195513411dddSThierry Reding 		 *
195613411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
195713411dddSThierry Reding 		 * cases where only a single display controller is used.
195813411dddSThierry Reding 		 */
195913411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1960cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1961cf6b1744SJulia Lawall 				of_node_put(np);
196213411dddSThierry Reding 				break;
1963cf6b1744SJulia Lawall 			}
196413411dddSThierry Reding 
196513411dddSThierry Reding 			value++;
196613411dddSThierry Reding 		}
196713411dddSThierry Reding 	}
196813411dddSThierry Reding 
196913411dddSThierry Reding 	dc->pipe = value;
197013411dddSThierry Reding 
197113411dddSThierry Reding 	return 0;
197213411dddSThierry Reding }
197313411dddSThierry Reding 
1974dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1975dee8268fSThierry Reding {
1976dee8268fSThierry Reding 	struct resource *regs;
1977dee8268fSThierry Reding 	struct tegra_dc *dc;
1978dee8268fSThierry Reding 	int err;
1979dee8268fSThierry Reding 
1980dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1981dee8268fSThierry Reding 	if (!dc)
1982dee8268fSThierry Reding 		return -ENOMEM;
1983dee8268fSThierry Reding 
1984b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
19858620fc62SThierry Reding 
1986dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1987dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1988dee8268fSThierry Reding 	dc->dev = &pdev->dev;
1989dee8268fSThierry Reding 
199013411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
199113411dddSThierry Reding 	if (err < 0)
199213411dddSThierry Reding 		return err;
199313411dddSThierry Reding 
1994dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1995dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1996dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1997dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1998dee8268fSThierry Reding 	}
1999dee8268fSThierry Reding 
2000ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2001ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2002ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2003ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2004ca48080aSStephen Warren 	}
2005ca48080aSStephen Warren 
2006a2f2f740SThierry Reding 	/* assert reset and disable clock */
2007a2f2f740SThierry Reding 	if (!dc->soc->broken_reset) {
2008a2f2f740SThierry Reding 		err = clk_prepare_enable(dc->clk);
2009a2f2f740SThierry Reding 		if (err < 0)
2010a2f2f740SThierry Reding 			return err;
2011a2f2f740SThierry Reding 
2012a2f2f740SThierry Reding 		usleep_range(2000, 4000);
2013a2f2f740SThierry Reding 
2014a2f2f740SThierry Reding 		err = reset_control_assert(dc->rst);
2015a2f2f740SThierry Reding 		if (err < 0)
2016a2f2f740SThierry Reding 			return err;
2017a2f2f740SThierry Reding 
2018a2f2f740SThierry Reding 		usleep_range(2000, 4000);
2019a2f2f740SThierry Reding 
2020a2f2f740SThierry Reding 		clk_disable_unprepare(dc->clk);
2021a2f2f740SThierry Reding 	}
202233a8eb8dSThierry Reding 
20239c012700SThierry Reding 	if (dc->soc->has_powergate) {
20249c012700SThierry Reding 		if (dc->pipe == 0)
20259c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
20269c012700SThierry Reding 		else
20279c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
20289c012700SThierry Reding 
202933a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
20309c012700SThierry Reding 	}
2031dee8268fSThierry Reding 
2032dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2033dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2034dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2035dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2036dee8268fSThierry Reding 
2037dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2038dee8268fSThierry Reding 	if (dc->irq < 0) {
2039dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2040dee8268fSThierry Reding 		return -ENXIO;
2041dee8268fSThierry Reding 	}
2042dee8268fSThierry Reding 
2043dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2044dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2045dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2046dee8268fSThierry Reding 		return err;
2047dee8268fSThierry Reding 	}
2048dee8268fSThierry Reding 
204933a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
205033a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
205133a8eb8dSThierry Reding 
205233a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
205333a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
205433a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
205533a8eb8dSThierry Reding 
2056dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2057dee8268fSThierry Reding 	if (err < 0) {
2058dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2059dee8268fSThierry Reding 			err);
2060dee8268fSThierry Reding 		return err;
2061dee8268fSThierry Reding 	}
2062dee8268fSThierry Reding 
2063dee8268fSThierry Reding 	return 0;
2064dee8268fSThierry Reding }
2065dee8268fSThierry Reding 
2066dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2067dee8268fSThierry Reding {
2068dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2069dee8268fSThierry Reding 	int err;
2070dee8268fSThierry Reding 
2071dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2072dee8268fSThierry Reding 	if (err < 0) {
2073dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2074dee8268fSThierry Reding 			err);
2075dee8268fSThierry Reding 		return err;
2076dee8268fSThierry Reding 	}
2077dee8268fSThierry Reding 
207859d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
207959d29c0eSThierry Reding 	if (err < 0) {
208059d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
208159d29c0eSThierry Reding 		return err;
208259d29c0eSThierry Reding 	}
208359d29c0eSThierry Reding 
208433a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
208533a8eb8dSThierry Reding 
208633a8eb8dSThierry Reding 	return 0;
208733a8eb8dSThierry Reding }
208833a8eb8dSThierry Reding 
208933a8eb8dSThierry Reding #ifdef CONFIG_PM
209033a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
209133a8eb8dSThierry Reding {
209233a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
209333a8eb8dSThierry Reding 	int err;
209433a8eb8dSThierry Reding 
20956ac1571bSDmitry Osipenko 	if (!dc->soc->broken_reset) {
209633a8eb8dSThierry Reding 		err = reset_control_assert(dc->rst);
209733a8eb8dSThierry Reding 		if (err < 0) {
209833a8eb8dSThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
209933a8eb8dSThierry Reding 			return err;
210033a8eb8dSThierry Reding 		}
21016ac1571bSDmitry Osipenko 	}
21029c012700SThierry Reding 
21039c012700SThierry Reding 	if (dc->soc->has_powergate)
21049c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
21059c012700SThierry Reding 
2106dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2107dee8268fSThierry Reding 
2108dee8268fSThierry Reding 	return 0;
2109dee8268fSThierry Reding }
2110dee8268fSThierry Reding 
211133a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
211233a8eb8dSThierry Reding {
211333a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
211433a8eb8dSThierry Reding 	int err;
211533a8eb8dSThierry Reding 
211633a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
211733a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
211833a8eb8dSThierry Reding 							dc->rst);
211933a8eb8dSThierry Reding 		if (err < 0) {
212033a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
212133a8eb8dSThierry Reding 			return err;
212233a8eb8dSThierry Reding 		}
212333a8eb8dSThierry Reding 	} else {
212433a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
212533a8eb8dSThierry Reding 		if (err < 0) {
212633a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
212733a8eb8dSThierry Reding 			return err;
212833a8eb8dSThierry Reding 		}
212933a8eb8dSThierry Reding 
21306ac1571bSDmitry Osipenko 		if (!dc->soc->broken_reset) {
213133a8eb8dSThierry Reding 			err = reset_control_deassert(dc->rst);
213233a8eb8dSThierry Reding 			if (err < 0) {
21336ac1571bSDmitry Osipenko 				dev_err(dev,
21346ac1571bSDmitry Osipenko 					"failed to deassert reset: %d\n", err);
213533a8eb8dSThierry Reding 				return err;
213633a8eb8dSThierry Reding 			}
213733a8eb8dSThierry Reding 		}
21386ac1571bSDmitry Osipenko 	}
213933a8eb8dSThierry Reding 
214033a8eb8dSThierry Reding 	return 0;
214133a8eb8dSThierry Reding }
214233a8eb8dSThierry Reding #endif
214333a8eb8dSThierry Reding 
214433a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
214533a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
214633a8eb8dSThierry Reding };
214733a8eb8dSThierry Reding 
2148dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2149dee8268fSThierry Reding 	.driver = {
2150dee8268fSThierry Reding 		.name = "tegra-dc",
2151dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
215233a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2153dee8268fSThierry Reding 	},
2154dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2155dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2156dee8268fSThierry Reding };
2157