xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision 2f701695fd3a5876cd302662554f6c0ece5197eb)
1dee8268fSThierry Reding /*
2dee8268fSThierry Reding  * Copyright (C) 2012 Avionic Design GmbH
3dee8268fSThierry Reding  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
4dee8268fSThierry Reding  *
5dee8268fSThierry Reding  * This program is free software; you can redistribute it and/or modify
6dee8268fSThierry Reding  * it under the terms of the GNU General Public License version 2 as
7dee8268fSThierry Reding  * published by the Free Software Foundation.
8dee8268fSThierry Reding  */
9dee8268fSThierry Reding 
10dee8268fSThierry Reding #include <linux/clk.h>
11dee8268fSThierry Reding #include <linux/debugfs.h>
12df06b759SThierry Reding #include <linux/iommu.h>
13ca48080aSStephen Warren #include <linux/reset.h>
14dee8268fSThierry Reding 
159c012700SThierry Reding #include <soc/tegra/pmc.h>
169c012700SThierry Reding 
17dee8268fSThierry Reding #include "dc.h"
18dee8268fSThierry Reding #include "drm.h"
19dee8268fSThierry Reding #include "gem.h"
20dee8268fSThierry Reding 
219d44189fSThierry Reding #include <drm/drm_atomic.h>
224aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
233cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
243cb9ae4fSDaniel Vetter 
258620fc62SThierry Reding struct tegra_dc_soc_info {
2642d0659bSThierry Reding 	bool supports_border_color;
278620fc62SThierry Reding 	bool supports_interlacing;
28e687651bSThierry Reding 	bool supports_cursor;
29c134f019SThierry Reding 	bool supports_block_linear;
30d1f3e1e0SThierry Reding 	unsigned int pitch_align;
319c012700SThierry Reding 	bool has_powergate;
328620fc62SThierry Reding };
338620fc62SThierry Reding 
34dee8268fSThierry Reding struct tegra_plane {
35dee8268fSThierry Reding 	struct drm_plane base;
36dee8268fSThierry Reding 	unsigned int index;
37dee8268fSThierry Reding };
38dee8268fSThierry Reding 
39dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
40dee8268fSThierry Reding {
41dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
42dee8268fSThierry Reding }
43dee8268fSThierry Reding 
44ca915b10SThierry Reding struct tegra_dc_state {
45ca915b10SThierry Reding 	struct drm_crtc_state base;
46ca915b10SThierry Reding 
47ca915b10SThierry Reding 	struct clk *clk;
48ca915b10SThierry Reding 	unsigned long pclk;
49ca915b10SThierry Reding 	unsigned int div;
5047802b09SThierry Reding 
5147802b09SThierry Reding 	u32 planes;
52ca915b10SThierry Reding };
53ca915b10SThierry Reding 
54ca915b10SThierry Reding static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
55ca915b10SThierry Reding {
56ca915b10SThierry Reding 	if (state)
57ca915b10SThierry Reding 		return container_of(state, struct tegra_dc_state, base);
58ca915b10SThierry Reding 
59ca915b10SThierry Reding 	return NULL;
60ca915b10SThierry Reding }
61ca915b10SThierry Reding 
628f604f8cSThierry Reding struct tegra_plane_state {
638f604f8cSThierry Reding 	struct drm_plane_state base;
648f604f8cSThierry Reding 
658f604f8cSThierry Reding 	struct tegra_bo_tiling tiling;
668f604f8cSThierry Reding 	u32 format;
678f604f8cSThierry Reding 	u32 swap;
688f604f8cSThierry Reding };
698f604f8cSThierry Reding 
708f604f8cSThierry Reding static inline struct tegra_plane_state *
718f604f8cSThierry Reding to_tegra_plane_state(struct drm_plane_state *state)
728f604f8cSThierry Reding {
738f604f8cSThierry Reding 	if (state)
748f604f8cSThierry Reding 		return container_of(state, struct tegra_plane_state, base);
758f604f8cSThierry Reding 
768f604f8cSThierry Reding 	return NULL;
778f604f8cSThierry Reding }
788f604f8cSThierry Reding 
79791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
80791ddb1eSThierry Reding {
81791ddb1eSThierry Reding 	stats->frames = 0;
82791ddb1eSThierry Reding 	stats->vblank = 0;
83791ddb1eSThierry Reding 	stats->underflow = 0;
84791ddb1eSThierry Reding 	stats->overflow = 0;
85791ddb1eSThierry Reding }
86791ddb1eSThierry Reding 
87d700ba7aSThierry Reding /*
8886df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
8986df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
9086df256fSThierry Reding  * active copy of some registers.
9186df256fSThierry Reding  */
9286df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
9386df256fSThierry Reding {
9486df256fSThierry Reding 	unsigned long flags;
9586df256fSThierry Reding 	u32 value;
9686df256fSThierry Reding 
9786df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
9886df256fSThierry Reding 
9986df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
10086df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
10186df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
10286df256fSThierry Reding 
10386df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
10486df256fSThierry Reding 	return value;
10586df256fSThierry Reding }
10686df256fSThierry Reding 
10786df256fSThierry Reding /*
108d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
109d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
110d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
111d700ba7aSThierry Reding  * on the next frame boundary otherwise.
112d700ba7aSThierry Reding  *
113d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
114d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
115d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
116d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
117d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
118d700ba7aSThierry Reding  */
11962b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
120205d48edSThierry Reding {
121205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
122205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
123205d48edSThierry Reding }
124205d48edSThierry Reding 
1258f604f8cSThierry Reding static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
12610288eeaSThierry Reding {
12710288eeaSThierry Reding 	/* assume no swapping of fetched data */
12810288eeaSThierry Reding 	if (swap)
12910288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
13010288eeaSThierry Reding 
1318f604f8cSThierry Reding 	switch (fourcc) {
13210288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
1338f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
1348f604f8cSThierry Reding 		break;
13510288eeaSThierry Reding 
13610288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
1378f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
1388f604f8cSThierry Reding 		break;
13910288eeaSThierry Reding 
14010288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
1418f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
1428f604f8cSThierry Reding 		break;
14310288eeaSThierry Reding 
14410288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
1458f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1468f604f8cSThierry Reding 		break;
14710288eeaSThierry Reding 
14810288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
14910288eeaSThierry Reding 		if (swap)
15010288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
15110288eeaSThierry Reding 
1528f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1538f604f8cSThierry Reding 		break;
15410288eeaSThierry Reding 
15510288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
1568f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
1578f604f8cSThierry Reding 		break;
15810288eeaSThierry Reding 
15910288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
1608f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
1618f604f8cSThierry Reding 		break;
16210288eeaSThierry Reding 
16310288eeaSThierry Reding 	default:
1648f604f8cSThierry Reding 		return -EINVAL;
16510288eeaSThierry Reding 	}
16610288eeaSThierry Reding 
1678f604f8cSThierry Reding 	return 0;
16810288eeaSThierry Reding }
16910288eeaSThierry Reding 
17010288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
17110288eeaSThierry Reding {
17210288eeaSThierry Reding 	switch (format) {
17310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
17410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
17510288eeaSThierry Reding 		if (planar)
17610288eeaSThierry Reding 			*planar = false;
17710288eeaSThierry Reding 
17810288eeaSThierry Reding 		return true;
17910288eeaSThierry Reding 
18010288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
18110288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
18210288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
18310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
18410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
18510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
18610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
18710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
18810288eeaSThierry Reding 		if (planar)
18910288eeaSThierry Reding 			*planar = true;
19010288eeaSThierry Reding 
19110288eeaSThierry Reding 		return true;
19210288eeaSThierry Reding 	}
19310288eeaSThierry Reding 
194fb35c6b6SThierry Reding 	if (planar)
195fb35c6b6SThierry Reding 		*planar = false;
196fb35c6b6SThierry Reding 
19710288eeaSThierry Reding 	return false;
19810288eeaSThierry Reding }
19910288eeaSThierry Reding 
20010288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
20110288eeaSThierry Reding 				  unsigned int bpp)
20210288eeaSThierry Reding {
20310288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
20410288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20510288eeaSThierry Reding 	u32 dda_inc;
20610288eeaSThierry Reding 	int max;
20710288eeaSThierry Reding 
20810288eeaSThierry Reding 	if (v)
20910288eeaSThierry Reding 		max = 15;
21010288eeaSThierry Reding 	else {
21110288eeaSThierry Reding 		switch (bpp) {
21210288eeaSThierry Reding 		case 2:
21310288eeaSThierry Reding 			max = 8;
21410288eeaSThierry Reding 			break;
21510288eeaSThierry Reding 
21610288eeaSThierry Reding 		default:
21710288eeaSThierry Reding 			WARN_ON_ONCE(1);
21810288eeaSThierry Reding 			/* fallthrough */
21910288eeaSThierry Reding 		case 4:
22010288eeaSThierry Reding 			max = 4;
22110288eeaSThierry Reding 			break;
22210288eeaSThierry Reding 		}
22310288eeaSThierry Reding 	}
22410288eeaSThierry Reding 
22510288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
22610288eeaSThierry Reding 	inf.full -= dfixed_const(1);
22710288eeaSThierry Reding 
22810288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
22910288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
23010288eeaSThierry Reding 
23110288eeaSThierry Reding 	return dda_inc;
23210288eeaSThierry Reding }
23310288eeaSThierry Reding 
23410288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
23510288eeaSThierry Reding {
23610288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
23710288eeaSThierry Reding 	return dfixed_frac(inf);
23810288eeaSThierry Reding }
23910288eeaSThierry Reding 
2404aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
24110288eeaSThierry Reding 				  const struct tegra_dc_window *window)
24210288eeaSThierry Reding {
24310288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
24493396d0fSSean Paul 	unsigned long value, flags;
24510288eeaSThierry Reding 	bool yuv, planar;
24610288eeaSThierry Reding 
24710288eeaSThierry Reding 	/*
24810288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
24910288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
25010288eeaSThierry Reding 	 */
25110288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
25210288eeaSThierry Reding 	if (!yuv)
25310288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
25410288eeaSThierry Reding 	else
25510288eeaSThierry Reding 		bpp = planar ? 1 : 2;
25610288eeaSThierry Reding 
25793396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
25893396d0fSSean Paul 
25910288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
26010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
26110288eeaSThierry Reding 
26210288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
26310288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
26410288eeaSThierry Reding 
26510288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
26610288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
26710288eeaSThierry Reding 
26810288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
26910288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
27010288eeaSThierry Reding 
27110288eeaSThierry Reding 	h_offset = window->src.x * bpp;
27210288eeaSThierry Reding 	v_offset = window->src.y;
27310288eeaSThierry Reding 	h_size = window->src.w * bpp;
27410288eeaSThierry Reding 	v_size = window->src.h;
27510288eeaSThierry Reding 
27610288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
27710288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
27810288eeaSThierry Reding 
27910288eeaSThierry Reding 	/*
28010288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
28110288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
28210288eeaSThierry Reding 	 */
28310288eeaSThierry Reding 	if (yuv && planar)
28410288eeaSThierry Reding 		bpp = 2;
28510288eeaSThierry Reding 
28610288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
28710288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
28810288eeaSThierry Reding 
28910288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
29010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
29110288eeaSThierry Reding 
29210288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
29310288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
29410288eeaSThierry Reding 
29510288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
29610288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
29710288eeaSThierry Reding 
29810288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
29910288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
30010288eeaSThierry Reding 
30110288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
30210288eeaSThierry Reding 
30310288eeaSThierry Reding 	if (yuv && planar) {
30410288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
30510288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
30610288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
30710288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
30810288eeaSThierry Reding 	} else {
30910288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
31010288eeaSThierry Reding 	}
31110288eeaSThierry Reding 
31210288eeaSThierry Reding 	if (window->bottom_up)
31310288eeaSThierry Reding 		v_offset += window->src.h - 1;
31410288eeaSThierry Reding 
31510288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
31610288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
31710288eeaSThierry Reding 
318c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
319c134f019SThierry Reding 		unsigned long height = window->tiling.value;
320c134f019SThierry Reding 
321c134f019SThierry Reding 		switch (window->tiling.mode) {
322c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
323c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
324c134f019SThierry Reding 			break;
325c134f019SThierry Reding 
326c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
327c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
328c134f019SThierry Reding 			break;
329c134f019SThierry Reding 
330c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
331c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
332c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
333c134f019SThierry Reding 			break;
334c134f019SThierry Reding 		}
335c134f019SThierry Reding 
336c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
33710288eeaSThierry Reding 	} else {
338c134f019SThierry Reding 		switch (window->tiling.mode) {
339c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
34010288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
34110288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
342c134f019SThierry Reding 			break;
343c134f019SThierry Reding 
344c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
345c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
346c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
347c134f019SThierry Reding 			break;
348c134f019SThierry Reding 
349c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3504aa3df71SThierry Reding 			/*
3514aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3524aa3df71SThierry Reding 			 * will already have filtered it out.
3534aa3df71SThierry Reding 			 */
3544aa3df71SThierry Reding 			break;
35510288eeaSThierry Reding 		}
35610288eeaSThierry Reding 
35710288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
358c134f019SThierry Reding 	}
35910288eeaSThierry Reding 
36010288eeaSThierry Reding 	value = WIN_ENABLE;
36110288eeaSThierry Reding 
36210288eeaSThierry Reding 	if (yuv) {
36310288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
36410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
36510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
36610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
36710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
36810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
36910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
37010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
37110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
37210288eeaSThierry Reding 
37310288eeaSThierry Reding 		value |= CSC_ENABLE;
37410288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
37510288eeaSThierry Reding 		value |= COLOR_EXPAND;
37610288eeaSThierry Reding 	}
37710288eeaSThierry Reding 
37810288eeaSThierry Reding 	if (window->bottom_up)
37910288eeaSThierry Reding 		value |= V_DIRECTION;
38010288eeaSThierry Reding 
38110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
38210288eeaSThierry Reding 
38310288eeaSThierry Reding 	/*
38410288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
38510288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
38610288eeaSThierry Reding 	 */
38710288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
38810288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
38910288eeaSThierry Reding 
39010288eeaSThierry Reding 	switch (index) {
39110288eeaSThierry Reding 	case 0:
39210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
39310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
39410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
39510288eeaSThierry Reding 		break;
39610288eeaSThierry Reding 
39710288eeaSThierry Reding 	case 1:
39810288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
39910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
40010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
40110288eeaSThierry Reding 		break;
40210288eeaSThierry Reding 
40310288eeaSThierry Reding 	case 2:
40410288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
40510288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
40610288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
40710288eeaSThierry Reding 		break;
40810288eeaSThierry Reding 	}
40910288eeaSThierry Reding 
41093396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
411c7679306SThierry Reding }
412c7679306SThierry Reding 
413c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
414c7679306SThierry Reding {
415c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
416c7679306SThierry Reding 
417c7679306SThierry Reding 	drm_plane_cleanup(plane);
418c7679306SThierry Reding 	kfree(p);
419c7679306SThierry Reding }
420c7679306SThierry Reding 
421c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
422c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
423c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
424c7679306SThierry Reding 	DRM_FORMAT_RGB565,
425c7679306SThierry Reding };
426c7679306SThierry Reding 
4274aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
428c7679306SThierry Reding {
4294aa3df71SThierry Reding 	tegra_plane_destroy(plane);
4304aa3df71SThierry Reding }
4314aa3df71SThierry Reding 
4328f604f8cSThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
4338f604f8cSThierry Reding {
4348f604f8cSThierry Reding 	struct tegra_plane_state *state;
4358f604f8cSThierry Reding 
4363b59b7acSThierry Reding 	if (plane->state)
437*2f701695SDaniel Vetter 		__drm_atomic_helper_plane_destroy_state(plane->state);
4388f604f8cSThierry Reding 
4398f604f8cSThierry Reding 	kfree(plane->state);
4408f604f8cSThierry Reding 	plane->state = NULL;
4418f604f8cSThierry Reding 
4428f604f8cSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
4438f604f8cSThierry Reding 	if (state) {
4448f604f8cSThierry Reding 		plane->state = &state->base;
4458f604f8cSThierry Reding 		plane->state->plane = plane;
4468f604f8cSThierry Reding 	}
4478f604f8cSThierry Reding }
4488f604f8cSThierry Reding 
4498f604f8cSThierry Reding static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
4508f604f8cSThierry Reding {
4518f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
4528f604f8cSThierry Reding 	struct tegra_plane_state *copy;
4538f604f8cSThierry Reding 
4543b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
4558f604f8cSThierry Reding 	if (!copy)
4568f604f8cSThierry Reding 		return NULL;
4578f604f8cSThierry Reding 
4583b59b7acSThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
4593b59b7acSThierry Reding 	copy->tiling = state->tiling;
4603b59b7acSThierry Reding 	copy->format = state->format;
4613b59b7acSThierry Reding 	copy->swap = state->swap;
4628f604f8cSThierry Reding 
4638f604f8cSThierry Reding 	return &copy->base;
4648f604f8cSThierry Reding }
4658f604f8cSThierry Reding 
4668f604f8cSThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
4678f604f8cSThierry Reding 					     struct drm_plane_state *state)
4688f604f8cSThierry Reding {
469*2f701695SDaniel Vetter 	__drm_atomic_helper_plane_destroy_state(state);
4708f604f8cSThierry Reding 	kfree(state);
4718f604f8cSThierry Reding }
4728f604f8cSThierry Reding 
4734aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
47407866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
47507866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
4764aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
4778f604f8cSThierry Reding 	.reset = tegra_plane_reset,
4788f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
4798f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
4804aa3df71SThierry Reding };
4814aa3df71SThierry Reding 
4824aa3df71SThierry Reding static int tegra_plane_prepare_fb(struct drm_plane *plane,
483d136dfeeSTvrtko Ursulin 				  const struct drm_plane_state *new_state)
4844aa3df71SThierry Reding {
4854aa3df71SThierry Reding 	return 0;
4864aa3df71SThierry Reding }
4874aa3df71SThierry Reding 
4884aa3df71SThierry Reding static void tegra_plane_cleanup_fb(struct drm_plane *plane,
489d136dfeeSTvrtko Ursulin 				   const struct drm_plane_state *old_fb)
4904aa3df71SThierry Reding {
4914aa3df71SThierry Reding }
4924aa3df71SThierry Reding 
49347802b09SThierry Reding static int tegra_plane_state_add(struct tegra_plane *plane,
49447802b09SThierry Reding 				 struct drm_plane_state *state)
49547802b09SThierry Reding {
49647802b09SThierry Reding 	struct drm_crtc_state *crtc_state;
49747802b09SThierry Reding 	struct tegra_dc_state *tegra;
49847802b09SThierry Reding 
49947802b09SThierry Reding 	/* Propagate errors from allocation or locking failures. */
50047802b09SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
50147802b09SThierry Reding 	if (IS_ERR(crtc_state))
50247802b09SThierry Reding 		return PTR_ERR(crtc_state);
50347802b09SThierry Reding 
50447802b09SThierry Reding 	tegra = to_dc_state(crtc_state);
50547802b09SThierry Reding 
50647802b09SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
50747802b09SThierry Reding 
50847802b09SThierry Reding 	return 0;
50947802b09SThierry Reding }
51047802b09SThierry Reding 
5114aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
5124aa3df71SThierry Reding 				    struct drm_plane_state *state)
5134aa3df71SThierry Reding {
5148f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
5158f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
51647802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
5174aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
518c7679306SThierry Reding 	int err;
519c7679306SThierry Reding 
5204aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
5214aa3df71SThierry Reding 	if (!state->crtc)
5224aa3df71SThierry Reding 		return 0;
5234aa3df71SThierry Reding 
5248f604f8cSThierry Reding 	err = tegra_dc_format(state->fb->pixel_format, &plane_state->format,
5258f604f8cSThierry Reding 			      &plane_state->swap);
5264aa3df71SThierry Reding 	if (err < 0)
5274aa3df71SThierry Reding 		return err;
5284aa3df71SThierry Reding 
5298f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
5308f604f8cSThierry Reding 	if (err < 0)
5318f604f8cSThierry Reding 		return err;
5328f604f8cSThierry Reding 
5338f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
5344aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
5354aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
5364aa3df71SThierry Reding 		return -EINVAL;
5374aa3df71SThierry Reding 	}
5384aa3df71SThierry Reding 
5394aa3df71SThierry Reding 	/*
5404aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
5414aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
5424aa3df71SThierry Reding 	 * configuration.
5434aa3df71SThierry Reding 	 */
5444aa3df71SThierry Reding 	if (drm_format_num_planes(state->fb->pixel_format) > 2) {
5454aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
5464aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
5474aa3df71SThierry Reding 			return -EINVAL;
5484aa3df71SThierry Reding 		}
5494aa3df71SThierry Reding 	}
5504aa3df71SThierry Reding 
55147802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
55247802b09SThierry Reding 	if (err < 0)
55347802b09SThierry Reding 		return err;
55447802b09SThierry Reding 
5554aa3df71SThierry Reding 	return 0;
5564aa3df71SThierry Reding }
5574aa3df71SThierry Reding 
5584aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
5594aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
5604aa3df71SThierry Reding {
5618f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
5624aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5634aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
5644aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
5654aa3df71SThierry Reding 	struct tegra_dc_window window;
5664aa3df71SThierry Reding 	unsigned int i;
5674aa3df71SThierry Reding 
5684aa3df71SThierry Reding 	/* rien ne va plus */
5694aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5704aa3df71SThierry Reding 		return;
5714aa3df71SThierry Reding 
572c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
5734aa3df71SThierry Reding 	window.src.x = plane->state->src_x >> 16;
5744aa3df71SThierry Reding 	window.src.y = plane->state->src_y >> 16;
5754aa3df71SThierry Reding 	window.src.w = plane->state->src_w >> 16;
5764aa3df71SThierry Reding 	window.src.h = plane->state->src_h >> 16;
5774aa3df71SThierry Reding 	window.dst.x = plane->state->crtc_x;
5784aa3df71SThierry Reding 	window.dst.y = plane->state->crtc_y;
5794aa3df71SThierry Reding 	window.dst.w = plane->state->crtc_w;
5804aa3df71SThierry Reding 	window.dst.h = plane->state->crtc_h;
581c7679306SThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
582c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
583c7679306SThierry Reding 
5848f604f8cSThierry Reding 	/* copy from state */
5858f604f8cSThierry Reding 	window.tiling = state->tiling;
5868f604f8cSThierry Reding 	window.format = state->format;
5878f604f8cSThierry Reding 	window.swap = state->swap;
588c7679306SThierry Reding 
5894aa3df71SThierry Reding 	for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
5904aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
591c7679306SThierry Reding 
5924aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
5934aa3df71SThierry Reding 		window.stride[i] = fb->pitches[i];
594c7679306SThierry Reding 	}
595c7679306SThierry Reding 
5964aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
5974aa3df71SThierry Reding }
5984aa3df71SThierry Reding 
5994aa3df71SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
6004aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
601c7679306SThierry Reding {
6024aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
6034aa3df71SThierry Reding 	struct tegra_dc *dc;
6044aa3df71SThierry Reding 	unsigned long flags;
6054aa3df71SThierry Reding 	u32 value;
6064aa3df71SThierry Reding 
6074aa3df71SThierry Reding 	/* rien ne va plus */
6084aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
6094aa3df71SThierry Reding 		return;
6104aa3df71SThierry Reding 
6114aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
6124aa3df71SThierry Reding 
6134aa3df71SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
6144aa3df71SThierry Reding 
6154aa3df71SThierry Reding 	value = WINDOW_A_SELECT << p->index;
6164aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
6174aa3df71SThierry Reding 
6184aa3df71SThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
6194aa3df71SThierry Reding 	value &= ~WIN_ENABLE;
6204aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
6214aa3df71SThierry Reding 
6224aa3df71SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
623c7679306SThierry Reding }
624c7679306SThierry Reding 
6254aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
6264aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
6274aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
6284aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
6294aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
6304aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
631c7679306SThierry Reding };
632c7679306SThierry Reding 
633c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
634c7679306SThierry Reding 						       struct tegra_dc *dc)
635c7679306SThierry Reding {
636518e6227SThierry Reding 	/*
637518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
638518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
639518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
640518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
641518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
642518e6227SThierry Reding 	 * here.
643518e6227SThierry Reding 	 *
644518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
645518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
646518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
647518e6227SThierry Reding 	 */
648518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
649c7679306SThierry Reding 	struct tegra_plane *plane;
650c7679306SThierry Reding 	unsigned int num_formats;
651c7679306SThierry Reding 	const u32 *formats;
652c7679306SThierry Reding 	int err;
653c7679306SThierry Reding 
654c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
655c7679306SThierry Reding 	if (!plane)
656c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
657c7679306SThierry Reding 
658c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
659c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
660c7679306SThierry Reding 
661518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
662c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
663b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_PRIMARY,
664b0b3b795SVille Syrjälä 				       NULL);
665c7679306SThierry Reding 	if (err < 0) {
666c7679306SThierry Reding 		kfree(plane);
667c7679306SThierry Reding 		return ERR_PTR(err);
668c7679306SThierry Reding 	}
669c7679306SThierry Reding 
6704aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
6714aa3df71SThierry Reding 
672c7679306SThierry Reding 	return &plane->base;
673c7679306SThierry Reding }
674c7679306SThierry Reding 
675c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
676c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
677c7679306SThierry Reding };
678c7679306SThierry Reding 
6794aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
6804aa3df71SThierry Reding 				     struct drm_plane_state *state)
681c7679306SThierry Reding {
68247802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
68347802b09SThierry Reding 	int err;
68447802b09SThierry Reding 
6854aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6864aa3df71SThierry Reding 	if (!state->crtc)
6874aa3df71SThierry Reding 		return 0;
688c7679306SThierry Reding 
689c7679306SThierry Reding 	/* scaling not supported for cursor */
6904aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
6914aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
692c7679306SThierry Reding 		return -EINVAL;
693c7679306SThierry Reding 
694c7679306SThierry Reding 	/* only square cursors supported */
6954aa3df71SThierry Reding 	if (state->src_w != state->src_h)
696c7679306SThierry Reding 		return -EINVAL;
697c7679306SThierry Reding 
6984aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
6994aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
7004aa3df71SThierry Reding 		return -EINVAL;
7014aa3df71SThierry Reding 
70247802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
70347802b09SThierry Reding 	if (err < 0)
70447802b09SThierry Reding 		return err;
70547802b09SThierry Reding 
7064aa3df71SThierry Reding 	return 0;
7074aa3df71SThierry Reding }
7084aa3df71SThierry Reding 
7094aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
7104aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
7114aa3df71SThierry Reding {
7124aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
7134aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
7144aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
7154aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
7164aa3df71SThierry Reding 
7174aa3df71SThierry Reding 	/* rien ne va plus */
7184aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
7194aa3df71SThierry Reding 		return;
7204aa3df71SThierry Reding 
7214aa3df71SThierry Reding 	switch (state->crtc_w) {
722c7679306SThierry Reding 	case 32:
723c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
724c7679306SThierry Reding 		break;
725c7679306SThierry Reding 
726c7679306SThierry Reding 	case 64:
727c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
728c7679306SThierry Reding 		break;
729c7679306SThierry Reding 
730c7679306SThierry Reding 	case 128:
731c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
732c7679306SThierry Reding 		break;
733c7679306SThierry Reding 
734c7679306SThierry Reding 	case 256:
735c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
736c7679306SThierry Reding 		break;
737c7679306SThierry Reding 
738c7679306SThierry Reding 	default:
7394aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
7404aa3df71SThierry Reding 		     state->crtc_h);
7414aa3df71SThierry Reding 		return;
742c7679306SThierry Reding 	}
743c7679306SThierry Reding 
744c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
745c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
746c7679306SThierry Reding 
747c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
748c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
749c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
750c7679306SThierry Reding #endif
751c7679306SThierry Reding 
752c7679306SThierry Reding 	/* enable cursor and set blend mode */
753c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
754c7679306SThierry Reding 	value |= CURSOR_ENABLE;
755c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
756c7679306SThierry Reding 
757c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
758c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
759c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
760c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
761c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
762c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
763c7679306SThierry Reding 	value |= CURSOR_ALPHA;
764c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
765c7679306SThierry Reding 
766c7679306SThierry Reding 	/* position the cursor */
7674aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
768c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
769c7679306SThierry Reding }
770c7679306SThierry Reding 
7714aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
7724aa3df71SThierry Reding 					struct drm_plane_state *old_state)
773c7679306SThierry Reding {
7744aa3df71SThierry Reding 	struct tegra_dc *dc;
775c7679306SThierry Reding 	u32 value;
776c7679306SThierry Reding 
7774aa3df71SThierry Reding 	/* rien ne va plus */
7784aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
7794aa3df71SThierry Reding 		return;
7804aa3df71SThierry Reding 
7814aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
782c7679306SThierry Reding 
783c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
784c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
785c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
786c7679306SThierry Reding }
787c7679306SThierry Reding 
788c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
78907866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
79007866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
791c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
7928f604f8cSThierry Reding 	.reset = tegra_plane_reset,
7938f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
7948f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
7954aa3df71SThierry Reding };
7964aa3df71SThierry Reding 
7974aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
7984aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
7994aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
8004aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
8014aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
8024aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
803c7679306SThierry Reding };
804c7679306SThierry Reding 
805c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
806c7679306SThierry Reding 						      struct tegra_dc *dc)
807c7679306SThierry Reding {
808c7679306SThierry Reding 	struct tegra_plane *plane;
809c7679306SThierry Reding 	unsigned int num_formats;
810c7679306SThierry Reding 	const u32 *formats;
811c7679306SThierry Reding 	int err;
812c7679306SThierry Reding 
813c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
814c7679306SThierry Reding 	if (!plane)
815c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
816c7679306SThierry Reding 
81747802b09SThierry Reding 	/*
818a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
819a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
820a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
821a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
822a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
82347802b09SThierry Reding 	 */
82447802b09SThierry Reding 	plane->index = 6;
82547802b09SThierry Reding 
826c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
827c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
828c7679306SThierry Reding 
829c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
830c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
831b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_CURSOR,
832b0b3b795SVille Syrjälä 				       NULL);
833c7679306SThierry Reding 	if (err < 0) {
834c7679306SThierry Reding 		kfree(plane);
835c7679306SThierry Reding 		return ERR_PTR(err);
836c7679306SThierry Reding 	}
837c7679306SThierry Reding 
8384aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
8394aa3df71SThierry Reding 
840c7679306SThierry Reding 	return &plane->base;
841c7679306SThierry Reding }
842c7679306SThierry Reding 
843c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
844dee8268fSThierry Reding {
845c7679306SThierry Reding 	tegra_plane_destroy(plane);
846dee8268fSThierry Reding }
847dee8268fSThierry Reding 
848c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
84907866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
85007866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
851c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
8528f604f8cSThierry Reding 	.reset = tegra_plane_reset,
8538f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
8548f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
855dee8268fSThierry Reding };
856dee8268fSThierry Reding 
857c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
858dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
859dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
860dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
861dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
862f925390eSThierry Reding 	DRM_FORMAT_YUYV,
863dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
864dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
865dee8268fSThierry Reding };
866dee8268fSThierry Reding 
8674aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
8684aa3df71SThierry Reding 	.prepare_fb = tegra_plane_prepare_fb,
8694aa3df71SThierry Reding 	.cleanup_fb = tegra_plane_cleanup_fb,
8704aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
8714aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
8724aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
8734aa3df71SThierry Reding };
8744aa3df71SThierry Reding 
875c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
876c7679306SThierry Reding 						       struct tegra_dc *dc,
877c7679306SThierry Reding 						       unsigned int index)
878dee8268fSThierry Reding {
879dee8268fSThierry Reding 	struct tegra_plane *plane;
880c7679306SThierry Reding 	unsigned int num_formats;
881c7679306SThierry Reding 	const u32 *formats;
882c7679306SThierry Reding 	int err;
883dee8268fSThierry Reding 
884f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
885dee8268fSThierry Reding 	if (!plane)
886c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
887dee8268fSThierry Reding 
888c7679306SThierry Reding 	plane->index = index;
889dee8268fSThierry Reding 
890c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
891c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
892c7679306SThierry Reding 
893c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
894c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
895b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_OVERLAY,
896b0b3b795SVille Syrjälä 				       NULL);
897f002abc1SThierry Reding 	if (err < 0) {
898f002abc1SThierry Reding 		kfree(plane);
899c7679306SThierry Reding 		return ERR_PTR(err);
900dee8268fSThierry Reding 	}
901c7679306SThierry Reding 
9024aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
9034aa3df71SThierry Reding 
904c7679306SThierry Reding 	return &plane->base;
905c7679306SThierry Reding }
906c7679306SThierry Reding 
907c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
908c7679306SThierry Reding {
909c7679306SThierry Reding 	struct drm_plane *plane;
910c7679306SThierry Reding 	unsigned int i;
911c7679306SThierry Reding 
912c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
913c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
914c7679306SThierry Reding 		if (IS_ERR(plane))
915c7679306SThierry Reding 			return PTR_ERR(plane);
916f002abc1SThierry Reding 	}
917dee8268fSThierry Reding 
918dee8268fSThierry Reding 	return 0;
919dee8268fSThierry Reding }
920dee8268fSThierry Reding 
92142e9ce05SThierry Reding u32 tegra_dc_get_vblank_counter(struct tegra_dc *dc)
92242e9ce05SThierry Reding {
92342e9ce05SThierry Reding 	if (dc->syncpt)
92442e9ce05SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
92542e9ce05SThierry Reding 
92642e9ce05SThierry Reding 	/* fallback to software emulated VBLANK counter */
92742e9ce05SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
92842e9ce05SThierry Reding }
92942e9ce05SThierry Reding 
930dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
931dee8268fSThierry Reding {
932dee8268fSThierry Reding 	unsigned long value, flags;
933dee8268fSThierry Reding 
934dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
935dee8268fSThierry Reding 
936dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
937dee8268fSThierry Reding 	value |= VBLANK_INT;
938dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
939dee8268fSThierry Reding 
940dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
941dee8268fSThierry Reding }
942dee8268fSThierry Reding 
943dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
944dee8268fSThierry Reding {
945dee8268fSThierry Reding 	unsigned long value, flags;
946dee8268fSThierry Reding 
947dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
948dee8268fSThierry Reding 
949dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
950dee8268fSThierry Reding 	value &= ~VBLANK_INT;
951dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
952dee8268fSThierry Reding 
953dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
954dee8268fSThierry Reding }
955dee8268fSThierry Reding 
956dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
957dee8268fSThierry Reding {
958dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
959dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
960dee8268fSThierry Reding 	unsigned long flags, base;
961dee8268fSThierry Reding 	struct tegra_bo *bo;
962dee8268fSThierry Reding 
9636b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
9646b59cc1cSThierry Reding 
9656b59cc1cSThierry Reding 	if (!dc->event) {
9666b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
967dee8268fSThierry Reding 		return;
9686b59cc1cSThierry Reding 	}
969dee8268fSThierry Reding 
970f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
971dee8268fSThierry Reding 
9728643bc6dSDan Carpenter 	spin_lock(&dc->lock);
97393396d0fSSean Paul 
974dee8268fSThierry Reding 	/* check if new start address has been latched */
97593396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
976dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
977dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
978dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
979dee8268fSThierry Reding 
9808643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
98193396d0fSSean Paul 
982f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
983ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
984ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
985dee8268fSThierry Reding 		dc->event = NULL;
986dee8268fSThierry Reding 	}
9876b59cc1cSThierry Reding 
9886b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
989dee8268fSThierry Reding }
990dee8268fSThierry Reding 
991f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
992f002abc1SThierry Reding {
993f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
994f002abc1SThierry Reding }
995f002abc1SThierry Reding 
996ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
997ca915b10SThierry Reding {
998ca915b10SThierry Reding 	struct tegra_dc_state *state;
999ca915b10SThierry Reding 
10003b59b7acSThierry Reding 	if (crtc->state)
1001ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
10023b59b7acSThierry Reding 
1003ca915b10SThierry Reding 	kfree(crtc->state);
1004ca915b10SThierry Reding 	crtc->state = NULL;
1005ca915b10SThierry Reding 
1006ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1007332bbe70SThierry Reding 	if (state) {
1008ca915b10SThierry Reding 		crtc->state = &state->base;
1009332bbe70SThierry Reding 		crtc->state->crtc = crtc;
1010332bbe70SThierry Reding 	}
101131930d4dSThierry Reding 
101231930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
1013ca915b10SThierry Reding }
1014ca915b10SThierry Reding 
1015ca915b10SThierry Reding static struct drm_crtc_state *
1016ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1017ca915b10SThierry Reding {
1018ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1019ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1020ca915b10SThierry Reding 
10213b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1022ca915b10SThierry Reding 	if (!copy)
1023ca915b10SThierry Reding 		return NULL;
1024ca915b10SThierry Reding 
10253b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
10263b59b7acSThierry Reding 	copy->clk = state->clk;
10273b59b7acSThierry Reding 	copy->pclk = state->pclk;
10283b59b7acSThierry Reding 	copy->div = state->div;
10293b59b7acSThierry Reding 	copy->planes = state->planes;
1030ca915b10SThierry Reding 
1031ca915b10SThierry Reding 	return &copy->base;
1032ca915b10SThierry Reding }
1033ca915b10SThierry Reding 
1034ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1035ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1036ca915b10SThierry Reding {
1037ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1038ca915b10SThierry Reding 	kfree(state);
1039ca915b10SThierry Reding }
1040ca915b10SThierry Reding 
1041dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
10421503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
104374f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1044f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1045ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1046ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1047ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1048dee8268fSThierry Reding };
1049dee8268fSThierry Reding 
1050dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1051dee8268fSThierry Reding 				struct drm_display_mode *mode)
1052dee8268fSThierry Reding {
10530444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10540444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1055dee8268fSThierry Reding 	unsigned long value;
1056dee8268fSThierry Reding 
1057dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1058dee8268fSThierry Reding 
1059dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1060dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1061dee8268fSThierry Reding 
1062dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1063dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1064dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1065dee8268fSThierry Reding 
1066dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1067dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1068dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1069dee8268fSThierry Reding 
1070dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1071dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1072dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1073dee8268fSThierry Reding 
1074dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1075dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1076dee8268fSThierry Reding 
1077dee8268fSThierry Reding 	return 0;
1078dee8268fSThierry Reding }
1079dee8268fSThierry Reding 
10809d910b60SThierry Reding /**
10819d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
10829d910b60SThierry Reding  *     state
10839d910b60SThierry Reding  * @dc: display controller
10849d910b60SThierry Reding  * @crtc_state: CRTC atomic state
10859d910b60SThierry Reding  * @clk: parent clock for display controller
10869d910b60SThierry Reding  * @pclk: pixel clock
10879d910b60SThierry Reding  * @div: shift clock divider
10889d910b60SThierry Reding  *
10899d910b60SThierry Reding  * Returns:
10909d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
10919d910b60SThierry Reding  */
1092ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1093ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1094ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1095ca915b10SThierry Reding 			       unsigned int div)
1096ca915b10SThierry Reding {
1097ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1098ca915b10SThierry Reding 
1099d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1100d2982748SThierry Reding 		return -EINVAL;
1101d2982748SThierry Reding 
1102ca915b10SThierry Reding 	state->clk = clk;
1103ca915b10SThierry Reding 	state->pclk = pclk;
1104ca915b10SThierry Reding 	state->div = div;
1105ca915b10SThierry Reding 
1106ca915b10SThierry Reding 	return 0;
1107ca915b10SThierry Reding }
1108ca915b10SThierry Reding 
110976d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
111076d59ed0SThierry Reding 				  struct tegra_dc_state *state)
111176d59ed0SThierry Reding {
111276d59ed0SThierry Reding 	u32 value;
111376d59ed0SThierry Reding 	int err;
111476d59ed0SThierry Reding 
111576d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
111676d59ed0SThierry Reding 	if (err < 0)
111776d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
111876d59ed0SThierry Reding 
111976d59ed0SThierry Reding 	/*
112076d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
112176d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
112276d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
112376d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
112476d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
112576d59ed0SThierry Reding 	 * should therefore be avoided.
112676d59ed0SThierry Reding 	 */
112776d59ed0SThierry Reding 	if (state->pclk > 0) {
112876d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
112976d59ed0SThierry Reding 		if (err < 0)
113076d59ed0SThierry Reding 			dev_err(dc->dev,
113176d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
113276d59ed0SThierry Reding 				state->pclk);
113376d59ed0SThierry Reding 	}
113476d59ed0SThierry Reding 
113576d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
113676d59ed0SThierry Reding 		      state->div);
113776d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
113876d59ed0SThierry Reding 
113976d59ed0SThierry Reding 	value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
114076d59ed0SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
114176d59ed0SThierry Reding }
114276d59ed0SThierry Reding 
1143003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1144003fc848SThierry Reding {
1145003fc848SThierry Reding 	u32 value;
1146003fc848SThierry Reding 
1147003fc848SThierry Reding 	/* stop the display controller */
1148003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1149003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1150003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1151003fc848SThierry Reding 
1152003fc848SThierry Reding 	tegra_dc_commit(dc);
1153003fc848SThierry Reding }
1154003fc848SThierry Reding 
1155003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1156003fc848SThierry Reding {
1157003fc848SThierry Reding 	u32 value;
1158003fc848SThierry Reding 
1159003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1160003fc848SThierry Reding 
1161003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1162003fc848SThierry Reding }
1163003fc848SThierry Reding 
1164003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1165003fc848SThierry Reding {
1166003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1167003fc848SThierry Reding 
1168003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1169003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1170003fc848SThierry Reding 			return 0;
1171003fc848SThierry Reding 
1172003fc848SThierry Reding 		usleep_range(1000, 2000);
1173003fc848SThierry Reding 	}
1174003fc848SThierry Reding 
1175003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1176003fc848SThierry Reding 	return -ETIMEDOUT;
1177003fc848SThierry Reding }
1178003fc848SThierry Reding 
1179003fc848SThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
1180003fc848SThierry Reding {
1181003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1182003fc848SThierry Reding 	u32 value;
1183003fc848SThierry Reding 
1184003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1185003fc848SThierry Reding 		tegra_dc_stop(dc);
1186003fc848SThierry Reding 
1187003fc848SThierry Reding 		/*
1188003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1189003fc848SThierry Reding 		 * in case this fails.
1190003fc848SThierry Reding 		 */
1191003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1192003fc848SThierry Reding 	}
1193003fc848SThierry Reding 
1194003fc848SThierry Reding 	/*
1195003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1196003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1197003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1198003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1199003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1200003fc848SThierry Reding 	 * to go idle.
1201003fc848SThierry Reding 	 *
1202003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1203003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1204003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1205003fc848SThierry Reding 	 *
1206003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1207003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1208003fc848SThierry Reding 	 * the RGB encoder?
1209003fc848SThierry Reding 	 */
1210003fc848SThierry Reding 	if (dc->rgb) {
1211003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1212003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1213003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1214003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1215003fc848SThierry Reding 	}
1216003fc848SThierry Reding 
1217003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1218003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
1219003fc848SThierry Reding }
1220003fc848SThierry Reding 
1221003fc848SThierry Reding static void tegra_crtc_enable(struct drm_crtc *crtc)
1222dee8268fSThierry Reding {
12234aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
122476d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1225dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1226dbb3f2f7SThierry Reding 	u32 value;
1227dee8268fSThierry Reding 
122876d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
122976d59ed0SThierry Reding 
1230dee8268fSThierry Reding 	/* program display mode */
1231dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1232dee8268fSThierry Reding 
12338620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
12348620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
12358620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
12368620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
12378620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
12388620fc62SThierry Reding 	}
1239666cb873SThierry Reding 
1240666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1241666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1242666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1243666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1244666cb873SThierry Reding 
1245666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1246666cb873SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1247666cb873SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1248666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1249666cb873SThierry Reding 
1250666cb873SThierry Reding 	tegra_dc_commit(dc);
1251dee8268fSThierry Reding 
12528ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1253dee8268fSThierry Reding }
1254dee8268fSThierry Reding 
12554aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
12564aa3df71SThierry Reding 				   struct drm_crtc_state *state)
12574aa3df71SThierry Reding {
12584aa3df71SThierry Reding 	return 0;
12594aa3df71SThierry Reding }
12604aa3df71SThierry Reding 
1261613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1262613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
12634aa3df71SThierry Reding {
12641503ca47SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
12651503ca47SThierry Reding 
12661503ca47SThierry Reding 	if (crtc->state->event) {
12671503ca47SThierry Reding 		crtc->state->event->pipe = drm_crtc_index(crtc);
12681503ca47SThierry Reding 
12691503ca47SThierry Reding 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
12701503ca47SThierry Reding 
12711503ca47SThierry Reding 		dc->event = crtc->state->event;
12721503ca47SThierry Reding 		crtc->state->event = NULL;
12731503ca47SThierry Reding 	}
12744aa3df71SThierry Reding }
12754aa3df71SThierry Reding 
1276613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1277613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
12784aa3df71SThierry Reding {
127947802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
128047802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
128147802b09SThierry Reding 
128247802b09SThierry Reding 	tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
128347802b09SThierry Reding 	tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
12844aa3df71SThierry Reding }
12854aa3df71SThierry Reding 
1286dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
1287dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
1288003fc848SThierry Reding 	.enable = tegra_crtc_enable,
12894aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
12904aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
12914aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
1292dee8268fSThierry Reding };
1293dee8268fSThierry Reding 
1294dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1295dee8268fSThierry Reding {
1296dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1297dee8268fSThierry Reding 	unsigned long status;
1298dee8268fSThierry Reding 
1299dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1300dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1301dee8268fSThierry Reding 
1302dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1303dee8268fSThierry Reding 		/*
1304dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1305dee8268fSThierry Reding 		*/
1306791ddb1eSThierry Reding 		dc->stats.frames++;
1307dee8268fSThierry Reding 	}
1308dee8268fSThierry Reding 
1309dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1310dee8268fSThierry Reding 		/*
1311dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1312dee8268fSThierry Reding 		*/
1313ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1314dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1315791ddb1eSThierry Reding 		dc->stats.vblank++;
1316dee8268fSThierry Reding 	}
1317dee8268fSThierry Reding 
1318dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1319dee8268fSThierry Reding 		/*
1320dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1321dee8268fSThierry Reding 		*/
1322791ddb1eSThierry Reding 		dc->stats.underflow++;
1323791ddb1eSThierry Reding 	}
1324791ddb1eSThierry Reding 
1325791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1326791ddb1eSThierry Reding 		/*
1327791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1328791ddb1eSThierry Reding 		*/
1329791ddb1eSThierry Reding 		dc->stats.overflow++;
1330dee8268fSThierry Reding 	}
1331dee8268fSThierry Reding 
1332dee8268fSThierry Reding 	return IRQ_HANDLED;
1333dee8268fSThierry Reding }
1334dee8268fSThierry Reding 
1335dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1336dee8268fSThierry Reding {
1337dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1338dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1339003fc848SThierry Reding 	int err = 0;
1340003fc848SThierry Reding 
1341003fc848SThierry Reding 	drm_modeset_lock_crtc(&dc->base, NULL);
1342003fc848SThierry Reding 
1343003fc848SThierry Reding 	if (!dc->base.state->active) {
1344003fc848SThierry Reding 		err = -EBUSY;
1345003fc848SThierry Reding 		goto unlock;
1346003fc848SThierry Reding 	}
1347dee8268fSThierry Reding 
1348dee8268fSThierry Reding #define DUMP_REG(name)						\
134903a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1350dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1351dee8268fSThierry Reding 
1352dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1353dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1354dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1355dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1356dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1357dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1358dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1359dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1360dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1361dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1362dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1363dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1364dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1365dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1366dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1367dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1368dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1369dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1370dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1371dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1372dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1373dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1374dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1375dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1376dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1377dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1378dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1379dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1380dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1381dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1382dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1383dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1384dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1385dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1386dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1387dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1388dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1389dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1390dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1391dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1392dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1393dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1394dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1395dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1396dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1397dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1398dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1399dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1400dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1401dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1402dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1403dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1404dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1405dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1406dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1407dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1408dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1409dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1410dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1411dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1412dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1413dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1414dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1415dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1416dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1417dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1418dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1419dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1420dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1421dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1422dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1423dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1424dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1425dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1426dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1427dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1428dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1429dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1430dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1431dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1432dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1433dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1434dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1435dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1436dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1437dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1438dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1439dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1440dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1441dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1442dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1443dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1444dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1445dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1446dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1447dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1448dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1449dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1450dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1451dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1452dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1453dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1454dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1455dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1456dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1457dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1458dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1459dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1460dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1461dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1462dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1463dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1464dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1465dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1466dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1467dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1468dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1469dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1470dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1471dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1472dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1473dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1474dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1475dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1476dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1477dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1478dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1479dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1480dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1481dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1482dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1483dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1484dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1485dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1486dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1487dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1488dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1489dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1490dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1491dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1492dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1493dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1494dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1495dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1496dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1497dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1498dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1499dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1500dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1501dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1502dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1503dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1504dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1505dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1506dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1507dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1508dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1509dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1510dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1511dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1512dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1513dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1514dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1515dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1516dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1517dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1518dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1519dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1520dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1521dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1522dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1523dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1524dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1525dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1526dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1527e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1528e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1529dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1530dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1531dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1532dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1533dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1534dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1535dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1536dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1537dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1538dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1539dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1540dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1541dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1542dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1543dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1544dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1545dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1546dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1547dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1548dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1549dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1550dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1551dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1552dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1553dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1554dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1555dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1556dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1557dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1558dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1559dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1560dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1561dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1562dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1563dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1564dee8268fSThierry Reding 
1565dee8268fSThierry Reding #undef DUMP_REG
1566dee8268fSThierry Reding 
1567003fc848SThierry Reding unlock:
1568003fc848SThierry Reding 	drm_modeset_unlock_crtc(&dc->base);
1569003fc848SThierry Reding 	return err;
1570dee8268fSThierry Reding }
1571dee8268fSThierry Reding 
15726ca1f62fSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
15736ca1f62fSThierry Reding {
15746ca1f62fSThierry Reding 	struct drm_info_node *node = s->private;
15756ca1f62fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1576003fc848SThierry Reding 	int err = 0;
15776ca1f62fSThierry Reding 	u32 value;
15786ca1f62fSThierry Reding 
1579003fc848SThierry Reding 	drm_modeset_lock_crtc(&dc->base, NULL);
1580003fc848SThierry Reding 
1581003fc848SThierry Reding 	if (!dc->base.state->active) {
1582003fc848SThierry Reding 		err = -EBUSY;
1583003fc848SThierry Reding 		goto unlock;
1584003fc848SThierry Reding 	}
1585003fc848SThierry Reding 
15866ca1f62fSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
15876ca1f62fSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
15886ca1f62fSThierry Reding 	tegra_dc_commit(dc);
15896ca1f62fSThierry Reding 
15906ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
15916ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
15926ca1f62fSThierry Reding 
15936ca1f62fSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
15946ca1f62fSThierry Reding 	seq_printf(s, "%08x\n", value);
15956ca1f62fSThierry Reding 
15966ca1f62fSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
15976ca1f62fSThierry Reding 
1598003fc848SThierry Reding unlock:
1599003fc848SThierry Reding 	drm_modeset_unlock_crtc(&dc->base);
1600003fc848SThierry Reding 	return err;
16016ca1f62fSThierry Reding }
16026ca1f62fSThierry Reding 
1603791ddb1eSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1604791ddb1eSThierry Reding {
1605791ddb1eSThierry Reding 	struct drm_info_node *node = s->private;
1606791ddb1eSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1607791ddb1eSThierry Reding 
1608791ddb1eSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1609791ddb1eSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1610791ddb1eSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1611791ddb1eSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1612791ddb1eSThierry Reding 
1613dee8268fSThierry Reding 	return 0;
1614dee8268fSThierry Reding }
1615dee8268fSThierry Reding 
1616dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1617dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
16186ca1f62fSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1619791ddb1eSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1620dee8268fSThierry Reding };
1621dee8268fSThierry Reding 
1622dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1623dee8268fSThierry Reding {
1624dee8268fSThierry Reding 	unsigned int i;
1625dee8268fSThierry Reding 	char *name;
1626dee8268fSThierry Reding 	int err;
1627dee8268fSThierry Reding 
1628dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1629dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1630dee8268fSThierry Reding 	kfree(name);
1631dee8268fSThierry Reding 
1632dee8268fSThierry Reding 	if (!dc->debugfs)
1633dee8268fSThierry Reding 		return -ENOMEM;
1634dee8268fSThierry Reding 
1635dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1636dee8268fSThierry Reding 				    GFP_KERNEL);
1637dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1638dee8268fSThierry Reding 		err = -ENOMEM;
1639dee8268fSThierry Reding 		goto remove;
1640dee8268fSThierry Reding 	}
1641dee8268fSThierry Reding 
1642dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1643dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1644dee8268fSThierry Reding 
1645dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1646dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1647dee8268fSThierry Reding 				       dc->debugfs, minor);
1648dee8268fSThierry Reding 	if (err < 0)
1649dee8268fSThierry Reding 		goto free;
1650dee8268fSThierry Reding 
1651dee8268fSThierry Reding 	dc->minor = minor;
1652dee8268fSThierry Reding 
1653dee8268fSThierry Reding 	return 0;
1654dee8268fSThierry Reding 
1655dee8268fSThierry Reding free:
1656dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1657dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1658dee8268fSThierry Reding remove:
1659dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1660dee8268fSThierry Reding 	dc->debugfs = NULL;
1661dee8268fSThierry Reding 
1662dee8268fSThierry Reding 	return err;
1663dee8268fSThierry Reding }
1664dee8268fSThierry Reding 
1665dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1666dee8268fSThierry Reding {
1667dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1668dee8268fSThierry Reding 				 dc->minor);
1669dee8268fSThierry Reding 	dc->minor = NULL;
1670dee8268fSThierry Reding 
1671dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1672dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1673dee8268fSThierry Reding 
1674dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1675dee8268fSThierry Reding 	dc->debugfs = NULL;
1676dee8268fSThierry Reding 
1677dee8268fSThierry Reding 	return 0;
1678dee8268fSThierry Reding }
1679dee8268fSThierry Reding 
1680dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1681dee8268fSThierry Reding {
16829910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
16832bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1684dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1685d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1686c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1687c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
168807d05cbfSThierry Reding 	u32 value;
1689dee8268fSThierry Reding 	int err;
1690dee8268fSThierry Reding 
16912bcdcbfaSThierry Reding 	dc->syncpt = host1x_syncpt_request(dc->dev, flags);
16922bcdcbfaSThierry Reding 	if (!dc->syncpt)
16932bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
16942bcdcbfaSThierry Reding 
1695df06b759SThierry Reding 	if (tegra->domain) {
1696df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1697df06b759SThierry Reding 		if (err < 0) {
1698df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1699df06b759SThierry Reding 				err);
1700df06b759SThierry Reding 			return err;
1701df06b759SThierry Reding 		}
1702df06b759SThierry Reding 
1703df06b759SThierry Reding 		dc->domain = tegra->domain;
1704df06b759SThierry Reding 	}
1705df06b759SThierry Reding 
1706c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1707c7679306SThierry Reding 	if (IS_ERR(primary)) {
1708c7679306SThierry Reding 		err = PTR_ERR(primary);
1709c7679306SThierry Reding 		goto cleanup;
1710c7679306SThierry Reding 	}
1711c7679306SThierry Reding 
1712c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1713c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1714c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1715c7679306SThierry Reding 			err = PTR_ERR(cursor);
1716c7679306SThierry Reding 			goto cleanup;
1717c7679306SThierry Reding 		}
1718c7679306SThierry Reding 	}
1719c7679306SThierry Reding 
1720c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1721f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1722c7679306SThierry Reding 	if (err < 0)
1723c7679306SThierry Reding 		goto cleanup;
1724c7679306SThierry Reding 
1725dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1726dee8268fSThierry Reding 
1727d1f3e1e0SThierry Reding 	/*
1728d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1729d1f3e1e0SThierry Reding 	 * controllers.
1730d1f3e1e0SThierry Reding 	 */
1731d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1732d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1733d1f3e1e0SThierry Reding 
17349910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1735dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1736dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1737c7679306SThierry Reding 		goto cleanup;
1738dee8268fSThierry Reding 	}
1739dee8268fSThierry Reding 
17409910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1741dee8268fSThierry Reding 	if (err < 0)
1742c7679306SThierry Reding 		goto cleanup;
1743dee8268fSThierry Reding 
1744dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
17459910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1746dee8268fSThierry Reding 		if (err < 0)
1747dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1748dee8268fSThierry Reding 	}
1749dee8268fSThierry Reding 
1750dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1751dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1752dee8268fSThierry Reding 	if (err < 0) {
1753dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1754dee8268fSThierry Reding 			err);
1755c7679306SThierry Reding 		goto cleanup;
1756dee8268fSThierry Reding 	}
1757dee8268fSThierry Reding 
175807d05cbfSThierry Reding 	/* initialize display controller */
175942e9ce05SThierry Reding 	if (dc->syncpt) {
176042e9ce05SThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt);
176107d05cbfSThierry Reding 
176242e9ce05SThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
176342e9ce05SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
176442e9ce05SThierry Reding 
176542e9ce05SThierry Reding 		value = SYNCPT_VSYNC_ENABLE | syncpt;
176642e9ce05SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
176742e9ce05SThierry Reding 	}
176807d05cbfSThierry Reding 
1769791ddb1eSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1770791ddb1eSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
177107d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
177207d05cbfSThierry Reding 
177307d05cbfSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
177407d05cbfSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
177507d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
177607d05cbfSThierry Reding 
177707d05cbfSThierry Reding 	/* initialize timer */
177807d05cbfSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
177907d05cbfSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
178007d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
178107d05cbfSThierry Reding 
178207d05cbfSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
178307d05cbfSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
178407d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
178507d05cbfSThierry Reding 
1786791ddb1eSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1787791ddb1eSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
178807d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
178907d05cbfSThierry Reding 
1790791ddb1eSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1791791ddb1eSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
179207d05cbfSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
179307d05cbfSThierry Reding 
179407d05cbfSThierry Reding 	if (dc->soc->supports_border_color)
179507d05cbfSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
179607d05cbfSThierry Reding 
1797791ddb1eSThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1798791ddb1eSThierry Reding 
1799dee8268fSThierry Reding 	return 0;
1800c7679306SThierry Reding 
1801c7679306SThierry Reding cleanup:
1802c7679306SThierry Reding 	if (cursor)
1803c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1804c7679306SThierry Reding 
1805c7679306SThierry Reding 	if (primary)
1806c7679306SThierry Reding 		drm_plane_cleanup(primary);
1807c7679306SThierry Reding 
1808c7679306SThierry Reding 	if (tegra->domain) {
1809c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1810c7679306SThierry Reding 		dc->domain = NULL;
1811c7679306SThierry Reding 	}
1812c7679306SThierry Reding 
1813c7679306SThierry Reding 	return err;
1814dee8268fSThierry Reding }
1815dee8268fSThierry Reding 
1816dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1817dee8268fSThierry Reding {
1818dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1819dee8268fSThierry Reding 	int err;
1820dee8268fSThierry Reding 
1821dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1822dee8268fSThierry Reding 
1823dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1824dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1825dee8268fSThierry Reding 		if (err < 0)
1826dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1827dee8268fSThierry Reding 	}
1828dee8268fSThierry Reding 
1829dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1830dee8268fSThierry Reding 	if (err) {
1831dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1832dee8268fSThierry Reding 		return err;
1833dee8268fSThierry Reding 	}
1834dee8268fSThierry Reding 
1835df06b759SThierry Reding 	if (dc->domain) {
1836df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1837df06b759SThierry Reding 		dc->domain = NULL;
1838df06b759SThierry Reding 	}
1839df06b759SThierry Reding 
18402bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
18412bcdcbfaSThierry Reding 
1842dee8268fSThierry Reding 	return 0;
1843dee8268fSThierry Reding }
1844dee8268fSThierry Reding 
1845dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1846dee8268fSThierry Reding 	.init = tegra_dc_init,
1847dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1848dee8268fSThierry Reding };
1849dee8268fSThierry Reding 
18508620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
185142d0659bSThierry Reding 	.supports_border_color = true,
18528620fc62SThierry Reding 	.supports_interlacing = false,
1853e687651bSThierry Reding 	.supports_cursor = false,
1854c134f019SThierry Reding 	.supports_block_linear = false,
1855d1f3e1e0SThierry Reding 	.pitch_align = 8,
18569c012700SThierry Reding 	.has_powergate = false,
18578620fc62SThierry Reding };
18588620fc62SThierry Reding 
18598620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
186042d0659bSThierry Reding 	.supports_border_color = true,
18618620fc62SThierry Reding 	.supports_interlacing = false,
1862e687651bSThierry Reding 	.supports_cursor = false,
1863c134f019SThierry Reding 	.supports_block_linear = false,
1864d1f3e1e0SThierry Reding 	.pitch_align = 8,
18659c012700SThierry Reding 	.has_powergate = false,
1866d1f3e1e0SThierry Reding };
1867d1f3e1e0SThierry Reding 
1868d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
186942d0659bSThierry Reding 	.supports_border_color = true,
1870d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1871d1f3e1e0SThierry Reding 	.supports_cursor = false,
1872d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1873d1f3e1e0SThierry Reding 	.pitch_align = 64,
18749c012700SThierry Reding 	.has_powergate = true,
18758620fc62SThierry Reding };
18768620fc62SThierry Reding 
18778620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
187842d0659bSThierry Reding 	.supports_border_color = false,
18798620fc62SThierry Reding 	.supports_interlacing = true,
1880e687651bSThierry Reding 	.supports_cursor = true,
1881c134f019SThierry Reding 	.supports_block_linear = true,
1882d1f3e1e0SThierry Reding 	.pitch_align = 64,
18839c012700SThierry Reding 	.has_powergate = true,
18848620fc62SThierry Reding };
18858620fc62SThierry Reding 
18865b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
18875b4f516fSThierry Reding 	.supports_border_color = false,
18885b4f516fSThierry Reding 	.supports_interlacing = true,
18895b4f516fSThierry Reding 	.supports_cursor = true,
18905b4f516fSThierry Reding 	.supports_block_linear = true,
18915b4f516fSThierry Reding 	.pitch_align = 64,
18925b4f516fSThierry Reding 	.has_powergate = true,
18935b4f516fSThierry Reding };
18945b4f516fSThierry Reding 
18958620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
18968620fc62SThierry Reding 	{
18975b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
18985b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
18995b4f516fSThierry Reding 	}, {
19008620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
19018620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
19028620fc62SThierry Reding 	}, {
19039c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
19049c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
19059c012700SThierry Reding 	}, {
19068620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
19078620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
19088620fc62SThierry Reding 	}, {
19098620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
19108620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
19118620fc62SThierry Reding 	}, {
19128620fc62SThierry Reding 		/* sentinel */
19138620fc62SThierry Reding 	}
19148620fc62SThierry Reding };
1915ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
19168620fc62SThierry Reding 
191713411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
191813411dddSThierry Reding {
191913411dddSThierry Reding 	struct device_node *np;
192013411dddSThierry Reding 	u32 value = 0;
192113411dddSThierry Reding 	int err;
192213411dddSThierry Reding 
192313411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
192413411dddSThierry Reding 	if (err < 0) {
192513411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
192613411dddSThierry Reding 
192713411dddSThierry Reding 		/*
192813411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
192913411dddSThierry Reding 		 * correct head number by looking up the position of this
193013411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
193113411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
193213411dddSThierry Reding 		 * that the translation into a flattened device tree blob
193313411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
193413411dddSThierry Reding 		 * head number.
193513411dddSThierry Reding 		 *
193613411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
193713411dddSThierry Reding 		 * cases where only a single display controller is used.
193813411dddSThierry Reding 		 */
193913411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1940cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1941cf6b1744SJulia Lawall 				of_node_put(np);
194213411dddSThierry Reding 				break;
1943cf6b1744SJulia Lawall 			}
194413411dddSThierry Reding 
194513411dddSThierry Reding 			value++;
194613411dddSThierry Reding 		}
194713411dddSThierry Reding 	}
194813411dddSThierry Reding 
194913411dddSThierry Reding 	dc->pipe = value;
195013411dddSThierry Reding 
195113411dddSThierry Reding 	return 0;
195213411dddSThierry Reding }
195313411dddSThierry Reding 
1954dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1955dee8268fSThierry Reding {
19568620fc62SThierry Reding 	const struct of_device_id *id;
1957dee8268fSThierry Reding 	struct resource *regs;
1958dee8268fSThierry Reding 	struct tegra_dc *dc;
1959dee8268fSThierry Reding 	int err;
1960dee8268fSThierry Reding 
1961dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1962dee8268fSThierry Reding 	if (!dc)
1963dee8268fSThierry Reding 		return -ENOMEM;
1964dee8268fSThierry Reding 
19658620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
19668620fc62SThierry Reding 	if (!id)
19678620fc62SThierry Reding 		return -ENODEV;
19688620fc62SThierry Reding 
1969dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1970dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1971dee8268fSThierry Reding 	dc->dev = &pdev->dev;
19728620fc62SThierry Reding 	dc->soc = id->data;
1973dee8268fSThierry Reding 
197413411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
197513411dddSThierry Reding 	if (err < 0)
197613411dddSThierry Reding 		return err;
197713411dddSThierry Reding 
1978dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1979dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1980dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1981dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1982dee8268fSThierry Reding 	}
1983dee8268fSThierry Reding 
1984ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1985ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1986ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1987ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1988ca48080aSStephen Warren 	}
1989ca48080aSStephen Warren 
19909c012700SThierry Reding 	if (dc->soc->has_powergate) {
19919c012700SThierry Reding 		if (dc->pipe == 0)
19929c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
19939c012700SThierry Reding 		else
19949c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
19959c012700SThierry Reding 
19969c012700SThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
19979c012700SThierry Reding 							dc->rst);
19989c012700SThierry Reding 		if (err < 0) {
19999c012700SThierry Reding 			dev_err(&pdev->dev, "failed to power partition: %d\n",
20009c012700SThierry Reding 				err);
2001dee8268fSThierry Reding 			return err;
20029c012700SThierry Reding 		}
20039c012700SThierry Reding 	} else {
20049c012700SThierry Reding 		err = clk_prepare_enable(dc->clk);
20059c012700SThierry Reding 		if (err < 0) {
20069c012700SThierry Reding 			dev_err(&pdev->dev, "failed to enable clock: %d\n",
20079c012700SThierry Reding 				err);
20089c012700SThierry Reding 			return err;
20099c012700SThierry Reding 		}
20109c012700SThierry Reding 
20119c012700SThierry Reding 		err = reset_control_deassert(dc->rst);
20129c012700SThierry Reding 		if (err < 0) {
20139c012700SThierry Reding 			dev_err(&pdev->dev, "failed to deassert reset: %d\n",
20149c012700SThierry Reding 				err);
20159c012700SThierry Reding 			return err;
20169c012700SThierry Reding 		}
20179c012700SThierry Reding 	}
2018dee8268fSThierry Reding 
2019dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2020dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2021dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
2022dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
2023dee8268fSThierry Reding 
2024dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2025dee8268fSThierry Reding 	if (dc->irq < 0) {
2026dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2027dee8268fSThierry Reding 		return -ENXIO;
2028dee8268fSThierry Reding 	}
2029dee8268fSThierry Reding 
2030dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
2031dee8268fSThierry Reding 	dc->client.ops = &dc_client_ops;
2032dee8268fSThierry Reding 	dc->client.dev = &pdev->dev;
2033dee8268fSThierry Reding 
2034dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2035dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2036dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2037dee8268fSThierry Reding 		return err;
2038dee8268fSThierry Reding 	}
2039dee8268fSThierry Reding 
2040dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2041dee8268fSThierry Reding 	if (err < 0) {
2042dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2043dee8268fSThierry Reding 			err);
2044dee8268fSThierry Reding 		return err;
2045dee8268fSThierry Reding 	}
2046dee8268fSThierry Reding 
2047dee8268fSThierry Reding 	platform_set_drvdata(pdev, dc);
2048dee8268fSThierry Reding 
2049dee8268fSThierry Reding 	return 0;
2050dee8268fSThierry Reding }
2051dee8268fSThierry Reding 
2052dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2053dee8268fSThierry Reding {
2054dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2055dee8268fSThierry Reding 	int err;
2056dee8268fSThierry Reding 
2057dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2058dee8268fSThierry Reding 	if (err < 0) {
2059dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2060dee8268fSThierry Reding 			err);
2061dee8268fSThierry Reding 		return err;
2062dee8268fSThierry Reding 	}
2063dee8268fSThierry Reding 
206459d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
206559d29c0eSThierry Reding 	if (err < 0) {
206659d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
206759d29c0eSThierry Reding 		return err;
206859d29c0eSThierry Reding 	}
206959d29c0eSThierry Reding 
20705482d75aSThierry Reding 	reset_control_assert(dc->rst);
20719c012700SThierry Reding 
20729c012700SThierry Reding 	if (dc->soc->has_powergate)
20739c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
20749c012700SThierry Reding 
2075dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2076dee8268fSThierry Reding 
2077dee8268fSThierry Reding 	return 0;
2078dee8268fSThierry Reding }
2079dee8268fSThierry Reding 
2080dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2081dee8268fSThierry Reding 	.driver = {
2082dee8268fSThierry Reding 		.name = "tegra-dc",
2083dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
2084dee8268fSThierry Reding 	},
2085dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2086dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2087dee8268fSThierry Reding };
2088