blob: afcca04f53674231e42ead899757fe69d5073071 [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/clk.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020011#include <linux/debugfs.h>
Stephen Warrenca480802013-11-06 16:20:54 -070012#include <linux/reset.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000013
Arto Merilainende2ba662013-03-22 16:34:08 +020014#include "dc.h"
15#include "drm.h"
16#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000017
Thierry Reding8620fc62013-12-12 11:03:59 +010018struct tegra_dc_soc_info {
19 bool supports_interlacing;
Thierry Redinge6876512013-12-20 13:58:33 +010020 bool supports_cursor;
Thierry Redingc134f012014-06-03 14:48:12 +020021 bool supports_block_linear;
Thierry Reding8620fc62013-12-12 11:03:59 +010022};
23
Thierry Redingf34bc782012-11-04 21:47:13 +010024struct tegra_plane {
25 struct drm_plane base;
26 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000027};
28
Thierry Redingf34bc782012-11-04 21:47:13 +010029static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
30{
31 return container_of(plane, struct tegra_plane, base);
32}
33
Thierry Reding10288ee2014-03-14 09:54:58 +010034static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
35{
36 /* assume no swapping of fetched data */
37 if (swap)
38 *swap = BYTE_SWAP_NOSWAP;
39
40 switch (format) {
41 case DRM_FORMAT_XBGR8888:
42 return WIN_COLOR_DEPTH_R8G8B8A8;
43
44 case DRM_FORMAT_XRGB8888:
45 return WIN_COLOR_DEPTH_B8G8R8A8;
46
47 case DRM_FORMAT_RGB565:
48 return WIN_COLOR_DEPTH_B5G6R5;
49
50 case DRM_FORMAT_UYVY:
51 return WIN_COLOR_DEPTH_YCbCr422;
52
53 case DRM_FORMAT_YUYV:
54 if (swap)
55 *swap = BYTE_SWAP_SWAP2;
56
57 return WIN_COLOR_DEPTH_YCbCr422;
58
59 case DRM_FORMAT_YUV420:
60 return WIN_COLOR_DEPTH_YCbCr420P;
61
62 case DRM_FORMAT_YUV422:
63 return WIN_COLOR_DEPTH_YCbCr422P;
64
65 default:
66 break;
67 }
68
69 WARN(1, "unsupported pixel format %u, using default\n", format);
70 return WIN_COLOR_DEPTH_B8G8R8A8;
71}
72
73static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
74{
75 switch (format) {
76 case WIN_COLOR_DEPTH_YCbCr422:
77 case WIN_COLOR_DEPTH_YUV422:
78 if (planar)
79 *planar = false;
80
81 return true;
82
83 case WIN_COLOR_DEPTH_YCbCr420P:
84 case WIN_COLOR_DEPTH_YUV420P:
85 case WIN_COLOR_DEPTH_YCbCr422P:
86 case WIN_COLOR_DEPTH_YUV422P:
87 case WIN_COLOR_DEPTH_YCbCr422R:
88 case WIN_COLOR_DEPTH_YUV422R:
89 case WIN_COLOR_DEPTH_YCbCr422RA:
90 case WIN_COLOR_DEPTH_YUV422RA:
91 if (planar)
92 *planar = true;
93
94 return true;
95 }
96
97 return false;
98}
99
100static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
101 unsigned int bpp)
102{
103 fixed20_12 outf = dfixed_init(out);
104 fixed20_12 inf = dfixed_init(in);
105 u32 dda_inc;
106 int max;
107
108 if (v)
109 max = 15;
110 else {
111 switch (bpp) {
112 case 2:
113 max = 8;
114 break;
115
116 default:
117 WARN_ON_ONCE(1);
118 /* fallthrough */
119 case 4:
120 max = 4;
121 break;
122 }
123 }
124
125 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
126 inf.full -= dfixed_const(1);
127
128 dda_inc = dfixed_div(inf, outf);
129 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
130
131 return dda_inc;
132}
133
134static inline u32 compute_initial_dda(unsigned int in)
135{
136 fixed20_12 inf = dfixed_init(in);
137 return dfixed_frac(inf);
138}
139
140static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
141 const struct tegra_dc_window *window)
142{
143 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
144 unsigned long value;
145 bool yuv, planar;
146
147 /*
148 * For YUV planar modes, the number of bytes per pixel takes into
149 * account only the luma component and therefore is 1.
150 */
151 yuv = tegra_dc_format_is_yuv(window->format, &planar);
152 if (!yuv)
153 bpp = window->bits_per_pixel / 8;
154 else
155 bpp = planar ? 1 : 2;
156
157 value = WINDOW_A_SELECT << index;
158 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
159
160 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
161 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
162
163 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
164 tegra_dc_writel(dc, value, DC_WIN_POSITION);
165
166 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
167 tegra_dc_writel(dc, value, DC_WIN_SIZE);
168
169 h_offset = window->src.x * bpp;
170 v_offset = window->src.y;
171 h_size = window->src.w * bpp;
172 v_size = window->src.h;
173
174 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
175 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
176
177 /*
178 * For DDA computations the number of bytes per pixel for YUV planar
179 * modes needs to take into account all Y, U and V components.
180 */
181 if (yuv && planar)
182 bpp = 2;
183
184 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
185 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
186
187 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
188 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
189
190 h_dda = compute_initial_dda(window->src.x);
191 v_dda = compute_initial_dda(window->src.y);
192
193 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
194 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
195
196 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
197 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
198
199 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
200
201 if (yuv && planar) {
202 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
203 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
204 value = window->stride[1] << 16 | window->stride[0];
205 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
206 } else {
207 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
208 }
209
210 if (window->bottom_up)
211 v_offset += window->src.h - 1;
212
213 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
214 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
215
Thierry Redingc134f012014-06-03 14:48:12 +0200216 if (dc->soc->supports_block_linear) {
217 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100218
Thierry Redingc134f012014-06-03 14:48:12 +0200219 switch (window->tiling.mode) {
220 case TEGRA_BO_TILING_MODE_PITCH:
221 value = DC_WINBUF_SURFACE_KIND_PITCH;
222 break;
223
224 case TEGRA_BO_TILING_MODE_TILED:
225 value = DC_WINBUF_SURFACE_KIND_TILED;
226 break;
227
228 case TEGRA_BO_TILING_MODE_BLOCK:
229 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
230 DC_WINBUF_SURFACE_KIND_BLOCK;
231 break;
232 }
233
234 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
235 } else {
236 switch (window->tiling.mode) {
237 case TEGRA_BO_TILING_MODE_PITCH:
238 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
239 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
240 break;
241
242 case TEGRA_BO_TILING_MODE_TILED:
243 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
244 DC_WIN_BUFFER_ADDR_MODE_TILE;
245 break;
246
247 case TEGRA_BO_TILING_MODE_BLOCK:
248 DRM_ERROR("hardware doesn't support block linear mode\n");
249 return -EINVAL;
250 }
251
252 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
253 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100254
255 value = WIN_ENABLE;
256
257 if (yuv) {
258 /* setup default colorspace conversion coefficients */
259 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
260 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
261 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
262 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
263 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
264 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
265 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
266 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
267
268 value |= CSC_ENABLE;
269 } else if (window->bits_per_pixel < 24) {
270 value |= COLOR_EXPAND;
271 }
272
273 if (window->bottom_up)
274 value |= V_DIRECTION;
275
276 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
277
278 /*
279 * Disable blending and assume Window A is the bottom-most window,
280 * Window C is the top-most window and Window B is in the middle.
281 */
282 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
283 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
284
285 switch (index) {
286 case 0:
287 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
288 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
289 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
290 break;
291
292 case 1:
293 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
294 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
295 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
296 break;
297
298 case 2:
299 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
300 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
301 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
302 break;
303 }
304
305 tegra_dc_writel(dc, WIN_A_UPDATE << index, DC_CMD_STATE_CONTROL);
306 tegra_dc_writel(dc, WIN_A_ACT_REQ << index, DC_CMD_STATE_CONTROL);
307
308 return 0;
309}
310
Thierry Redingf34bc782012-11-04 21:47:13 +0100311static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
312 struct drm_framebuffer *fb, int crtc_x,
313 int crtc_y, unsigned int crtc_w,
314 unsigned int crtc_h, uint32_t src_x,
315 uint32_t src_y, uint32_t src_w, uint32_t src_h)
316{
317 struct tegra_plane *p = to_tegra_plane(plane);
318 struct tegra_dc *dc = to_tegra_dc(crtc);
319 struct tegra_dc_window window;
320 unsigned int i;
Thierry Redingc134f012014-06-03 14:48:12 +0200321 int err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100322
323 memset(&window, 0, sizeof(window));
324 window.src.x = src_x >> 16;
325 window.src.y = src_y >> 16;
326 window.src.w = src_w >> 16;
327 window.src.h = src_h >> 16;
328 window.dst.x = crtc_x;
329 window.dst.y = crtc_y;
330 window.dst.w = crtc_w;
331 window.dst.h = crtc_h;
Thierry Redingf9253902014-01-29 20:31:17 +0100332 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
Thierry Redingf34bc782012-11-04 21:47:13 +0100333 window.bits_per_pixel = fb->bits_per_pixel;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200334 window.bottom_up = tegra_fb_is_bottom_up(fb);
Thierry Redingc134f012014-06-03 14:48:12 +0200335
336 err = tegra_fb_get_tiling(fb, &window.tiling);
337 if (err < 0)
338 return err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100339
340 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
Arto Merilainende2ba662013-03-22 16:34:08 +0200341 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingf34bc782012-11-04 21:47:13 +0100342
Arto Merilainende2ba662013-03-22 16:34:08 +0200343 window.base[i] = bo->paddr + fb->offsets[i];
Thierry Redingf34bc782012-11-04 21:47:13 +0100344
345 /*
346 * Tegra doesn't support different strides for U and V planes
347 * so we display a warning if the user tries to display a
348 * framebuffer with such a configuration.
349 */
350 if (i >= 2) {
351 if (fb->pitches[i] != window.stride[1])
352 DRM_ERROR("unsupported UV-plane configuration\n");
353 } else {
354 window.stride[i] = fb->pitches[i];
355 }
356 }
357
358 return tegra_dc_setup_window(dc, p->index, &window);
359}
360
361static int tegra_plane_disable(struct drm_plane *plane)
362{
363 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
364 struct tegra_plane *p = to_tegra_plane(plane);
365 unsigned long value;
366
Thierry Reding2678aeb2013-03-18 11:09:13 +0100367 if (!plane->crtc)
368 return 0;
369
Thierry Redingf34bc782012-11-04 21:47:13 +0100370 value = WINDOW_A_SELECT << p->index;
371 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
372
373 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
374 value &= ~WIN_ENABLE;
375 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
376
377 tegra_dc_writel(dc, WIN_A_UPDATE << p->index, DC_CMD_STATE_CONTROL);
378 tegra_dc_writel(dc, WIN_A_ACT_REQ << p->index, DC_CMD_STATE_CONTROL);
379
380 return 0;
381}
382
383static void tegra_plane_destroy(struct drm_plane *plane)
384{
Thierry Redingf002abc2013-10-14 14:06:02 +0200385 struct tegra_plane *p = to_tegra_plane(plane);
386
Thierry Redingf34bc782012-11-04 21:47:13 +0100387 tegra_plane_disable(plane);
388 drm_plane_cleanup(plane);
Thierry Redingf002abc2013-10-14 14:06:02 +0200389 kfree(p);
Thierry Redingf34bc782012-11-04 21:47:13 +0100390}
391
392static const struct drm_plane_funcs tegra_plane_funcs = {
393 .update_plane = tegra_plane_update,
394 .disable_plane = tegra_plane_disable,
395 .destroy = tegra_plane_destroy,
396};
397
398static const uint32_t plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100399 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100400 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100401 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100402 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100403 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100404 DRM_FORMAT_YUV420,
405 DRM_FORMAT_YUV422,
406};
407
408static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
409{
410 unsigned int i;
411 int err = 0;
412
413 for (i = 0; i < 2; i++) {
414 struct tegra_plane *plane;
415
Thierry Redingf002abc2013-10-14 14:06:02 +0200416 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
Thierry Redingf34bc782012-11-04 21:47:13 +0100417 if (!plane)
418 return -ENOMEM;
419
420 plane->index = 1 + i;
421
422 err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
423 &tegra_plane_funcs, plane_formats,
424 ARRAY_SIZE(plane_formats), false);
Thierry Redingf002abc2013-10-14 14:06:02 +0200425 if (err < 0) {
426 kfree(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100427 return err;
Thierry Redingf002abc2013-10-14 14:06:02 +0200428 }
Thierry Redingf34bc782012-11-04 21:47:13 +0100429 }
430
431 return 0;
432}
433
Thierry Reding23fb4742012-11-28 11:38:24 +0100434static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
435 struct drm_framebuffer *fb)
436{
Arto Merilainende2ba662013-03-22 16:34:08 +0200437 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200438 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200439 struct tegra_bo_tiling tiling;
Thierry Redingf9253902014-01-29 20:31:17 +0100440 unsigned int format, swap;
Thierry Reding23fb4742012-11-28 11:38:24 +0100441 unsigned long value;
Thierry Redingc134f012014-06-03 14:48:12 +0200442 int err;
443
444 err = tegra_fb_get_tiling(fb, &tiling);
445 if (err < 0)
446 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100447
448 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
449
450 value = fb->offsets[0] + y * fb->pitches[0] +
451 x * fb->bits_per_pixel / 8;
452
Arto Merilainende2ba662013-03-22 16:34:08 +0200453 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100454 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100455
456 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200457 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100458 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100459
Thierry Redingc134f012014-06-03 14:48:12 +0200460 if (dc->soc->supports_block_linear) {
461 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200462
Thierry Redingc134f012014-06-03 14:48:12 +0200463 switch (tiling.mode) {
464 case TEGRA_BO_TILING_MODE_PITCH:
465 value = DC_WINBUF_SURFACE_KIND_PITCH;
466 break;
467
468 case TEGRA_BO_TILING_MODE_TILED:
469 value = DC_WINBUF_SURFACE_KIND_TILED;
470 break;
471
472 case TEGRA_BO_TILING_MODE_BLOCK:
473 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
474 DC_WINBUF_SURFACE_KIND_BLOCK;
475 break;
476 }
477
478 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
479 } else {
480 switch (tiling.mode) {
481 case TEGRA_BO_TILING_MODE_PITCH:
482 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
483 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
484 break;
485
486 case TEGRA_BO_TILING_MODE_TILED:
487 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
488 DC_WIN_BUFFER_ADDR_MODE_TILE;
489 break;
490
491 case TEGRA_BO_TILING_MODE_BLOCK:
492 DRM_ERROR("hardware doesn't support block linear mode\n");
493 return -EINVAL;
494 }
495
496 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
497 }
Thierry Reding773af772013-10-04 22:34:01 +0200498
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200499 /* make sure bottom-up buffers are properly displayed */
500 if (tegra_fb_is_bottom_up(fb)) {
501 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100502 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200503 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
504
505 v_offset += fb->height - 1;
506 } else {
507 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100508 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200509 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
510 }
511
512 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
513 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
514
Thierry Reding23fb4742012-11-28 11:38:24 +0100515 value = GENERAL_UPDATE | WIN_A_UPDATE;
516 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
517
518 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
519 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
520
521 return 0;
522}
523
Thierry Reding6e5ff992012-11-28 11:45:47 +0100524void tegra_dc_enable_vblank(struct tegra_dc *dc)
525{
526 unsigned long value, flags;
527
528 spin_lock_irqsave(&dc->lock, flags);
529
530 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
531 value |= VBLANK_INT;
532 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
533
534 spin_unlock_irqrestore(&dc->lock, flags);
535}
536
537void tegra_dc_disable_vblank(struct tegra_dc *dc)
538{
539 unsigned long value, flags;
540
541 spin_lock_irqsave(&dc->lock, flags);
542
543 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
544 value &= ~VBLANK_INT;
545 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
546
547 spin_unlock_irqrestore(&dc->lock, flags);
548}
549
Thierry Redinge6876512013-12-20 13:58:33 +0100550static int tegra_dc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file,
551 uint32_t handle, uint32_t width,
552 uint32_t height, int32_t hot_x, int32_t hot_y)
553{
554 unsigned long value = CURSOR_CLIP_DISPLAY;
555 struct tegra_dc *dc = to_tegra_dc(crtc);
556 struct drm_gem_object *gem;
557 struct tegra_bo *bo = NULL;
558
559 if (!dc->soc->supports_cursor)
560 return -ENXIO;
561
562 if (width != height)
563 return -EINVAL;
564
565 switch (width) {
566 case 32:
567 value |= CURSOR_SIZE_32x32;
568 break;
569
570 case 64:
571 value |= CURSOR_SIZE_64x64;
572 break;
573
574 case 128:
575 value |= CURSOR_SIZE_128x128;
576
577 case 256:
578 value |= CURSOR_SIZE_256x256;
579 break;
580
581 default:
582 return -EINVAL;
583 }
584
585 if (handle) {
586 gem = drm_gem_object_lookup(crtc->dev, file, handle);
587 if (!gem)
588 return -ENOENT;
589
590 bo = to_tegra_bo(gem);
591 }
592
593 if (bo) {
594 unsigned long addr = (bo->paddr & 0xfffffc00) >> 10;
595#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
596 unsigned long high = (bo->paddr & 0xfffffffc) >> 32;
597#endif
598
599 tegra_dc_writel(dc, value | addr, DC_DISP_CURSOR_START_ADDR);
600
601#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
602 tegra_dc_writel(dc, high, DC_DISP_CURSOR_START_ADDR_HI);
603#endif
604
605 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
606 value |= CURSOR_ENABLE;
607 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
608
609 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
610 value &= ~CURSOR_DST_BLEND_MASK;
611 value &= ~CURSOR_SRC_BLEND_MASK;
612 value |= CURSOR_MODE_NORMAL;
613 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
614 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
615 value |= CURSOR_ALPHA;
616 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
617 } else {
618 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
619 value &= ~CURSOR_ENABLE;
620 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
621 }
622
623 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
624 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
625
626 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
627 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
628
629 return 0;
630}
631
632static int tegra_dc_cursor_move(struct drm_crtc *crtc, int x, int y)
633{
634 struct tegra_dc *dc = to_tegra_dc(crtc);
635 unsigned long value;
636
637 if (!dc->soc->supports_cursor)
638 return -ENXIO;
639
640 value = ((y & 0x3fff) << 16) | (x & 0x3fff);
641 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
642
643 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
644 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
645
646 /* XXX: only required on generations earlier than Tegra124? */
647 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
648 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
649
650 return 0;
651}
652
Thierry Reding3c03c462012-11-28 12:00:18 +0100653static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
654{
655 struct drm_device *drm = dc->base.dev;
656 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100657 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200658 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100659
660 if (!dc->event)
661 return;
662
Matt Roperf4510a22014-04-01 15:22:40 -0700663 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100664
665 /* check if new start address has been latched */
666 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
667 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
668 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
669
Matt Roperf4510a22014-04-01 15:22:40 -0700670 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Reding3c03c462012-11-28 12:00:18 +0100671 spin_lock_irqsave(&drm->event_lock, flags);
672 drm_send_vblank_event(drm, dc->pipe, dc->event);
673 drm_vblank_put(drm, dc->pipe);
674 dc->event = NULL;
675 spin_unlock_irqrestore(&drm->event_lock, flags);
676 }
677}
678
679void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
680{
681 struct tegra_dc *dc = to_tegra_dc(crtc);
682 struct drm_device *drm = crtc->dev;
683 unsigned long flags;
684
685 spin_lock_irqsave(&drm->event_lock, flags);
686
687 if (dc->event && dc->event->base.file_priv == file) {
688 dc->event->base.destroy(&dc->event->base);
689 drm_vblank_put(drm, dc->pipe);
690 dc->event = NULL;
691 }
692
693 spin_unlock_irqrestore(&drm->event_lock, flags);
694}
695
696static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000697 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100698{
699 struct tegra_dc *dc = to_tegra_dc(crtc);
700 struct drm_device *drm = crtc->dev;
701
702 if (dc->event)
703 return -EBUSY;
704
705 if (event) {
706 event->pipe = dc->pipe;
707 dc->event = event;
708 drm_vblank_get(drm, dc->pipe);
709 }
710
711 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700712 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100713
714 return 0;
715}
716
Thierry Redingf002abc2013-10-14 14:06:02 +0200717static void drm_crtc_clear(struct drm_crtc *crtc)
718{
719 memset(crtc, 0, sizeof(*crtc));
720}
721
722static void tegra_dc_destroy(struct drm_crtc *crtc)
723{
724 drm_crtc_cleanup(crtc);
725 drm_crtc_clear(crtc);
726}
727
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000728static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Redinge6876512013-12-20 13:58:33 +0100729 .cursor_set2 = tegra_dc_cursor_set2,
730 .cursor_move = tegra_dc_cursor_move,
Thierry Reding3c03c462012-11-28 12:00:18 +0100731 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000732 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +0200733 .destroy = tegra_dc_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000734};
735
Thierry Redingf34bc782012-11-04 21:47:13 +0100736static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000737{
Thierry Redingf002abc2013-10-14 14:06:02 +0200738 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100739 struct drm_device *drm = crtc->dev;
740 struct drm_plane *plane;
741
Daniel Vetter2b4c3662014-04-23 15:15:32 +0200742 drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
Thierry Redingf34bc782012-11-04 21:47:13 +0100743 if (plane->crtc == crtc) {
744 tegra_plane_disable(plane);
745 plane->crtc = NULL;
746
747 if (plane->fb) {
748 drm_framebuffer_unreference(plane->fb);
749 plane->fb = NULL;
750 }
751 }
752 }
Thierry Redingf002abc2013-10-14 14:06:02 +0200753
754 drm_vblank_off(drm, dc->pipe);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000755}
756
757static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
758 const struct drm_display_mode *mode,
759 struct drm_display_mode *adjusted)
760{
761 return true;
762}
763
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000764static int tegra_dc_set_timings(struct tegra_dc *dc,
765 struct drm_display_mode *mode)
766{
Thierry Reding0444c0f2014-04-16 09:22:38 +0200767 unsigned int h_ref_to_sync = 1;
768 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000769 unsigned long value;
770
771 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
772
773 value = (v_ref_to_sync << 16) | h_ref_to_sync;
774 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
775
776 value = ((mode->vsync_end - mode->vsync_start) << 16) |
777 ((mode->hsync_end - mode->hsync_start) << 0);
778 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
779
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000780 value = ((mode->vtotal - mode->vsync_end) << 16) |
781 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +0000782 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
783
784 value = ((mode->vsync_start - mode->vdisplay) << 16) |
785 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000786 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
787
788 value = (mode->vdisplay << 16) | mode->hdisplay;
789 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
790
791 return 0;
792}
793
794static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100795 struct drm_display_mode *mode)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000796{
Thierry Reding91eded92014-03-26 13:32:21 +0100797 unsigned long pclk = mode->clock * 1000;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000798 struct tegra_dc *dc = to_tegra_dc(crtc);
799 struct tegra_output *output = NULL;
800 struct drm_encoder *encoder;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100801 unsigned int div;
802 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000803 long err;
804
805 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
806 if (encoder->crtc == crtc) {
807 output = encoder_to_output(encoder);
808 break;
809 }
810
811 if (!output)
812 return -ENODEV;
813
814 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100815 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
816 * respectively, each of which divides the base pll_d by 2.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000817 */
Thierry Reding91eded92014-03-26 13:32:21 +0100818 err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000819 if (err < 0) {
820 dev_err(dc->dev, "failed to setup clock: %ld\n", err);
821 return err;
822 }
823
Thierry Reding91eded92014-03-26 13:32:21 +0100824 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100825
826 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
827 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000828
829 return 0;
830}
831
832static int tegra_crtc_mode_set(struct drm_crtc *crtc,
833 struct drm_display_mode *mode,
834 struct drm_display_mode *adjusted,
835 int x, int y, struct drm_framebuffer *old_fb)
836{
Matt Roperf4510a22014-04-01 15:22:40 -0700837 struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000838 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100839 struct tegra_dc_window window;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100840 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000841 int err;
842
Thierry Reding6e5ff992012-11-28 11:45:47 +0100843 drm_vblank_pre_modeset(crtc->dev, dc->pipe);
844
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100845 err = tegra_crtc_setup_clk(crtc, mode);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000846 if (err) {
847 dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
848 return err;
849 }
850
851 /* program display mode */
852 tegra_dc_set_timings(dc, mode);
853
Thierry Reding8620fc62013-12-12 11:03:59 +0100854 /* interlacing isn't supported yet, so disable it */
855 if (dc->soc->supports_interlacing) {
856 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
857 value &= ~INTERLACE_ENABLE;
858 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
859 }
860
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000861 /* setup window parameters */
Thierry Redingf34bc782012-11-04 21:47:13 +0100862 memset(&window, 0, sizeof(window));
863 window.src.x = 0;
864 window.src.y = 0;
865 window.src.w = mode->hdisplay;
866 window.src.h = mode->vdisplay;
867 window.dst.x = 0;
868 window.dst.y = 0;
869 window.dst.w = mode->hdisplay;
870 window.dst.h = mode->vdisplay;
Thierry Redingf9253902014-01-29 20:31:17 +0100871 window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
872 &window.swap);
Matt Roperf4510a22014-04-01 15:22:40 -0700873 window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
874 window.stride[0] = crtc->primary->fb->pitches[0];
Arto Merilainende2ba662013-03-22 16:34:08 +0200875 window.base[0] = bo->paddr;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000876
Thierry Redingf34bc782012-11-04 21:47:13 +0100877 err = tegra_dc_setup_window(dc, 0, &window);
878 if (err < 0)
879 dev_err(dc->dev, "failed to enable root plane\n");
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000880
881 return 0;
882}
883
Thierry Reding23fb4742012-11-28 11:38:24 +0100884static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
885 struct drm_framebuffer *old_fb)
886{
887 struct tegra_dc *dc = to_tegra_dc(crtc);
888
Matt Roperf4510a22014-04-01 15:22:40 -0700889 return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
Thierry Reding23fb4742012-11-28 11:38:24 +0100890}
891
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000892static void tegra_crtc_prepare(struct drm_crtc *crtc)
893{
894 struct tegra_dc *dc = to_tegra_dc(crtc);
895 unsigned int syncpt;
896 unsigned long value;
897
898 /* hardware initialization */
Stephen Warrenca480802013-11-06 16:20:54 -0700899 reset_control_deassert(dc->rst);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000900 usleep_range(10000, 20000);
901
902 if (dc->pipe)
903 syncpt = SYNCPT_VBLANK1;
904 else
905 syncpt = SYNCPT_VBLANK0;
906
907 /* initialize display controller */
908 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
909 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
910
911 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
912 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
913
914 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
915 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
916 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
917
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000918 /* initialize timer */
919 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
920 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
921 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
922
923 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
924 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
925 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
926
927 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000928 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +0100929
930 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
931 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000932}
933
934static void tegra_crtc_commit(struct drm_crtc *crtc)
935{
936 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000937 unsigned long value;
938
Thierry Reding3b9e71e2013-01-15 12:21:36 +0100939 value = GENERAL_UPDATE | WIN_A_UPDATE;
940 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000941
Thierry Reding3b9e71e2013-01-15 12:21:36 +0100942 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding6e5ff992012-11-28 11:45:47 +0100943 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000944
Thierry Reding6e5ff992012-11-28 11:45:47 +0100945 drm_vblank_post_modeset(crtc->dev, dc->pipe);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000946}
947
948static void tegra_crtc_load_lut(struct drm_crtc *crtc)
949{
950}
951
952static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +0100953 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000954 .mode_fixup = tegra_crtc_mode_fixup,
955 .mode_set = tegra_crtc_mode_set,
Thierry Reding23fb4742012-11-28 11:38:24 +0100956 .mode_set_base = tegra_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000957 .prepare = tegra_crtc_prepare,
958 .commit = tegra_crtc_commit,
959 .load_lut = tegra_crtc_load_lut,
960};
961
Thierry Reding6e5ff992012-11-28 11:45:47 +0100962static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000963{
964 struct tegra_dc *dc = data;
965 unsigned long status;
966
967 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
968 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
969
970 if (status & FRAME_END_INT) {
971 /*
972 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
973 */
974 }
975
976 if (status & VBLANK_INT) {
977 /*
978 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
979 */
980 drm_handle_vblank(dc->base.dev, dc->pipe);
Thierry Reding3c03c462012-11-28 12:00:18 +0100981 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000982 }
983
984 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
985 /*
986 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
987 */
988 }
989
990 return IRQ_HANDLED;
991}
992
993static int tegra_dc_show_regs(struct seq_file *s, void *data)
994{
995 struct drm_info_node *node = s->private;
996 struct tegra_dc *dc = node->info_ent->data;
997
998#define DUMP_REG(name) \
999 seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
1000 tegra_dc_readl(dc, name))
1001
1002 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1003 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1004 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1005 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1006 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1007 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1008 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1009 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1010 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1011 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1012 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1013 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1014 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1015 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1016 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1017 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1018 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1019 DUMP_REG(DC_CMD_INT_STATUS);
1020 DUMP_REG(DC_CMD_INT_MASK);
1021 DUMP_REG(DC_CMD_INT_ENABLE);
1022 DUMP_REG(DC_CMD_INT_TYPE);
1023 DUMP_REG(DC_CMD_INT_POLARITY);
1024 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1025 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1026 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1027 DUMP_REG(DC_CMD_STATE_ACCESS);
1028 DUMP_REG(DC_CMD_STATE_CONTROL);
1029 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1030 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1031 DUMP_REG(DC_COM_CRC_CONTROL);
1032 DUMP_REG(DC_COM_CRC_CHECKSUM);
1033 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1034 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1035 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1036 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1037 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1038 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1039 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1040 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1041 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1042 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1043 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1044 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1045 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1046 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1047 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1048 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1049 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1050 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1051 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1052 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1053 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1054 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1055 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1056 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1057 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1058 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1059 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1060 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1061 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1062 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1063 DUMP_REG(DC_COM_SPI_CONTROL);
1064 DUMP_REG(DC_COM_SPI_START_BYTE);
1065 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1066 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1067 DUMP_REG(DC_COM_HSPI_CS_DC);
1068 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1069 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1070 DUMP_REG(DC_COM_GPIO_CTRL);
1071 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1072 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1073 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1074 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1075 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1076 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1077 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1078 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1079 DUMP_REG(DC_DISP_REF_TO_SYNC);
1080 DUMP_REG(DC_DISP_SYNC_WIDTH);
1081 DUMP_REG(DC_DISP_BACK_PORCH);
1082 DUMP_REG(DC_DISP_ACTIVE);
1083 DUMP_REG(DC_DISP_FRONT_PORCH);
1084 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1085 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1086 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1087 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1088 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1089 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1090 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1091 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1092 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1093 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1094 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1095 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1096 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1097 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1098 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1099 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1100 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1101 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1102 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1103 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1104 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1105 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1106 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1107 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1108 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1109 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1110 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1111 DUMP_REG(DC_DISP_M0_CONTROL);
1112 DUMP_REG(DC_DISP_M1_CONTROL);
1113 DUMP_REG(DC_DISP_DI_CONTROL);
1114 DUMP_REG(DC_DISP_PP_CONTROL);
1115 DUMP_REG(DC_DISP_PP_SELECT_A);
1116 DUMP_REG(DC_DISP_PP_SELECT_B);
1117 DUMP_REG(DC_DISP_PP_SELECT_C);
1118 DUMP_REG(DC_DISP_PP_SELECT_D);
1119 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1120 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1121 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1122 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1123 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1124 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1125 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1126 DUMP_REG(DC_DISP_BORDER_COLOR);
1127 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1128 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1129 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1130 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1131 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1132 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1133 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1134 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1135 DUMP_REG(DC_DISP_CURSOR_POSITION);
1136 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1137 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1138 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1139 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1140 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1141 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1142 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1143 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1144 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1145 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1146 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1147 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1148 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1149 DUMP_REG(DC_DISP_SD_CONTROL);
1150 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1151 DUMP_REG(DC_DISP_SD_LUT(0));
1152 DUMP_REG(DC_DISP_SD_LUT(1));
1153 DUMP_REG(DC_DISP_SD_LUT(2));
1154 DUMP_REG(DC_DISP_SD_LUT(3));
1155 DUMP_REG(DC_DISP_SD_LUT(4));
1156 DUMP_REG(DC_DISP_SD_LUT(5));
1157 DUMP_REG(DC_DISP_SD_LUT(6));
1158 DUMP_REG(DC_DISP_SD_LUT(7));
1159 DUMP_REG(DC_DISP_SD_LUT(8));
1160 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1161 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1162 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1163 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1164 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1165 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1166 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1167 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1168 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1169 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1170 DUMP_REG(DC_DISP_SD_BL_TF(0));
1171 DUMP_REG(DC_DISP_SD_BL_TF(1));
1172 DUMP_REG(DC_DISP_SD_BL_TF(2));
1173 DUMP_REG(DC_DISP_SD_BL_TF(3));
1174 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1175 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1176 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001177 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1178 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001179 DUMP_REG(DC_WIN_WIN_OPTIONS);
1180 DUMP_REG(DC_WIN_BYTE_SWAP);
1181 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1182 DUMP_REG(DC_WIN_COLOR_DEPTH);
1183 DUMP_REG(DC_WIN_POSITION);
1184 DUMP_REG(DC_WIN_SIZE);
1185 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1186 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1187 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1188 DUMP_REG(DC_WIN_DDA_INC);
1189 DUMP_REG(DC_WIN_LINE_STRIDE);
1190 DUMP_REG(DC_WIN_BUF_STRIDE);
1191 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1192 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1193 DUMP_REG(DC_WIN_DV_CONTROL);
1194 DUMP_REG(DC_WIN_BLEND_NOKEY);
1195 DUMP_REG(DC_WIN_BLEND_1WIN);
1196 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1197 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001198 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001199 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1200 DUMP_REG(DC_WINBUF_START_ADDR);
1201 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1202 DUMP_REG(DC_WINBUF_START_ADDR_U);
1203 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1204 DUMP_REG(DC_WINBUF_START_ADDR_V);
1205 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1206 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1207 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1208 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1209 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1210 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1211 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1212 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1213 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1214
1215#undef DUMP_REG
1216
1217 return 0;
1218}
1219
1220static struct drm_info_list debugfs_files[] = {
1221 { "regs", tegra_dc_show_regs, 0, NULL },
1222};
1223
1224static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1225{
1226 unsigned int i;
1227 char *name;
1228 int err;
1229
1230 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1231 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1232 kfree(name);
1233
1234 if (!dc->debugfs)
1235 return -ENOMEM;
1236
1237 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1238 GFP_KERNEL);
1239 if (!dc->debugfs_files) {
1240 err = -ENOMEM;
1241 goto remove;
1242 }
1243
1244 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1245 dc->debugfs_files[i].data = dc;
1246
1247 err = drm_debugfs_create_files(dc->debugfs_files,
1248 ARRAY_SIZE(debugfs_files),
1249 dc->debugfs, minor);
1250 if (err < 0)
1251 goto free;
1252
1253 dc->minor = minor;
1254
1255 return 0;
1256
1257free:
1258 kfree(dc->debugfs_files);
1259 dc->debugfs_files = NULL;
1260remove:
1261 debugfs_remove(dc->debugfs);
1262 dc->debugfs = NULL;
1263
1264 return err;
1265}
1266
1267static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1268{
1269 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1270 dc->minor);
1271 dc->minor = NULL;
1272
1273 kfree(dc->debugfs_files);
1274 dc->debugfs_files = NULL;
1275
1276 debugfs_remove(dc->debugfs);
1277 dc->debugfs = NULL;
1278
1279 return 0;
1280}
1281
Thierry Reding53fa7f72013-09-24 15:35:40 +02001282static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001283{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001284 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001285 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001286 int err;
1287
Thierry Reding9910f5c2014-05-22 09:57:15 +02001288 drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001289 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1290 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1291
Thierry Reding9910f5c2014-05-22 09:57:15 +02001292 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001293 if (err < 0 && err != -ENODEV) {
1294 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1295 return err;
1296 }
1297
Thierry Reding9910f5c2014-05-22 09:57:15 +02001298 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001299 if (err < 0)
1300 return err;
1301
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001302 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001303 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001304 if (err < 0)
1305 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1306 }
1307
Thierry Reding6e5ff992012-11-28 11:45:47 +01001308 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001309 dev_name(dc->dev), dc);
1310 if (err < 0) {
1311 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1312 err);
1313 return err;
1314 }
1315
1316 return 0;
1317}
1318
Thierry Reding53fa7f72013-09-24 15:35:40 +02001319static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001320{
Thierry Reding776dc382013-10-14 14:43:22 +02001321 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001322 int err;
1323
1324 devm_free_irq(dc->dev, dc->irq, dc);
1325
1326 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1327 err = tegra_dc_debugfs_exit(dc);
1328 if (err < 0)
1329 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1330 }
1331
1332 err = tegra_dc_rgb_exit(dc);
1333 if (err) {
1334 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1335 return err;
1336 }
1337
1338 return 0;
1339}
1340
1341static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001342 .init = tegra_dc_init,
1343 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001344};
1345
Thierry Reding8620fc62013-12-12 11:03:59 +01001346static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
1347 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001348 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001349 .supports_block_linear = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001350};
1351
1352static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
1353 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001354 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001355 .supports_block_linear = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001356};
1357
1358static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
1359 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001360 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001361 .supports_block_linear = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001362};
1363
1364static const struct of_device_id tegra_dc_of_match[] = {
1365 {
1366 .compatible = "nvidia,tegra124-dc",
1367 .data = &tegra124_dc_soc_info,
1368 }, {
1369 .compatible = "nvidia,tegra30-dc",
1370 .data = &tegra30_dc_soc_info,
1371 }, {
1372 .compatible = "nvidia,tegra20-dc",
1373 .data = &tegra20_dc_soc_info,
1374 }, {
1375 /* sentinel */
1376 }
1377};
1378
Thierry Reding13411dd2014-01-09 17:08:36 +01001379static int tegra_dc_parse_dt(struct tegra_dc *dc)
1380{
1381 struct device_node *np;
1382 u32 value = 0;
1383 int err;
1384
1385 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1386 if (err < 0) {
1387 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1388
1389 /*
1390 * If the nvidia,head property isn't present, try to find the
1391 * correct head number by looking up the position of this
1392 * display controller's node within the device tree. Assuming
1393 * that the nodes are ordered properly in the DTS file and
1394 * that the translation into a flattened device tree blob
1395 * preserves that ordering this will actually yield the right
1396 * head number.
1397 *
1398 * If those assumptions don't hold, this will still work for
1399 * cases where only a single display controller is used.
1400 */
1401 for_each_matching_node(np, tegra_dc_of_match) {
1402 if (np == dc->dev->of_node)
1403 break;
1404
1405 value++;
1406 }
1407 }
1408
1409 dc->pipe = value;
1410
1411 return 0;
1412}
1413
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001414static int tegra_dc_probe(struct platform_device *pdev)
1415{
Thierry Reding8620fc62013-12-12 11:03:59 +01001416 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001417 struct resource *regs;
1418 struct tegra_dc *dc;
1419 int err;
1420
1421 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1422 if (!dc)
1423 return -ENOMEM;
1424
Thierry Reding8620fc62013-12-12 11:03:59 +01001425 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1426 if (!id)
1427 return -ENODEV;
1428
Thierry Reding6e5ff992012-11-28 11:45:47 +01001429 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001430 INIT_LIST_HEAD(&dc->list);
1431 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001432 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001433
Thierry Reding13411dd2014-01-09 17:08:36 +01001434 err = tegra_dc_parse_dt(dc);
1435 if (err < 0)
1436 return err;
1437
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001438 dc->clk = devm_clk_get(&pdev->dev, NULL);
1439 if (IS_ERR(dc->clk)) {
1440 dev_err(&pdev->dev, "failed to get clock\n");
1441 return PTR_ERR(dc->clk);
1442 }
1443
Stephen Warrenca480802013-11-06 16:20:54 -07001444 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1445 if (IS_ERR(dc->rst)) {
1446 dev_err(&pdev->dev, "failed to get reset\n");
1447 return PTR_ERR(dc->rst);
1448 }
1449
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001450 err = clk_prepare_enable(dc->clk);
1451 if (err < 0)
1452 return err;
1453
1454 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001455 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1456 if (IS_ERR(dc->regs))
1457 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001458
1459 dc->irq = platform_get_irq(pdev, 0);
1460 if (dc->irq < 0) {
1461 dev_err(&pdev->dev, "failed to get IRQ\n");
1462 return -ENXIO;
1463 }
1464
Thierry Reding776dc382013-10-14 14:43:22 +02001465 INIT_LIST_HEAD(&dc->client.list);
1466 dc->client.ops = &dc_client_ops;
1467 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001468
1469 err = tegra_dc_rgb_probe(dc);
1470 if (err < 0 && err != -ENODEV) {
1471 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1472 return err;
1473 }
1474
Thierry Reding776dc382013-10-14 14:43:22 +02001475 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001476 if (err < 0) {
1477 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1478 err);
1479 return err;
1480 }
1481
1482 platform_set_drvdata(pdev, dc);
1483
1484 return 0;
1485}
1486
1487static int tegra_dc_remove(struct platform_device *pdev)
1488{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001489 struct tegra_dc *dc = platform_get_drvdata(pdev);
1490 int err;
1491
Thierry Reding776dc382013-10-14 14:43:22 +02001492 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001493 if (err < 0) {
1494 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1495 err);
1496 return err;
1497 }
1498
Thierry Reding59d29c02013-10-14 14:26:42 +02001499 err = tegra_dc_rgb_remove(dc);
1500 if (err < 0) {
1501 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1502 return err;
1503 }
1504
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001505 clk_disable_unprepare(dc->clk);
1506
1507 return 0;
1508}
1509
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001510struct platform_driver tegra_dc_driver = {
1511 .driver = {
1512 .name = "tegra-dc",
1513 .owner = THIS_MODULE,
1514 .of_match_table = tegra_dc_of_match,
1515 },
1516 .probe = tegra_dc_probe,
1517 .remove = tegra_dc_remove,
1518};