xref: /openbmc/linux/drivers/gpu/drm/tegra/dc.c (revision f925390efccb24016a4fafe77721770021ed754a)
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>
12ca48080aSStephen Warren #include <linux/reset.h>
13dee8268fSThierry Reding 
14dee8268fSThierry Reding #include "dc.h"
15dee8268fSThierry Reding #include "drm.h"
16dee8268fSThierry Reding #include "gem.h"
17dee8268fSThierry Reding 
188620fc62SThierry Reding struct tegra_dc_soc_info {
198620fc62SThierry Reding 	bool supports_interlacing;
208620fc62SThierry Reding };
218620fc62SThierry Reding 
22dee8268fSThierry Reding struct tegra_plane {
23dee8268fSThierry Reding 	struct drm_plane base;
24dee8268fSThierry Reding 	unsigned int index;
25dee8268fSThierry Reding };
26dee8268fSThierry Reding 
27dee8268fSThierry Reding static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
28dee8268fSThierry Reding {
29dee8268fSThierry Reding 	return container_of(plane, struct tegra_plane, base);
30dee8268fSThierry Reding }
31dee8268fSThierry Reding 
32dee8268fSThierry Reding static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
33dee8268fSThierry Reding 			      struct drm_framebuffer *fb, int crtc_x,
34dee8268fSThierry Reding 			      int crtc_y, unsigned int crtc_w,
35dee8268fSThierry Reding 			      unsigned int crtc_h, uint32_t src_x,
36dee8268fSThierry Reding 			      uint32_t src_y, uint32_t src_w, uint32_t src_h)
37dee8268fSThierry Reding {
38dee8268fSThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
39dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
40dee8268fSThierry Reding 	struct tegra_dc_window window;
41dee8268fSThierry Reding 	unsigned int i;
42dee8268fSThierry Reding 
43dee8268fSThierry Reding 	memset(&window, 0, sizeof(window));
44dee8268fSThierry Reding 	window.src.x = src_x >> 16;
45dee8268fSThierry Reding 	window.src.y = src_y >> 16;
46dee8268fSThierry Reding 	window.src.w = src_w >> 16;
47dee8268fSThierry Reding 	window.src.h = src_h >> 16;
48dee8268fSThierry Reding 	window.dst.x = crtc_x;
49dee8268fSThierry Reding 	window.dst.y = crtc_y;
50dee8268fSThierry Reding 	window.dst.w = crtc_w;
51dee8268fSThierry Reding 	window.dst.h = crtc_h;
52*f925390eSThierry Reding 	window.format = tegra_dc_format(fb->pixel_format, &window.swap);
53dee8268fSThierry Reding 	window.bits_per_pixel = fb->bits_per_pixel;
54db7fbdfdSThierry Reding 	window.bottom_up = tegra_fb_is_bottom_up(fb);
55773af77fSThierry Reding 	window.tiled = tegra_fb_is_tiled(fb);
56dee8268fSThierry Reding 
57dee8268fSThierry Reding 	for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
58dee8268fSThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
59dee8268fSThierry Reding 
60dee8268fSThierry Reding 		window.base[i] = bo->paddr + fb->offsets[i];
61dee8268fSThierry Reding 
62dee8268fSThierry Reding 		/*
63dee8268fSThierry Reding 		 * Tegra doesn't support different strides for U and V planes
64dee8268fSThierry Reding 		 * so we display a warning if the user tries to display a
65dee8268fSThierry Reding 		 * framebuffer with such a configuration.
66dee8268fSThierry Reding 		 */
67dee8268fSThierry Reding 		if (i >= 2) {
68dee8268fSThierry Reding 			if (fb->pitches[i] != window.stride[1])
69dee8268fSThierry Reding 				DRM_ERROR("unsupported UV-plane configuration\n");
70dee8268fSThierry Reding 		} else {
71dee8268fSThierry Reding 			window.stride[i] = fb->pitches[i];
72dee8268fSThierry Reding 		}
73dee8268fSThierry Reding 	}
74dee8268fSThierry Reding 
75dee8268fSThierry Reding 	return tegra_dc_setup_window(dc, p->index, &window);
76dee8268fSThierry Reding }
77dee8268fSThierry Reding 
78dee8268fSThierry Reding static int tegra_plane_disable(struct drm_plane *plane)
79dee8268fSThierry Reding {
80dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(plane->crtc);
81dee8268fSThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
82dee8268fSThierry Reding 	unsigned long value;
83dee8268fSThierry Reding 
84dee8268fSThierry Reding 	if (!plane->crtc)
85dee8268fSThierry Reding 		return 0;
86dee8268fSThierry Reding 
87dee8268fSThierry Reding 	value = WINDOW_A_SELECT << p->index;
88dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
89dee8268fSThierry Reding 
90dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
91dee8268fSThierry Reding 	value &= ~WIN_ENABLE;
92dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
93dee8268fSThierry Reding 
94dee8268fSThierry Reding 	tegra_dc_writel(dc, WIN_A_UPDATE << p->index, DC_CMD_STATE_CONTROL);
95dee8268fSThierry Reding 	tegra_dc_writel(dc, WIN_A_ACT_REQ << p->index, DC_CMD_STATE_CONTROL);
96dee8268fSThierry Reding 
97dee8268fSThierry Reding 	return 0;
98dee8268fSThierry Reding }
99dee8268fSThierry Reding 
100dee8268fSThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
101dee8268fSThierry Reding {
102f002abc1SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
103f002abc1SThierry Reding 
104dee8268fSThierry Reding 	tegra_plane_disable(plane);
105dee8268fSThierry Reding 	drm_plane_cleanup(plane);
106f002abc1SThierry Reding 	kfree(p);
107dee8268fSThierry Reding }
108dee8268fSThierry Reding 
109dee8268fSThierry Reding static const struct drm_plane_funcs tegra_plane_funcs = {
110dee8268fSThierry Reding 	.update_plane = tegra_plane_update,
111dee8268fSThierry Reding 	.disable_plane = tegra_plane_disable,
112dee8268fSThierry Reding 	.destroy = tegra_plane_destroy,
113dee8268fSThierry Reding };
114dee8268fSThierry Reding 
115dee8268fSThierry Reding static const uint32_t plane_formats[] = {
116dee8268fSThierry Reding 	DRM_FORMAT_XBGR8888,
117dee8268fSThierry Reding 	DRM_FORMAT_XRGB8888,
118dee8268fSThierry Reding 	DRM_FORMAT_RGB565,
119dee8268fSThierry Reding 	DRM_FORMAT_UYVY,
120*f925390eSThierry Reding 	DRM_FORMAT_YUYV,
121dee8268fSThierry Reding 	DRM_FORMAT_YUV420,
122dee8268fSThierry Reding 	DRM_FORMAT_YUV422,
123dee8268fSThierry Reding };
124dee8268fSThierry Reding 
125dee8268fSThierry Reding static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
126dee8268fSThierry Reding {
127dee8268fSThierry Reding 	unsigned int i;
128dee8268fSThierry Reding 	int err = 0;
129dee8268fSThierry Reding 
130dee8268fSThierry Reding 	for (i = 0; i < 2; i++) {
131dee8268fSThierry Reding 		struct tegra_plane *plane;
132dee8268fSThierry Reding 
133f002abc1SThierry Reding 		plane = kzalloc(sizeof(*plane), GFP_KERNEL);
134dee8268fSThierry Reding 		if (!plane)
135dee8268fSThierry Reding 			return -ENOMEM;
136dee8268fSThierry Reding 
137dee8268fSThierry Reding 		plane->index = 1 + i;
138dee8268fSThierry Reding 
139dee8268fSThierry Reding 		err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
140dee8268fSThierry Reding 				     &tegra_plane_funcs, plane_formats,
141dee8268fSThierry Reding 				     ARRAY_SIZE(plane_formats), false);
142f002abc1SThierry Reding 		if (err < 0) {
143f002abc1SThierry Reding 			kfree(plane);
144dee8268fSThierry Reding 			return err;
145dee8268fSThierry Reding 		}
146f002abc1SThierry Reding 	}
147dee8268fSThierry Reding 
148dee8268fSThierry Reding 	return 0;
149dee8268fSThierry Reding }
150dee8268fSThierry Reding 
151dee8268fSThierry Reding static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
152dee8268fSThierry Reding 			     struct drm_framebuffer *fb)
153dee8268fSThierry Reding {
154dee8268fSThierry Reding 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
155db7fbdfdSThierry Reding 	unsigned int h_offset = 0, v_offset = 0;
156*f925390eSThierry Reding 	unsigned int format, swap;
157dee8268fSThierry Reding 	unsigned long value;
158dee8268fSThierry Reding 
159dee8268fSThierry Reding 	tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
160dee8268fSThierry Reding 
161dee8268fSThierry Reding 	value = fb->offsets[0] + y * fb->pitches[0] +
162dee8268fSThierry Reding 		x * fb->bits_per_pixel / 8;
163dee8268fSThierry Reding 
164dee8268fSThierry Reding 	tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
165dee8268fSThierry Reding 	tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
166*f925390eSThierry Reding 
167*f925390eSThierry Reding 	format = tegra_dc_format(fb->pixel_format, &swap);
168dee8268fSThierry Reding 	tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
169*f925390eSThierry Reding 	tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
170dee8268fSThierry Reding 
171773af77fSThierry Reding 	if (tegra_fb_is_tiled(fb)) {
172773af77fSThierry Reding 		value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
173773af77fSThierry Reding 			DC_WIN_BUFFER_ADDR_MODE_TILE;
174773af77fSThierry Reding 	} else {
175773af77fSThierry Reding 		value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
176773af77fSThierry Reding 			DC_WIN_BUFFER_ADDR_MODE_LINEAR;
177773af77fSThierry Reding 	}
178773af77fSThierry Reding 
179773af77fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
180773af77fSThierry Reding 
181db7fbdfdSThierry Reding 	/* make sure bottom-up buffers are properly displayed */
182db7fbdfdSThierry Reding 	if (tegra_fb_is_bottom_up(fb)) {
183db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
184db7fbdfdSThierry Reding 		value |= INVERT_V;
185db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
186db7fbdfdSThierry Reding 
187db7fbdfdSThierry Reding 		v_offset += fb->height - 1;
188db7fbdfdSThierry Reding 	} else {
189db7fbdfdSThierry Reding 		value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
190db7fbdfdSThierry Reding 		value &= ~INVERT_V;
191db7fbdfdSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
192db7fbdfdSThierry Reding 	}
193db7fbdfdSThierry Reding 
194db7fbdfdSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
195db7fbdfdSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
196db7fbdfdSThierry Reding 
197dee8268fSThierry Reding 	value = GENERAL_UPDATE | WIN_A_UPDATE;
198dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
199dee8268fSThierry Reding 
200dee8268fSThierry Reding 	value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
201dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
202dee8268fSThierry Reding 
203dee8268fSThierry Reding 	return 0;
204dee8268fSThierry Reding }
205dee8268fSThierry Reding 
206dee8268fSThierry Reding void tegra_dc_enable_vblank(struct tegra_dc *dc)
207dee8268fSThierry Reding {
208dee8268fSThierry Reding 	unsigned long value, flags;
209dee8268fSThierry Reding 
210dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
211dee8268fSThierry Reding 
212dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
213dee8268fSThierry Reding 	value |= VBLANK_INT;
214dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
215dee8268fSThierry Reding 
216dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
217dee8268fSThierry Reding }
218dee8268fSThierry Reding 
219dee8268fSThierry Reding void tegra_dc_disable_vblank(struct tegra_dc *dc)
220dee8268fSThierry Reding {
221dee8268fSThierry Reding 	unsigned long value, flags;
222dee8268fSThierry Reding 
223dee8268fSThierry Reding 	spin_lock_irqsave(&dc->lock, flags);
224dee8268fSThierry Reding 
225dee8268fSThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
226dee8268fSThierry Reding 	value &= ~VBLANK_INT;
227dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
228dee8268fSThierry Reding 
229dee8268fSThierry Reding 	spin_unlock_irqrestore(&dc->lock, flags);
230dee8268fSThierry Reding }
231dee8268fSThierry Reding 
232dee8268fSThierry Reding static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
233dee8268fSThierry Reding {
234dee8268fSThierry Reding 	struct drm_device *drm = dc->base.dev;
235dee8268fSThierry Reding 	struct drm_crtc *crtc = &dc->base;
236dee8268fSThierry Reding 	unsigned long flags, base;
237dee8268fSThierry Reding 	struct tegra_bo *bo;
238dee8268fSThierry Reding 
239dee8268fSThierry Reding 	if (!dc->event)
240dee8268fSThierry Reding 		return;
241dee8268fSThierry Reding 
242f4510a27SMatt Roper 	bo = tegra_fb_get_plane(crtc->primary->fb, 0);
243dee8268fSThierry Reding 
244dee8268fSThierry Reding 	/* check if new start address has been latched */
245dee8268fSThierry Reding 	tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
246dee8268fSThierry Reding 	base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
247dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
248dee8268fSThierry Reding 
249f4510a27SMatt Roper 	if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
250dee8268fSThierry Reding 		spin_lock_irqsave(&drm->event_lock, flags);
251dee8268fSThierry Reding 		drm_send_vblank_event(drm, dc->pipe, dc->event);
252dee8268fSThierry Reding 		drm_vblank_put(drm, dc->pipe);
253dee8268fSThierry Reding 		dc->event = NULL;
254dee8268fSThierry Reding 		spin_unlock_irqrestore(&drm->event_lock, flags);
255dee8268fSThierry Reding 	}
256dee8268fSThierry Reding }
257dee8268fSThierry Reding 
258dee8268fSThierry Reding void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
259dee8268fSThierry Reding {
260dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
261dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
262dee8268fSThierry Reding 	unsigned long flags;
263dee8268fSThierry Reding 
264dee8268fSThierry Reding 	spin_lock_irqsave(&drm->event_lock, flags);
265dee8268fSThierry Reding 
266dee8268fSThierry Reding 	if (dc->event && dc->event->base.file_priv == file) {
267dee8268fSThierry Reding 		dc->event->base.destroy(&dc->event->base);
268dee8268fSThierry Reding 		drm_vblank_put(drm, dc->pipe);
269dee8268fSThierry Reding 		dc->event = NULL;
270dee8268fSThierry Reding 	}
271dee8268fSThierry Reding 
272dee8268fSThierry Reding 	spin_unlock_irqrestore(&drm->event_lock, flags);
273dee8268fSThierry Reding }
274dee8268fSThierry Reding 
275dee8268fSThierry Reding static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
276dee8268fSThierry Reding 			      struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
277dee8268fSThierry Reding {
278dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
279dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
280dee8268fSThierry Reding 
281dee8268fSThierry Reding 	if (dc->event)
282dee8268fSThierry Reding 		return -EBUSY;
283dee8268fSThierry Reding 
284dee8268fSThierry Reding 	if (event) {
285dee8268fSThierry Reding 		event->pipe = dc->pipe;
286dee8268fSThierry Reding 		dc->event = event;
287dee8268fSThierry Reding 		drm_vblank_get(drm, dc->pipe);
288dee8268fSThierry Reding 	}
289dee8268fSThierry Reding 
290dee8268fSThierry Reding 	tegra_dc_set_base(dc, 0, 0, fb);
291f4510a27SMatt Roper 	crtc->primary->fb = fb;
292dee8268fSThierry Reding 
293dee8268fSThierry Reding 	return 0;
294dee8268fSThierry Reding }
295dee8268fSThierry Reding 
296f002abc1SThierry Reding static void drm_crtc_clear(struct drm_crtc *crtc)
297f002abc1SThierry Reding {
298f002abc1SThierry Reding 	memset(crtc, 0, sizeof(*crtc));
299f002abc1SThierry Reding }
300f002abc1SThierry Reding 
301f002abc1SThierry Reding static void tegra_dc_destroy(struct drm_crtc *crtc)
302f002abc1SThierry Reding {
303f002abc1SThierry Reding 	drm_crtc_cleanup(crtc);
304f002abc1SThierry Reding 	drm_crtc_clear(crtc);
305f002abc1SThierry Reding }
306f002abc1SThierry Reding 
307dee8268fSThierry Reding static const struct drm_crtc_funcs tegra_crtc_funcs = {
308dee8268fSThierry Reding 	.page_flip = tegra_dc_page_flip,
309dee8268fSThierry Reding 	.set_config = drm_crtc_helper_set_config,
310f002abc1SThierry Reding 	.destroy = tegra_dc_destroy,
311dee8268fSThierry Reding };
312dee8268fSThierry Reding 
313dee8268fSThierry Reding static void tegra_crtc_disable(struct drm_crtc *crtc)
314dee8268fSThierry Reding {
315f002abc1SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
316dee8268fSThierry Reding 	struct drm_device *drm = crtc->dev;
317dee8268fSThierry Reding 	struct drm_plane *plane;
318dee8268fSThierry Reding 
3192b4c3661SDaniel Vetter 	drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
320dee8268fSThierry Reding 		if (plane->crtc == crtc) {
321dee8268fSThierry Reding 			tegra_plane_disable(plane);
322dee8268fSThierry Reding 			plane->crtc = NULL;
323dee8268fSThierry Reding 
324dee8268fSThierry Reding 			if (plane->fb) {
325dee8268fSThierry Reding 				drm_framebuffer_unreference(plane->fb);
326dee8268fSThierry Reding 				plane->fb = NULL;
327dee8268fSThierry Reding 			}
328dee8268fSThierry Reding 		}
329dee8268fSThierry Reding 	}
330f002abc1SThierry Reding 
331f002abc1SThierry Reding 	drm_vblank_off(drm, dc->pipe);
332dee8268fSThierry Reding }
333dee8268fSThierry Reding 
334dee8268fSThierry Reding static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
335dee8268fSThierry Reding 				  const struct drm_display_mode *mode,
336dee8268fSThierry Reding 				  struct drm_display_mode *adjusted)
337dee8268fSThierry Reding {
338dee8268fSThierry Reding 	return true;
339dee8268fSThierry Reding }
340dee8268fSThierry Reding 
341dee8268fSThierry Reding static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
342dee8268fSThierry Reding 				  unsigned int bpp)
343dee8268fSThierry Reding {
344dee8268fSThierry Reding 	fixed20_12 outf = dfixed_init(out);
345dee8268fSThierry Reding 	fixed20_12 inf = dfixed_init(in);
346dee8268fSThierry Reding 	u32 dda_inc;
347dee8268fSThierry Reding 	int max;
348dee8268fSThierry Reding 
349dee8268fSThierry Reding 	if (v)
350dee8268fSThierry Reding 		max = 15;
351dee8268fSThierry Reding 	else {
352dee8268fSThierry Reding 		switch (bpp) {
353dee8268fSThierry Reding 		case 2:
354dee8268fSThierry Reding 			max = 8;
355dee8268fSThierry Reding 			break;
356dee8268fSThierry Reding 
357dee8268fSThierry Reding 		default:
358dee8268fSThierry Reding 			WARN_ON_ONCE(1);
359dee8268fSThierry Reding 			/* fallthrough */
360dee8268fSThierry Reding 		case 4:
361dee8268fSThierry Reding 			max = 4;
362dee8268fSThierry Reding 			break;
363dee8268fSThierry Reding 		}
364dee8268fSThierry Reding 	}
365dee8268fSThierry Reding 
366dee8268fSThierry Reding 	outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
367dee8268fSThierry Reding 	inf.full -= dfixed_const(1);
368dee8268fSThierry Reding 
369dee8268fSThierry Reding 	dda_inc = dfixed_div(inf, outf);
370dee8268fSThierry Reding 	dda_inc = min_t(u32, dda_inc, dfixed_const(max));
371dee8268fSThierry Reding 
372dee8268fSThierry Reding 	return dda_inc;
373dee8268fSThierry Reding }
374dee8268fSThierry Reding 
375dee8268fSThierry Reding static inline u32 compute_initial_dda(unsigned int in)
376dee8268fSThierry Reding {
377dee8268fSThierry Reding 	fixed20_12 inf = dfixed_init(in);
378dee8268fSThierry Reding 	return dfixed_frac(inf);
379dee8268fSThierry Reding }
380dee8268fSThierry Reding 
381dee8268fSThierry Reding static int tegra_dc_set_timings(struct tegra_dc *dc,
382dee8268fSThierry Reding 				struct drm_display_mode *mode)
383dee8268fSThierry Reding {
384dee8268fSThierry Reding 	/* TODO: For HDMI compliance, h & v ref_to_sync should be set to 1 */
385dee8268fSThierry Reding 	unsigned int h_ref_to_sync = 0;
386dee8268fSThierry Reding 	unsigned int v_ref_to_sync = 0;
387dee8268fSThierry Reding 	unsigned long value;
388dee8268fSThierry Reding 
389dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
390dee8268fSThierry Reding 
391dee8268fSThierry Reding 	value = (v_ref_to_sync << 16) | h_ref_to_sync;
392dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
393dee8268fSThierry Reding 
394dee8268fSThierry Reding 	value = ((mode->vsync_end - mode->vsync_start) << 16) |
395dee8268fSThierry Reding 		((mode->hsync_end - mode->hsync_start) <<  0);
396dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
397dee8268fSThierry Reding 
398dee8268fSThierry Reding 	value = ((mode->vtotal - mode->vsync_end) << 16) |
399dee8268fSThierry Reding 		((mode->htotal - mode->hsync_end) <<  0);
400dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
401dee8268fSThierry Reding 
402dee8268fSThierry Reding 	value = ((mode->vsync_start - mode->vdisplay) << 16) |
403dee8268fSThierry Reding 		((mode->hsync_start - mode->hdisplay) <<  0);
404dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
405dee8268fSThierry Reding 
406dee8268fSThierry Reding 	value = (mode->vdisplay << 16) | mode->hdisplay;
407dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
408dee8268fSThierry Reding 
409dee8268fSThierry Reding 	return 0;
410dee8268fSThierry Reding }
411dee8268fSThierry Reding 
412dee8268fSThierry Reding static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
413dee8268fSThierry Reding 				struct drm_display_mode *mode,
414dee8268fSThierry Reding 				unsigned long *div)
415dee8268fSThierry Reding {
416dee8268fSThierry Reding 	unsigned long pclk = mode->clock * 1000, rate;
417dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
418dee8268fSThierry Reding 	struct tegra_output *output = NULL;
419dee8268fSThierry Reding 	struct drm_encoder *encoder;
420dee8268fSThierry Reding 	long err;
421dee8268fSThierry Reding 
422dee8268fSThierry Reding 	list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
423dee8268fSThierry Reding 		if (encoder->crtc == crtc) {
424dee8268fSThierry Reding 			output = encoder_to_output(encoder);
425dee8268fSThierry Reding 			break;
426dee8268fSThierry Reding 		}
427dee8268fSThierry Reding 
428dee8268fSThierry Reding 	if (!output)
429dee8268fSThierry Reding 		return -ENODEV;
430dee8268fSThierry Reding 
431dee8268fSThierry Reding 	/*
432dee8268fSThierry Reding 	 * This assumes that the display controller will divide its parent
433dee8268fSThierry Reding 	 * clock by 2 to generate the pixel clock.
434dee8268fSThierry Reding 	 */
435dee8268fSThierry Reding 	err = tegra_output_setup_clock(output, dc->clk, pclk * 2);
436dee8268fSThierry Reding 	if (err < 0) {
437dee8268fSThierry Reding 		dev_err(dc->dev, "failed to setup clock: %ld\n", err);
438dee8268fSThierry Reding 		return err;
439dee8268fSThierry Reding 	}
440dee8268fSThierry Reding 
441dee8268fSThierry Reding 	rate = clk_get_rate(dc->clk);
442dee8268fSThierry Reding 	*div = (rate * 2 / pclk) - 2;
443dee8268fSThierry Reding 
444dee8268fSThierry Reding 	DRM_DEBUG_KMS("rate: %lu, div: %lu\n", rate, *div);
445dee8268fSThierry Reding 
446dee8268fSThierry Reding 	return 0;
447dee8268fSThierry Reding }
448dee8268fSThierry Reding 
449dee8268fSThierry Reding static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
450dee8268fSThierry Reding {
451dee8268fSThierry Reding 	switch (format) {
452dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
453dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
454dee8268fSThierry Reding 		if (planar)
455dee8268fSThierry Reding 			*planar = false;
456dee8268fSThierry Reding 
457dee8268fSThierry Reding 		return true;
458dee8268fSThierry Reding 
459dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
460dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
461dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
462dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
463dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
464dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
465dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
466dee8268fSThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
467dee8268fSThierry Reding 		if (planar)
468dee8268fSThierry Reding 			*planar = true;
469dee8268fSThierry Reding 
470dee8268fSThierry Reding 		return true;
471dee8268fSThierry Reding 	}
472dee8268fSThierry Reding 
473dee8268fSThierry Reding 	return false;
474dee8268fSThierry Reding }
475dee8268fSThierry Reding 
476dee8268fSThierry Reding int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
477dee8268fSThierry Reding 			  const struct tegra_dc_window *window)
478dee8268fSThierry Reding {
479dee8268fSThierry Reding 	unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
480dee8268fSThierry Reding 	unsigned long value;
481dee8268fSThierry Reding 	bool yuv, planar;
482dee8268fSThierry Reding 
483dee8268fSThierry Reding 	/*
484dee8268fSThierry Reding 	 * For YUV planar modes, the number of bytes per pixel takes into
485dee8268fSThierry Reding 	 * account only the luma component and therefore is 1.
486dee8268fSThierry Reding 	 */
487dee8268fSThierry Reding 	yuv = tegra_dc_format_is_yuv(window->format, &planar);
488dee8268fSThierry Reding 	if (!yuv)
489dee8268fSThierry Reding 		bpp = window->bits_per_pixel / 8;
490dee8268fSThierry Reding 	else
491dee8268fSThierry Reding 		bpp = planar ? 1 : 2;
492dee8268fSThierry Reding 
493dee8268fSThierry Reding 	value = WINDOW_A_SELECT << index;
494dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
495dee8268fSThierry Reding 
496dee8268fSThierry Reding 	tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
497*f925390eSThierry Reding 	tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
498dee8268fSThierry Reding 
499dee8268fSThierry Reding 	value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
500dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_POSITION);
501dee8268fSThierry Reding 
502dee8268fSThierry Reding 	value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
503dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_SIZE);
504dee8268fSThierry Reding 
505dee8268fSThierry Reding 	h_offset = window->src.x * bpp;
506dee8268fSThierry Reding 	v_offset = window->src.y;
507dee8268fSThierry Reding 	h_size = window->src.w * bpp;
508dee8268fSThierry Reding 	v_size = window->src.h;
509dee8268fSThierry Reding 
510dee8268fSThierry Reding 	value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
511dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
512dee8268fSThierry Reding 
513dee8268fSThierry Reding 	/*
514dee8268fSThierry Reding 	 * For DDA computations the number of bytes per pixel for YUV planar
515dee8268fSThierry Reding 	 * modes needs to take into account all Y, U and V components.
516dee8268fSThierry Reding 	 */
517dee8268fSThierry Reding 	if (yuv && planar)
518dee8268fSThierry Reding 		bpp = 2;
519dee8268fSThierry Reding 
520dee8268fSThierry Reding 	h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
521dee8268fSThierry Reding 	v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
522dee8268fSThierry Reding 
523dee8268fSThierry Reding 	value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
524dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
525dee8268fSThierry Reding 
526dee8268fSThierry Reding 	h_dda = compute_initial_dda(window->src.x);
527dee8268fSThierry Reding 	v_dda = compute_initial_dda(window->src.y);
528dee8268fSThierry Reding 
529dee8268fSThierry Reding 	tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
530dee8268fSThierry Reding 	tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
531dee8268fSThierry Reding 
532dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
533dee8268fSThierry Reding 	tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
534dee8268fSThierry Reding 
535dee8268fSThierry Reding 	tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
536dee8268fSThierry Reding 
537dee8268fSThierry Reding 	if (yuv && planar) {
538dee8268fSThierry Reding 		tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
539dee8268fSThierry Reding 		tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
540dee8268fSThierry Reding 		value = window->stride[1] << 16 | window->stride[0];
541dee8268fSThierry Reding 		tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
542dee8268fSThierry Reding 	} else {
543dee8268fSThierry Reding 		tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
544dee8268fSThierry Reding 	}
545dee8268fSThierry Reding 
546db7fbdfdSThierry Reding 	if (window->bottom_up)
547db7fbdfdSThierry Reding 		v_offset += window->src.h - 1;
548db7fbdfdSThierry Reding 
549dee8268fSThierry Reding 	tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
550dee8268fSThierry Reding 	tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
551dee8268fSThierry Reding 
552773af77fSThierry Reding 	if (window->tiled) {
553773af77fSThierry Reding 		value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
554773af77fSThierry Reding 			DC_WIN_BUFFER_ADDR_MODE_TILE;
555773af77fSThierry Reding 	} else {
556773af77fSThierry Reding 		value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
557773af77fSThierry Reding 			DC_WIN_BUFFER_ADDR_MODE_LINEAR;
558773af77fSThierry Reding 	}
559773af77fSThierry Reding 
560773af77fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
561773af77fSThierry Reding 
562dee8268fSThierry Reding 	value = WIN_ENABLE;
563dee8268fSThierry Reding 
564dee8268fSThierry Reding 	if (yuv) {
565dee8268fSThierry Reding 		/* setup default colorspace conversion coefficients */
566dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
567dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
568dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
569dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
570dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
571dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
572dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
573dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
574dee8268fSThierry Reding 
575dee8268fSThierry Reding 		value |= CSC_ENABLE;
576dee8268fSThierry Reding 	} else if (window->bits_per_pixel < 24) {
577dee8268fSThierry Reding 		value |= COLOR_EXPAND;
578dee8268fSThierry Reding 	}
579dee8268fSThierry Reding 
580db7fbdfdSThierry Reding 	if (window->bottom_up)
581db7fbdfdSThierry Reding 		value |= INVERT_V;
582db7fbdfdSThierry Reding 
583dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
584dee8268fSThierry Reding 
585dee8268fSThierry Reding 	/*
586dee8268fSThierry Reding 	 * Disable blending and assume Window A is the bottom-most window,
587dee8268fSThierry Reding 	 * Window C is the top-most window and Window B is in the middle.
588dee8268fSThierry Reding 	 */
589dee8268fSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
590dee8268fSThierry Reding 	tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
591dee8268fSThierry Reding 
592dee8268fSThierry Reding 	switch (index) {
593dee8268fSThierry Reding 	case 0:
594dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
595dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
596dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
597dee8268fSThierry Reding 		break;
598dee8268fSThierry Reding 
599dee8268fSThierry Reding 	case 1:
600dee8268fSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
601dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
602dee8268fSThierry Reding 		tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
603dee8268fSThierry Reding 		break;
604dee8268fSThierry Reding 
605dee8268fSThierry Reding 	case 2:
606dee8268fSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
607dee8268fSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
608dee8268fSThierry Reding 		tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
609dee8268fSThierry Reding 		break;
610dee8268fSThierry Reding 	}
611dee8268fSThierry Reding 
612dee8268fSThierry Reding 	tegra_dc_writel(dc, WIN_A_UPDATE << index, DC_CMD_STATE_CONTROL);
613dee8268fSThierry Reding 	tegra_dc_writel(dc, WIN_A_ACT_REQ << index, DC_CMD_STATE_CONTROL);
614dee8268fSThierry Reding 
615dee8268fSThierry Reding 	return 0;
616dee8268fSThierry Reding }
617dee8268fSThierry Reding 
618*f925390eSThierry Reding unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
619dee8268fSThierry Reding {
620*f925390eSThierry Reding 	/* assume no swapping of fetched data */
621*f925390eSThierry Reding 	if (swap)
622*f925390eSThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
623*f925390eSThierry Reding 
624dee8268fSThierry Reding 	switch (format) {
625dee8268fSThierry Reding 	case DRM_FORMAT_XBGR8888:
626dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_R8G8B8A8;
627dee8268fSThierry Reding 
628dee8268fSThierry Reding 	case DRM_FORMAT_XRGB8888:
629dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_B8G8R8A8;
630dee8268fSThierry Reding 
631dee8268fSThierry Reding 	case DRM_FORMAT_RGB565:
632dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_B5G6R5;
633dee8268fSThierry Reding 
634dee8268fSThierry Reding 	case DRM_FORMAT_UYVY:
635dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
636dee8268fSThierry Reding 
637*f925390eSThierry Reding 	case DRM_FORMAT_YUYV:
638*f925390eSThierry Reding 		if (swap)
639*f925390eSThierry Reding 			*swap = BYTE_SWAP_SWAP2;
640*f925390eSThierry Reding 
641*f925390eSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422;
642*f925390eSThierry Reding 
643dee8268fSThierry Reding 	case DRM_FORMAT_YUV420:
644dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr420P;
645dee8268fSThierry Reding 
646dee8268fSThierry Reding 	case DRM_FORMAT_YUV422:
647dee8268fSThierry Reding 		return WIN_COLOR_DEPTH_YCbCr422P;
648dee8268fSThierry Reding 
649dee8268fSThierry Reding 	default:
650dee8268fSThierry Reding 		break;
651dee8268fSThierry Reding 	}
652dee8268fSThierry Reding 
653dee8268fSThierry Reding 	WARN(1, "unsupported pixel format %u, using default\n", format);
654dee8268fSThierry Reding 	return WIN_COLOR_DEPTH_B8G8R8A8;
655dee8268fSThierry Reding }
656dee8268fSThierry Reding 
657dee8268fSThierry Reding static int tegra_crtc_mode_set(struct drm_crtc *crtc,
658dee8268fSThierry Reding 			       struct drm_display_mode *mode,
659dee8268fSThierry Reding 			       struct drm_display_mode *adjusted,
660dee8268fSThierry Reding 			       int x, int y, struct drm_framebuffer *old_fb)
661dee8268fSThierry Reding {
662f4510a27SMatt Roper 	struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
663dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
664dee8268fSThierry Reding 	struct tegra_dc_window window;
665dee8268fSThierry Reding 	unsigned long div, value;
666dee8268fSThierry Reding 	int err;
667dee8268fSThierry Reding 
668dee8268fSThierry Reding 	drm_vblank_pre_modeset(crtc->dev, dc->pipe);
669dee8268fSThierry Reding 
670dee8268fSThierry Reding 	err = tegra_crtc_setup_clk(crtc, mode, &div);
671dee8268fSThierry Reding 	if (err) {
672dee8268fSThierry Reding 		dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
673dee8268fSThierry Reding 		return err;
674dee8268fSThierry Reding 	}
675dee8268fSThierry Reding 
676dee8268fSThierry Reding 	/* program display mode */
677dee8268fSThierry Reding 	tegra_dc_set_timings(dc, mode);
678dee8268fSThierry Reding 
6798620fc62SThierry Reding 	/* interlacing isn't supported yet, so disable it */
6808620fc62SThierry Reding 	if (dc->soc->supports_interlacing) {
6818620fc62SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
6828620fc62SThierry Reding 		value &= ~INTERLACE_ENABLE;
6838620fc62SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
6848620fc62SThierry Reding 	}
6858620fc62SThierry Reding 
686dee8268fSThierry Reding 	value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
687dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
688dee8268fSThierry Reding 
689dee8268fSThierry Reding 	/* setup window parameters */
690dee8268fSThierry Reding 	memset(&window, 0, sizeof(window));
691dee8268fSThierry Reding 	window.src.x = 0;
692dee8268fSThierry Reding 	window.src.y = 0;
693dee8268fSThierry Reding 	window.src.w = mode->hdisplay;
694dee8268fSThierry Reding 	window.src.h = mode->vdisplay;
695dee8268fSThierry Reding 	window.dst.x = 0;
696dee8268fSThierry Reding 	window.dst.y = 0;
697dee8268fSThierry Reding 	window.dst.w = mode->hdisplay;
698dee8268fSThierry Reding 	window.dst.h = mode->vdisplay;
699*f925390eSThierry Reding 	window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
700*f925390eSThierry Reding 					&window.swap);
701f4510a27SMatt Roper 	window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
702f4510a27SMatt Roper 	window.stride[0] = crtc->primary->fb->pitches[0];
703dee8268fSThierry Reding 	window.base[0] = bo->paddr;
704dee8268fSThierry Reding 
705dee8268fSThierry Reding 	err = tegra_dc_setup_window(dc, 0, &window);
706dee8268fSThierry Reding 	if (err < 0)
707dee8268fSThierry Reding 		dev_err(dc->dev, "failed to enable root plane\n");
708dee8268fSThierry Reding 
709dee8268fSThierry Reding 	return 0;
710dee8268fSThierry Reding }
711dee8268fSThierry Reding 
712dee8268fSThierry Reding static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
713dee8268fSThierry Reding 				    struct drm_framebuffer *old_fb)
714dee8268fSThierry Reding {
715dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
716dee8268fSThierry Reding 
717f4510a27SMatt Roper 	return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
718dee8268fSThierry Reding }
719dee8268fSThierry Reding 
720dee8268fSThierry Reding static void tegra_crtc_prepare(struct drm_crtc *crtc)
721dee8268fSThierry Reding {
722dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
723dee8268fSThierry Reding 	unsigned int syncpt;
724dee8268fSThierry Reding 	unsigned long value;
725dee8268fSThierry Reding 
726dee8268fSThierry Reding 	/* hardware initialization */
727ca48080aSStephen Warren 	reset_control_deassert(dc->rst);
728dee8268fSThierry Reding 	usleep_range(10000, 20000);
729dee8268fSThierry Reding 
730dee8268fSThierry Reding 	if (dc->pipe)
731dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK1;
732dee8268fSThierry Reding 	else
733dee8268fSThierry Reding 		syncpt = SYNCPT_VBLANK0;
734dee8268fSThierry Reding 
735dee8268fSThierry Reding 	/* initialize display controller */
736dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
737dee8268fSThierry Reding 	tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
738dee8268fSThierry Reding 
739dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
740dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
741dee8268fSThierry Reding 
742dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
743dee8268fSThierry Reding 		WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
744dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
745dee8268fSThierry Reding 
746dee8268fSThierry Reding 	value = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
747dee8268fSThierry Reding 		PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
748dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
749dee8268fSThierry Reding 
750dee8268fSThierry Reding 	/* initialize timer */
751dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
752dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
753dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
754dee8268fSThierry Reding 
755dee8268fSThierry Reding 	value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
756dee8268fSThierry Reding 		WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
757dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
758dee8268fSThierry Reding 
759dee8268fSThierry Reding 	value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
760dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
761dee8268fSThierry Reding 
762dee8268fSThierry Reding 	value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
763dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
764dee8268fSThierry Reding }
765dee8268fSThierry Reding 
766dee8268fSThierry Reding static void tegra_crtc_commit(struct drm_crtc *crtc)
767dee8268fSThierry Reding {
768dee8268fSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(crtc);
769dee8268fSThierry Reding 	unsigned long value;
770dee8268fSThierry Reding 
771dee8268fSThierry Reding 	value = GENERAL_UPDATE | WIN_A_UPDATE;
772dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
773dee8268fSThierry Reding 
774dee8268fSThierry Reding 	value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
775dee8268fSThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
776dee8268fSThierry Reding 
777dee8268fSThierry Reding 	drm_vblank_post_modeset(crtc->dev, dc->pipe);
778dee8268fSThierry Reding }
779dee8268fSThierry Reding 
780dee8268fSThierry Reding static void tegra_crtc_load_lut(struct drm_crtc *crtc)
781dee8268fSThierry Reding {
782dee8268fSThierry Reding }
783dee8268fSThierry Reding 
784dee8268fSThierry Reding static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
785dee8268fSThierry Reding 	.disable = tegra_crtc_disable,
786dee8268fSThierry Reding 	.mode_fixup = tegra_crtc_mode_fixup,
787dee8268fSThierry Reding 	.mode_set = tegra_crtc_mode_set,
788dee8268fSThierry Reding 	.mode_set_base = tegra_crtc_mode_set_base,
789dee8268fSThierry Reding 	.prepare = tegra_crtc_prepare,
790dee8268fSThierry Reding 	.commit = tegra_crtc_commit,
791dee8268fSThierry Reding 	.load_lut = tegra_crtc_load_lut,
792dee8268fSThierry Reding };
793dee8268fSThierry Reding 
794dee8268fSThierry Reding static irqreturn_t tegra_dc_irq(int irq, void *data)
795dee8268fSThierry Reding {
796dee8268fSThierry Reding 	struct tegra_dc *dc = data;
797dee8268fSThierry Reding 	unsigned long status;
798dee8268fSThierry Reding 
799dee8268fSThierry Reding 	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
800dee8268fSThierry Reding 	tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
801dee8268fSThierry Reding 
802dee8268fSThierry Reding 	if (status & FRAME_END_INT) {
803dee8268fSThierry Reding 		/*
804dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): frame end\n", __func__);
805dee8268fSThierry Reding 		*/
806dee8268fSThierry Reding 	}
807dee8268fSThierry Reding 
808dee8268fSThierry Reding 	if (status & VBLANK_INT) {
809dee8268fSThierry Reding 		/*
810dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
811dee8268fSThierry Reding 		*/
812dee8268fSThierry Reding 		drm_handle_vblank(dc->base.dev, dc->pipe);
813dee8268fSThierry Reding 		tegra_dc_finish_page_flip(dc);
814dee8268fSThierry Reding 	}
815dee8268fSThierry Reding 
816dee8268fSThierry Reding 	if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
817dee8268fSThierry Reding 		/*
818dee8268fSThierry Reding 		dev_dbg(dc->dev, "%s(): underflow\n", __func__);
819dee8268fSThierry Reding 		*/
820dee8268fSThierry Reding 	}
821dee8268fSThierry Reding 
822dee8268fSThierry Reding 	return IRQ_HANDLED;
823dee8268fSThierry Reding }
824dee8268fSThierry Reding 
825dee8268fSThierry Reding static int tegra_dc_show_regs(struct seq_file *s, void *data)
826dee8268fSThierry Reding {
827dee8268fSThierry Reding 	struct drm_info_node *node = s->private;
828dee8268fSThierry Reding 	struct tegra_dc *dc = node->info_ent->data;
829dee8268fSThierry Reding 
830dee8268fSThierry Reding #define DUMP_REG(name)						\
831dee8268fSThierry Reding 	seq_printf(s, "%-40s %#05x %08lx\n", #name, name,	\
832dee8268fSThierry Reding 		   tegra_dc_readl(dc, name))
833dee8268fSThierry Reding 
834dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
835dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
836dee8268fSThierry Reding 	DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
837dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
838dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
839dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
840dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
841dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
842dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
843dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
844dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
845dee8268fSThierry Reding 	DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
846dee8268fSThierry Reding 	DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
847dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
848dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
849dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE);
850dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
851dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_STATUS);
852dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_MASK);
853dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_ENABLE);
854dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_TYPE);
855dee8268fSThierry Reding 	DUMP_REG(DC_CMD_INT_POLARITY);
856dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
857dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
858dee8268fSThierry Reding 	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
859dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_ACCESS);
860dee8268fSThierry Reding 	DUMP_REG(DC_CMD_STATE_CONTROL);
861dee8268fSThierry Reding 	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
862dee8268fSThierry Reding 	DUMP_REG(DC_CMD_REG_ACT_CONTROL);
863dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CONTROL);
864dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM);
865dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
866dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
867dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
868dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
869dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
870dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
871dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
872dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
873dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
874dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
875dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
876dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
877dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
878dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
879dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
880dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
881dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
882dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
883dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
884dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
885dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
886dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
887dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
888dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
889dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
890dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_MISC_CONTROL);
891dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_CONTROL);
892dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
893dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_CONTROL);
894dee8268fSThierry Reding 	DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
895dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_CONTROL);
896dee8268fSThierry Reding 	DUMP_REG(DC_COM_SPI_START_BYTE);
897dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
898dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
899dee8268fSThierry Reding 	DUMP_REG(DC_COM_HSPI_CS_DC);
900dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
901dee8268fSThierry Reding 	DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
902dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_CTRL);
903dee8268fSThierry Reding 	DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
904dee8268fSThierry Reding 	DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
905dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
906dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
907dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
908dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
909dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
910dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
911dee8268fSThierry Reding 	DUMP_REG(DC_DISP_REF_TO_SYNC);
912dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SYNC_WIDTH);
913dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BACK_PORCH);
914dee8268fSThierry Reding 	DUMP_REG(DC_DISP_ACTIVE);
915dee8268fSThierry Reding 	DUMP_REG(DC_DISP_FRONT_PORCH);
916dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
917dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
918dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
919dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
920dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
921dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
922dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
923dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
924dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
925dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
926dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
927dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
928dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
929dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
930dee8268fSThierry Reding 	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
931dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
932dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
933dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
934dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
935dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
936dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
937dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
938dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
939dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
940dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
941dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
942dee8268fSThierry Reding 	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
943dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M0_CONTROL);
944dee8268fSThierry Reding 	DUMP_REG(DC_DISP_M1_CONTROL);
945dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DI_CONTROL);
946dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_CONTROL);
947dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_A);
948dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_B);
949dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_C);
950dee8268fSThierry Reding 	DUMP_REG(DC_DISP_PP_SELECT_D);
951dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
952dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
953dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
954dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
955dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
956dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
957dee8268fSThierry Reding 	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
958dee8268fSThierry Reding 	DUMP_REG(DC_DISP_BORDER_COLOR);
959dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
960dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
961dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
962dee8268fSThierry Reding 	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
963dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
964dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
965dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
966dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
967dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION);
968dee8268fSThierry Reding 	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
969dee8268fSThierry Reding 	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
970dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
971dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
972dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
973dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
974dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
975dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
976dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
977dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
978dee8268fSThierry Reding 	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
979dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
980dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
981dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CONTROL);
982dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_CSC_COEFF);
983dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(0));
984dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(1));
985dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(2));
986dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(3));
987dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(4));
988dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(5));
989dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(6));
990dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(7));
991dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_LUT(8));
992dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
993dee8268fSThierry Reding 	DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
994dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
995dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
996dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
997dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
998dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
999dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1000dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1001dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1002dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(0));
1003dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(1));
1004dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(2));
1005dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_TF(3));
1006dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_BL_CONTROL);
1007dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1008dee8268fSThierry Reding 	DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
1009dee8268fSThierry Reding 	DUMP_REG(DC_WIN_WIN_OPTIONS);
1010dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BYTE_SWAP);
1011dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_CONTROL);
1012dee8268fSThierry Reding 	DUMP_REG(DC_WIN_COLOR_DEPTH);
1013dee8268fSThierry Reding 	DUMP_REG(DC_WIN_POSITION);
1014dee8268fSThierry Reding 	DUMP_REG(DC_WIN_SIZE);
1015dee8268fSThierry Reding 	DUMP_REG(DC_WIN_PRESCALED_SIZE);
1016dee8268fSThierry Reding 	DUMP_REG(DC_WIN_H_INITIAL_DDA);
1017dee8268fSThierry Reding 	DUMP_REG(DC_WIN_V_INITIAL_DDA);
1018dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DDA_INC);
1019dee8268fSThierry Reding 	DUMP_REG(DC_WIN_LINE_STRIDE);
1020dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUF_STRIDE);
1021dee8268fSThierry Reding 	DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1022dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1023dee8268fSThierry Reding 	DUMP_REG(DC_WIN_DV_CONTROL);
1024dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_NOKEY);
1025dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_1WIN);
1026dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_X);
1027dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_2WIN_Y);
1028dee8268fSThierry Reding 	DUMP_REG(DC_WIN_BLEND_3WIN_XY);
1029dee8268fSThierry Reding 	DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1030dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR);
1031dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_NS);
1032dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U);
1033dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1034dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V);
1035dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1036dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1037dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1038dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1039dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1040dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1041dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1042dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1043dee8268fSThierry Reding 	DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1044dee8268fSThierry Reding 
1045dee8268fSThierry Reding #undef DUMP_REG
1046dee8268fSThierry Reding 
1047dee8268fSThierry Reding 	return 0;
1048dee8268fSThierry Reding }
1049dee8268fSThierry Reding 
1050dee8268fSThierry Reding static struct drm_info_list debugfs_files[] = {
1051dee8268fSThierry Reding 	{ "regs", tegra_dc_show_regs, 0, NULL },
1052dee8268fSThierry Reding };
1053dee8268fSThierry Reding 
1054dee8268fSThierry Reding static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1055dee8268fSThierry Reding {
1056dee8268fSThierry Reding 	unsigned int i;
1057dee8268fSThierry Reding 	char *name;
1058dee8268fSThierry Reding 	int err;
1059dee8268fSThierry Reding 
1060dee8268fSThierry Reding 	name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1061dee8268fSThierry Reding 	dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1062dee8268fSThierry Reding 	kfree(name);
1063dee8268fSThierry Reding 
1064dee8268fSThierry Reding 	if (!dc->debugfs)
1065dee8268fSThierry Reding 		return -ENOMEM;
1066dee8268fSThierry Reding 
1067dee8268fSThierry Reding 	dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1068dee8268fSThierry Reding 				    GFP_KERNEL);
1069dee8268fSThierry Reding 	if (!dc->debugfs_files) {
1070dee8268fSThierry Reding 		err = -ENOMEM;
1071dee8268fSThierry Reding 		goto remove;
1072dee8268fSThierry Reding 	}
1073dee8268fSThierry Reding 
1074dee8268fSThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1075dee8268fSThierry Reding 		dc->debugfs_files[i].data = dc;
1076dee8268fSThierry Reding 
1077dee8268fSThierry Reding 	err = drm_debugfs_create_files(dc->debugfs_files,
1078dee8268fSThierry Reding 				       ARRAY_SIZE(debugfs_files),
1079dee8268fSThierry Reding 				       dc->debugfs, minor);
1080dee8268fSThierry Reding 	if (err < 0)
1081dee8268fSThierry Reding 		goto free;
1082dee8268fSThierry Reding 
1083dee8268fSThierry Reding 	dc->minor = minor;
1084dee8268fSThierry Reding 
1085dee8268fSThierry Reding 	return 0;
1086dee8268fSThierry Reding 
1087dee8268fSThierry Reding free:
1088dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1089dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1090dee8268fSThierry Reding remove:
1091dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1092dee8268fSThierry Reding 	dc->debugfs = NULL;
1093dee8268fSThierry Reding 
1094dee8268fSThierry Reding 	return err;
1095dee8268fSThierry Reding }
1096dee8268fSThierry Reding 
1097dee8268fSThierry Reding static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1098dee8268fSThierry Reding {
1099dee8268fSThierry Reding 	drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1100dee8268fSThierry Reding 				 dc->minor);
1101dee8268fSThierry Reding 	dc->minor = NULL;
1102dee8268fSThierry Reding 
1103dee8268fSThierry Reding 	kfree(dc->debugfs_files);
1104dee8268fSThierry Reding 	dc->debugfs_files = NULL;
1105dee8268fSThierry Reding 
1106dee8268fSThierry Reding 	debugfs_remove(dc->debugfs);
1107dee8268fSThierry Reding 	dc->debugfs = NULL;
1108dee8268fSThierry Reding 
1109dee8268fSThierry Reding 	return 0;
1110dee8268fSThierry Reding }
1111dee8268fSThierry Reding 
1112dee8268fSThierry Reding static int tegra_dc_init(struct host1x_client *client)
1113dee8268fSThierry Reding {
1114dee8268fSThierry Reding 	struct tegra_drm *tegra = dev_get_drvdata(client->parent);
1115dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1116dee8268fSThierry Reding 	int err;
1117dee8268fSThierry Reding 
1118dee8268fSThierry Reding 	drm_crtc_init(tegra->drm, &dc->base, &tegra_crtc_funcs);
1119dee8268fSThierry Reding 	drm_mode_crtc_set_gamma_size(&dc->base, 256);
1120dee8268fSThierry Reding 	drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1121dee8268fSThierry Reding 
1122dee8268fSThierry Reding 	err = tegra_dc_rgb_init(tegra->drm, dc);
1123dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1124dee8268fSThierry Reding 		dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1125dee8268fSThierry Reding 		return err;
1126dee8268fSThierry Reding 	}
1127dee8268fSThierry Reding 
1128dee8268fSThierry Reding 	err = tegra_dc_add_planes(tegra->drm, dc);
1129dee8268fSThierry Reding 	if (err < 0)
1130dee8268fSThierry Reding 		return err;
1131dee8268fSThierry Reding 
1132dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1133dee8268fSThierry Reding 		err = tegra_dc_debugfs_init(dc, tegra->drm->primary);
1134dee8268fSThierry Reding 		if (err < 0)
1135dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1136dee8268fSThierry Reding 	}
1137dee8268fSThierry Reding 
1138dee8268fSThierry Reding 	err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
1139dee8268fSThierry Reding 			       dev_name(dc->dev), dc);
1140dee8268fSThierry Reding 	if (err < 0) {
1141dee8268fSThierry Reding 		dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1142dee8268fSThierry Reding 			err);
1143dee8268fSThierry Reding 		return err;
1144dee8268fSThierry Reding 	}
1145dee8268fSThierry Reding 
1146dee8268fSThierry Reding 	return 0;
1147dee8268fSThierry Reding }
1148dee8268fSThierry Reding 
1149dee8268fSThierry Reding static int tegra_dc_exit(struct host1x_client *client)
1150dee8268fSThierry Reding {
1151dee8268fSThierry Reding 	struct tegra_dc *dc = host1x_client_to_dc(client);
1152dee8268fSThierry Reding 	int err;
1153dee8268fSThierry Reding 
1154dee8268fSThierry Reding 	devm_free_irq(dc->dev, dc->irq, dc);
1155dee8268fSThierry Reding 
1156dee8268fSThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1157dee8268fSThierry Reding 		err = tegra_dc_debugfs_exit(dc);
1158dee8268fSThierry Reding 		if (err < 0)
1159dee8268fSThierry Reding 			dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1160dee8268fSThierry Reding 	}
1161dee8268fSThierry Reding 
1162dee8268fSThierry Reding 	err = tegra_dc_rgb_exit(dc);
1163dee8268fSThierry Reding 	if (err) {
1164dee8268fSThierry Reding 		dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1165dee8268fSThierry Reding 		return err;
1166dee8268fSThierry Reding 	}
1167dee8268fSThierry Reding 
1168dee8268fSThierry Reding 	return 0;
1169dee8268fSThierry Reding }
1170dee8268fSThierry Reding 
1171dee8268fSThierry Reding static const struct host1x_client_ops dc_client_ops = {
1172dee8268fSThierry Reding 	.init = tegra_dc_init,
1173dee8268fSThierry Reding 	.exit = tegra_dc_exit,
1174dee8268fSThierry Reding };
1175dee8268fSThierry Reding 
11768620fc62SThierry Reding static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
11778620fc62SThierry Reding 	.supports_interlacing = false,
11788620fc62SThierry Reding };
11798620fc62SThierry Reding 
11808620fc62SThierry Reding static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
11818620fc62SThierry Reding 	.supports_interlacing = false,
11828620fc62SThierry Reding };
11838620fc62SThierry Reding 
11848620fc62SThierry Reding static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
11858620fc62SThierry Reding 	.supports_interlacing = true,
11868620fc62SThierry Reding };
11878620fc62SThierry Reding 
11888620fc62SThierry Reding static const struct of_device_id tegra_dc_of_match[] = {
11898620fc62SThierry Reding 	{
11908620fc62SThierry Reding 		.compatible = "nvidia,tegra124-dc",
11918620fc62SThierry Reding 		.data = &tegra124_dc_soc_info,
11928620fc62SThierry Reding 	}, {
11938620fc62SThierry Reding 		.compatible = "nvidia,tegra30-dc",
11948620fc62SThierry Reding 		.data = &tegra30_dc_soc_info,
11958620fc62SThierry Reding 	}, {
11968620fc62SThierry Reding 		.compatible = "nvidia,tegra20-dc",
11978620fc62SThierry Reding 		.data = &tegra20_dc_soc_info,
11988620fc62SThierry Reding 	}, {
11998620fc62SThierry Reding 		/* sentinel */
12008620fc62SThierry Reding 	}
12018620fc62SThierry Reding };
12028620fc62SThierry Reding 
120313411dddSThierry Reding static int tegra_dc_parse_dt(struct tegra_dc *dc)
120413411dddSThierry Reding {
120513411dddSThierry Reding 	struct device_node *np;
120613411dddSThierry Reding 	u32 value = 0;
120713411dddSThierry Reding 	int err;
120813411dddSThierry Reding 
120913411dddSThierry Reding 	err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
121013411dddSThierry Reding 	if (err < 0) {
121113411dddSThierry Reding 		dev_err(dc->dev, "missing \"nvidia,head\" property\n");
121213411dddSThierry Reding 
121313411dddSThierry Reding 		/*
121413411dddSThierry Reding 		 * If the nvidia,head property isn't present, try to find the
121513411dddSThierry Reding 		 * correct head number by looking up the position of this
121613411dddSThierry Reding 		 * display controller's node within the device tree. Assuming
121713411dddSThierry Reding 		 * that the nodes are ordered properly in the DTS file and
121813411dddSThierry Reding 		 * that the translation into a flattened device tree blob
121913411dddSThierry Reding 		 * preserves that ordering this will actually yield the right
122013411dddSThierry Reding 		 * head number.
122113411dddSThierry Reding 		 *
122213411dddSThierry Reding 		 * If those assumptions don't hold, this will still work for
122313411dddSThierry Reding 		 * cases where only a single display controller is used.
122413411dddSThierry Reding 		 */
122513411dddSThierry Reding 		for_each_matching_node(np, tegra_dc_of_match) {
122613411dddSThierry Reding 			if (np == dc->dev->of_node)
122713411dddSThierry Reding 				break;
122813411dddSThierry Reding 
122913411dddSThierry Reding 			value++;
123013411dddSThierry Reding 		}
123113411dddSThierry Reding 	}
123213411dddSThierry Reding 
123313411dddSThierry Reding 	dc->pipe = value;
123413411dddSThierry Reding 
123513411dddSThierry Reding 	return 0;
123613411dddSThierry Reding }
123713411dddSThierry Reding 
1238dee8268fSThierry Reding static int tegra_dc_probe(struct platform_device *pdev)
1239dee8268fSThierry Reding {
12408620fc62SThierry Reding 	const struct of_device_id *id;
1241dee8268fSThierry Reding 	struct resource *regs;
1242dee8268fSThierry Reding 	struct tegra_dc *dc;
1243dee8268fSThierry Reding 	int err;
1244dee8268fSThierry Reding 
1245dee8268fSThierry Reding 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1246dee8268fSThierry Reding 	if (!dc)
1247dee8268fSThierry Reding 		return -ENOMEM;
1248dee8268fSThierry Reding 
12498620fc62SThierry Reding 	id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
12508620fc62SThierry Reding 	if (!id)
12518620fc62SThierry Reding 		return -ENODEV;
12528620fc62SThierry Reding 
1253dee8268fSThierry Reding 	spin_lock_init(&dc->lock);
1254dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->list);
1255dee8268fSThierry Reding 	dc->dev = &pdev->dev;
12568620fc62SThierry Reding 	dc->soc = id->data;
1257dee8268fSThierry Reding 
125813411dddSThierry Reding 	err = tegra_dc_parse_dt(dc);
125913411dddSThierry Reding 	if (err < 0)
126013411dddSThierry Reding 		return err;
126113411dddSThierry Reding 
1262dee8268fSThierry Reding 	dc->clk = devm_clk_get(&pdev->dev, NULL);
1263dee8268fSThierry Reding 	if (IS_ERR(dc->clk)) {
1264dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get clock\n");
1265dee8268fSThierry Reding 		return PTR_ERR(dc->clk);
1266dee8268fSThierry Reding 	}
1267dee8268fSThierry Reding 
1268ca48080aSStephen Warren 	dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1269ca48080aSStephen Warren 	if (IS_ERR(dc->rst)) {
1270ca48080aSStephen Warren 		dev_err(&pdev->dev, "failed to get reset\n");
1271ca48080aSStephen Warren 		return PTR_ERR(dc->rst);
1272ca48080aSStephen Warren 	}
1273ca48080aSStephen Warren 
1274dee8268fSThierry Reding 	err = clk_prepare_enable(dc->clk);
1275dee8268fSThierry Reding 	if (err < 0)
1276dee8268fSThierry Reding 		return err;
1277dee8268fSThierry Reding 
1278dee8268fSThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1279dee8268fSThierry Reding 	dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1280dee8268fSThierry Reding 	if (IS_ERR(dc->regs))
1281dee8268fSThierry Reding 		return PTR_ERR(dc->regs);
1282dee8268fSThierry Reding 
1283dee8268fSThierry Reding 	dc->irq = platform_get_irq(pdev, 0);
1284dee8268fSThierry Reding 	if (dc->irq < 0) {
1285dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ\n");
1286dee8268fSThierry Reding 		return -ENXIO;
1287dee8268fSThierry Reding 	}
1288dee8268fSThierry Reding 
1289dee8268fSThierry Reding 	INIT_LIST_HEAD(&dc->client.list);
1290dee8268fSThierry Reding 	dc->client.ops = &dc_client_ops;
1291dee8268fSThierry Reding 	dc->client.dev = &pdev->dev;
1292dee8268fSThierry Reding 
1293dee8268fSThierry Reding 	err = tegra_dc_rgb_probe(dc);
1294dee8268fSThierry Reding 	if (err < 0 && err != -ENODEV) {
1295dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1296dee8268fSThierry Reding 		return err;
1297dee8268fSThierry Reding 	}
1298dee8268fSThierry Reding 
1299dee8268fSThierry Reding 	err = host1x_client_register(&dc->client);
1300dee8268fSThierry Reding 	if (err < 0) {
1301dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1302dee8268fSThierry Reding 			err);
1303dee8268fSThierry Reding 		return err;
1304dee8268fSThierry Reding 	}
1305dee8268fSThierry Reding 
1306dee8268fSThierry Reding 	platform_set_drvdata(pdev, dc);
1307dee8268fSThierry Reding 
1308dee8268fSThierry Reding 	return 0;
1309dee8268fSThierry Reding }
1310dee8268fSThierry Reding 
1311dee8268fSThierry Reding static int tegra_dc_remove(struct platform_device *pdev)
1312dee8268fSThierry Reding {
1313dee8268fSThierry Reding 	struct tegra_dc *dc = platform_get_drvdata(pdev);
1314dee8268fSThierry Reding 	int err;
1315dee8268fSThierry Reding 
1316dee8268fSThierry Reding 	err = host1x_client_unregister(&dc->client);
1317dee8268fSThierry Reding 	if (err < 0) {
1318dee8268fSThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1319dee8268fSThierry Reding 			err);
1320dee8268fSThierry Reding 		return err;
1321dee8268fSThierry Reding 	}
1322dee8268fSThierry Reding 
132359d29c0eSThierry Reding 	err = tegra_dc_rgb_remove(dc);
132459d29c0eSThierry Reding 	if (err < 0) {
132559d29c0eSThierry Reding 		dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
132659d29c0eSThierry Reding 		return err;
132759d29c0eSThierry Reding 	}
132859d29c0eSThierry Reding 
1329dee8268fSThierry Reding 	clk_disable_unprepare(dc->clk);
1330dee8268fSThierry Reding 
1331dee8268fSThierry Reding 	return 0;
1332dee8268fSThierry Reding }
1333dee8268fSThierry Reding 
1334dee8268fSThierry Reding struct platform_driver tegra_dc_driver = {
1335dee8268fSThierry Reding 	.driver = {
1336dee8268fSThierry Reding 		.name = "tegra-dc",
1337dee8268fSThierry Reding 		.owner = THIS_MODULE,
1338dee8268fSThierry Reding 		.of_match_table = tegra_dc_of_match,
1339dee8268fSThierry Reding 	},
1340dee8268fSThierry Reding 	.probe = tegra_dc_probe,
1341dee8268fSThierry Reding 	.remove = tegra_dc_remove,
1342dee8268fSThierry Reding };
1343