xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision bcb0b461454c9cb3b5804cf75bacaadb52348864)
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>
1333a8eb8dSThierry Reding #include <linux/pm_runtime.h>
14ca48080aSStephen Warren #include <linux/reset.h>
15dee8268fSThierry Reding 
169c012700SThierry Reding #include <soc/tegra/pmc.h>
179c012700SThierry Reding 
18dee8268fSThierry Reding #include "dc.h"
19dee8268fSThierry Reding #include "drm.h"
20dee8268fSThierry Reding #include "gem.h"
21dee8268fSThierry Reding 
229d44189fSThierry Reding #include <drm/drm_atomic.h>
234aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
243cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
253cb9ae4fSDaniel Vetter 
268620fc62SThierry Reding struct tegra_dc_soc_info {
2742d0659bSThierry Reding 	bool supports_border_color;
288620fc62SThierry Reding 	bool supports_interlacing;
29e687651bSThierry Reding 	bool supports_cursor;
30c134f019SThierry Reding 	bool supports_block_linear;
31d1f3e1e0SThierry Reding 	unsigned int pitch_align;
329c012700SThierry Reding 	bool has_powergate;
338620fc62SThierry Reding };
348620fc62SThierry Reding 
35dee8268fSThierry Reding struct tegra_plane {
36dee8268fSThierry Reding 	struct drm_plane base;
37dee8268fSThierry Reding 	unsigned int index;
38dee8268fSThierry Reding };
39dee8268fSThierry Reding 
40dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
41dee8268fSThierry Reding {
42dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
43dee8268fSThierry Reding }
44dee8268fSThierry Reding 
45ca915b10SThierry Reding struct tegra_dc_state {
46ca915b10SThierry Reding 	struct drm_crtc_state base;
47ca915b10SThierry Reding 
48ca915b10SThierry Reding 	struct clk *clk;
49ca915b10SThierry Reding 	unsigned long pclk;
50ca915b10SThierry Reding 	unsigned int div;
5147802b09SThierry Reding 
5247802b09SThierry Reding 	u32 planes;
53ca915b10SThierry Reding };
54ca915b10SThierry Reding 
55ca915b10SThierry Reding static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
56ca915b10SThierry Reding {
57ca915b10SThierry Reding 	if (state)
58ca915b10SThierry Reding 		return container_of(state, struct tegra_dc_state, base);
59ca915b10SThierry Reding 
60ca915b10SThierry Reding 	return NULL;
61ca915b10SThierry Reding }
62ca915b10SThierry Reding 
638f604f8cSThierry Reding struct tegra_plane_state {
648f604f8cSThierry Reding 	struct drm_plane_state base;
658f604f8cSThierry Reding 
668f604f8cSThierry Reding 	struct tegra_bo_tiling tiling;
678f604f8cSThierry Reding 	u32 format;
688f604f8cSThierry Reding 	u32 swap;
698f604f8cSThierry Reding };
708f604f8cSThierry Reding 
718f604f8cSThierry Reding static inline struct tegra_plane_state *
728f604f8cSThierry Reding to_tegra_plane_state(struct drm_plane_state *state)
738f604f8cSThierry Reding {
748f604f8cSThierry Reding 	if (state)
758f604f8cSThierry Reding 		return container_of(state, struct tegra_plane_state, base);
768f604f8cSThierry Reding 
778f604f8cSThierry Reding 	return NULL;
788f604f8cSThierry Reding }
798f604f8cSThierry Reding 
80791ddb1eSThierry Reding static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
81791ddb1eSThierry Reding {
82791ddb1eSThierry Reding 	stats->frames = 0;
83791ddb1eSThierry Reding 	stats->vblank = 0;
84791ddb1eSThierry Reding 	stats->underflow = 0;
85791ddb1eSThierry Reding 	stats->overflow = 0;
86791ddb1eSThierry Reding }
87791ddb1eSThierry Reding 
88d700ba7aSThierry Reding /*
8986df256fSThierry Reding  * Reads the active copy of a register. This takes the dc->lock spinlock to
9086df256fSThierry Reding  * prevent races with the VBLANK processing which also needs access to the
9186df256fSThierry Reding  * active copy of some registers.
9286df256fSThierry Reding  */
9386df256fSThierry Reding static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
9486df256fSThierry Reding {
9586df256fSThierry Reding 	unsigned long flags;
9686df256fSThierry Reding 	u32 value;
9786df256fSThierry Reding 
9886df256fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
9986df256fSThierry Reding 
10086df256fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
10186df256fSThierry Reding 	value = tegra_dc_readl(dc, offset);
10286df256fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
10386df256fSThierry Reding 
10486df256fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
10586df256fSThierry Reding 	return value;
10686df256fSThierry Reding }
10786df256fSThierry Reding 
10886df256fSThierry Reding /*
109d700ba7aSThierry Reding  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
110d700ba7aSThierry Reding  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
111d700ba7aSThierry Reding  * Latching happens mmediately if the display controller is in STOP mode or
112d700ba7aSThierry Reding  * on the next frame boundary otherwise.
113d700ba7aSThierry Reding  *
114d700ba7aSThierry Reding  * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
115d700ba7aSThierry Reding  * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
116d700ba7aSThierry Reding  * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
117d700ba7aSThierry Reding  * into the ACTIVE copy, either immediately if the display controller is in
118d700ba7aSThierry Reding  * STOP mode, or at the next frame boundary otherwise.
119d700ba7aSThierry Reding  */
12062b9e063SThierry Reding void tegra_dc_commit(struct tegra_dc *dc)
121205d48edSThierry Reding {
122205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
123205d48edSThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
124205d48edSThierry Reding }
125205d48edSThierry Reding 
1268f604f8cSThierry Reding static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
12710288eeaSThierry Reding {
12810288eeaSThierry Reding 	/* assume no swapping of fetched data */
12910288eeaSThierry Reding 	if (swap)
13010288eeaSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
13110288eeaSThierry Reding 
1328f604f8cSThierry Reding 	switch (fourcc) {
13310288eeaSThierry Reding 	case DRM_FORMAT_XBGR8888:
1348f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
1358f604f8cSThierry Reding 		break;
13610288eeaSThierry Reding 
13710288eeaSThierry Reding 	case DRM_FORMAT_XRGB8888:
1388f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
1398f604f8cSThierry Reding 		break;
14010288eeaSThierry Reding 
14110288eeaSThierry Reding 	case DRM_FORMAT_RGB565:
1428f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
1438f604f8cSThierry Reding 		break;
14410288eeaSThierry Reding 
14510288eeaSThierry Reding 	case DRM_FORMAT_UYVY:
1468f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1478f604f8cSThierry Reding 		break;
14810288eeaSThierry Reding 
14910288eeaSThierry Reding 	case DRM_FORMAT_YUYV:
15010288eeaSThierry Reding 		if (swap)
15110288eeaSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
15210288eeaSThierry Reding 
1538f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
1548f604f8cSThierry Reding 		break;
15510288eeaSThierry Reding 
15610288eeaSThierry Reding 	case DRM_FORMAT_YUV420:
1578f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
1588f604f8cSThierry Reding 		break;
15910288eeaSThierry Reding 
16010288eeaSThierry Reding 	case DRM_FORMAT_YUV422:
1618f604f8cSThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
1628f604f8cSThierry Reding 		break;
16310288eeaSThierry Reding 
16410288eeaSThierry Reding 	default:
1658f604f8cSThierry Reding 		return -EINVAL;
16610288eeaSThierry Reding 	}
16710288eeaSThierry Reding 
1688f604f8cSThierry Reding 	return 0;
16910288eeaSThierry Reding }
17010288eeaSThierry Reding 
17110288eeaSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
17210288eeaSThierry Reding {
17310288eeaSThierry Reding 	switch (format) {
17410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
17510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
17610288eeaSThierry Reding 		if (planar)
17710288eeaSThierry Reding 			*planar = false;
17810288eeaSThierry Reding 
17910288eeaSThierry Reding 		return true;
18010288eeaSThierry Reding 
18110288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
18210288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
18310288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
18410288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
18510288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
18610288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
18710288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
18810288eeaSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
18910288eeaSThierry Reding 		if (planar)
19010288eeaSThierry Reding 			*planar = true;
19110288eeaSThierry Reding 
19210288eeaSThierry Reding 		return true;
19310288eeaSThierry Reding 	}
19410288eeaSThierry Reding 
195fb35c6b6SThierry Reding 	if (planar)
196fb35c6b6SThierry Reding 		*planar = false;
197fb35c6b6SThierry Reding 
19810288eeaSThierry Reding 	return false;
19910288eeaSThierry Reding }
20010288eeaSThierry Reding 
20110288eeaSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
20210288eeaSThierry Reding 				  unsigned int bpp)
20310288eeaSThierry Reding {
20410288eeaSThierry Reding 	fixed20_12 outf = dfixed_init(out);
20510288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
20610288eeaSThierry Reding 	u32 dda_inc;
20710288eeaSThierry Reding 	int max;
20810288eeaSThierry Reding 
20910288eeaSThierry Reding 	if (v)
21010288eeaSThierry Reding 		max = 15;
21110288eeaSThierry Reding 	else {
21210288eeaSThierry Reding 		switch (bpp) {
21310288eeaSThierry Reding 		case 2:
21410288eeaSThierry Reding 			max = 8;
21510288eeaSThierry Reding 			break;
21610288eeaSThierry Reding 
21710288eeaSThierry Reding 		default:
21810288eeaSThierry Reding 			WARN_ON_ONCE(1);
21910288eeaSThierry Reding 			/* fallthrough */
22010288eeaSThierry Reding 		case 4:
22110288eeaSThierry Reding 			max = 4;
22210288eeaSThierry Reding 			break;
22310288eeaSThierry Reding 		}
22410288eeaSThierry Reding 	}
22510288eeaSThierry Reding 
22610288eeaSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
22710288eeaSThierry Reding 	inf.full -= dfixed_const(1);
22810288eeaSThierry Reding 
22910288eeaSThierry Reding 	dda_inc = dfixed_div(inf, outf);
23010288eeaSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
23110288eeaSThierry Reding 
23210288eeaSThierry Reding 	return dda_inc;
23310288eeaSThierry Reding }
23410288eeaSThierry Reding 
23510288eeaSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
23610288eeaSThierry Reding {
23710288eeaSThierry Reding 	fixed20_12 inf = dfixed_init(in);
23810288eeaSThierry Reding 	return dfixed_frac(inf);
23910288eeaSThierry Reding }
24010288eeaSThierry Reding 
2414aa3df71SThierry Reding static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
24210288eeaSThierry Reding 				  const struct tegra_dc_window *window)
24310288eeaSThierry Reding {
24410288eeaSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
24593396d0fSSean Paul 	unsigned long value, flags;
24610288eeaSThierry Reding 	bool yuv, planar;
24710288eeaSThierry Reding 
24810288eeaSThierry Reding 	/*
24910288eeaSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
25010288eeaSThierry Reding 	 * account only the luma component and therefore is 1.
25110288eeaSThierry Reding 	 */
25210288eeaSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
25310288eeaSThierry Reding 	if (!yuv)
25410288eeaSThierry Reding 		bpp = window->bits_per_pixel / 8;
25510288eeaSThierry Reding 	else
25610288eeaSThierry Reding 		bpp = planar ? 1 : 2;
25710288eeaSThierry Reding 
25893396d0fSSean Paul 	spin_lock_irqsave(&dc->lock, flags);
25993396d0fSSean Paul 
26010288eeaSThierry Reding 	value = WINDOW_A_SELECT << index;
26110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
26210288eeaSThierry Reding 
26310288eeaSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
26410288eeaSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
26510288eeaSThierry Reding 
26610288eeaSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
26710288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
26810288eeaSThierry Reding 
26910288eeaSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
27010288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
27110288eeaSThierry Reding 
27210288eeaSThierry Reding 	h_offset = window->src.x * bpp;
27310288eeaSThierry Reding 	v_offset = window->src.y;
27410288eeaSThierry Reding 	h_size = window->src.w * bpp;
27510288eeaSThierry Reding 	v_size = window->src.h;
27610288eeaSThierry Reding 
27710288eeaSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
27810288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
27910288eeaSThierry Reding 
28010288eeaSThierry Reding 	/*
28110288eeaSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
28210288eeaSThierry Reding 	 * modes needs to take into account all Y, U and V components.
28310288eeaSThierry Reding 	 */
28410288eeaSThierry Reding 	if (yuv && planar)
28510288eeaSThierry Reding 		bpp = 2;
28610288eeaSThierry Reding 
28710288eeaSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
28810288eeaSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
28910288eeaSThierry Reding 
29010288eeaSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
29110288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
29210288eeaSThierry Reding 
29310288eeaSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
29410288eeaSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
29510288eeaSThierry Reding 
29610288eeaSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
29710288eeaSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
29810288eeaSThierry Reding 
29910288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
30010288eeaSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
30110288eeaSThierry Reding 
30210288eeaSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
30310288eeaSThierry Reding 
30410288eeaSThierry Reding 	if (yuv && planar) {
30510288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
30610288eeaSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
30710288eeaSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
30810288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
30910288eeaSThierry Reding 	} else {
31010288eeaSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
31110288eeaSThierry Reding 	}
31210288eeaSThierry Reding 
31310288eeaSThierry Reding 	if (window->bottom_up)
31410288eeaSThierry Reding 		v_offset += window->src.h - 1;
31510288eeaSThierry Reding 
31610288eeaSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
31710288eeaSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
31810288eeaSThierry Reding 
319c134f019SThierry Reding 	if (dc->soc->supports_block_linear) {
320c134f019SThierry Reding 		unsigned long height = window->tiling.value;
321c134f019SThierry Reding 
322c134f019SThierry Reding 		switch (window->tiling.mode) {
323c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
324c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_PITCH;
325c134f019SThierry Reding 			break;
326c134f019SThierry Reding 
327c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
328c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_TILED;
329c134f019SThierry Reding 			break;
330c134f019SThierry Reding 
331c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
332c134f019SThierry Reding 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
333c134f019SThierry Reding 				DC_WINBUF_SURFACE_KIND_BLOCK;
334c134f019SThierry Reding 			break;
335c134f019SThierry Reding 		}
336c134f019SThierry Reding 
337c134f019SThierry Reding 		tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
33810288eeaSThierry Reding 	} else {
339c134f019SThierry Reding 		switch (window->tiling.mode) {
340c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_PITCH:
34110288eeaSThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
34210288eeaSThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_LINEAR;
343c134f019SThierry Reding 			break;
344c134f019SThierry Reding 
345c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_TILED:
346c134f019SThierry Reding 			value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
347c134f019SThierry Reding 				DC_WIN_BUFFER_ADDR_MODE_TILE;
348c134f019SThierry Reding 			break;
349c134f019SThierry Reding 
350c134f019SThierry Reding 		case TEGRA_BO_TILING_MODE_BLOCK:
3514aa3df71SThierry Reding 			/*
3524aa3df71SThierry Reding 			 * No need to handle this here because ->atomic_check
3534aa3df71SThierry Reding 			 * will already have filtered it out.
3544aa3df71SThierry Reding 			 */
3554aa3df71SThierry Reding 			break;
35610288eeaSThierry Reding 		}
35710288eeaSThierry Reding 
35810288eeaSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
359c134f019SThierry Reding 	}
36010288eeaSThierry Reding 
36110288eeaSThierry Reding 	value = WIN_ENABLE;
36210288eeaSThierry Reding 
36310288eeaSThierry Reding 	if (yuv) {
36410288eeaSThierry Reding 		/* setup default colorspace conversion coefficients */
36510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
36610288eeaSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
36710288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
36810288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
36910288eeaSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
37010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
37110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
37210288eeaSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
37310288eeaSThierry Reding 
37410288eeaSThierry Reding 		value |= CSC_ENABLE;
37510288eeaSThierry Reding 	} else if (window->bits_per_pixel < 24) {
37610288eeaSThierry Reding 		value |= COLOR_EXPAND;
37710288eeaSThierry Reding 	}
37810288eeaSThierry Reding 
37910288eeaSThierry Reding 	if (window->bottom_up)
38010288eeaSThierry Reding 		value |= V_DIRECTION;
38110288eeaSThierry Reding 
38210288eeaSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
38310288eeaSThierry Reding 
38410288eeaSThierry Reding 	/*
38510288eeaSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
38610288eeaSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
38710288eeaSThierry Reding 	 */
38810288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
38910288eeaSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
39010288eeaSThierry Reding 
39110288eeaSThierry Reding 	switch (index) {
39210288eeaSThierry Reding 	case 0:
39310288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
39410288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
39510288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
39610288eeaSThierry Reding 		break;
39710288eeaSThierry Reding 
39810288eeaSThierry Reding 	case 1:
39910288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
40010288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
40110288eeaSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
40210288eeaSThierry Reding 		break;
40310288eeaSThierry Reding 
40410288eeaSThierry Reding 	case 2:
40510288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
40610288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
40710288eeaSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
40810288eeaSThierry Reding 		break;
40910288eeaSThierry Reding 	}
41010288eeaSThierry Reding 
41193396d0fSSean Paul 	spin_unlock_irqrestore(&dc->lock, flags);
412c7679306SThierry Reding }
413c7679306SThierry Reding 
414c7679306SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
415c7679306SThierry Reding {
416c7679306SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
417c7679306SThierry Reding 
418c7679306SThierry Reding 	drm_plane_cleanup(plane);
419c7679306SThierry Reding 	kfree(p);
420c7679306SThierry Reding }
421c7679306SThierry Reding 
422c7679306SThierry Reding static const u32 tegra_primary_plane_formats[] = {
423c7679306SThierry Reding 	DRM_FORMAT_XBGR8888,
424c7679306SThierry Reding 	DRM_FORMAT_XRGB8888,
425c7679306SThierry Reding 	DRM_FORMAT_RGB565,
426c7679306SThierry Reding };
427c7679306SThierry Reding 
4284aa3df71SThierry Reding static void tegra_primary_plane_destroy(struct drm_plane *plane)
429c7679306SThierry Reding {
4304aa3df71SThierry Reding 	tegra_plane_destroy(plane);
4314aa3df71SThierry Reding }
4324aa3df71SThierry Reding 
4338f604f8cSThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
4348f604f8cSThierry Reding {
4358f604f8cSThierry Reding 	struct tegra_plane_state *state;
4368f604f8cSThierry Reding 
4373b59b7acSThierry Reding 	if (plane->state)
4382f701695SDaniel Vetter 		__drm_atomic_helper_plane_destroy_state(plane->state);
4398f604f8cSThierry Reding 
4408f604f8cSThierry Reding 	kfree(plane->state);
4418f604f8cSThierry Reding 	plane->state = NULL;
4428f604f8cSThierry Reding 
4438f604f8cSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
4448f604f8cSThierry Reding 	if (state) {
4458f604f8cSThierry Reding 		plane->state = &state->base;
4468f604f8cSThierry Reding 		plane->state->plane = plane;
4478f604f8cSThierry Reding 	}
4488f604f8cSThierry Reding }
4498f604f8cSThierry Reding 
4508f604f8cSThierry Reding static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
4518f604f8cSThierry Reding {
4528f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
4538f604f8cSThierry Reding 	struct tegra_plane_state *copy;
4548f604f8cSThierry Reding 
4553b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
4568f604f8cSThierry Reding 	if (!copy)
4578f604f8cSThierry Reding 		return NULL;
4588f604f8cSThierry Reding 
4593b59b7acSThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
4603b59b7acSThierry Reding 	copy->tiling = state->tiling;
4613b59b7acSThierry Reding 	copy->format = state->format;
4623b59b7acSThierry Reding 	copy->swap = state->swap;
4638f604f8cSThierry Reding 
4648f604f8cSThierry Reding 	return &copy->base;
4658f604f8cSThierry Reding }
4668f604f8cSThierry Reding 
4678f604f8cSThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
4688f604f8cSThierry Reding 					     struct drm_plane_state *state)
4698f604f8cSThierry Reding {
4702f701695SDaniel Vetter 	__drm_atomic_helper_plane_destroy_state(state);
4718f604f8cSThierry Reding 	kfree(state);
4728f604f8cSThierry Reding }
4738f604f8cSThierry Reding 
4744aa3df71SThierry Reding static const struct drm_plane_funcs tegra_primary_plane_funcs = {
47507866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
47607866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
4774aa3df71SThierry Reding 	.destroy = tegra_primary_plane_destroy,
4788f604f8cSThierry Reding 	.reset = tegra_plane_reset,
4798f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
4808f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
4814aa3df71SThierry Reding };
4824aa3df71SThierry Reding 
48347802b09SThierry Reding static int tegra_plane_state_add(struct tegra_plane *plane,
48447802b09SThierry Reding 				 struct drm_plane_state *state)
48547802b09SThierry Reding {
48647802b09SThierry Reding 	struct drm_crtc_state *crtc_state;
48747802b09SThierry Reding 	struct tegra_dc_state *tegra;
48847802b09SThierry Reding 
48947802b09SThierry Reding 	/* Propagate errors from allocation or locking failures. */
49047802b09SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
49147802b09SThierry Reding 	if (IS_ERR(crtc_state))
49247802b09SThierry Reding 		return PTR_ERR(crtc_state);
49347802b09SThierry Reding 
49447802b09SThierry Reding 	tegra = to_dc_state(crtc_state);
49547802b09SThierry Reding 
49647802b09SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
49747802b09SThierry Reding 
49847802b09SThierry Reding 	return 0;
49947802b09SThierry Reding }
50047802b09SThierry Reding 
5014aa3df71SThierry Reding static int tegra_plane_atomic_check(struct drm_plane *plane,
5024aa3df71SThierry Reding 				    struct drm_plane_state *state)
5034aa3df71SThierry Reding {
5048f604f8cSThierry Reding 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
5058f604f8cSThierry Reding 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
50647802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
5074aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
508c7679306SThierry Reding 	int err;
509c7679306SThierry Reding 
5104aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
5114aa3df71SThierry Reding 	if (!state->crtc)
5124aa3df71SThierry Reding 		return 0;
5134aa3df71SThierry Reding 
5148f604f8cSThierry Reding 	err = tegra_dc_format(state->fb->pixel_format, &plane_state->format,
5158f604f8cSThierry Reding 			      &plane_state->swap);
5164aa3df71SThierry Reding 	if (err < 0)
5174aa3df71SThierry Reding 		return err;
5184aa3df71SThierry Reding 
5198f604f8cSThierry Reding 	err = tegra_fb_get_tiling(state->fb, tiling);
5208f604f8cSThierry Reding 	if (err < 0)
5218f604f8cSThierry Reding 		return err;
5228f604f8cSThierry Reding 
5238f604f8cSThierry Reding 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
5244aa3df71SThierry Reding 	    !dc->soc->supports_block_linear) {
5254aa3df71SThierry Reding 		DRM_ERROR("hardware doesn't support block linear mode\n");
5264aa3df71SThierry Reding 		return -EINVAL;
5274aa3df71SThierry Reding 	}
5284aa3df71SThierry Reding 
5294aa3df71SThierry Reding 	/*
5304aa3df71SThierry Reding 	 * Tegra doesn't support different strides for U and V planes so we
5314aa3df71SThierry Reding 	 * error out if the user tries to display a framebuffer with such a
5324aa3df71SThierry Reding 	 * configuration.
5334aa3df71SThierry Reding 	 */
534*bcb0b461SVille Syrjälä 	if (state->fb->format->num_planes > 2) {
5354aa3df71SThierry Reding 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
5364aa3df71SThierry Reding 			DRM_ERROR("unsupported UV-plane configuration\n");
5374aa3df71SThierry Reding 			return -EINVAL;
5384aa3df71SThierry Reding 		}
5394aa3df71SThierry Reding 	}
5404aa3df71SThierry Reding 
54147802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
54247802b09SThierry Reding 	if (err < 0)
54347802b09SThierry Reding 		return err;
54447802b09SThierry Reding 
5454aa3df71SThierry Reding 	return 0;
5464aa3df71SThierry Reding }
5474aa3df71SThierry Reding 
5484aa3df71SThierry Reding static void tegra_plane_atomic_update(struct drm_plane *plane,
5494aa3df71SThierry Reding 				      struct drm_plane_state *old_state)
5504aa3df71SThierry Reding {
5518f604f8cSThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
5524aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
5534aa3df71SThierry Reding 	struct drm_framebuffer *fb = plane->state->fb;
5544aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
5554aa3df71SThierry Reding 	struct tegra_dc_window window;
5564aa3df71SThierry Reding 	unsigned int i;
5574aa3df71SThierry Reding 
5584aa3df71SThierry Reding 	/* rien ne va plus */
5594aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
5604aa3df71SThierry Reding 		return;
5614aa3df71SThierry Reding 
562c7679306SThierry Reding 	memset(&window, 0, sizeof(window));
5634aa3df71SThierry Reding 	window.src.x = plane->state->src_x >> 16;
5644aa3df71SThierry Reding 	window.src.y = plane->state->src_y >> 16;
5654aa3df71SThierry Reding 	window.src.w = plane->state->src_w >> 16;
5664aa3df71SThierry Reding 	window.src.h = plane->state->src_h >> 16;
5674aa3df71SThierry Reding 	window.dst.x = plane->state->crtc_x;
5684aa3df71SThierry Reding 	window.dst.y = plane->state->crtc_y;
5694aa3df71SThierry Reding 	window.dst.w = plane->state->crtc_w;
5704aa3df71SThierry Reding 	window.dst.h = plane->state->crtc_h;
571c7679306SThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
572c7679306SThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
573c7679306SThierry Reding 
5748f604f8cSThierry Reding 	/* copy from state */
5758f604f8cSThierry Reding 	window.tiling = state->tiling;
5768f604f8cSThierry Reding 	window.format = state->format;
5778f604f8cSThierry Reding 	window.swap = state->swap;
578c7679306SThierry Reding 
579*bcb0b461SVille Syrjälä 	for (i = 0; i < fb->format->num_planes; i++) {
5804aa3df71SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
581c7679306SThierry Reding 
5824aa3df71SThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
58308ee0178SDmitry Osipenko 
58408ee0178SDmitry Osipenko 		/*
58508ee0178SDmitry Osipenko 		 * Tegra uses a shared stride for UV planes. Framebuffers are
58608ee0178SDmitry Osipenko 		 * already checked for this in the tegra_plane_atomic_check()
58708ee0178SDmitry Osipenko 		 * function, so it's safe to ignore the V-plane pitch here.
58808ee0178SDmitry Osipenko 		 */
58908ee0178SDmitry Osipenko 		if (i < 2)
5904aa3df71SThierry Reding 			window.stride[i] = fb->pitches[i];
591c7679306SThierry Reding 	}
592c7679306SThierry Reding 
5934aa3df71SThierry Reding 	tegra_dc_setup_window(dc, p->index, &window);
5944aa3df71SThierry Reding }
5954aa3df71SThierry Reding 
5964aa3df71SThierry Reding static void tegra_plane_atomic_disable(struct drm_plane *plane,
5974aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
598c7679306SThierry Reding {
5994aa3df71SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
6004aa3df71SThierry Reding 	struct tegra_dc *dc;
6014aa3df71SThierry Reding 	unsigned long flags;
6024aa3df71SThierry Reding 	u32 value;
6034aa3df71SThierry Reding 
6044aa3df71SThierry Reding 	/* rien ne va plus */
6054aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
6064aa3df71SThierry Reding 		return;
6074aa3df71SThierry Reding 
6084aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
6094aa3df71SThierry Reding 
6104aa3df71SThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
6114aa3df71SThierry Reding 
6124aa3df71SThierry Reding 	value = WINDOW_A_SELECT << p->index;
6134aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
6144aa3df71SThierry Reding 
6154aa3df71SThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
6164aa3df71SThierry Reding 	value &= ~WIN_ENABLE;
6174aa3df71SThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
6184aa3df71SThierry Reding 
6194aa3df71SThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
620c7679306SThierry Reding }
621c7679306SThierry Reding 
6224aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
6234aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
6244aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
6254aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
626c7679306SThierry Reding };
627c7679306SThierry Reding 
628c7679306SThierry Reding static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
629c7679306SThierry Reding 						       struct tegra_dc *dc)
630c7679306SThierry Reding {
631518e6227SThierry Reding 	/*
632518e6227SThierry Reding 	 * Ideally this would use drm_crtc_mask(), but that would require the
633518e6227SThierry Reding 	 * CRTC to already be in the mode_config's list of CRTCs. However, it
634518e6227SThierry Reding 	 * will only be added to that list in the drm_crtc_init_with_planes()
635518e6227SThierry Reding 	 * (in tegra_dc_init()), which in turn requires registration of these
636518e6227SThierry Reding 	 * planes. So we have ourselves a nice little chicken and egg problem
637518e6227SThierry Reding 	 * here.
638518e6227SThierry Reding 	 *
639518e6227SThierry Reding 	 * We work around this by manually creating the mask from the number
640518e6227SThierry Reding 	 * of CRTCs that have been registered, and should therefore always be
641518e6227SThierry Reding 	 * the same as drm_crtc_index() after registration.
642518e6227SThierry Reding 	 */
643518e6227SThierry Reding 	unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
644c7679306SThierry Reding 	struct tegra_plane *plane;
645c7679306SThierry Reding 	unsigned int num_formats;
646c7679306SThierry Reding 	const u32 *formats;
647c7679306SThierry Reding 	int err;
648c7679306SThierry Reding 
649c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
650c7679306SThierry Reding 	if (!plane)
651c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
652c7679306SThierry Reding 
653c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
654c7679306SThierry Reding 	formats = tegra_primary_plane_formats;
655c7679306SThierry Reding 
656518e6227SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
657c7679306SThierry Reding 				       &tegra_primary_plane_funcs, formats,
658b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_PRIMARY,
659b0b3b795SVille Syrjälä 				       NULL);
660c7679306SThierry Reding 	if (err < 0) {
661c7679306SThierry Reding 		kfree(plane);
662c7679306SThierry Reding 		return ERR_PTR(err);
663c7679306SThierry Reding 	}
664c7679306SThierry Reding 
6654aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
6664aa3df71SThierry Reding 
667c7679306SThierry Reding 	return &plane->base;
668c7679306SThierry Reding }
669c7679306SThierry Reding 
670c7679306SThierry Reding static const u32 tegra_cursor_plane_formats[] = {
671c7679306SThierry Reding 	DRM_FORMAT_RGBA8888,
672c7679306SThierry Reding };
673c7679306SThierry Reding 
6744aa3df71SThierry Reding static int tegra_cursor_atomic_check(struct drm_plane *plane,
6754aa3df71SThierry Reding 				     struct drm_plane_state *state)
676c7679306SThierry Reding {
67747802b09SThierry Reding 	struct tegra_plane *tegra = to_tegra_plane(plane);
67847802b09SThierry Reding 	int err;
67947802b09SThierry Reding 
6804aa3df71SThierry Reding 	/* no need for further checks if the plane is being disabled */
6814aa3df71SThierry Reding 	if (!state->crtc)
6824aa3df71SThierry Reding 		return 0;
683c7679306SThierry Reding 
684c7679306SThierry Reding 	/* scaling not supported for cursor */
6854aa3df71SThierry Reding 	if ((state->src_w >> 16 != state->crtc_w) ||
6864aa3df71SThierry Reding 	    (state->src_h >> 16 != state->crtc_h))
687c7679306SThierry Reding 		return -EINVAL;
688c7679306SThierry Reding 
689c7679306SThierry Reding 	/* only square cursors supported */
6904aa3df71SThierry Reding 	if (state->src_w != state->src_h)
691c7679306SThierry Reding 		return -EINVAL;
692c7679306SThierry Reding 
6934aa3df71SThierry Reding 	if (state->crtc_w != 32 && state->crtc_w != 64 &&
6944aa3df71SThierry Reding 	    state->crtc_w != 128 && state->crtc_w != 256)
6954aa3df71SThierry Reding 		return -EINVAL;
6964aa3df71SThierry Reding 
69747802b09SThierry Reding 	err = tegra_plane_state_add(tegra, state);
69847802b09SThierry Reding 	if (err < 0)
69947802b09SThierry Reding 		return err;
70047802b09SThierry Reding 
7014aa3df71SThierry Reding 	return 0;
7024aa3df71SThierry Reding }
7034aa3df71SThierry Reding 
7044aa3df71SThierry Reding static void tegra_cursor_atomic_update(struct drm_plane *plane,
7054aa3df71SThierry Reding 				       struct drm_plane_state *old_state)
7064aa3df71SThierry Reding {
7074aa3df71SThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
7084aa3df71SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
7094aa3df71SThierry Reding 	struct drm_plane_state *state = plane->state;
7104aa3df71SThierry Reding 	u32 value = CURSOR_CLIP_DISPLAY;
7114aa3df71SThierry Reding 
7124aa3df71SThierry Reding 	/* rien ne va plus */
7134aa3df71SThierry Reding 	if (!plane->state->crtc || !plane->state->fb)
7144aa3df71SThierry Reding 		return;
7154aa3df71SThierry Reding 
7164aa3df71SThierry Reding 	switch (state->crtc_w) {
717c7679306SThierry Reding 	case 32:
718c7679306SThierry Reding 		value |= CURSOR_SIZE_32x32;
719c7679306SThierry Reding 		break;
720c7679306SThierry Reding 
721c7679306SThierry Reding 	case 64:
722c7679306SThierry Reding 		value |= CURSOR_SIZE_64x64;
723c7679306SThierry Reding 		break;
724c7679306SThierry Reding 
725c7679306SThierry Reding 	case 128:
726c7679306SThierry Reding 		value |= CURSOR_SIZE_128x128;
727c7679306SThierry Reding 		break;
728c7679306SThierry Reding 
729c7679306SThierry Reding 	case 256:
730c7679306SThierry Reding 		value |= CURSOR_SIZE_256x256;
731c7679306SThierry Reding 		break;
732c7679306SThierry Reding 
733c7679306SThierry Reding 	default:
7344aa3df71SThierry Reding 		WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
7354aa3df71SThierry Reding 		     state->crtc_h);
7364aa3df71SThierry Reding 		return;
737c7679306SThierry Reding 	}
738c7679306SThierry Reding 
739c7679306SThierry Reding 	value |= (bo->paddr >> 10) & 0x3fffff;
740c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
741c7679306SThierry Reding 
742c7679306SThierry Reding #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
743c7679306SThierry Reding 	value = (bo->paddr >> 32) & 0x3;
744c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
745c7679306SThierry Reding #endif
746c7679306SThierry Reding 
747c7679306SThierry Reding 	/* enable cursor and set blend mode */
748c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
749c7679306SThierry Reding 	value |= CURSOR_ENABLE;
750c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
751c7679306SThierry Reding 
752c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
753c7679306SThierry Reding 	value &= ~CURSOR_DST_BLEND_MASK;
754c7679306SThierry Reding 	value &= ~CURSOR_SRC_BLEND_MASK;
755c7679306SThierry Reding 	value |= CURSOR_MODE_NORMAL;
756c7679306SThierry Reding 	value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
757c7679306SThierry Reding 	value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
758c7679306SThierry Reding 	value |= CURSOR_ALPHA;
759c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
760c7679306SThierry Reding 
761c7679306SThierry Reding 	/* position the cursor */
7624aa3df71SThierry Reding 	value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
763c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
764c7679306SThierry Reding }
765c7679306SThierry Reding 
7664aa3df71SThierry Reding static void tegra_cursor_atomic_disable(struct drm_plane *plane,
7674aa3df71SThierry Reding 					struct drm_plane_state *old_state)
768c7679306SThierry Reding {
7694aa3df71SThierry Reding 	struct tegra_dc *dc;
770c7679306SThierry Reding 	u32 value;
771c7679306SThierry Reding 
7724aa3df71SThierry Reding 	/* rien ne va plus */
7734aa3df71SThierry Reding 	if (!old_state || !old_state->crtc)
7744aa3df71SThierry Reding 		return;
7754aa3df71SThierry Reding 
7764aa3df71SThierry Reding 	dc = to_tegra_dc(old_state->crtc);
777c7679306SThierry Reding 
778c7679306SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
779c7679306SThierry Reding 	value &= ~CURSOR_ENABLE;
780c7679306SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
781c7679306SThierry Reding }
782c7679306SThierry Reding 
783c7679306SThierry Reding static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
78407866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
78507866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
786c7679306SThierry Reding 	.destroy = tegra_plane_destroy,
7878f604f8cSThierry Reding 	.reset = tegra_plane_reset,
7888f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
7898f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
7904aa3df71SThierry Reding };
7914aa3df71SThierry Reding 
7924aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
7934aa3df71SThierry Reding 	.atomic_check = tegra_cursor_atomic_check,
7944aa3df71SThierry Reding 	.atomic_update = tegra_cursor_atomic_update,
7954aa3df71SThierry Reding 	.atomic_disable = tegra_cursor_atomic_disable,
796c7679306SThierry Reding };
797c7679306SThierry Reding 
798c7679306SThierry Reding static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
799c7679306SThierry Reding 						      struct tegra_dc *dc)
800c7679306SThierry Reding {
801c7679306SThierry Reding 	struct tegra_plane *plane;
802c7679306SThierry Reding 	unsigned int num_formats;
803c7679306SThierry Reding 	const u32 *formats;
804c7679306SThierry Reding 	int err;
805c7679306SThierry Reding 
806c7679306SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
807c7679306SThierry Reding 	if (!plane)
808c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
809c7679306SThierry Reding 
81047802b09SThierry Reding 	/*
811a1df3b24SThierry Reding 	 * This index is kind of fake. The cursor isn't a regular plane, but
812a1df3b24SThierry Reding 	 * its update and activation request bits in DC_CMD_STATE_CONTROL do
813a1df3b24SThierry Reding 	 * use the same programming. Setting this fake index here allows the
814a1df3b24SThierry Reding 	 * code in tegra_add_plane_state() to do the right thing without the
815a1df3b24SThierry Reding 	 * need to special-casing the cursor plane.
81647802b09SThierry Reding 	 */
81747802b09SThierry Reding 	plane->index = 6;
81847802b09SThierry Reding 
819c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
820c7679306SThierry Reding 	formats = tegra_cursor_plane_formats;
821c7679306SThierry Reding 
822c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
823c7679306SThierry Reding 				       &tegra_cursor_plane_funcs, formats,
824b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_CURSOR,
825b0b3b795SVille Syrjälä 				       NULL);
826c7679306SThierry Reding 	if (err < 0) {
827c7679306SThierry Reding 		kfree(plane);
828c7679306SThierry Reding 		return ERR_PTR(err);
829c7679306SThierry Reding 	}
830c7679306SThierry Reding 
8314aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
8324aa3df71SThierry Reding 
833c7679306SThierry Reding 	return &plane->base;
834c7679306SThierry Reding }
835c7679306SThierry Reding 
836c7679306SThierry Reding static void tegra_overlay_plane_destroy(struct drm_plane *plane)
837dee8268fSThierry Reding {
838c7679306SThierry Reding 	tegra_plane_destroy(plane);
839dee8268fSThierry Reding }
840dee8268fSThierry Reding 
841c7679306SThierry Reding static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
84207866963SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
84307866963SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
844c7679306SThierry Reding 	.destroy = tegra_overlay_plane_destroy,
8458f604f8cSThierry Reding 	.reset = tegra_plane_reset,
8468f604f8cSThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
8478f604f8cSThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
848dee8268fSThierry Reding };
849dee8268fSThierry Reding 
850c7679306SThierry Reding static const uint32_t tegra_overlay_plane_formats[] = {
851dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
852dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
853dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
854dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
855f925390eSThierry Reding 	DRM_FORMAT_YUYV,
856dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
857dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
858dee8268fSThierry Reding };
859dee8268fSThierry Reding 
8604aa3df71SThierry Reding static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
8614aa3df71SThierry Reding 	.atomic_check = tegra_plane_atomic_check,
8624aa3df71SThierry Reding 	.atomic_update = tegra_plane_atomic_update,
8634aa3df71SThierry Reding 	.atomic_disable = tegra_plane_atomic_disable,
8644aa3df71SThierry Reding };
8654aa3df71SThierry Reding 
866c7679306SThierry Reding static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
867c7679306SThierry Reding 						       struct tegra_dc *dc,
868c7679306SThierry Reding 						       unsigned int index)
869dee8268fSThierry Reding {
870dee8268fSThierry Reding 	struct tegra_plane *plane;
871c7679306SThierry Reding 	unsigned int num_formats;
872c7679306SThierry Reding 	const u32 *formats;
873c7679306SThierry Reding 	int err;
874dee8268fSThierry Reding 
875f002abc1SThierry Reding 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
876dee8268fSThierry Reding 	if (!plane)
877c7679306SThierry Reding 		return ERR_PTR(-ENOMEM);
878dee8268fSThierry Reding 
879c7679306SThierry Reding 	plane->index = index;
880dee8268fSThierry Reding 
881c7679306SThierry Reding 	num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
882c7679306SThierry Reding 	formats = tegra_overlay_plane_formats;
883c7679306SThierry Reding 
884c7679306SThierry Reding 	err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
885c7679306SThierry Reding 				       &tegra_overlay_plane_funcs, formats,
886b0b3b795SVille Syrjälä 				       num_formats, DRM_PLANE_TYPE_OVERLAY,
887b0b3b795SVille Syrjälä 				       NULL);
888f002abc1SThierry Reding 	if (err < 0) {
889f002abc1SThierry Reding 		kfree(plane);
890c7679306SThierry Reding 		return ERR_PTR(err);
891dee8268fSThierry Reding 	}
892c7679306SThierry Reding 
8934aa3df71SThierry Reding 	drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
8944aa3df71SThierry Reding 
895c7679306SThierry Reding 	return &plane->base;
896c7679306SThierry Reding }
897c7679306SThierry Reding 
898c7679306SThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
899c7679306SThierry Reding {
900c7679306SThierry Reding 	struct drm_plane *plane;
901c7679306SThierry Reding 	unsigned int i;
902c7679306SThierry Reding 
903c7679306SThierry Reding 	for (i = 0; i < 2; i++) {
904c7679306SThierry Reding 		plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
905c7679306SThierry Reding 		if (IS_ERR(plane))
906c7679306SThierry Reding 			return PTR_ERR(plane);
907f002abc1SThierry Reding 	}
908dee8268fSThierry Reding 
909dee8268fSThierry Reding 	return 0;
910dee8268fSThierry Reding }
911dee8268fSThierry Reding 
91242e9ce05SThierry Reding u32 tegra_dc_get_vblank_counter(struct tegra_dc *dc)
91342e9ce05SThierry Reding {
91442e9ce05SThierry Reding 	if (dc->syncpt)
91542e9ce05SThierry Reding 		return host1x_syncpt_read(dc->syncpt);
91642e9ce05SThierry Reding 
91742e9ce05SThierry Reding 	/* fallback to software emulated VBLANK counter */
91842e9ce05SThierry Reding 	return drm_crtc_vblank_count(&dc->base);
91942e9ce05SThierry Reding }
92042e9ce05SThierry Reding 
921dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
922dee8268fSThierry Reding {
923dee8268fSThierry Reding 	unsigned long value, flags;
924dee8268fSThierry Reding 
925dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
926dee8268fSThierry Reding 
927dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
928dee8268fSThierry Reding 	value |= VBLANK_INT;
929dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
930dee8268fSThierry Reding 
931dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
932dee8268fSThierry Reding }
933dee8268fSThierry Reding 
934dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
935dee8268fSThierry Reding {
936dee8268fSThierry Reding 	unsigned long value, flags;
937dee8268fSThierry Reding 
938dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
939dee8268fSThierry Reding 
940dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
941dee8268fSThierry Reding 	value &= ~VBLANK_INT;
942dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
943dee8268fSThierry Reding 
944dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
945dee8268fSThierry Reding }
946dee8268fSThierry Reding 
947dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
948dee8268fSThierry Reding {
949dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
950dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
951dee8268fSThierry Reding 	unsigned long flags, base;
952dee8268fSThierry Reding 	struct tegra_bo *bo;
953dee8268fSThierry Reding 
9546b59cc1cSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
9556b59cc1cSThierry Reding 
9566b59cc1cSThierry Reding 	if (!dc->event) {
9576b59cc1cSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
958dee8268fSThierry Reding 		return;
9596b59cc1cSThierry Reding 	}
960dee8268fSThierry Reding 
961f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
962dee8268fSThierry Reding 
9638643bc6dSDan Carpenter 	spin_lock(&dc->lock);
96493396d0fSSean Paul 
965dee8268fSThierry Reding 	/* check if new start address has been latched */
96693396d0fSSean Paul 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
967dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
968dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
969dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
970dee8268fSThierry Reding 
9718643bc6dSDan Carpenter 	spin_unlock(&dc->lock);
97293396d0fSSean Paul 
973f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
974ed7dae58SThierry Reding 		drm_crtc_send_vblank_event(crtc, dc->event);
975ed7dae58SThierry Reding 		drm_crtc_vblank_put(crtc);
976dee8268fSThierry Reding 		dc->event = NULL;
977dee8268fSThierry Reding 	}
9786b59cc1cSThierry Reding 
9796b59cc1cSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
980dee8268fSThierry Reding }
981dee8268fSThierry Reding 
982f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
983f002abc1SThierry Reding {
984f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
985f002abc1SThierry Reding }
986f002abc1SThierry Reding 
987ca915b10SThierry Reding static void tegra_crtc_reset(struct drm_crtc *crtc)
988ca915b10SThierry Reding {
989ca915b10SThierry Reding 	struct tegra_dc_state *state;
990ca915b10SThierry Reding 
9913b59b7acSThierry Reding 	if (crtc->state)
992ec2dc6a0SDaniel Vetter 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
9933b59b7acSThierry Reding 
994ca915b10SThierry Reding 	kfree(crtc->state);
995ca915b10SThierry Reding 	crtc->state = NULL;
996ca915b10SThierry Reding 
997ca915b10SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
998332bbe70SThierry Reding 	if (state) {
999ca915b10SThierry Reding 		crtc->state = &state->base;
1000332bbe70SThierry Reding 		crtc->state->crtc = crtc;
1001332bbe70SThierry Reding 	}
100231930d4dSThierry Reding 
100331930d4dSThierry Reding 	drm_crtc_vblank_reset(crtc);
1004ca915b10SThierry Reding }
1005ca915b10SThierry Reding 
1006ca915b10SThierry Reding static struct drm_crtc_state *
1007ca915b10SThierry Reding tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1008ca915b10SThierry Reding {
1009ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1010ca915b10SThierry Reding 	struct tegra_dc_state *copy;
1011ca915b10SThierry Reding 
10123b59b7acSThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1013ca915b10SThierry Reding 	if (!copy)
1014ca915b10SThierry Reding 		return NULL;
1015ca915b10SThierry Reding 
10163b59b7acSThierry Reding 	__drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
10173b59b7acSThierry Reding 	copy->clk = state->clk;
10183b59b7acSThierry Reding 	copy->pclk = state->pclk;
10193b59b7acSThierry Reding 	copy->div = state->div;
10203b59b7acSThierry Reding 	copy->planes = state->planes;
1021ca915b10SThierry Reding 
1022ca915b10SThierry Reding 	return &copy->base;
1023ca915b10SThierry Reding }
1024ca915b10SThierry Reding 
1025ca915b10SThierry Reding static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1026ca915b10SThierry Reding 					    struct drm_crtc_state *state)
1027ca915b10SThierry Reding {
1028ec2dc6a0SDaniel Vetter 	__drm_atomic_helper_crtc_destroy_state(state);
1029ca915b10SThierry Reding 	kfree(state);
1030ca915b10SThierry Reding }
1031ca915b10SThierry Reding 
1032dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
10331503ca47SThierry Reding 	.page_flip = drm_atomic_helper_page_flip,
103474f48791SThierry Reding 	.set_config = drm_atomic_helper_set_config,
1035f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
1036ca915b10SThierry Reding 	.reset = tegra_crtc_reset,
1037ca915b10SThierry Reding 	.atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1038ca915b10SThierry Reding 	.atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1039dee8268fSThierry Reding };
1040dee8268fSThierry Reding 
1041dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
1042dee8268fSThierry Reding 				struct drm_display_mode *mode)
1043dee8268fSThierry Reding {
10440444c0ffSThierry Reding 	unsigned int h_ref_to_sync = 1;
10450444c0ffSThierry Reding 	unsigned int v_ref_to_sync = 1;
1046dee8268fSThierry Reding 	unsigned long value;
1047dee8268fSThierry Reding 
1048dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1049dee8268fSThierry Reding 
1050dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
1051dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1052dee8268fSThierry Reding 
1053dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
1054dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
1055dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1056dee8268fSThierry Reding 
1057dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
1058dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
1059dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1060dee8268fSThierry Reding 
1061dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
1062dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
1063dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1064dee8268fSThierry Reding 
1065dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
1066dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1067dee8268fSThierry Reding 
1068dee8268fSThierry Reding 	return 0;
1069dee8268fSThierry Reding }
1070dee8268fSThierry Reding 
10719d910b60SThierry Reding /**
10729d910b60SThierry Reding  * tegra_dc_state_setup_clock - check clock settings and store them in atomic
10739d910b60SThierry Reding  *     state
10749d910b60SThierry Reding  * @dc: display controller
10759d910b60SThierry Reding  * @crtc_state: CRTC atomic state
10769d910b60SThierry Reding  * @clk: parent clock for display controller
10779d910b60SThierry Reding  * @pclk: pixel clock
10789d910b60SThierry Reding  * @div: shift clock divider
10799d910b60SThierry Reding  *
10809d910b60SThierry Reding  * Returns:
10819d910b60SThierry Reding  * 0 on success or a negative error-code on failure.
10829d910b60SThierry Reding  */
1083ca915b10SThierry Reding int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1084ca915b10SThierry Reding 			       struct drm_crtc_state *crtc_state,
1085ca915b10SThierry Reding 			       struct clk *clk, unsigned long pclk,
1086ca915b10SThierry Reding 			       unsigned int div)
1087ca915b10SThierry Reding {
1088ca915b10SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc_state);
1089ca915b10SThierry Reding 
1090d2982748SThierry Reding 	if (!clk_has_parent(dc->clk, clk))
1091d2982748SThierry Reding 		return -EINVAL;
1092d2982748SThierry Reding 
1093ca915b10SThierry Reding 	state->clk = clk;
1094ca915b10SThierry Reding 	state->pclk = pclk;
1095ca915b10SThierry Reding 	state->div = div;
1096ca915b10SThierry Reding 
1097ca915b10SThierry Reding 	return 0;
1098ca915b10SThierry Reding }
1099ca915b10SThierry Reding 
110076d59ed0SThierry Reding static void tegra_dc_commit_state(struct tegra_dc *dc,
110176d59ed0SThierry Reding 				  struct tegra_dc_state *state)
110276d59ed0SThierry Reding {
110376d59ed0SThierry Reding 	u32 value;
110476d59ed0SThierry Reding 	int err;
110576d59ed0SThierry Reding 
110676d59ed0SThierry Reding 	err = clk_set_parent(dc->clk, state->clk);
110776d59ed0SThierry Reding 	if (err < 0)
110876d59ed0SThierry Reding 		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
110976d59ed0SThierry Reding 
111076d59ed0SThierry Reding 	/*
111176d59ed0SThierry Reding 	 * Outputs may not want to change the parent clock rate. This is only
111276d59ed0SThierry Reding 	 * relevant to Tegra20 where only a single display PLL is available.
111376d59ed0SThierry Reding 	 * Since that PLL would typically be used for HDMI, an internal LVDS
111476d59ed0SThierry Reding 	 * panel would need to be driven by some other clock such as PLL_P
111576d59ed0SThierry Reding 	 * which is shared with other peripherals. Changing the clock rate
111676d59ed0SThierry Reding 	 * should therefore be avoided.
111776d59ed0SThierry Reding 	 */
111876d59ed0SThierry Reding 	if (state->pclk > 0) {
111976d59ed0SThierry Reding 		err = clk_set_rate(state->clk, state->pclk);
112076d59ed0SThierry Reding 		if (err < 0)
112176d59ed0SThierry Reding 			dev_err(dc->dev,
112276d59ed0SThierry Reding 				"failed to set clock rate to %lu Hz\n",
112376d59ed0SThierry Reding 				state->pclk);
112476d59ed0SThierry Reding 	}
112576d59ed0SThierry Reding 
112676d59ed0SThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
112776d59ed0SThierry Reding 		      state->div);
112876d59ed0SThierry Reding 	DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
112976d59ed0SThierry Reding 
113076d59ed0SThierry Reding 	value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
113176d59ed0SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
113276d59ed0SThierry Reding }
113376d59ed0SThierry Reding 
1134003fc848SThierry Reding static void tegra_dc_stop(struct tegra_dc *dc)
1135003fc848SThierry Reding {
1136003fc848SThierry Reding 	u32 value;
1137003fc848SThierry Reding 
1138003fc848SThierry Reding 	/* stop the display controller */
1139003fc848SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1140003fc848SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1141003fc848SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1142003fc848SThierry Reding 
1143003fc848SThierry Reding 	tegra_dc_commit(dc);
1144003fc848SThierry Reding }
1145003fc848SThierry Reding 
1146003fc848SThierry Reding static bool tegra_dc_idle(struct tegra_dc *dc)
1147003fc848SThierry Reding {
1148003fc848SThierry Reding 	u32 value;
1149003fc848SThierry Reding 
1150003fc848SThierry Reding 	value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1151003fc848SThierry Reding 
1152003fc848SThierry Reding 	return (value & DISP_CTRL_MODE_MASK) == 0;
1153003fc848SThierry Reding }
1154003fc848SThierry Reding 
1155003fc848SThierry Reding static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1156003fc848SThierry Reding {
1157003fc848SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
1158003fc848SThierry Reding 
1159003fc848SThierry Reding 	while (time_before(jiffies, timeout)) {
1160003fc848SThierry Reding 		if (tegra_dc_idle(dc))
1161003fc848SThierry Reding 			return 0;
1162003fc848SThierry Reding 
1163003fc848SThierry Reding 		usleep_range(1000, 2000);
1164003fc848SThierry Reding 	}
1165003fc848SThierry Reding 
1166003fc848SThierry Reding 	dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1167003fc848SThierry Reding 	return -ETIMEDOUT;
1168003fc848SThierry Reding }
1169003fc848SThierry Reding 
1170003fc848SThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
1171003fc848SThierry Reding {
1172003fc848SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1173003fc848SThierry Reding 	u32 value;
1174003fc848SThierry Reding 
1175003fc848SThierry Reding 	if (!tegra_dc_idle(dc)) {
1176003fc848SThierry Reding 		tegra_dc_stop(dc);
1177003fc848SThierry Reding 
1178003fc848SThierry Reding 		/*
1179003fc848SThierry Reding 		 * Ignore the return value, there isn't anything useful to do
1180003fc848SThierry Reding 		 * in case this fails.
1181003fc848SThierry Reding 		 */
1182003fc848SThierry Reding 		tegra_dc_wait_idle(dc, 100);
1183003fc848SThierry Reding 	}
1184003fc848SThierry Reding 
1185003fc848SThierry Reding 	/*
1186003fc848SThierry Reding 	 * This should really be part of the RGB encoder driver, but clearing
1187003fc848SThierry Reding 	 * these bits has the side-effect of stopping the display controller.
1188003fc848SThierry Reding 	 * When that happens no VBLANK interrupts will be raised. At the same
1189003fc848SThierry Reding 	 * time the encoder is disabled before the display controller, so the
1190003fc848SThierry Reding 	 * above code is always going to timeout waiting for the controller
1191003fc848SThierry Reding 	 * to go idle.
1192003fc848SThierry Reding 	 *
1193003fc848SThierry Reding 	 * Given the close coupling between the RGB encoder and the display
1194003fc848SThierry Reding 	 * controller doing it here is still kind of okay. None of the other
1195003fc848SThierry Reding 	 * encoder drivers require these bits to be cleared.
1196003fc848SThierry Reding 	 *
1197003fc848SThierry Reding 	 * XXX: Perhaps given that the display controller is switched off at
1198003fc848SThierry Reding 	 * this point anyway maybe clearing these bits isn't even useful for
1199003fc848SThierry Reding 	 * the RGB encoder?
1200003fc848SThierry Reding 	 */
1201003fc848SThierry Reding 	if (dc->rgb) {
1202003fc848SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1203003fc848SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1204003fc848SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1205003fc848SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1206003fc848SThierry Reding 	}
1207003fc848SThierry Reding 
1208003fc848SThierry Reding 	tegra_dc_stats_reset(&dc->stats);
1209003fc848SThierry Reding 	drm_crtc_vblank_off(crtc);
121033a8eb8dSThierry Reding 
121133a8eb8dSThierry Reding 	pm_runtime_put_sync(dc->dev);
1212003fc848SThierry Reding }
1213003fc848SThierry Reding 
1214003fc848SThierry Reding static void tegra_crtc_enable(struct drm_crtc *crtc)
1215dee8268fSThierry Reding {
12164aa3df71SThierry Reding 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
121776d59ed0SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
1218dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
1219dbb3f2f7SThierry Reding 	u32 value;
1220dee8268fSThierry Reding 
122133a8eb8dSThierry Reding 	pm_runtime_get_sync(dc->dev);
122233a8eb8dSThierry Reding 
122333a8eb8dSThierry Reding 	/* initialize display controller */
122433a8eb8dSThierry Reding 	if (dc->syncpt) {
122533a8eb8dSThierry Reding 		u32 syncpt = host1x_syncpt_id(dc->syncpt);
122633a8eb8dSThierry Reding 
122733a8eb8dSThierry Reding 		value = SYNCPT_CNTRL_NO_STALL;
122833a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
122933a8eb8dSThierry Reding 
123033a8eb8dSThierry Reding 		value = SYNCPT_VSYNC_ENABLE | syncpt;
123133a8eb8dSThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
123233a8eb8dSThierry Reding 	}
123333a8eb8dSThierry Reding 
123433a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
123533a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
123633a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
123733a8eb8dSThierry Reding 
123833a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
123933a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
124033a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
124133a8eb8dSThierry Reding 
124233a8eb8dSThierry Reding 	/* initialize timer */
124333a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
124433a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
124533a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
124633a8eb8dSThierry Reding 
124733a8eb8dSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
124833a8eb8dSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
124933a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
125033a8eb8dSThierry Reding 
125133a8eb8dSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
125233a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
125333a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
125433a8eb8dSThierry Reding 
125533a8eb8dSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
125633a8eb8dSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
125733a8eb8dSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
125833a8eb8dSThierry Reding 
125933a8eb8dSThierry Reding 	if (dc->soc->supports_border_color)
126033a8eb8dSThierry Reding 		tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
126133a8eb8dSThierry Reding 
126233a8eb8dSThierry Reding 	/* apply PLL and pixel clock changes */
126376d59ed0SThierry Reding 	tegra_dc_commit_state(dc, state);
126476d59ed0SThierry Reding 
1265dee8268fSThierry Reding 	/* program display mode */
1266dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
1267dee8268fSThierry Reding 
12688620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
12698620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
12708620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
12718620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
12728620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
12738620fc62SThierry Reding 	}
1274666cb873SThierry Reding 
1275666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1276666cb873SThierry Reding 	value &= ~DISP_CTRL_MODE_MASK;
1277666cb873SThierry Reding 	value |= DISP_CTRL_MODE_C_DISPLAY;
1278666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1279666cb873SThierry Reding 
1280666cb873SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1281666cb873SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1282666cb873SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1283666cb873SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1284666cb873SThierry Reding 
1285666cb873SThierry Reding 	tegra_dc_commit(dc);
1286dee8268fSThierry Reding 
12878ff64c17SThierry Reding 	drm_crtc_vblank_on(crtc);
1288dee8268fSThierry Reding }
1289dee8268fSThierry Reding 
12904aa3df71SThierry Reding static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
12914aa3df71SThierry Reding 				   struct drm_crtc_state *state)
12924aa3df71SThierry Reding {
12934aa3df71SThierry Reding 	return 0;
12944aa3df71SThierry Reding }
12954aa3df71SThierry Reding 
1296613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1297613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
12984aa3df71SThierry Reding {
12991503ca47SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
13001503ca47SThierry Reding 
13011503ca47SThierry Reding 	if (crtc->state->event) {
13021503ca47SThierry Reding 		crtc->state->event->pipe = drm_crtc_index(crtc);
13031503ca47SThierry Reding 
13041503ca47SThierry Reding 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
13051503ca47SThierry Reding 
13061503ca47SThierry Reding 		dc->event = crtc->state->event;
13071503ca47SThierry Reding 		crtc->state->event = NULL;
13081503ca47SThierry Reding 	}
13094aa3df71SThierry Reding }
13104aa3df71SThierry Reding 
1311613d2b27SMaarten Lankhorst static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1312613d2b27SMaarten Lankhorst 				    struct drm_crtc_state *old_crtc_state)
13134aa3df71SThierry Reding {
131447802b09SThierry Reding 	struct tegra_dc_state *state = to_dc_state(crtc->state);
131547802b09SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
131647802b09SThierry Reding 
131747802b09SThierry Reding 	tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
131847802b09SThierry Reding 	tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
13194aa3df71SThierry Reding }
13204aa3df71SThierry Reding 
1321dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
1322dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
1323003fc848SThierry Reding 	.enable = tegra_crtc_enable,
13244aa3df71SThierry Reding 	.atomic_check = tegra_crtc_atomic_check,
13254aa3df71SThierry Reding 	.atomic_begin = tegra_crtc_atomic_begin,
13264aa3df71SThierry Reding 	.atomic_flush = tegra_crtc_atomic_flush,
1327dee8268fSThierry Reding };
1328dee8268fSThierry Reding 
1329dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
1330dee8268fSThierry Reding {
1331dee8268fSThierry Reding 	struct tegra_dc *dc = data;
1332dee8268fSThierry Reding 	unsigned long status;
1333dee8268fSThierry Reding 
1334dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1335dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1336dee8268fSThierry Reding 
1337dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
1338dee8268fSThierry Reding 		/*
1339dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1340dee8268fSThierry Reding 		*/
1341791ddb1eSThierry Reding 		dc->stats.frames++;
1342dee8268fSThierry Reding 	}
1343dee8268fSThierry Reding 
1344dee8268fSThierry Reding 	if (status & VBLANK_INT) {
1345dee8268fSThierry Reding 		/*
1346dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1347dee8268fSThierry Reding 		*/
1348ed7dae58SThierry Reding 		drm_crtc_handle_vblank(&dc->base);
1349dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
1350791ddb1eSThierry Reding 		dc->stats.vblank++;
1351dee8268fSThierry Reding 	}
1352dee8268fSThierry Reding 
1353dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1354dee8268fSThierry Reding 		/*
1355dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1356dee8268fSThierry Reding 		*/
1357791ddb1eSThierry Reding 		dc->stats.underflow++;
1358791ddb1eSThierry Reding 	}
1359791ddb1eSThierry Reding 
1360791ddb1eSThierry Reding 	if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1361791ddb1eSThierry Reding 		/*
1362791ddb1eSThierry Reding 		dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1363791ddb1eSThierry Reding 		*/
1364791ddb1eSThierry Reding 		dc->stats.overflow++;
1365dee8268fSThierry Reding 	}
1366dee8268fSThierry Reding 
1367dee8268fSThierry Reding 	return IRQ_HANDLED;
1368dee8268fSThierry Reding }
1369dee8268fSThierry Reding 
1370dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
1371dee8268fSThierry Reding {
1372dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
1373dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1374003fc848SThierry Reding 	int err = 0;
1375003fc848SThierry Reding 
1376003fc848SThierry Reding 	drm_modeset_lock_crtc(&dc->base, NULL);
1377003fc848SThierry Reding 
1378003fc848SThierry Reding 	if (!dc->base.state->active) {
1379003fc848SThierry Reding 		err = -EBUSY;
1380003fc848SThierry Reding 		goto unlock;
1381003fc848SThierry Reding 	}
1382dee8268fSThierry Reding 
1383dee8268fSThierry Reding #define DUMP_REG(name)						\
138403a60569SThierry Reding 	seq_printf(s, "%-40s %#05x %08x\n", #name, name,	\
1385dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
1386dee8268fSThierry Reding 
1387dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1388dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1389dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1390dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1391dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1392dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1393dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1394dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1395dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1396dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1397dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1398dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1399dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1400dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1401dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1402dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
1403dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1404dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
1405dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
1406dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
1407dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
1408dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
1409dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1410dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1411dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1412dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
1413dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
1414dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1415dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1416dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
1417dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
1418dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1419dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1420dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1421dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1422dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1423dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1424dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1425dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1426dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1427dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1428dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1429dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1430dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1431dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1432dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1433dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1434dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1435dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1436dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1437dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1438dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1439dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1440dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1441dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1442dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1443dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1444dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1445dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1446dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1447dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1448dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
1449dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
1450dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1451dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1452dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
1453dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1454dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1455dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
1456dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1457dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1458dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1459dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1460dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1461dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1462dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1463dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1464dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
1465dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
1466dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
1467dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
1468dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
1469dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1470dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1471dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1472dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1473dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1474dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1475dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1476dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1477dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1478dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1479dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1480dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1481dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1482dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1483dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1484dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1485dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1486dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1487dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1488dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1489dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1490dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1491dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1492dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1493dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1494dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1495dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1496dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
1497dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
1498dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
1499dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
1500dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
1501dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
1502dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
1503dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
1504dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1505dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1506dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1507dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1508dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1509dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1510dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1511dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
1512dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1513dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1514dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1515dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1516dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1517dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1518dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1519dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1520dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
1521dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1522dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1523dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1524dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1525dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1526dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1527dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1528dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1529dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1530dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1531dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1532dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1533dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1534dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
1535dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
1536dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
1537dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
1538dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
1539dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
1540dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
1541dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
1542dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
1543dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
1544dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
1545dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1546dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1547dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1548dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1549dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1550dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1551dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1552dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1553dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1554dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1555dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1556dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1557dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1558dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1559dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1560dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1561dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1562e687651bSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1563e687651bSThierry Reding 	DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
1564dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1565dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1566dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1567dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1568dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1569dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1570dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1571dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1572dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1573dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1574dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1575dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1576dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1577dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1578dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1579dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1580dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1581dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1582dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1583dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1584dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1585dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1586dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1587dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1588dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1589dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1590dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1591dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1592dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1593dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1594dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1595dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1596dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1597dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1598dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1599dee8268fSThierry Reding 
1600dee8268fSThierry Reding #undef DUMP_REG
1601dee8268fSThierry Reding 
1602003fc848SThierry Reding unlock:
1603003fc848SThierry Reding 	drm_modeset_unlock_crtc(&dc->base);
1604003fc848SThierry Reding 	return err;
1605dee8268fSThierry Reding }
1606dee8268fSThierry Reding 
16076ca1f62fSThierry Reding static int tegra_dc_show_crc(struct seq_file *s, void *data)
16086ca1f62fSThierry Reding {
16096ca1f62fSThierry Reding 	struct drm_info_node *node = s->private;
16106ca1f62fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1611003fc848SThierry Reding 	int err = 0;
16126ca1f62fSThierry Reding 	u32 value;
16136ca1f62fSThierry Reding 
1614003fc848SThierry Reding 	drm_modeset_lock_crtc(&dc->base, NULL);
1615003fc848SThierry Reding 
1616003fc848SThierry Reding 	if (!dc->base.state->active) {
1617003fc848SThierry Reding 		err = -EBUSY;
1618003fc848SThierry Reding 		goto unlock;
1619003fc848SThierry Reding 	}
1620003fc848SThierry Reding 
16216ca1f62fSThierry Reding 	value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
16226ca1f62fSThierry Reding 	tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
16236ca1f62fSThierry Reding 	tegra_dc_commit(dc);
16246ca1f62fSThierry Reding 
16256ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16266ca1f62fSThierry Reding 	drm_crtc_wait_one_vblank(&dc->base);
16276ca1f62fSThierry Reding 
16286ca1f62fSThierry Reding 	value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
16296ca1f62fSThierry Reding 	seq_printf(s, "%08x\n", value);
16306ca1f62fSThierry Reding 
16316ca1f62fSThierry Reding 	tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
16326ca1f62fSThierry Reding 
1633003fc848SThierry Reding unlock:
1634003fc848SThierry Reding 	drm_modeset_unlock_crtc(&dc->base);
1635003fc848SThierry Reding 	return err;
16366ca1f62fSThierry Reding }
16376ca1f62fSThierry Reding 
1638791ddb1eSThierry Reding static int tegra_dc_show_stats(struct seq_file *s, void *data)
1639791ddb1eSThierry Reding {
1640791ddb1eSThierry Reding 	struct drm_info_node *node = s->private;
1641791ddb1eSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
1642791ddb1eSThierry Reding 
1643791ddb1eSThierry Reding 	seq_printf(s, "frames: %lu\n", dc->stats.frames);
1644791ddb1eSThierry Reding 	seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1645791ddb1eSThierry Reding 	seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1646791ddb1eSThierry Reding 	seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1647791ddb1eSThierry Reding 
1648dee8268fSThierry Reding 	return 0;
1649dee8268fSThierry Reding }
1650dee8268fSThierry Reding 
1651dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1652dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
16536ca1f62fSThierry Reding 	{ "crc", tegra_dc_show_crc, 0, NULL },
1654791ddb1eSThierry Reding 	{ "stats", tegra_dc_show_stats, 0, NULL },
1655dee8268fSThierry Reding };
1656dee8268fSThierry Reding 
1657dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1658dee8268fSThierry Reding {
1659dee8268fSThierry Reding 	unsigned int i;
1660dee8268fSThierry Reding 	char *name;
1661dee8268fSThierry Reding 	int err;
1662dee8268fSThierry Reding 
1663dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1664dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1665dee8268fSThierry Reding 	kfree(name);
1666dee8268fSThierry Reding 
1667dee8268fSThierry Reding 	if (!dc->debugfs)
1668dee8268fSThierry Reding 		return -ENOMEM;
1669dee8268fSThierry Reding 
1670dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1671dee8268fSThierry Reding 				    GFP_KERNEL);
1672dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1673dee8268fSThierry Reding 		err = -ENOMEM;
1674dee8268fSThierry Reding 		goto remove;
1675dee8268fSThierry Reding 	}
1676dee8268fSThierry Reding 
1677dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1678dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1679dee8268fSThierry Reding 
1680dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1681dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1682dee8268fSThierry Reding 				       dc->debugfs, minor);
1683dee8268fSThierry Reding 	if (err < 0)
1684dee8268fSThierry Reding 		goto free;
1685dee8268fSThierry Reding 
1686dee8268fSThierry Reding 	dc->minor = minor;
1687dee8268fSThierry Reding 
1688dee8268fSThierry Reding 	return 0;
1689dee8268fSThierry Reding 
1690dee8268fSThierry Reding free:
1691dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1692dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1693dee8268fSThierry Reding remove:
1694dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1695dee8268fSThierry Reding 	dc->debugfs = NULL;
1696dee8268fSThierry Reding 
1697dee8268fSThierry Reding 	return err;
1698dee8268fSThierry Reding }
1699dee8268fSThierry Reding 
1700dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1701dee8268fSThierry Reding {
1702dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1703dee8268fSThierry Reding 				 dc->minor);
1704dee8268fSThierry Reding 	dc->minor = NULL;
1705dee8268fSThierry Reding 
1706dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1707dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1708dee8268fSThierry Reding 
1709dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1710dee8268fSThierry Reding 	dc->debugfs = NULL;
1711dee8268fSThierry Reding 
1712dee8268fSThierry Reding 	return 0;
1713dee8268fSThierry Reding }
1714dee8268fSThierry Reding 
1715dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1716dee8268fSThierry Reding {
17179910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
17182bcdcbfaSThierry Reding 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1719dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1720d1f3e1e0SThierry Reding 	struct tegra_drm *tegra = drm->dev_private;
1721c7679306SThierry Reding 	struct drm_plane *primary = NULL;
1722c7679306SThierry Reding 	struct drm_plane *cursor = NULL;
1723dee8268fSThierry Reding 	int err;
1724dee8268fSThierry Reding 
17252bcdcbfaSThierry Reding 	dc->syncpt = host1x_syncpt_request(dc->dev, flags);
17262bcdcbfaSThierry Reding 	if (!dc->syncpt)
17272bcdcbfaSThierry Reding 		dev_warn(dc->dev, "failed to allocate syncpoint\n");
17282bcdcbfaSThierry Reding 
1729df06b759SThierry Reding 	if (tegra->domain) {
1730df06b759SThierry Reding 		err = iommu_attach_device(tegra->domain, dc->dev);
1731df06b759SThierry Reding 		if (err < 0) {
1732df06b759SThierry Reding 			dev_err(dc->dev, "failed to attach to domain: %d\n",
1733df06b759SThierry Reding 				err);
1734df06b759SThierry Reding 			return err;
1735df06b759SThierry Reding 		}
1736df06b759SThierry Reding 
1737df06b759SThierry Reding 		dc->domain = tegra->domain;
1738df06b759SThierry Reding 	}
1739df06b759SThierry Reding 
1740c7679306SThierry Reding 	primary = tegra_dc_primary_plane_create(drm, dc);
1741c7679306SThierry Reding 	if (IS_ERR(primary)) {
1742c7679306SThierry Reding 		err = PTR_ERR(primary);
1743c7679306SThierry Reding 		goto cleanup;
1744c7679306SThierry Reding 	}
1745c7679306SThierry Reding 
1746c7679306SThierry Reding 	if (dc->soc->supports_cursor) {
1747c7679306SThierry Reding 		cursor = tegra_dc_cursor_plane_create(drm, dc);
1748c7679306SThierry Reding 		if (IS_ERR(cursor)) {
1749c7679306SThierry Reding 			err = PTR_ERR(cursor);
1750c7679306SThierry Reding 			goto cleanup;
1751c7679306SThierry Reding 		}
1752c7679306SThierry Reding 	}
1753c7679306SThierry Reding 
1754c7679306SThierry Reding 	err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1755f9882876SVille Syrjälä 					&tegra_crtc_funcs, NULL);
1756c7679306SThierry Reding 	if (err < 0)
1757c7679306SThierry Reding 		goto cleanup;
1758c7679306SThierry Reding 
1759dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1760dee8268fSThierry Reding 
1761d1f3e1e0SThierry Reding 	/*
1762d1f3e1e0SThierry Reding 	 * Keep track of the minimum pitch alignment across all display
1763d1f3e1e0SThierry Reding 	 * controllers.
1764d1f3e1e0SThierry Reding 	 */
1765d1f3e1e0SThierry Reding 	if (dc->soc->pitch_align > tegra->pitch_align)
1766d1f3e1e0SThierry Reding 		tegra->pitch_align = dc->soc->pitch_align;
1767d1f3e1e0SThierry Reding 
17689910f5c4SThierry Reding 	err = tegra_dc_rgb_init(drm, dc);
1769dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1770dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1771c7679306SThierry Reding 		goto cleanup;
1772dee8268fSThierry Reding 	}
1773dee8268fSThierry Reding 
17749910f5c4SThierry Reding 	err = tegra_dc_add_planes(drm, dc);
1775dee8268fSThierry Reding 	if (err < 0)
1776c7679306SThierry Reding 		goto cleanup;
1777dee8268fSThierry Reding 
1778dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
17799910f5c4SThierry Reding 		err = tegra_dc_debugfs_init(dc, drm->primary);
1780dee8268fSThierry Reding 		if (err < 0)
1781dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1782dee8268fSThierry Reding 	}
1783dee8268fSThierry Reding 
1784dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1785dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1786dee8268fSThierry Reding 	if (err < 0) {
1787dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1788dee8268fSThierry Reding 			err);
1789c7679306SThierry Reding 		goto cleanup;
1790dee8268fSThierry Reding 	}
1791dee8268fSThierry Reding 
1792dee8268fSThierry Reding 	return 0;
1793c7679306SThierry Reding 
1794c7679306SThierry Reding cleanup:
1795c7679306SThierry Reding 	if (cursor)
1796c7679306SThierry Reding 		drm_plane_cleanup(cursor);
1797c7679306SThierry Reding 
1798c7679306SThierry Reding 	if (primary)
1799c7679306SThierry Reding 		drm_plane_cleanup(primary);
1800c7679306SThierry Reding 
1801c7679306SThierry Reding 	if (tegra->domain) {
1802c7679306SThierry Reding 		iommu_detach_device(tegra->domain, dc->dev);
1803c7679306SThierry Reding 		dc->domain = NULL;
1804c7679306SThierry Reding 	}
1805c7679306SThierry Reding 
1806c7679306SThierry Reding 	return err;
1807dee8268fSThierry Reding }
1808dee8268fSThierry Reding 
1809dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1810dee8268fSThierry Reding {
1811dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1812dee8268fSThierry Reding 	int err;
1813dee8268fSThierry Reding 
1814dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1815dee8268fSThierry Reding 
1816dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1817dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1818dee8268fSThierry Reding 		if (err < 0)
1819dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1820dee8268fSThierry Reding 	}
1821dee8268fSThierry Reding 
1822dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1823dee8268fSThierry Reding 	if (err) {
1824dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1825dee8268fSThierry Reding 		return err;
1826dee8268fSThierry Reding 	}
1827dee8268fSThierry Reding 
1828df06b759SThierry Reding 	if (dc->domain) {
1829df06b759SThierry Reding 		iommu_detach_device(dc->domain, dc->dev);
1830df06b759SThierry Reding 		dc->domain = NULL;
1831df06b759SThierry Reding 	}
1832df06b759SThierry Reding 
18332bcdcbfaSThierry Reding 	host1x_syncpt_free(dc->syncpt);
18342bcdcbfaSThierry Reding 
1835dee8268fSThierry Reding 	return 0;
1836dee8268fSThierry Reding }
1837dee8268fSThierry Reding 
1838dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1839dee8268fSThierry Reding 	.init = tegra_dc_init,
1840dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1841dee8268fSThierry Reding };
1842dee8268fSThierry Reding 
18438620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
184442d0659bSThierry Reding 	.supports_border_color = true,
18458620fc62SThierry Reding 	.supports_interlacing = false,
1846e687651bSThierry Reding 	.supports_cursor = false,
1847c134f019SThierry Reding 	.supports_block_linear = false,
1848d1f3e1e0SThierry Reding 	.pitch_align = 8,
18499c012700SThierry Reding 	.has_powergate = false,
18508620fc62SThierry Reding };
18518620fc62SThierry Reding 
18528620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
185342d0659bSThierry Reding 	.supports_border_color = true,
18548620fc62SThierry Reding 	.supports_interlacing = false,
1855e687651bSThierry Reding 	.supports_cursor = false,
1856c134f019SThierry Reding 	.supports_block_linear = false,
1857d1f3e1e0SThierry Reding 	.pitch_align = 8,
18589c012700SThierry Reding 	.has_powergate = false,
1859d1f3e1e0SThierry Reding };
1860d1f3e1e0SThierry Reding 
1861d1f3e1e0SThierry Reding static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
186242d0659bSThierry Reding 	.supports_border_color = true,
1863d1f3e1e0SThierry Reding 	.supports_interlacing = false,
1864d1f3e1e0SThierry Reding 	.supports_cursor = false,
1865d1f3e1e0SThierry Reding 	.supports_block_linear = false,
1866d1f3e1e0SThierry Reding 	.pitch_align = 64,
18679c012700SThierry Reding 	.has_powergate = true,
18688620fc62SThierry Reding };
18698620fc62SThierry Reding 
18708620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
187142d0659bSThierry Reding 	.supports_border_color = false,
18728620fc62SThierry Reding 	.supports_interlacing = true,
1873e687651bSThierry Reding 	.supports_cursor = true,
1874c134f019SThierry Reding 	.supports_block_linear = true,
1875d1f3e1e0SThierry Reding 	.pitch_align = 64,
18769c012700SThierry Reding 	.has_powergate = true,
18778620fc62SThierry Reding };
18788620fc62SThierry Reding 
18795b4f516fSThierry Reding static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
18805b4f516fSThierry Reding 	.supports_border_color = false,
18815b4f516fSThierry Reding 	.supports_interlacing = true,
18825b4f516fSThierry Reding 	.supports_cursor = true,
18835b4f516fSThierry Reding 	.supports_block_linear = true,
18845b4f516fSThierry Reding 	.pitch_align = 64,
18855b4f516fSThierry Reding 	.has_powergate = true,
18865b4f516fSThierry Reding };
18875b4f516fSThierry Reding 
18888620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
18898620fc62SThierry Reding 	{
18905b4f516fSThierry Reding 		.compatible = "nvidia,tegra210-dc",
18915b4f516fSThierry Reding 		.data = &tegra210_dc_soc_info,
18925b4f516fSThierry Reding 	}, {
18938620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
18948620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
18958620fc62SThierry Reding 	}, {
18969c012700SThierry Reding 		.compatible = "nvidia,tegra114-dc",
18979c012700SThierry Reding 		.data = &tegra114_dc_soc_info,
18989c012700SThierry Reding 	}, {
18998620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
19008620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
19018620fc62SThierry Reding 	}, {
19028620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
19038620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
19048620fc62SThierry Reding 	}, {
19058620fc62SThierry Reding 		/* sentinel */
19068620fc62SThierry Reding 	}
19078620fc62SThierry Reding };
1908ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
19098620fc62SThierry Reding 
191013411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
191113411dddSThierry Reding {
191213411dddSThierry Reding 	struct device_node *np;
191313411dddSThierry Reding 	u32 value = 0;
191413411dddSThierry Reding 	int err;
191513411dddSThierry Reding 
191613411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
191713411dddSThierry Reding 	if (err < 0) {
191813411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
191913411dddSThierry Reding 
192013411dddSThierry Reding 		/*
192113411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
192213411dddSThierry Reding 		 * correct head number by looking up the position of this
192313411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
192413411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
192513411dddSThierry Reding 		 * that the translation into a flattened device tree blob
192613411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
192713411dddSThierry Reding 		 * head number.
192813411dddSThierry Reding 		 *
192913411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
193013411dddSThierry Reding 		 * cases where only a single display controller is used.
193113411dddSThierry Reding 		 */
193213411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
1933cf6b1744SJulia Lawall 			if (np == dc->dev->of_node) {
1934cf6b1744SJulia Lawall 				of_node_put(np);
193513411dddSThierry Reding 				break;
1936cf6b1744SJulia Lawall 			}
193713411dddSThierry Reding 
193813411dddSThierry Reding 			value++;
193913411dddSThierry Reding 		}
194013411dddSThierry Reding 	}
194113411dddSThierry Reding 
194213411dddSThierry Reding 	dc->pipe = value;
194313411dddSThierry Reding 
194413411dddSThierry Reding 	return 0;
194513411dddSThierry Reding }
194613411dddSThierry Reding 
1947dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1948dee8268fSThierry Reding {
19498620fc62SThierry Reding 	const struct of_device_id *id;
1950dee8268fSThierry Reding 	struct resource *regs;
1951dee8268fSThierry Reding 	struct tegra_dc *dc;
1952dee8268fSThierry Reding 	int err;
1953dee8268fSThierry Reding 
1954dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1955dee8268fSThierry Reding 	if (!dc)
1956dee8268fSThierry Reding 		return -ENOMEM;
1957dee8268fSThierry Reding 
19588620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
19598620fc62SThierry Reding 	if (!id)
19608620fc62SThierry Reding 		return -ENODEV;
19618620fc62SThierry Reding 
1962dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1963dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1964dee8268fSThierry Reding 	dc->dev = &pdev->dev;
19658620fc62SThierry Reding 	dc->soc = id->data;
1966dee8268fSThierry Reding 
196713411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
196813411dddSThierry Reding 	if (err < 0)
196913411dddSThierry Reding 		return err;
197013411dddSThierry Reding 
1971dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1972dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1973dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1974dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1975dee8268fSThierry Reding 	}
1976dee8268fSThierry Reding 
1977ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1978ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1979ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1980ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1981ca48080aSStephen Warren 	}
1982ca48080aSStephen Warren 
198333a8eb8dSThierry Reding 	reset_control_assert(dc->rst);
198433a8eb8dSThierry Reding 
19859c012700SThierry Reding 	if (dc->soc->has_powergate) {
19869c012700SThierry Reding 		if (dc->pipe == 0)
19879c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DIS;
19889c012700SThierry Reding 		else
19899c012700SThierry Reding 			dc->powergate = TEGRA_POWERGATE_DISB;
19909c012700SThierry Reding 
199133a8eb8dSThierry Reding 		tegra_powergate_power_off(dc->powergate);
19929c012700SThierry Reding 	}
1993dee8268fSThierry Reding 
1994dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1995dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1996dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1997dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1998dee8268fSThierry Reding 
1999dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
2000dee8268fSThierry Reding 	if (dc->irq < 0) {
2001dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
2002dee8268fSThierry Reding 		return -ENXIO;
2003dee8268fSThierry Reding 	}
2004dee8268fSThierry Reding 
2005dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
2006dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
2007dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2008dee8268fSThierry Reding 		return err;
2009dee8268fSThierry Reding 	}
2010dee8268fSThierry Reding 
201133a8eb8dSThierry Reding 	platform_set_drvdata(pdev, dc);
201233a8eb8dSThierry Reding 	pm_runtime_enable(&pdev->dev);
201333a8eb8dSThierry Reding 
201433a8eb8dSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
201533a8eb8dSThierry Reding 	dc->client.ops = &dc_client_ops;
201633a8eb8dSThierry Reding 	dc->client.dev = &pdev->dev;
201733a8eb8dSThierry Reding 
2018dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
2019dee8268fSThierry Reding 	if (err < 0) {
2020dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2021dee8268fSThierry Reding 			err);
2022dee8268fSThierry Reding 		return err;
2023dee8268fSThierry Reding 	}
2024dee8268fSThierry Reding 
2025dee8268fSThierry Reding 	return 0;
2026dee8268fSThierry Reding }
2027dee8268fSThierry Reding 
2028dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
2029dee8268fSThierry Reding {
2030dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
2031dee8268fSThierry Reding 	int err;
2032dee8268fSThierry Reding 
2033dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
2034dee8268fSThierry Reding 	if (err < 0) {
2035dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2036dee8268fSThierry Reding 			err);
2037dee8268fSThierry Reding 		return err;
2038dee8268fSThierry Reding 	}
2039dee8268fSThierry Reding 
204059d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
204159d29c0eSThierry Reding 	if (err < 0) {
204259d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
204359d29c0eSThierry Reding 		return err;
204459d29c0eSThierry Reding 	}
204559d29c0eSThierry Reding 
204633a8eb8dSThierry Reding 	pm_runtime_disable(&pdev->dev);
204733a8eb8dSThierry Reding 
204833a8eb8dSThierry Reding 	return 0;
204933a8eb8dSThierry Reding }
205033a8eb8dSThierry Reding 
205133a8eb8dSThierry Reding #ifdef CONFIG_PM
205233a8eb8dSThierry Reding static int tegra_dc_suspend(struct device *dev)
205333a8eb8dSThierry Reding {
205433a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
205533a8eb8dSThierry Reding 	int err;
205633a8eb8dSThierry Reding 
205733a8eb8dSThierry Reding 	err = reset_control_assert(dc->rst);
205833a8eb8dSThierry Reding 	if (err < 0) {
205933a8eb8dSThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
206033a8eb8dSThierry Reding 		return err;
206133a8eb8dSThierry Reding 	}
20629c012700SThierry Reding 
20639c012700SThierry Reding 	if (dc->soc->has_powergate)
20649c012700SThierry Reding 		tegra_powergate_power_off(dc->powergate);
20659c012700SThierry Reding 
2066dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
2067dee8268fSThierry Reding 
2068dee8268fSThierry Reding 	return 0;
2069dee8268fSThierry Reding }
2070dee8268fSThierry Reding 
207133a8eb8dSThierry Reding static int tegra_dc_resume(struct device *dev)
207233a8eb8dSThierry Reding {
207333a8eb8dSThierry Reding 	struct tegra_dc *dc = dev_get_drvdata(dev);
207433a8eb8dSThierry Reding 	int err;
207533a8eb8dSThierry Reding 
207633a8eb8dSThierry Reding 	if (dc->soc->has_powergate) {
207733a8eb8dSThierry Reding 		err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
207833a8eb8dSThierry Reding 							dc->rst);
207933a8eb8dSThierry Reding 		if (err < 0) {
208033a8eb8dSThierry Reding 			dev_err(dev, "failed to power partition: %d\n", err);
208133a8eb8dSThierry Reding 			return err;
208233a8eb8dSThierry Reding 		}
208333a8eb8dSThierry Reding 	} else {
208433a8eb8dSThierry Reding 		err = clk_prepare_enable(dc->clk);
208533a8eb8dSThierry Reding 		if (err < 0) {
208633a8eb8dSThierry Reding 			dev_err(dev, "failed to enable clock: %d\n", err);
208733a8eb8dSThierry Reding 			return err;
208833a8eb8dSThierry Reding 		}
208933a8eb8dSThierry Reding 
209033a8eb8dSThierry Reding 		err = reset_control_deassert(dc->rst);
209133a8eb8dSThierry Reding 		if (err < 0) {
209233a8eb8dSThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
209333a8eb8dSThierry Reding 			return err;
209433a8eb8dSThierry Reding 		}
209533a8eb8dSThierry Reding 	}
209633a8eb8dSThierry Reding 
209733a8eb8dSThierry Reding 	return 0;
209833a8eb8dSThierry Reding }
209933a8eb8dSThierry Reding #endif
210033a8eb8dSThierry Reding 
210133a8eb8dSThierry Reding static const struct dev_pm_ops tegra_dc_pm_ops = {
210233a8eb8dSThierry Reding 	SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
210333a8eb8dSThierry Reding };
210433a8eb8dSThierry Reding 
2105dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
2106dee8268fSThierry Reding 	.driver = {
2107dee8268fSThierry Reding 		.name = "tegra-dc",
2108dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
210933a8eb8dSThierry Reding 		.pm = &tegra_dc_pm_ops,
2110dee8268fSThierry Reding 	},
2111dee8268fSThierry Reding 	.probe = tegra_dc_probe,
2112dee8268fSThierry Reding 	.remove = tegra_dc_remove,
2113dee8268fSThierry Reding };
2114