xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision b9ff7aeaefbb39d9882547fa4b02e6e34a7b9463)
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>
13*b9ff7aeaSThierry 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 
278620fc62SThierry Reding struct tegra_dc_soc_info {
2842d0659bSThierry Reding 	bool supports_border_color;
298620fc62SThierry Reding 	bool supports_interlacing;
30e687651bSThierry Reding 	bool supports_cursor;
31c134f019SThierry Reding 	bool supports_block_linear;
32d1f3e1e0SThierry Reding 	unsigned int pitch_align;
339c012700SThierry Reding 	bool has_powergate;
346ac1571bSDmitry Osipenko 	bool broken_reset;
358620fc62SThierry Reding };
368620fc62SThierry Reding 
37dee8268fSThierry Reding struct tegra_plane {
38dee8268fSThierry Reding 	struct drm_plane base;
39dee8268fSThierry Reding 	unsigned int index;
40dee8268fSThierry Reding };
41dee8268fSThierry Reding 
42dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
43dee8268fSThierry Reding {
44dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
45dee8268fSThierry Reding }
46dee8268fSThierry Reding 
47ca915b10SThierry Reding struct tegra_dc_state {
48ca915b10SThierry Reding 	struct drm_crtc_state base;
49ca915b10SThierry Reding 
50ca915b10SThierry Reding 	struct clk *clk;
51ca915b10SThierry Reding 	unsigned long pclk;
52ca915b10SThierry Reding 	unsigned int div;
5347802b09SThierry Reding 
5447802b09SThierry Reding 	u32 planes;
55ca915b10SThierry Reding };
56ca915b10SThierry Reding 
57ca915b10SThierry Reding static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
58ca915b10SThierry Reding {
59ca915b10SThierry Reding 	if (state)
60ca915b10SThierry Reding 		return container_of(state, struct tegra_dc_state, base);
61ca915b10SThierry Reding 
62ca915b10SThierry Reding 	return NULL;
63ca915b10SThierry Reding }
64ca915b10SThierry Reding 
658f604f8cSThierry Reding struct tegra_plane_state {
668f604f8cSThierry Reding 	struct drm_plane_state base;
678f604f8cSThierry Reding 
688f604f8cSThierry Reding 	struct tegra_bo_tiling tiling;
698f604f8cSThierry Reding 	u32 format;
708f604f8cSThierry Reding 	u32 swap;
718f604f8cSThierry Reding };
728f604f8cSThierry Reding 
738f604f8cSThierry Reding static inline struct tegra_plane_state *
748f604f8cSThierry Reding to_tegra_plane_state(struct drm_plane_state *state)
758f604f8cSThierry Reding {
768f604f8cSThierry Reding 	if (state)
778f604f8cSThierry Reding 		return container_of(state, struct tegra_plane_state, base);
788f604f8cSThierry Reding 
798f604f8cSThierry Reding 	return NULL;
808f604f8cSThierry Reding }
818f604f8cSThierry Reding 
82791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
83791ddb1eSThierry Reding {
84791ddb1eSThierry Reding 	stats->frames = 0;
85791ddb1eSThierry Reding 	stats->vblank = 0;
86791ddb1eSThierry Reding 	stats->underflow = 0;
87791ddb1eSThierry Reding 	stats->overflow = 0;
88791ddb1eSThierry Reding }
89791ddb1eSThierry Reding 
90d700ba7aSThierry Reding /*
9186df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
9286df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
9386df256fSThierry Reding  * active copy of some registers.
9486df256fSThierry Reding  */
9586df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
9686df256fSThierry Reding {
9786df256fSThierry Reding 	unsigned long flags;
9886df256fSThierry Reding 	u32 value;
9986df256fSThierry Reding 
10086df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
10186df256fSThierry Reding 
10286df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
10386df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
10486df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
10586df256fSThierry Reding 
10686df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
10786df256fSThierry Reding 	return value;
10886df256fSThierry Reding }
10986df256fSThierry Reding 
11086df256fSThierry Reding /*
111d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
112d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
113d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
114d700ba7aSThierry Reding  * on the next frame boundary otherwise.
115d700ba7aSThierry Reding  *
116d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
117d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
118d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
119d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
120d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
121d700ba7aSThierry Reding  */
12262b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
123205d48edSThierry Reding {
124205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
125205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
126205d48edSThierry Reding }
127205d48edSThierry Reding 
1288f604f8cSThierry Reding static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
12910288eeaSThierry Reding {
13010288eeaSThierry Reding 	/* assume no swapping of fetched data */
13110288eeaSThierry Reding 	if (swap)
13210288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
13310288eeaSThierry Reding 
1348f604f8cSThierry Reding 	switch (fourcc) {
13510288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
1368f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
1378f604f8cSThierry Reding 		break;
13810288eeaSThierry Reding 
13910288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
1408f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
1418f604f8cSThierry Reding 		break;
14210288eeaSThierry Reding 
14310288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
1448f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
1458f604f8cSThierry Reding 		break;
14610288eeaSThierry Reding 
14710288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
1488f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1498f604f8cSThierry Reding 		break;
15010288eeaSThierry Reding 
15110288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
15210288eeaSThierry Reding 		if (swap)
15310288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
15410288eeaSThierry Reding 
1558f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1568f604f8cSThierry Reding 		break;
15710288eeaSThierry Reding 
15810288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
1598f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
1608f604f8cSThierry Reding 		break;
16110288eeaSThierry Reding 
16210288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
1638f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
1648f604f8cSThierry Reding 		break;
16510288eeaSThierry Reding 
16610288eeaSThierry Reding 	default:
1678f604f8cSThierry Reding 		return -EINVAL;
16810288eeaSThierry Reding 	}
16910288eeaSThierry Reding 
1708f604f8cSThierry Reding 	return 0;
17110288eeaSThierry Reding }
17210288eeaSThierry Reding 
17310288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
17410288eeaSThierry Reding {
17510288eeaSThierry Reding 	switch (format) {
17610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
17710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
17810288eeaSThierry Reding 		if (planar)
17910288eeaSThierry Reding 			*planar = false;
18010288eeaSThierry Reding 
18110288eeaSThierry Reding 		return true;
18210288eeaSThierry Reding 
18310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
18410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
18510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
18610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
18710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
18810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
18910288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
19010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
19110288eeaSThierry Reding 		if (planar)
19210288eeaSThierry Reding 			*planar = true;
19310288eeaSThierry Reding 
19410288eeaSThierry Reding 		return true;
19510288eeaSThierry Reding 	}
19610288eeaSThierry Reding 
197fb35c6b6SThierry Reding 	if (planar)
198fb35c6b6SThierry Reding 		*planar = false;
199fb35c6b6SThierry Reding 
20010288eeaSThierry Reding 	return false;
20110288eeaSThierry Reding }
20210288eeaSThierry Reding 
20310288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
20410288eeaSThierry Reding 				  unsigned int bpp)
20510288eeaSThierry Reding {
20610288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
20710288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20810288eeaSThierry Reding 	u32 dda_inc;
20910288eeaSThierry Reding 	int max;
21010288eeaSThierry Reding 
21110288eeaSThierry Reding 	if (v)
21210288eeaSThierry Reding 		max = 15;
21310288eeaSThierry Reding 	else {
21410288eeaSThierry Reding 		switch (bpp) {
21510288eeaSThierry Reding 		case 2:
21610288eeaSThierry Reding 			max = 8;
21710288eeaSThierry Reding 			break;
21810288eeaSThierry Reding 
21910288eeaSThierry Reding 		default:
22010288eeaSThierry Reding 			WARN_ON_ONCE(1);
22110288eeaSThierry Reding 			/* fallthrough */
22210288eeaSThierry Reding 		case 4:
22310288eeaSThierry Reding 			max = 4;
22410288eeaSThierry Reding 			break;
22510288eeaSThierry Reding 		}
22610288eeaSThierry Reding 	}
22710288eeaSThierry Reding 
22810288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
22910288eeaSThierry Reding 	inf.full -= dfixed_const(1);
23010288eeaSThierry Reding 
23110288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
23210288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
23310288eeaSThierry Reding 
23410288eeaSThierry Reding 	return dda_inc;
23510288eeaSThierry Reding }
23610288eeaSThierry Reding 
23710288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
23810288eeaSThierry Reding {
23910288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
24010288eeaSThierry Reding 	return dfixed_frac(inf);
24110288eeaSThierry Reding }
24210288eeaSThierry Reding 
2434aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
24410288eeaSThierry Reding 				  const struct tegra_dc_window *window)
24510288eeaSThierry Reding {
24610288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
24793396d0fSSean Paul 	unsigned long value, flags;
24810288eeaSThierry Reding 	bool yuv, planar;
24910288eeaSThierry Reding 
25010288eeaSThierry Reding 	/*
25110288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
25210288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
25310288eeaSThierry Reding 	 */
25410288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
25510288eeaSThierry Reding 	if (!yuv)
25610288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
25710288eeaSThierry Reding 	else
25810288eeaSThierry Reding 		bpp = planar ? 1 : 2;
25910288eeaSThierry Reding 
26093396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
26193396d0fSSean Paul 
26210288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
26310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
26410288eeaSThierry Reding 
26510288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
26610288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
26710288eeaSThierry Reding 
26810288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
26910288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
27010288eeaSThierry Reding 
27110288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
27210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
27310288eeaSThierry Reding 
27410288eeaSThierry Reding 	h_offset = window->src.x * bpp;
27510288eeaSThierry Reding 	v_offset = window->src.y;
27610288eeaSThierry Reding 	h_size = window->src.w * bpp;
27710288eeaSThierry Reding 	v_size = window->src.h;
27810288eeaSThierry Reding 
27910288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
28010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
28110288eeaSThierry Reding 
28210288eeaSThierry Reding 	/*
28310288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
28410288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
28510288eeaSThierry Reding 	 */
28610288eeaSThierry Reding 	if (yuv && planar)
28710288eeaSThierry Reding 		bpp = 2;
28810288eeaSThierry Reding 
28910288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
29010288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
29110288eeaSThierry Reding 
29210288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
29310288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
29410288eeaSThierry Reding 
29510288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
29610288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
29710288eeaSThierry Reding 
29810288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
29910288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
30010288eeaSThierry Reding 
30110288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
30210288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
30310288eeaSThierry Reding 
30410288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
30510288eeaSThierry Reding 
30610288eeaSThierry Reding 	if (yuv && planar) {
30710288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
30810288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
30910288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
31010288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
31110288eeaSThierry Reding 	} else {
31210288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
31310288eeaSThierry Reding 	}
31410288eeaSThierry Reding 
31510288eeaSThierry Reding 	if (window->bottom_up)
31610288eeaSThierry Reding 		v_offset += window->src.h - 1;
31710288eeaSThierry Reding 
31810288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
31910288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
32010288eeaSThierry Reding 
321c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
322c134f019SThierry Reding 		unsigned long height = window->tiling.value;
323c134f019SThierry Reding 
324c134f019SThierry Reding 		switch (window->tiling.mode) {
325c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
326c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
327c134f019SThierry Reding 			break;
328c134f019SThierry Reding 
329c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
330c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
331c134f019SThierry Reding 			break;
332c134f019SThierry Reding 
333c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
334c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
335c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
336c134f019SThierry Reding 			break;
337c134f019SThierry Reding 		}
338c134f019SThierry Reding 
339c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
34010288eeaSThierry Reding 	} else {
341c134f019SThierry Reding 		switch (window->tiling.mode) {
342c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
34310288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
34410288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
345c134f019SThierry Reding 			break;
346c134f019SThierry Reding 
347c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
348c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
349c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
350c134f019SThierry Reding 			break;
351c134f019SThierry Reding 
352c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3534aa3df71SThierry Reding 			/*
3544aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3554aa3df71SThierry Reding 			 * will already have filtered it out.
3564aa3df71SThierry Reding 			 */
3574aa3df71SThierry Reding 			break;
35810288eeaSThierry Reding 		}
35910288eeaSThierry Reding 
36010288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
361c134f019SThierry Reding 	}
36210288eeaSThierry Reding 
36310288eeaSThierry Reding 	value = WIN_ENABLE;
36410288eeaSThierry Reding 
36510288eeaSThierry Reding 	if (yuv) {
36610288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
36710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
36810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
36910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
37010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
37110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
37210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
37310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
37410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
37510288eeaSThierry Reding 
37610288eeaSThierry Reding 		value |= CSC_ENABLE;
37710288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
37810288eeaSThierry Reding 		value |= COLOR_EXPAND;
37910288eeaSThierry Reding 	}
38010288eeaSThierry Reding 
38110288eeaSThierry Reding 	if (window->bottom_up)
38210288eeaSThierry Reding 		value |= V_DIRECTION;
38310288eeaSThierry Reding 
38410288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
38510288eeaSThierry Reding 
38610288eeaSThierry Reding 	/*
38710288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
38810288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
38910288eeaSThierry Reding 	 */
39010288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
39110288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
39210288eeaSThierry Reding 
39310288eeaSThierry Reding 	switch (index) {
39410288eeaSThierry Reding 	case 0:
39510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
39610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
39710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
39810288eeaSThierry Reding 		break;
39910288eeaSThierry Reding 
40010288eeaSThierry Reding 	case 1:
40110288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
40210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
40310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
40410288eeaSThierry Reding 		break;
40510288eeaSThierry Reding 
40610288eeaSThierry Reding 	case 2:
40710288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
40810288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
40910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
41010288eeaSThierry Reding 		break;
41110288eeaSThierry Reding 	}
41210288eeaSThierry Reding 
41393396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
414c7679306SThierry Reding }
415c7679306SThierry Reding 
416c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
417c7679306SThierry Reding {
418c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
419c7679306SThierry Reding 
420c7679306SThierry Reding 	drm_plane_cleanup(plane);
421c7679306SThierry Reding 	kfree(p);
422c7679306SThierry Reding }
423c7679306SThierry Reding 
424c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
425c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
426c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
427c7679306SThierry Reding 	DRM_FORMAT_RGB565,
428c7679306SThierry Reding };
429c7679306SThierry Reding 
4304aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
431c7679306SThierry Reding {
4324aa3df71SThierry Reding 	tegra_plane_destroy(plane);
4334aa3df71SThierry Reding }
4344aa3df71SThierry Reding 
4358f604f8cSThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
4368f604f8cSThierry Reding {
4378f604f8cSThierry Reding 	struct tegra_plane_state *state;
4388f604f8cSThierry Reding 
4393b59b7acSThierry Reding 	if (plane->state)
4402f701695SDaniel Vetter 		__drm_atomic_helper_plane_destroy_state(plane->state);
4418f604f8cSThierry Reding 
4428f604f8cSThierry Reding 	kfree(plane->state);
4438f604f8cSThierry Reding 	plane->state = NULL;
4448f604f8cSThierry Reding 
4458f604f8cSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
4468f604f8cSThierry Reding 	if (state) {
4478f604f8cSThierry Reding 		plane->state = &state->base;
4488f604f8cSThierry Reding 		plane->state->plane = plane;
4498f604f8cSThierry Reding 	}
4508f604f8cSThierry Reding }
4518f604f8cSThierry Reding 
4528f604f8cSThierry Reding static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
4538f604f8cSThierry Reding {
4548f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
4558f604f8cSThierry Reding 	struct tegra_plane_state *copy;
4568f604f8cSThierry Reding 
4573b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
4588f604f8cSThierry Reding 	if (!copy)
4598f604f8cSThierry Reding 		return NULL;
4608f604f8cSThierry Reding 
4613b59b7acSThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
4623b59b7acSThierry Reding 	copy->tiling = state->tiling;
4633b59b7acSThierry Reding 	copy->format = state->format;
4643b59b7acSThierry Reding 	copy->swap = state->swap;
4658f604f8cSThierry Reding 
4668f604f8cSThierry Reding 	return &copy->base;
4678f604f8cSThierry Reding }
4688f604f8cSThierry Reding 
4698f604f8cSThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
4708f604f8cSThierry Reding 					     struct drm_plane_state *state)
4718f604f8cSThierry Reding {
4722f701695SDaniel Vetter 	__drm_atomic_helper_plane_destroy_state(state);
4738f604f8cSThierry Reding 	kfree(state);
4748f604f8cSThierry Reding }
4758f604f8cSThierry Reding 
4764aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
47707866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
47807866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
4794aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
4808f604f8cSThierry Reding 	.reset = tegra_plane_reset,
4818f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
4828f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
4834aa3df71SThierry Reding };
4844aa3df71SThierry Reding 
48547802b09SThierry Reding static int tegra_plane_state_add(struct tegra_plane *plane,
48647802b09SThierry Reding 				 struct drm_plane_state *state)
48747802b09SThierry Reding {
48847802b09SThierry Reding 	struct drm_crtc_state *crtc_state;
48947802b09SThierry Reding 	struct tegra_dc_state *tegra;
4907d205857SDmitry Osipenko 	struct drm_rect clip;
4917d205857SDmitry Osipenko 	int err;
49247802b09SThierry Reding 
49347802b09SThierry Reding 	/* Propagate errors from allocation or locking failures. */
49447802b09SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
49547802b09SThierry Reding 	if (IS_ERR(crtc_state))
49647802b09SThierry Reding 		return PTR_ERR(crtc_state);
49747802b09SThierry Reding 
4987d205857SDmitry Osipenko 	clip.x1 = 0;
4997d205857SDmitry Osipenko 	clip.y1 = 0;
5007d205857SDmitry Osipenko 	clip.x2 = crtc_state->mode.hdisplay;
5017d205857SDmitry Osipenko 	clip.y2 = crtc_state->mode.vdisplay;
5027d205857SDmitry Osipenko 
5037d205857SDmitry Osipenko 	/* Check plane state for visibility and calculate clipping bounds */
5047d205857SDmitry Osipenko 	err = drm_plane_helper_check_state(state, &clip, 0, INT_MAX,
5057d205857SDmitry Osipenko 					   true, true);
5067d205857SDmitry Osipenko 	if (err < 0)
5077d205857SDmitry Osipenko 		return err;
5087d205857SDmitry Osipenko 
50947802b09SThierry Reding 	tegra = to_dc_state(crtc_state);
51047802b09SThierry Reding 
51147802b09SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
51247802b09SThierry Reding 
51347802b09SThierry Reding 	return 0;
51447802b09SThierry Reding }
51547802b09SThierry Reding 
5164aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
5174aa3df71SThierry Reding 				    struct drm_plane_state *state)
5184aa3df71SThierry Reding {
5198f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
5208f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
52147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
5224aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
523c7679306SThierry Reding 	int err;
524c7679306SThierry Reding 
5254aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
5264aa3df71SThierry Reding 	if (!state->crtc)
5274aa3df71SThierry Reding 		return 0;
5284aa3df71SThierry Reding 
529438b74a5SVille Syrjälä 	err = tegra_dc_format(state->fb->format->format, &plane_state->format,
5308f604f8cSThierry Reding 			      &plane_state->swap);
5314aa3df71SThierry Reding 	if (err < 0)
5324aa3df71SThierry Reding 		return err;
5334aa3df71SThierry Reding 
5348f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
5358f604f8cSThierry Reding 	if (err < 0)
5368f604f8cSThierry Reding 		return err;
5378f604f8cSThierry Reding 
5388f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
5394aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
5404aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
5414aa3df71SThierry Reding 		return -EINVAL;
5424aa3df71SThierry Reding 	}
5434aa3df71SThierry Reding 
5444aa3df71SThierry Reding 	/*
5454aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
5464aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
5474aa3df71SThierry Reding 	 * configuration.
5484aa3df71SThierry Reding 	 */
549bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
5504aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
5514aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
5524aa3df71SThierry Reding 			return -EINVAL;
5534aa3df71SThierry Reding 		}
5544aa3df71SThierry Reding 	}
5554aa3df71SThierry Reding 
55647802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
55747802b09SThierry Reding 	if (err < 0)
55847802b09SThierry Reding 		return err;
55947802b09SThierry Reding 
5604aa3df71SThierry Reding 	return 0;
5614aa3df71SThierry Reding }
5624aa3df71SThierry Reding 
56380d3eef1SDmitry Osipenko static void tegra_dc_disable_window(struct tegra_dc *dc, int index)
56480d3eef1SDmitry Osipenko {
56580d3eef1SDmitry Osipenko 	unsigned long flags;
56680d3eef1SDmitry Osipenko 	u32 value;
56780d3eef1SDmitry Osipenko 
56880d3eef1SDmitry Osipenko 	spin_lock_irqsave(&dc->lock, flags);
56980d3eef1SDmitry Osipenko 
57080d3eef1SDmitry Osipenko 	value = WINDOW_A_SELECT << index;
57180d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
57280d3eef1SDmitry Osipenko 
57380d3eef1SDmitry Osipenko 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
57480d3eef1SDmitry Osipenko 	value &= ~WIN_ENABLE;
57580d3eef1SDmitry Osipenko 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
57680d3eef1SDmitry Osipenko 
57780d3eef1SDmitry Osipenko 	spin_unlock_irqrestore(&dc->lock, flags);
57880d3eef1SDmitry Osipenko }
57980d3eef1SDmitry Osipenko 
5804aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
5814aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
5824aa3df71SThierry Reding {
5838f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
5844aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5854aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
5864aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
5874aa3df71SThierry Reding 	struct tegra_dc_window window;
5884aa3df71SThierry Reding 	unsigned int i;
5894aa3df71SThierry Reding 
5904aa3df71SThierry Reding 	/* rien ne va plus */
5914aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5924aa3df71SThierry Reding 		return;
5934aa3df71SThierry Reding 
59480d3eef1SDmitry Osipenko 	if (!plane->state->visible)
59580d3eef1SDmitry Osipenko 		return tegra_dc_disable_window(dc, p->index);
59680d3eef1SDmitry Osipenko 
597c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
5987d205857SDmitry Osipenko 	window.src.x = plane->state->src.x1 >> 16;
5997d205857SDmitry Osipenko 	window.src.y = plane->state->src.y1 >> 16;
6007d205857SDmitry Osipenko 	window.src.w = drm_rect_width(&plane->state->src) >> 16;
6017d205857SDmitry Osipenko 	window.src.h = drm_rect_height(&plane->state->src) >> 16;
6027d205857SDmitry Osipenko 	window.dst.x = plane->state->dst.x1;
6037d205857SDmitry Osipenko 	window.dst.y = plane->state->dst.y1;
6047d205857SDmitry Osipenko 	window.dst.w = drm_rect_width(&plane->state->dst);
6057d205857SDmitry Osipenko 	window.dst.h = drm_rect_height(&plane->state->dst);
606272725c7SVille Syrjälä 	window.bits_per_pixel = fb->format->cpp[0] * 8;
607c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
608c7679306SThierry Reding 
6098f604f8cSThierry Reding 	/* copy from state */
6108f604f8cSThierry Reding 	window.tiling = state->tiling;
6118f604f8cSThierry Reding 	window.format = state->format;
6128f604f8cSThierry Reding 	window.swap = state->swap;
613c7679306SThierry Reding 
614bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
6154aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
616c7679306SThierry Reding 
6174aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
61808ee0178SDmitry Osipenko 
61908ee0178SDmitry Osipenko 		/*
62008ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
62108ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
62208ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
62308ee0178SDmitry Osipenko 		 */
62408ee0178SDmitry Osipenko 		if (i < 2)
6254aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
626c7679306SThierry Reding 	}
627c7679306SThierry Reding 
6284aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
6294aa3df71SThierry Reding }
6304aa3df71SThierry Reding 
6314aa3df71SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
6324aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
633c7679306SThierry Reding {
6344aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
6354aa3df71SThierry Reding 	struct tegra_dc *dc;
6364aa3df71SThierry Reding 
6374aa3df71SThierry Reding 	/* rien ne va plus */
6384aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
6394aa3df71SThierry Reding 		return;
6404aa3df71SThierry Reding 
6414aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
6424aa3df71SThierry Reding 
64380d3eef1SDmitry Osipenko 	tegra_dc_disable_window(dc, p->index);
644c7679306SThierry Reding }
645c7679306SThierry Reding 
6464aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
6474aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
6484aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
6494aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
650c7679306SThierry Reding };
651c7679306SThierry Reding 
652c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
653c7679306SThierry Reding 						       struct tegra_dc *dc)
654c7679306SThierry Reding {
655518e6227SThierry Reding 	/*
656518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
657518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
658518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
659518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
660518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
661518e6227SThierry Reding 	 * here.
662518e6227SThierry Reding 	 *
663518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
664518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
665518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
666518e6227SThierry Reding 	 */
667518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
668c7679306SThierry Reding 	struct tegra_plane *plane;
669c7679306SThierry Reding 	unsigned int num_formats;
670c7679306SThierry Reding 	const u32 *formats;
671c7679306SThierry Reding 	int err;
672c7679306SThierry Reding 
673c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
674c7679306SThierry Reding 	if (!plane)
675c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
676c7679306SThierry Reding 
677c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
678c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
679c7679306SThierry Reding 
680518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
681c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
682e6fc3b68SBen Widawsky 				       num_formats, NULL,
683e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_PRIMARY, NULL);
684c7679306SThierry Reding 	if (err < 0) {
685c7679306SThierry Reding 		kfree(plane);
686c7679306SThierry Reding 		return ERR_PTR(err);
687c7679306SThierry Reding 	}
688c7679306SThierry Reding 
6894aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
6904aa3df71SThierry Reding 
691c7679306SThierry Reding 	return &plane->base;
692c7679306SThierry Reding }
693c7679306SThierry Reding 
694c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
695c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
696c7679306SThierry Reding };
697c7679306SThierry Reding 
6984aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
6994aa3df71SThierry Reding 				     struct drm_plane_state *state)
700c7679306SThierry Reding {
70147802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
70247802b09SThierry Reding 	int err;
70347802b09SThierry Reding 
7044aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
7054aa3df71SThierry Reding 	if (!state->crtc)
7064aa3df71SThierry Reding 		return 0;
707c7679306SThierry Reding 
708c7679306SThierry Reding 	/* scaling not supported for cursor */
7094aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
7104aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
711c7679306SThierry Reding 		return -EINVAL;
712c7679306SThierry Reding 
713c7679306SThierry Reding 	/* only square cursors supported */
7144aa3df71SThierry Reding 	if (state->src_w != state->src_h)
715c7679306SThierry Reding 		return -EINVAL;
716c7679306SThierry Reding 
7174aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
7184aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
7194aa3df71SThierry Reding 		return -EINVAL;
7204aa3df71SThierry Reding 
72147802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
72247802b09SThierry Reding 	if (err < 0)
72347802b09SThierry Reding 		return err;
72447802b09SThierry Reding 
7254aa3df71SThierry Reding 	return 0;
7264aa3df71SThierry Reding }
7274aa3df71SThierry Reding 
7284aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
7294aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
7304aa3df71SThierry Reding {
7314aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
7324aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
7334aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
7344aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
7354aa3df71SThierry Reding 
7364aa3df71SThierry Reding 	/* rien ne va plus */
7374aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
7384aa3df71SThierry Reding 		return;
7394aa3df71SThierry Reding 
7404aa3df71SThierry Reding 	switch (state->crtc_w) {
741c7679306SThierry Reding 	case 32:
742c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
743c7679306SThierry Reding 		break;
744c7679306SThierry Reding 
745c7679306SThierry Reding 	case 64:
746c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
747c7679306SThierry Reding 		break;
748c7679306SThierry Reding 
749c7679306SThierry Reding 	case 128:
750c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
751c7679306SThierry Reding 		break;
752c7679306SThierry Reding 
753c7679306SThierry Reding 	case 256:
754c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
755c7679306SThierry Reding 		break;
756c7679306SThierry Reding 
757c7679306SThierry Reding 	default:
7584aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
7594aa3df71SThierry Reding 		     state->crtc_h);
7604aa3df71SThierry Reding 		return;
761c7679306SThierry Reding 	}
762c7679306SThierry Reding 
763c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
764c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
765c7679306SThierry Reding 
766c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
767c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
768c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
769c7679306SThierry Reding #endif
770c7679306SThierry Reding 
771c7679306SThierry Reding 	/* enable cursor and set blend mode */
772c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
773c7679306SThierry Reding 	value |= CURSOR_ENABLE;
774c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
775c7679306SThierry Reding 
776c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
777c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
778c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
779c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
780c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
781c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
782c7679306SThierry Reding 	value |= CURSOR_ALPHA;
783c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
784c7679306SThierry Reding 
785c7679306SThierry Reding 	/* position the cursor */
7864aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
787c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
788c7679306SThierry Reding }
789c7679306SThierry Reding 
7904aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
7914aa3df71SThierry Reding 					struct drm_plane_state *old_state)
792c7679306SThierry Reding {
7934aa3df71SThierry Reding 	struct tegra_dc *dc;
794c7679306SThierry Reding 	u32 value;
795c7679306SThierry Reding 
7964aa3df71SThierry Reding 	/* rien ne va plus */
7974aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
7984aa3df71SThierry Reding 		return;
7994aa3df71SThierry Reding 
8004aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
801c7679306SThierry Reding 
802c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
803c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
804c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
805c7679306SThierry Reding }
806c7679306SThierry Reding 
807c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
80807866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
80907866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
810c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
8118f604f8cSThierry Reding 	.reset = tegra_plane_reset,
8128f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
8138f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
8144aa3df71SThierry Reding };
8154aa3df71SThierry Reding 
8164aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
8174aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
8184aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
8194aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
820c7679306SThierry Reding };
821c7679306SThierry Reding 
822c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
823c7679306SThierry Reding 						      struct tegra_dc *dc)
824c7679306SThierry Reding {
825c7679306SThierry Reding 	struct tegra_plane *plane;
826c7679306SThierry Reding 	unsigned int num_formats;
827c7679306SThierry Reding 	const u32 *formats;
828c7679306SThierry Reding 	int err;
829c7679306SThierry Reding 
830c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
831c7679306SThierry Reding 	if (!plane)
832c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
833c7679306SThierry Reding 
83447802b09SThierry Reding 	/*
835a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
836a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
837a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
838a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
839a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
84047802b09SThierry Reding 	 */
84147802b09SThierry Reding 	plane->index = 6;
84247802b09SThierry Reding 
843c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
844c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
845c7679306SThierry Reding 
846c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
847c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
848e6fc3b68SBen Widawsky 				       num_formats, NULL,
849e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_CURSOR, NULL);
850c7679306SThierry Reding 	if (err < 0) {
851c7679306SThierry Reding 		kfree(plane);
852c7679306SThierry Reding 		return ERR_PTR(err);
853c7679306SThierry Reding 	}
854c7679306SThierry Reding 
8554aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
8564aa3df71SThierry Reding 
857c7679306SThierry Reding 	return &plane->base;
858c7679306SThierry Reding }
859c7679306SThierry Reding 
860c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
861dee8268fSThierry Reding {
862c7679306SThierry Reding 	tegra_plane_destroy(plane);
863dee8268fSThierry Reding }
864dee8268fSThierry Reding 
865c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
86607866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
86707866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
868c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
8698f604f8cSThierry Reding 	.reset = tegra_plane_reset,
8708f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
8718f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
872dee8268fSThierry Reding };
873dee8268fSThierry Reding 
874c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
875dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
876dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
877dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
878dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
879f925390eSThierry Reding 	DRM_FORMAT_YUYV,
880dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
881dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
882dee8268fSThierry Reding };
883dee8268fSThierry Reding 
8844aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
8854aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
8864aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
8874aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
8884aa3df71SThierry Reding };
8894aa3df71SThierry Reding 
890c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
891c7679306SThierry Reding 						       struct tegra_dc *dc,
892c7679306SThierry Reding 						       unsigned int index)
893dee8268fSThierry Reding {
894dee8268fSThierry Reding 	struct tegra_plane *plane;
895c7679306SThierry Reding 	unsigned int num_formats;
896c7679306SThierry Reding 	const u32 *formats;
897c7679306SThierry Reding 	int err;
898dee8268fSThierry Reding 
899f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
900dee8268fSThierry Reding 	if (!plane)
901c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
902dee8268fSThierry Reding 
903c7679306SThierry Reding 	plane->index = index;
904dee8268fSThierry Reding 
905c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
906c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
907c7679306SThierry Reding 
908c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
909c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
910e6fc3b68SBen Widawsky 				       num_formats, NULL,
911e6fc3b68SBen Widawsky 				       DRM_PLANE_TYPE_OVERLAY, NULL);
912f002abc1SThierry Reding 	if (err < 0) {
913f002abc1SThierry Reding 		kfree(plane);
914c7679306SThierry Reding 		return ERR_PTR(err);
915dee8268fSThierry Reding 	}
916c7679306SThierry Reding 
9174aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
9184aa3df71SThierry Reding 
919c7679306SThierry Reding 	return &plane->base;
920c7679306SThierry Reding }
921c7679306SThierry Reding 
922c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
923c7679306SThierry Reding {
924c7679306SThierry Reding 	struct drm_plane *plane;
925c7679306SThierry Reding 	unsigned int i;
926c7679306SThierry Reding 
927c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
928c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
929c7679306SThierry Reding 		if (IS_ERR(plane))
930c7679306SThierry Reding 			return PTR_ERR(plane);
931f002abc1SThierry Reding 	}
932dee8268fSThierry Reding 
933dee8268fSThierry Reding 	return 0;
934dee8268fSThierry Reding }
935dee8268fSThierry Reding 
93610437d9bSShawn Guo static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
93742e9ce05SThierry Reding {
93810437d9bSShawn Guo 	struct tegra_dc *dc = to_tegra_dc(crtc);
93910437d9bSShawn Guo 
94042e9ce05SThierry Reding 	if (dc->syncpt)
94142e9ce05SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
94242e9ce05SThierry Reding 
94342e9ce05SThierry Reding 	/* fallback to software emulated VBLANK counter */
94442e9ce05SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
94542e9ce05SThierry Reding }
94642e9ce05SThierry Reding 
94710437d9bSShawn Guo static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
948dee8268fSThierry Reding {
94910437d9bSShawn Guo 	struct tegra_dc *dc = to_tegra_dc(crtc);
950dee8268fSThierry Reding 	unsigned long value, flags;
951dee8268fSThierry Reding 
952dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
953dee8268fSThierry Reding 
954dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
955dee8268fSThierry Reding 	value |= VBLANK_INT;
956dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
957dee8268fSThierry Reding 
958dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
95910437d9bSShawn Guo 
96010437d9bSShawn Guo 	return 0;
961dee8268fSThierry Reding }
962dee8268fSThierry Reding 
96310437d9bSShawn Guo static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
964dee8268fSThierry Reding {
96510437d9bSShawn Guo 	struct tegra_dc *dc = to_tegra_dc(crtc);
966dee8268fSThierry Reding 	unsigned long value, flags;
967dee8268fSThierry Reding 
968dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
969dee8268fSThierry Reding 
970dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
971dee8268fSThierry Reding 	value &= ~VBLANK_INT;
972dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
973dee8268fSThierry Reding 
974dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
975dee8268fSThierry Reding }
976dee8268fSThierry Reding 
977dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
978dee8268fSThierry Reding {
979dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
980dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
981dee8268fSThierry Reding 	unsigned long flags, base;
982dee8268fSThierry Reding 	struct tegra_bo *bo;
983dee8268fSThierry Reding 
9846b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
9856b59cc1cSThierry Reding 
9866b59cc1cSThierry Reding 	if (!dc->event) {
9876b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
988dee8268fSThierry Reding 		return;
9896b59cc1cSThierry Reding 	}
990dee8268fSThierry Reding 
991f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
992dee8268fSThierry Reding 
9938643bc6dSDan Carpenter 	spin_lock(&dc->lock);
99493396d0fSSean Paul 
995dee8268fSThierry Reding 	/* check if new start address has been latched */
99693396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
997dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
998dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
999dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
1000dee8268fSThierry Reding 
10018643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
100293396d0fSSean Paul 
1003f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
1004ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
1005ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
1006dee8268fSThierry Reding 		dc->event = NULL;
1007dee8268fSThierry Reding 	}
10086b59cc1cSThierry Reding 
10096b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
1010dee8268fSThierry Reding }
1011dee8268fSThierry Reding 
1012f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
1013f002abc1SThierry Reding {
1014f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
1015f002abc1SThierry Reding }
1016f002abc1SThierry Reding 
1017ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
1018ca915b10SThierry Reding {
1019ca915b10SThierry Reding 	struct tegra_dc_state *state;
1020ca915b10SThierry Reding 
10213b59b7acSThierry Reding 	if (crtc->state)
1022ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
10233b59b7acSThierry Reding 
1024ca915b10SThierry Reding 	kfree(crtc->state);
1025ca915b10SThierry Reding 	crtc->state = NULL;
1026ca915b10SThierry Reding 
1027ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1028332bbe70SThierry Reding 	if (state) {
1029ca915b10SThierry Reding 		crtc->state = &state->base;
1030332bbe70SThierry Reding 		crtc->state->crtc = crtc;
1031332bbe70SThierry Reding 	}
103231930d4dSThierry Reding 
103331930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
1034ca915b10SThierry Reding }
1035ca915b10SThierry Reding 
1036ca915b10SThierry Reding static struct drm_crtc_state *
1037ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1038ca915b10SThierry Reding {
1039ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1040ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1041ca915b10SThierry Reding 
10423b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1043ca915b10SThierry Reding 	if (!copy)
1044ca915b10SThierry Reding 		return NULL;
1045ca915b10SThierry Reding 
10463b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
10473b59b7acSThierry Reding 	copy->clk = state->clk;
10483b59b7acSThierry Reding 	copy->pclk = state->pclk;
10493b59b7acSThierry Reding 	copy->div = state->div;
10503b59b7acSThierry Reding 	copy->planes = state->planes;
1051ca915b10SThierry Reding 
1052ca915b10SThierry Reding 	return &copy->base;
1053ca915b10SThierry Reding }
1054ca915b10SThierry Reding 
1055ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1056ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1057ca915b10SThierry Reding {
1058ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1059ca915b10SThierry Reding 	kfree(state);
1060ca915b10SThierry Reding }
1061ca915b10SThierry Reding 
1062dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
10631503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
106474f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1065f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1066ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1067ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1068ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
106910437d9bSShawn Guo 	.get_vblank_counter = tegra_dc_get_vblank_counter,
107010437d9bSShawn Guo 	.enable_vblank = tegra_dc_enable_vblank,
107110437d9bSShawn Guo 	.disable_vblank = tegra_dc_disable_vblank,
1072dee8268fSThierry Reding };
1073dee8268fSThierry Reding 
1074dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1075dee8268fSThierry Reding 				struct drm_display_mode *mode)
1076dee8268fSThierry Reding {
10770444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10780444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1079dee8268fSThierry Reding 	unsigned long value;
1080dee8268fSThierry Reding 
1081dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1082dee8268fSThierry Reding 
1083dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1084dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1085dee8268fSThierry Reding 
1086dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1087dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1088dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1089dee8268fSThierry Reding 
1090dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1091dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1092dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1093dee8268fSThierry Reding 
1094dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1095dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1096dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1097dee8268fSThierry Reding 
1098dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1099dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1100dee8268fSThierry Reding 
1101dee8268fSThierry Reding 	return 0;
1102dee8268fSThierry Reding }
1103dee8268fSThierry Reding 
11049d910b60SThierry Reding /**
11059d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
11069d910b60SThierry Reding  *     state
11079d910b60SThierry Reding  * @dc: display controller
11089d910b60SThierry Reding  * @crtc_state: CRTC atomic state
11099d910b60SThierry Reding  * @clk: parent clock for display controller
11109d910b60SThierry Reding  * @pclk: pixel clock
11119d910b60SThierry Reding  * @div: shift clock divider
11129d910b60SThierry Reding  *
11139d910b60SThierry Reding  * Returns:
11149d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
11159d910b60SThierry Reding  */
1116ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1117ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1118ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1119ca915b10SThierry Reding 			       unsigned int div)
1120ca915b10SThierry Reding {
1121ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1122ca915b10SThierry Reding 
1123d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1124d2982748SThierry Reding 		return -EINVAL;
1125d2982748SThierry Reding 
1126ca915b10SThierry Reding 	state->clk = clk;
1127ca915b10SThierry Reding 	state->pclk = pclk;
1128ca915b10SThierry Reding 	state->div = div;
1129ca915b10SThierry Reding 
1130ca915b10SThierry Reding 	return 0;
1131ca915b10SThierry Reding }
1132ca915b10SThierry Reding 
113376d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
113476d59ed0SThierry Reding 				  struct tegra_dc_state *state)
113576d59ed0SThierry Reding {
113676d59ed0SThierry Reding 	u32 value;
113776d59ed0SThierry Reding 	int err;
113876d59ed0SThierry Reding 
113976d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
114076d59ed0SThierry Reding 	if (err < 0)
114176d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
114276d59ed0SThierry Reding 
114376d59ed0SThierry Reding 	/*
114476d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
114576d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
114676d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
114776d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
114876d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
114976d59ed0SThierry Reding 	 * should therefore be avoided.
115076d59ed0SThierry Reding 	 */
115176d59ed0SThierry Reding 	if (state->pclk > 0) {
115276d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
115376d59ed0SThierry Reding 		if (err < 0)
115476d59ed0SThierry Reding 			dev_err(dc->dev,
115576d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
115676d59ed0SThierry Reding 				state->pclk);
115776d59ed0SThierry Reding 	}
115876d59ed0SThierry Reding 
115976d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
116076d59ed0SThierry Reding 		      state->div);
116176d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
116276d59ed0SThierry Reding 
116376d59ed0SThierry Reding 	value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
116476d59ed0SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
116576d59ed0SThierry Reding }
116676d59ed0SThierry Reding 
1167003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1168003fc848SThierry Reding {
1169003fc848SThierry Reding 	u32 value;
1170003fc848SThierry Reding 
1171003fc848SThierry Reding 	/* stop the display controller */
1172003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1173003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1174003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1175003fc848SThierry Reding 
1176003fc848SThierry Reding 	tegra_dc_commit(dc);
1177003fc848SThierry Reding }
1178003fc848SThierry Reding 
1179003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1180003fc848SThierry Reding {
1181003fc848SThierry Reding 	u32 value;
1182003fc848SThierry Reding 
1183003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1184003fc848SThierry Reding 
1185003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1186003fc848SThierry Reding }
1187003fc848SThierry Reding 
1188003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1189003fc848SThierry Reding {
1190003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1191003fc848SThierry Reding 
1192003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1193003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1194003fc848SThierry Reding 			return 0;
1195003fc848SThierry Reding 
1196003fc848SThierry Reding 		usleep_range(1000, 2000);
1197003fc848SThierry Reding 	}
1198003fc848SThierry Reding 
1199003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1200003fc848SThierry Reding 	return -ETIMEDOUT;
1201003fc848SThierry Reding }
1202003fc848SThierry Reding 
120364581714SLaurent Pinchart static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
120464581714SLaurent Pinchart 				      struct drm_crtc_state *old_state)
1205003fc848SThierry Reding {
1206003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1207003fc848SThierry Reding 	u32 value;
1208003fc848SThierry Reding 
1209003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1210003fc848SThierry Reding 		tegra_dc_stop(dc);
1211003fc848SThierry Reding 
1212003fc848SThierry Reding 		/*
1213003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1214003fc848SThierry Reding 		 * in case this fails.
1215003fc848SThierry Reding 		 */
1216003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1217003fc848SThierry Reding 	}
1218003fc848SThierry Reding 
1219003fc848SThierry Reding 	/*
1220003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1221003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1222003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1223003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1224003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1225003fc848SThierry Reding 	 * to go idle.
1226003fc848SThierry Reding 	 *
1227003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1228003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1229003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1230003fc848SThierry Reding 	 *
1231003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1232003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1233003fc848SThierry Reding 	 * the RGB encoder?
1234003fc848SThierry Reding 	 */
1235003fc848SThierry Reding 	if (dc->rgb) {
1236003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1237003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1238003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1239003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1240003fc848SThierry Reding 	}
1241003fc848SThierry Reding 
1242003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1243003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
124433a8eb8dSThierry Reding 
124533a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1246003fc848SThierry Reding }
1247003fc848SThierry Reding 
12480b20a0f8SLaurent Pinchart static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
12490b20a0f8SLaurent Pinchart 				     struct drm_crtc_state *old_state)
1250dee8268fSThierry Reding {
12514aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
125276d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1253dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1254dbb3f2f7SThierry Reding 	u32 value;
1255dee8268fSThierry Reding 
125633a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
125733a8eb8dSThierry Reding 
125833a8eb8dSThierry Reding 	/* initialize display controller */
125933a8eb8dSThierry Reding 	if (dc->syncpt) {
126033a8eb8dSThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt);
126133a8eb8dSThierry Reding 
126233a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
126333a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
126433a8eb8dSThierry Reding 
126533a8eb8dSThierry Reding 		value = SYNCPT_VSYNC_ENABLE | syncpt;
126633a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
126733a8eb8dSThierry Reding 	}
126833a8eb8dSThierry Reding 
126933a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
127033a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
127133a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
127233a8eb8dSThierry Reding 
127333a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
127433a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
127533a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
127633a8eb8dSThierry Reding 
127733a8eb8dSThierry Reding 	/* initialize timer */
127833a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
127933a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
128033a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
128133a8eb8dSThierry Reding 
128233a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
128333a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
128433a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
128533a8eb8dSThierry Reding 
128633a8eb8dSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
128733a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
128833a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
128933a8eb8dSThierry Reding 
129033a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
129133a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
129233a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
129333a8eb8dSThierry Reding 
129433a8eb8dSThierry Reding 	if (dc->soc->supports_border_color)
129533a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
129633a8eb8dSThierry Reding 
129733a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
129876d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
129976d59ed0SThierry Reding 
1300dee8268fSThierry Reding 	/* program display mode */
1301dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1302dee8268fSThierry Reding 
13038620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
13048620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
13058620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
13068620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
13078620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
13088620fc62SThierry Reding 	}
1309666cb873SThierry Reding 
1310666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1311666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1312666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1313666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1314666cb873SThierry Reding 
1315666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1316666cb873SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1317666cb873SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1318666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1319666cb873SThierry Reding 
1320666cb873SThierry Reding 	tegra_dc_commit(dc);
1321dee8268fSThierry Reding 
13228ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1323dee8268fSThierry Reding }
1324dee8268fSThierry Reding 
13254aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
13264aa3df71SThierry Reding 				   struct drm_crtc_state *state)
13274aa3df71SThierry Reding {
13284aa3df71SThierry Reding 	return 0;
13294aa3df71SThierry Reding }
13304aa3df71SThierry Reding 
1331613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1332613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
13334aa3df71SThierry Reding {
13341503ca47SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
13351503ca47SThierry Reding 
13361503ca47SThierry Reding 	if (crtc->state->event) {
13371503ca47SThierry Reding 		crtc->state->event->pipe = drm_crtc_index(crtc);
13381503ca47SThierry Reding 
13391503ca47SThierry Reding 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
13401503ca47SThierry Reding 
13411503ca47SThierry Reding 		dc->event = crtc->state->event;
13421503ca47SThierry Reding 		crtc->state->event = NULL;
13431503ca47SThierry Reding 	}
13444aa3df71SThierry Reding }
13454aa3df71SThierry Reding 
1346613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1347613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
13484aa3df71SThierry Reding {
134947802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
135047802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
135147802b09SThierry Reding 
135247802b09SThierry Reding 	tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
135347802b09SThierry Reding 	tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
13544aa3df71SThierry Reding }
13554aa3df71SThierry Reding 
1356dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
13574aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
13584aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
13594aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
13600b20a0f8SLaurent Pinchart 	.atomic_enable = tegra_crtc_atomic_enable,
136164581714SLaurent Pinchart 	.atomic_disable = tegra_crtc_atomic_disable,
1362dee8268fSThierry Reding };
1363dee8268fSThierry Reding 
1364dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1365dee8268fSThierry Reding {
1366dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1367dee8268fSThierry Reding 	unsigned long status;
1368dee8268fSThierry Reding 
1369dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1370dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1371dee8268fSThierry Reding 
1372dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1373dee8268fSThierry Reding 		/*
1374dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1375dee8268fSThierry Reding 		*/
1376791ddb1eSThierry Reding 		dc->stats.frames++;
1377dee8268fSThierry Reding 	}
1378dee8268fSThierry Reding 
1379dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1380dee8268fSThierry Reding 		/*
1381dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1382dee8268fSThierry Reding 		*/
1383ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1384dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1385791ddb1eSThierry Reding 		dc->stats.vblank++;
1386dee8268fSThierry Reding 	}
1387dee8268fSThierry Reding 
1388dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1389dee8268fSThierry Reding 		/*
1390dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1391dee8268fSThierry Reding 		*/
1392791ddb1eSThierry Reding 		dc->stats.underflow++;
1393791ddb1eSThierry Reding 	}
1394791ddb1eSThierry Reding 
1395791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1396791ddb1eSThierry Reding 		/*
1397791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1398791ddb1eSThierry Reding 		*/
1399791ddb1eSThierry Reding 		dc->stats.overflow++;
1400dee8268fSThierry Reding 	}
1401dee8268fSThierry Reding 
1402dee8268fSThierry Reding 	return IRQ_HANDLED;
1403dee8268fSThierry Reding }
1404dee8268fSThierry Reding 
1405dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1406dee8268fSThierry Reding {
1407dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1408dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1409003fc848SThierry Reding 	int err = 0;
1410003fc848SThierry Reding 
141199612b27SDaniel Vetter 	drm_modeset_lock(&dc->base.mutex, NULL);
1412003fc848SThierry Reding 
1413003fc848SThierry Reding 	if (!dc->base.state->active) {
1414003fc848SThierry Reding 		err = -EBUSY;
1415003fc848SThierry Reding 		goto unlock;
1416003fc848SThierry Reding 	}
1417dee8268fSThierry Reding 
1418dee8268fSThierry Reding #define DUMP_REG(name)						\
141903a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1420dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1421dee8268fSThierry Reding 
1422dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1423dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1424dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1425dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1426dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1427dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1428dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1429dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1430dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1431dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1432dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1433dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1434dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1435dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1436dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1437dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1438dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1439dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1440dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1441dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1442dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1443dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1444dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1445dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1446dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1447dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1448dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1449dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1450dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1451dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1452dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1453dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1454dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1455dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1456dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1457dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1458dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1459dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1460dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1461dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1462dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1463dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1464dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1465dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1466dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1467dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1468dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1469dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1470dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1471dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1472dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1473dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1474dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1475dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1476dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1477dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1478dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1479dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1480dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1481dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1482dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1483dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1484dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1485dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1486dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1487dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1488dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1489dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1490dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1491dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1492dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1493dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1494dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1495dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1496dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1497dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1498dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1499dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1500dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1501dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1502dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1503dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1504dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1505dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1506dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1507dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1508dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1509dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1510dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1511dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1512dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1513dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1514dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1515dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1516dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1517dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1518dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1519dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1520dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1521dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1522dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1523dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1524dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1525dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1526dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1527dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1528dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1529dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1530dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1531dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1532dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1533dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1534dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1535dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1536dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1537dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1538dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1539dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1540dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1541dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1542dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1543dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1544dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1545dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1546dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1547dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1548dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1549dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1550dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1551dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1552dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1553dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1554dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1555dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1556dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1557dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1558dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1559dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1560dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1561dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1562dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1563dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1564dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1565dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1566dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1567dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1568dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1569dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1570dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1571dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1572dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1573dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1574dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1575dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1576dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1577dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1578dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1579dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1580dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1581dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1582dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1583dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1584dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1585dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1586dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1587dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1588dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1589dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1590dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1591dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1592dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1593dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1594dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1595dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1596dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1597e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1598e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1599dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1600dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1601dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1602dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1603dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1604dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1605dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1606dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1607dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1608dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1609dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1610dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1611dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1612dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1613dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1614dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1615dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1616dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1617dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1618dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1619dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1620dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1621dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1622dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1623dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1624dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1625dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1626dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1627dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1628dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1629dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1630dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1631dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1632dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1633dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1634dee8268fSThierry Reding 
1635dee8268fSThierry Reding #undef DUMP_REG
1636dee8268fSThierry Reding 
1637003fc848SThierry Reding unlock:
163899612b27SDaniel Vetter 	drm_modeset_unlock(&dc->base.mutex);
1639003fc848SThierry Reding 	return err;
1640dee8268fSThierry Reding }
1641dee8268fSThierry Reding 
16426ca1f62fSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
16436ca1f62fSThierry Reding {
16446ca1f62fSThierry Reding 	struct drm_info_node *node = s->private;
16456ca1f62fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1646003fc848SThierry Reding 	int err = 0;
16476ca1f62fSThierry Reding 	u32 value;
16486ca1f62fSThierry Reding 
164999612b27SDaniel Vetter 	drm_modeset_lock(&dc->base.mutex, NULL);
1650003fc848SThierry Reding 
1651003fc848SThierry Reding 	if (!dc->base.state->active) {
1652003fc848SThierry Reding 		err = -EBUSY;
1653003fc848SThierry Reding 		goto unlock;
1654003fc848SThierry Reding 	}
1655003fc848SThierry Reding 
16566ca1f62fSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
16576ca1f62fSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
16586ca1f62fSThierry Reding 	tegra_dc_commit(dc);
16596ca1f62fSThierry Reding 
16606ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16616ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16626ca1f62fSThierry Reding 
16636ca1f62fSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
16646ca1f62fSThierry Reding 	seq_printf(s, "%08x\n", value);
16656ca1f62fSThierry Reding 
16666ca1f62fSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
16676ca1f62fSThierry Reding 
1668003fc848SThierry Reding unlock:
166999612b27SDaniel Vetter 	drm_modeset_unlock(&dc->base.mutex);
1670003fc848SThierry Reding 	return err;
16716ca1f62fSThierry Reding }
16726ca1f62fSThierry Reding 
1673791ddb1eSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1674791ddb1eSThierry Reding {
1675791ddb1eSThierry Reding 	struct drm_info_node *node = s->private;
1676791ddb1eSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1677791ddb1eSThierry Reding 
1678791ddb1eSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1679791ddb1eSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1680791ddb1eSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1681791ddb1eSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1682791ddb1eSThierry Reding 
1683dee8268fSThierry Reding 	return 0;
1684dee8268fSThierry Reding }
1685dee8268fSThierry Reding 
1686dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1687dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
16886ca1f62fSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1689791ddb1eSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1690dee8268fSThierry Reding };
1691dee8268fSThierry Reding 
1692dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1693dee8268fSThierry Reding {
1694dee8268fSThierry Reding 	unsigned int i;
1695dee8268fSThierry Reding 	char *name;
1696dee8268fSThierry Reding 	int err;
1697dee8268fSThierry Reding 
1698dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1699dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1700dee8268fSThierry Reding 	kfree(name);
1701dee8268fSThierry Reding 
1702dee8268fSThierry Reding 	if (!dc->debugfs)
1703dee8268fSThierry Reding 		return -ENOMEM;
1704dee8268fSThierry Reding 
1705dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1706dee8268fSThierry Reding 				    GFP_KERNEL);
1707dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1708dee8268fSThierry Reding 		err = -ENOMEM;
1709dee8268fSThierry Reding 		goto remove;
1710dee8268fSThierry Reding 	}
1711dee8268fSThierry Reding 
1712dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1713dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1714dee8268fSThierry Reding 
1715dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1716dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1717dee8268fSThierry Reding 				       dc->debugfs, minor);
1718dee8268fSThierry Reding 	if (err < 0)
1719dee8268fSThierry Reding 		goto free;
1720dee8268fSThierry Reding 
1721dee8268fSThierry Reding 	dc->minor = minor;
1722dee8268fSThierry Reding 
1723dee8268fSThierry Reding 	return 0;
1724dee8268fSThierry Reding 
1725dee8268fSThierry Reding free:
1726dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1727dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1728dee8268fSThierry Reding remove:
1729dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1730dee8268fSThierry Reding 	dc->debugfs = NULL;
1731dee8268fSThierry Reding 
1732dee8268fSThierry Reding 	return err;
1733dee8268fSThierry Reding }
1734dee8268fSThierry Reding 
1735dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1736dee8268fSThierry Reding {
1737dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1738dee8268fSThierry Reding 				 dc->minor);
1739dee8268fSThierry Reding 	dc->minor = NULL;
1740dee8268fSThierry Reding 
1741dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1742dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1743dee8268fSThierry Reding 
1744dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1745dee8268fSThierry Reding 	dc->debugfs = NULL;
1746dee8268fSThierry Reding 
1747dee8268fSThierry Reding 	return 0;
1748dee8268fSThierry Reding }
1749dee8268fSThierry Reding 
1750dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1751dee8268fSThierry Reding {
17529910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
17532bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1754dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1755d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1756c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1757c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1758dee8268fSThierry Reding 	int err;
1759dee8268fSThierry Reding 
1760617dd7ccSThierry Reding 	dc->syncpt = host1x_syncpt_request(client, flags);
17612bcdcbfaSThierry Reding 	if (!dc->syncpt)
17622bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
17632bcdcbfaSThierry Reding 
1764df06b759SThierry Reding 	if (tegra->domain) {
1765df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1766df06b759SThierry Reding 		if (err < 0) {
1767df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1768df06b759SThierry Reding 				err);
1769df06b759SThierry Reding 			return err;
1770df06b759SThierry Reding 		}
1771df06b759SThierry Reding 
1772df06b759SThierry Reding 		dc->domain = tegra->domain;
1773df06b759SThierry Reding 	}
1774df06b759SThierry Reding 
1775c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1776c7679306SThierry Reding 	if (IS_ERR(primary)) {
1777c7679306SThierry Reding 		err = PTR_ERR(primary);
1778c7679306SThierry Reding 		goto cleanup;
1779c7679306SThierry Reding 	}
1780c7679306SThierry Reding 
1781c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1782c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1783c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1784c7679306SThierry Reding 			err = PTR_ERR(cursor);
1785c7679306SThierry Reding 			goto cleanup;
1786c7679306SThierry Reding 		}
1787c7679306SThierry Reding 	}
1788c7679306SThierry Reding 
1789c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1790f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1791c7679306SThierry Reding 	if (err < 0)
1792c7679306SThierry Reding 		goto cleanup;
1793c7679306SThierry Reding 
1794dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1795dee8268fSThierry Reding 
1796d1f3e1e0SThierry Reding 	/*
1797d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1798d1f3e1e0SThierry Reding 	 * controllers.
1799d1f3e1e0SThierry Reding 	 */
1800d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1801d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1802d1f3e1e0SThierry Reding 
18039910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1804dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1805dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1806c7679306SThierry Reding 		goto cleanup;
1807dee8268fSThierry Reding 	}
1808dee8268fSThierry Reding 
18099910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1810dee8268fSThierry Reding 	if (err < 0)
1811c7679306SThierry Reding 		goto cleanup;
1812dee8268fSThierry Reding 
1813dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
18149910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1815dee8268fSThierry Reding 		if (err < 0)
1816dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1817dee8268fSThierry Reding 	}
1818dee8268fSThierry Reding 
1819dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1820dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1821dee8268fSThierry Reding 	if (err < 0) {
1822dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1823dee8268fSThierry Reding 			err);
1824c7679306SThierry Reding 		goto cleanup;
1825dee8268fSThierry Reding 	}
1826dee8268fSThierry Reding 
1827dee8268fSThierry Reding 	return 0;
1828c7679306SThierry Reding 
1829c7679306SThierry Reding cleanup:
1830c7679306SThierry Reding 	if (cursor)
1831c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1832c7679306SThierry Reding 
1833c7679306SThierry Reding 	if (primary)
1834c7679306SThierry Reding 		drm_plane_cleanup(primary);
1835c7679306SThierry Reding 
1836c7679306SThierry Reding 	if (tegra->domain) {
1837c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1838c7679306SThierry Reding 		dc->domain = NULL;
1839c7679306SThierry Reding 	}
1840c7679306SThierry Reding 
1841c7679306SThierry Reding 	return err;
1842dee8268fSThierry Reding }
1843dee8268fSThierry Reding 
1844dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1845dee8268fSThierry Reding {
1846dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1847dee8268fSThierry Reding 	int err;
1848dee8268fSThierry Reding 
1849dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1850dee8268fSThierry Reding 
1851dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1852dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1853dee8268fSThierry Reding 		if (err < 0)
1854dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1855dee8268fSThierry Reding 	}
1856dee8268fSThierry Reding 
1857dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1858dee8268fSThierry Reding 	if (err) {
1859dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1860dee8268fSThierry Reding 		return err;
1861dee8268fSThierry Reding 	}
1862dee8268fSThierry Reding 
1863df06b759SThierry Reding 	if (dc->domain) {
1864df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1865df06b759SThierry Reding 		dc->domain = NULL;
1866df06b759SThierry Reding 	}
1867df06b759SThierry Reding 
18682bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
18692bcdcbfaSThierry Reding 
1870dee8268fSThierry Reding 	return 0;
1871dee8268fSThierry Reding }
1872dee8268fSThierry Reding 
1873dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1874dee8268fSThierry Reding 	.init = tegra_dc_init,
1875dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1876dee8268fSThierry Reding };
1877dee8268fSThierry Reding 
18788620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
187942d0659bSThierry Reding 	.supports_border_color = true,
18808620fc62SThierry Reding 	.supports_interlacing = false,
1881e687651bSThierry Reding 	.supports_cursor = false,
1882c134f019SThierry Reding 	.supports_block_linear = false,
1883d1f3e1e0SThierry Reding 	.pitch_align = 8,
18849c012700SThierry Reding 	.has_powergate = false,
18856ac1571bSDmitry Osipenko 	.broken_reset = true,
18868620fc62SThierry Reding };
18878620fc62SThierry Reding 
18888620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
188942d0659bSThierry Reding 	.supports_border_color = true,
18908620fc62SThierry Reding 	.supports_interlacing = false,
1891e687651bSThierry Reding 	.supports_cursor = false,
1892c134f019SThierry Reding 	.supports_block_linear = false,
1893d1f3e1e0SThierry Reding 	.pitch_align = 8,
18949c012700SThierry Reding 	.has_powergate = false,
18956ac1571bSDmitry Osipenko 	.broken_reset = false,
1896d1f3e1e0SThierry Reding };
1897d1f3e1e0SThierry Reding 
1898d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
189942d0659bSThierry Reding 	.supports_border_color = true,
1900d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1901d1f3e1e0SThierry Reding 	.supports_cursor = false,
1902d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1903d1f3e1e0SThierry Reding 	.pitch_align = 64,
19049c012700SThierry Reding 	.has_powergate = true,
19056ac1571bSDmitry Osipenko 	.broken_reset = false,
19068620fc62SThierry Reding };
19078620fc62SThierry Reding 
19088620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
190942d0659bSThierry Reding 	.supports_border_color = false,
19108620fc62SThierry Reding 	.supports_interlacing = true,
1911e687651bSThierry Reding 	.supports_cursor = true,
1912c134f019SThierry Reding 	.supports_block_linear = true,
1913d1f3e1e0SThierry Reding 	.pitch_align = 64,
19149c012700SThierry Reding 	.has_powergate = true,
19156ac1571bSDmitry Osipenko 	.broken_reset = false,
19168620fc62SThierry Reding };
19178620fc62SThierry Reding 
19185b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
19195b4f516fSThierry Reding 	.supports_border_color = false,
19205b4f516fSThierry Reding 	.supports_interlacing = true,
19215b4f516fSThierry Reding 	.supports_cursor = true,
19225b4f516fSThierry Reding 	.supports_block_linear = true,
19235b4f516fSThierry Reding 	.pitch_align = 64,
19245b4f516fSThierry Reding 	.has_powergate = true,
19256ac1571bSDmitry Osipenko 	.broken_reset = false,
19265b4f516fSThierry Reding };
19275b4f516fSThierry Reding 
19288620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
19298620fc62SThierry Reding 	{
19305b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
19315b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
19325b4f516fSThierry Reding 	}, {
19338620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
19348620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
19358620fc62SThierry Reding 	}, {
19369c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
19379c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
19389c012700SThierry Reding 	}, {
19398620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
19408620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
19418620fc62SThierry Reding 	}, {
19428620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
19438620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
19448620fc62SThierry Reding 	}, {
19458620fc62SThierry Reding 		/* sentinel */
19468620fc62SThierry Reding 	}
19478620fc62SThierry Reding };
1948ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
19498620fc62SThierry Reding 
195013411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
195113411dddSThierry Reding {
195213411dddSThierry Reding 	struct device_node *np;
195313411dddSThierry Reding 	u32 value = 0;
195413411dddSThierry Reding 	int err;
195513411dddSThierry Reding 
195613411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
195713411dddSThierry Reding 	if (err < 0) {
195813411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
195913411dddSThierry Reding 
196013411dddSThierry Reding 		/*
196113411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
196213411dddSThierry Reding 		 * correct head number by looking up the position of this
196313411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
196413411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
196513411dddSThierry Reding 		 * that the translation into a flattened device tree blob
196613411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
196713411dddSThierry Reding 		 * head number.
196813411dddSThierry Reding 		 *
196913411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
197013411dddSThierry Reding 		 * cases where only a single display controller is used.
197113411dddSThierry Reding 		 */
197213411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1973cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1974cf6b1744SJulia Lawall 				of_node_put(np);
197513411dddSThierry Reding 				break;
1976cf6b1744SJulia Lawall 			}
197713411dddSThierry Reding 
197813411dddSThierry Reding 			value++;
197913411dddSThierry Reding 		}
198013411dddSThierry Reding 	}
198113411dddSThierry Reding 
198213411dddSThierry Reding 	dc->pipe = value;
198313411dddSThierry Reding 
198413411dddSThierry Reding 	return 0;
198513411dddSThierry Reding }
198613411dddSThierry Reding 
1987dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1988dee8268fSThierry Reding {
1989dee8268fSThierry Reding 	struct resource *regs;
1990dee8268fSThierry Reding 	struct tegra_dc *dc;
1991dee8268fSThierry Reding 	int err;
1992dee8268fSThierry Reding 
1993dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1994dee8268fSThierry Reding 	if (!dc)
1995dee8268fSThierry Reding 		return -ENOMEM;
1996dee8268fSThierry Reding 
1997*b9ff7aeaSThierry Reding 	dc->soc = of_device_get_match_data(&pdev->dev);
19988620fc62SThierry Reding 
1999dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
2000dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
2001dee8268fSThierry Reding 	dc->dev = &pdev->dev;
2002dee8268fSThierry Reding 
200313411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
200413411dddSThierry Reding 	if (err < 0)
200513411dddSThierry Reding 		return err;
200613411dddSThierry Reding 
2007dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
2008dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
2009dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
2010dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
2011dee8268fSThierry Reding 	}
2012dee8268fSThierry Reding 
2013ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2014ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
2015ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
2016ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
2017ca48080aSStephen Warren 	}
2018ca48080aSStephen Warren 
20196ac1571bSDmitry Osipenko 	if (!dc->soc->broken_reset)
202033a8eb8dSThierry Reding 		reset_control_assert(dc->rst);
202133a8eb8dSThierry Reding 
20229c012700SThierry Reding 	if (dc->soc->has_powergate) {
20239c012700SThierry Reding 		if (dc->pipe == 0)
20249c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
20259c012700SThierry Reding 		else
20269c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
20279c012700SThierry Reding 
202833a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
20299c012700SThierry Reding 	}
2030dee8268fSThierry Reding 
2031dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2032dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2033dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2034dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2035dee8268fSThierry Reding 
2036dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2037dee8268fSThierry Reding 	if (dc->irq < 0) {
2038dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2039dee8268fSThierry Reding 		return -ENXIO;
2040dee8268fSThierry Reding 	}
2041dee8268fSThierry Reding 
2042dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2043dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2044dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2045dee8268fSThierry Reding 		return err;
2046dee8268fSThierry Reding 	}
2047dee8268fSThierry Reding 
204833a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
204933a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
205033a8eb8dSThierry Reding 
205133a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
205233a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
205333a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
205433a8eb8dSThierry Reding 
2055dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2056dee8268fSThierry Reding 	if (err < 0) {
2057dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2058dee8268fSThierry Reding 			err);
2059dee8268fSThierry Reding 		return err;
2060dee8268fSThierry Reding 	}
2061dee8268fSThierry Reding 
2062dee8268fSThierry Reding 	return 0;
2063dee8268fSThierry Reding }
2064dee8268fSThierry Reding 
2065dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2066dee8268fSThierry Reding {
2067dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2068dee8268fSThierry Reding 	int err;
2069dee8268fSThierry Reding 
2070dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2071dee8268fSThierry Reding 	if (err < 0) {
2072dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2073dee8268fSThierry Reding 			err);
2074dee8268fSThierry Reding 		return err;
2075dee8268fSThierry Reding 	}
2076dee8268fSThierry Reding 
207759d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
207859d29c0eSThierry Reding 	if (err < 0) {
207959d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
208059d29c0eSThierry Reding 		return err;
208159d29c0eSThierry Reding 	}
208259d29c0eSThierry Reding 
208333a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
208433a8eb8dSThierry Reding 
208533a8eb8dSThierry Reding 	return 0;
208633a8eb8dSThierry Reding }
208733a8eb8dSThierry Reding 
208833a8eb8dSThierry Reding #ifdef CONFIG_PM
208933a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
209033a8eb8dSThierry Reding {
209133a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
209233a8eb8dSThierry Reding 	int err;
209333a8eb8dSThierry Reding 
20946ac1571bSDmitry Osipenko 	if (!dc->soc->broken_reset) {
209533a8eb8dSThierry Reding 		err = reset_control_assert(dc->rst);
209633a8eb8dSThierry Reding 		if (err < 0) {
209733a8eb8dSThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
209833a8eb8dSThierry Reding 			return err;
209933a8eb8dSThierry Reding 		}
21006ac1571bSDmitry Osipenko 	}
21019c012700SThierry Reding 
21029c012700SThierry Reding 	if (dc->soc->has_powergate)
21039c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
21049c012700SThierry Reding 
2105dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2106dee8268fSThierry Reding 
2107dee8268fSThierry Reding 	return 0;
2108dee8268fSThierry Reding }
2109dee8268fSThierry Reding 
211033a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
211133a8eb8dSThierry Reding {
211233a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
211333a8eb8dSThierry Reding 	int err;
211433a8eb8dSThierry Reding 
211533a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
211633a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
211733a8eb8dSThierry Reding 							dc->rst);
211833a8eb8dSThierry Reding 		if (err < 0) {
211933a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
212033a8eb8dSThierry Reding 			return err;
212133a8eb8dSThierry Reding 		}
212233a8eb8dSThierry Reding 	} else {
212333a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
212433a8eb8dSThierry Reding 		if (err < 0) {
212533a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
212633a8eb8dSThierry Reding 			return err;
212733a8eb8dSThierry Reding 		}
212833a8eb8dSThierry Reding 
21296ac1571bSDmitry Osipenko 		if (!dc->soc->broken_reset) {
213033a8eb8dSThierry Reding 			err = reset_control_deassert(dc->rst);
213133a8eb8dSThierry Reding 			if (err < 0) {
21326ac1571bSDmitry Osipenko 				dev_err(dev,
21336ac1571bSDmitry Osipenko 					"failed to deassert reset: %d\n", err);
213433a8eb8dSThierry Reding 				return err;
213533a8eb8dSThierry Reding 			}
213633a8eb8dSThierry Reding 		}
21376ac1571bSDmitry Osipenko 	}
213833a8eb8dSThierry Reding 
213933a8eb8dSThierry Reding 	return 0;
214033a8eb8dSThierry Reding }
214133a8eb8dSThierry Reding #endif
214233a8eb8dSThierry Reding 
214333a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
214433a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
214533a8eb8dSThierry Reding };
214633a8eb8dSThierry Reding 
2147dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2148dee8268fSThierry Reding 	.driver = {
2149dee8268fSThierry Reding 		.name = "tegra-dc",
2150dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
215133a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2152dee8268fSThierry Reding 	},
2153dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2154dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2155dee8268fSThierry Reding };
2156