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