blob: a56fccc428d506ac9b753031bf33b8a6d5f8f31d [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>
Thierry Redingdf06b752014-06-26 21:41:53 +020012#include <linux/iommu.h>
Stephen Warrenca480802013-11-06 16:20:54 -070013#include <linux/reset.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000014
Thierry Reding9c012702014-07-07 15:32:53 +020015#include <soc/tegra/pmc.h>
16
Arto Merilainende2ba662013-03-22 16:34:08 +020017#include "dc.h"
18#include "drm.h"
19#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000020
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010021#include <drm/drm_plane_helper.h>
22
Thierry Reding8620fc62013-12-12 11:03:59 +010023struct tegra_dc_soc_info {
Thierry Reding42d06592014-12-08 15:45:39 +010024 bool supports_border_color;
Thierry Reding8620fc62013-12-12 11:03:59 +010025 bool supports_interlacing;
Thierry Redinge6876512013-12-20 13:58:33 +010026 bool supports_cursor;
Thierry Redingc134f012014-06-03 14:48:12 +020027 bool supports_block_linear;
Thierry Redingd1f3e1e2014-07-11 08:29:14 +020028 unsigned int pitch_align;
Thierry Reding9c012702014-07-07 15:32:53 +020029 bool has_powergate;
Thierry Reding8620fc62013-12-12 11:03:59 +010030};
31
Thierry Redingf34bc782012-11-04 21:47:13 +010032struct tegra_plane {
33 struct drm_plane base;
34 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000035};
36
Thierry Redingf34bc782012-11-04 21:47:13 +010037static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
38{
39 return container_of(plane, struct tegra_plane, base);
40}
41
Thierry Reding205d48e2014-10-21 13:41:46 +020042static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
43{
44 u32 value = WIN_A_ACT_REQ << index;
45
46 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
47 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
48}
49
50static void tegra_dc_cursor_commit(struct tegra_dc *dc)
51{
52 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
53 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
54}
55
Thierry Redingd700ba72014-12-08 15:50:04 +010056/*
57 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
58 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
59 * Latching happens mmediately if the display controller is in STOP mode or
60 * on the next frame boundary otherwise.
61 *
62 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
63 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
64 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
65 * into the ACTIVE copy, either immediately if the display controller is in
66 * STOP mode, or at the next frame boundary otherwise.
67 */
Thierry Reding205d48e2014-10-21 13:41:46 +020068static void tegra_dc_commit(struct tegra_dc *dc)
69{
70 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
71 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
72}
73
Thierry Reding10288ee2014-03-14 09:54:58 +010074static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
75{
76 /* assume no swapping of fetched data */
77 if (swap)
78 *swap = BYTE_SWAP_NOSWAP;
79
80 switch (format) {
81 case DRM_FORMAT_XBGR8888:
82 return WIN_COLOR_DEPTH_R8G8B8A8;
83
84 case DRM_FORMAT_XRGB8888:
85 return WIN_COLOR_DEPTH_B8G8R8A8;
86
87 case DRM_FORMAT_RGB565:
88 return WIN_COLOR_DEPTH_B5G6R5;
89
90 case DRM_FORMAT_UYVY:
91 return WIN_COLOR_DEPTH_YCbCr422;
92
93 case DRM_FORMAT_YUYV:
94 if (swap)
95 *swap = BYTE_SWAP_SWAP2;
96
97 return WIN_COLOR_DEPTH_YCbCr422;
98
99 case DRM_FORMAT_YUV420:
100 return WIN_COLOR_DEPTH_YCbCr420P;
101
102 case DRM_FORMAT_YUV422:
103 return WIN_COLOR_DEPTH_YCbCr422P;
104
105 default:
106 break;
107 }
108
109 WARN(1, "unsupported pixel format %u, using default\n", format);
110 return WIN_COLOR_DEPTH_B8G8R8A8;
111}
112
113static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
114{
115 switch (format) {
116 case WIN_COLOR_DEPTH_YCbCr422:
117 case WIN_COLOR_DEPTH_YUV422:
118 if (planar)
119 *planar = false;
120
121 return true;
122
123 case WIN_COLOR_DEPTH_YCbCr420P:
124 case WIN_COLOR_DEPTH_YUV420P:
125 case WIN_COLOR_DEPTH_YCbCr422P:
126 case WIN_COLOR_DEPTH_YUV422P:
127 case WIN_COLOR_DEPTH_YCbCr422R:
128 case WIN_COLOR_DEPTH_YUV422R:
129 case WIN_COLOR_DEPTH_YCbCr422RA:
130 case WIN_COLOR_DEPTH_YUV422RA:
131 if (planar)
132 *planar = true;
133
134 return true;
135 }
136
137 return false;
138}
139
140static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
141 unsigned int bpp)
142{
143 fixed20_12 outf = dfixed_init(out);
144 fixed20_12 inf = dfixed_init(in);
145 u32 dda_inc;
146 int max;
147
148 if (v)
149 max = 15;
150 else {
151 switch (bpp) {
152 case 2:
153 max = 8;
154 break;
155
156 default:
157 WARN_ON_ONCE(1);
158 /* fallthrough */
159 case 4:
160 max = 4;
161 break;
162 }
163 }
164
165 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
166 inf.full -= dfixed_const(1);
167
168 dda_inc = dfixed_div(inf, outf);
169 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
170
171 return dda_inc;
172}
173
174static inline u32 compute_initial_dda(unsigned int in)
175{
176 fixed20_12 inf = dfixed_init(in);
177 return dfixed_frac(inf);
178}
179
180static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
181 const struct tegra_dc_window *window)
182{
183 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
Sean Paul93396d02014-11-19 13:04:49 -0500184 unsigned long value, flags;
Thierry Reding10288ee2014-03-14 09:54:58 +0100185 bool yuv, planar;
186
187 /*
188 * For YUV planar modes, the number of bytes per pixel takes into
189 * account only the luma component and therefore is 1.
190 */
191 yuv = tegra_dc_format_is_yuv(window->format, &planar);
192 if (!yuv)
193 bpp = window->bits_per_pixel / 8;
194 else
195 bpp = planar ? 1 : 2;
196
Sean Paul93396d02014-11-19 13:04:49 -0500197 spin_lock_irqsave(&dc->lock, flags);
198
Thierry Reding10288ee2014-03-14 09:54:58 +0100199 value = WINDOW_A_SELECT << index;
200 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
201
202 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
203 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
204
205 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
206 tegra_dc_writel(dc, value, DC_WIN_POSITION);
207
208 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
209 tegra_dc_writel(dc, value, DC_WIN_SIZE);
210
211 h_offset = window->src.x * bpp;
212 v_offset = window->src.y;
213 h_size = window->src.w * bpp;
214 v_size = window->src.h;
215
216 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
217 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
218
219 /*
220 * For DDA computations the number of bytes per pixel for YUV planar
221 * modes needs to take into account all Y, U and V components.
222 */
223 if (yuv && planar)
224 bpp = 2;
225
226 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
227 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
228
229 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
230 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
231
232 h_dda = compute_initial_dda(window->src.x);
233 v_dda = compute_initial_dda(window->src.y);
234
235 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
236 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
237
238 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
239 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
240
241 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
242
243 if (yuv && planar) {
244 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
245 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
246 value = window->stride[1] << 16 | window->stride[0];
247 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
248 } else {
249 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
250 }
251
252 if (window->bottom_up)
253 v_offset += window->src.h - 1;
254
255 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
256 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
257
Thierry Redingc134f012014-06-03 14:48:12 +0200258 if (dc->soc->supports_block_linear) {
259 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100260
Thierry Redingc134f012014-06-03 14:48:12 +0200261 switch (window->tiling.mode) {
262 case TEGRA_BO_TILING_MODE_PITCH:
263 value = DC_WINBUF_SURFACE_KIND_PITCH;
264 break;
265
266 case TEGRA_BO_TILING_MODE_TILED:
267 value = DC_WINBUF_SURFACE_KIND_TILED;
268 break;
269
270 case TEGRA_BO_TILING_MODE_BLOCK:
271 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
272 DC_WINBUF_SURFACE_KIND_BLOCK;
273 break;
274 }
275
276 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
277 } else {
278 switch (window->tiling.mode) {
279 case TEGRA_BO_TILING_MODE_PITCH:
280 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
281 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
282 break;
283
284 case TEGRA_BO_TILING_MODE_TILED:
285 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
286 DC_WIN_BUFFER_ADDR_MODE_TILE;
287 break;
288
289 case TEGRA_BO_TILING_MODE_BLOCK:
290 DRM_ERROR("hardware doesn't support block linear mode\n");
Sean Paul93396d02014-11-19 13:04:49 -0500291 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc134f012014-06-03 14:48:12 +0200292 return -EINVAL;
293 }
294
295 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
296 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100297
298 value = WIN_ENABLE;
299
300 if (yuv) {
301 /* setup default colorspace conversion coefficients */
302 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
303 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
304 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
305 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
306 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
307 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
308 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
309 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
310
311 value |= CSC_ENABLE;
312 } else if (window->bits_per_pixel < 24) {
313 value |= COLOR_EXPAND;
314 }
315
316 if (window->bottom_up)
317 value |= V_DIRECTION;
318
319 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
320
321 /*
322 * Disable blending and assume Window A is the bottom-most window,
323 * Window C is the top-most window and Window B is in the middle.
324 */
325 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
326 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
327
328 switch (index) {
329 case 0:
330 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
331 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
332 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
333 break;
334
335 case 1:
336 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
337 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
338 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
339 break;
340
341 case 2:
342 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
343 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
344 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
345 break;
346 }
347
Thierry Reding205d48e2014-10-21 13:41:46 +0200348 tegra_dc_window_commit(dc, index);
Thierry Reding10288ee2014-03-14 09:54:58 +0100349
Sean Paul93396d02014-11-19 13:04:49 -0500350 spin_unlock_irqrestore(&dc->lock, flags);
351
Thierry Reding10288ee2014-03-14 09:54:58 +0100352 return 0;
353}
354
Thierry Redingc7679302014-10-21 13:51:53 +0200355static int tegra_window_plane_disable(struct drm_plane *plane)
356{
357 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
358 struct tegra_plane *p = to_tegra_plane(plane);
Sean Paul93396d02014-11-19 13:04:49 -0500359 unsigned long flags;
Thierry Redingc7679302014-10-21 13:51:53 +0200360 u32 value;
361
362 if (!plane->crtc)
363 return 0;
364
Sean Paul93396d02014-11-19 13:04:49 -0500365 spin_lock_irqsave(&dc->lock, flags);
366
Thierry Redingc7679302014-10-21 13:51:53 +0200367 value = WINDOW_A_SELECT << p->index;
368 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
369
370 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
371 value &= ~WIN_ENABLE;
372 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
373
374 tegra_dc_window_commit(dc, p->index);
375
Sean Paul93396d02014-11-19 13:04:49 -0500376 spin_unlock_irqrestore(&dc->lock, flags);
377
Thierry Redingc7679302014-10-21 13:51:53 +0200378 return 0;
379}
380
381static void tegra_plane_destroy(struct drm_plane *plane)
382{
383 struct tegra_plane *p = to_tegra_plane(plane);
384
385 drm_plane_cleanup(plane);
386 kfree(p);
387}
388
389static const u32 tegra_primary_plane_formats[] = {
390 DRM_FORMAT_XBGR8888,
391 DRM_FORMAT_XRGB8888,
392 DRM_FORMAT_RGB565,
393};
394
395static int tegra_primary_plane_update(struct drm_plane *plane,
396 struct drm_crtc *crtc,
397 struct drm_framebuffer *fb, int crtc_x,
398 int crtc_y, unsigned int crtc_w,
399 unsigned int crtc_h, uint32_t src_x,
400 uint32_t src_y, uint32_t src_w,
401 uint32_t src_h)
402{
403 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
404 struct tegra_plane *p = to_tegra_plane(plane);
405 struct tegra_dc *dc = to_tegra_dc(crtc);
406 struct tegra_dc_window window;
407 int err;
408
409 memset(&window, 0, sizeof(window));
410 window.src.x = src_x >> 16;
411 window.src.y = src_y >> 16;
412 window.src.w = src_w >> 16;
413 window.src.h = src_h >> 16;
414 window.dst.x = crtc_x;
415 window.dst.y = crtc_y;
416 window.dst.w = crtc_w;
417 window.dst.h = crtc_h;
418 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
419 window.bits_per_pixel = fb->bits_per_pixel;
420 window.bottom_up = tegra_fb_is_bottom_up(fb);
421
422 err = tegra_fb_get_tiling(fb, &window.tiling);
423 if (err < 0)
424 return err;
425
426 window.base[0] = bo->paddr + fb->offsets[0];
427 window.stride[0] = fb->pitches[0];
428
429 err = tegra_dc_setup_window(dc, p->index, &window);
430 if (err < 0)
431 return err;
432
433 return 0;
434}
435
436static void tegra_primary_plane_destroy(struct drm_plane *plane)
437{
438 tegra_window_plane_disable(plane);
439 tegra_plane_destroy(plane);
440}
441
442static const struct drm_plane_funcs tegra_primary_plane_funcs = {
443 .update_plane = tegra_primary_plane_update,
444 .disable_plane = tegra_window_plane_disable,
445 .destroy = tegra_primary_plane_destroy,
446};
447
448static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
449 struct tegra_dc *dc)
450{
Thierry Reding518e6222014-12-16 18:04:08 +0100451 /*
452 * Ideally this would use drm_crtc_mask(), but that would require the
453 * CRTC to already be in the mode_config's list of CRTCs. However, it
454 * will only be added to that list in the drm_crtc_init_with_planes()
455 * (in tegra_dc_init()), which in turn requires registration of these
456 * planes. So we have ourselves a nice little chicken and egg problem
457 * here.
458 *
459 * We work around this by manually creating the mask from the number
460 * of CRTCs that have been registered, and should therefore always be
461 * the same as drm_crtc_index() after registration.
462 */
463 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Redingc7679302014-10-21 13:51:53 +0200464 struct tegra_plane *plane;
465 unsigned int num_formats;
466 const u32 *formats;
467 int err;
468
469 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
470 if (!plane)
471 return ERR_PTR(-ENOMEM);
472
473 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
474 formats = tegra_primary_plane_formats;
475
Thierry Reding518e6222014-12-16 18:04:08 +0100476 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc7679302014-10-21 13:51:53 +0200477 &tegra_primary_plane_funcs, formats,
478 num_formats, DRM_PLANE_TYPE_PRIMARY);
479 if (err < 0) {
480 kfree(plane);
481 return ERR_PTR(err);
482 }
483
484 return &plane->base;
485}
486
487static const u32 tegra_cursor_plane_formats[] = {
488 DRM_FORMAT_RGBA8888,
489};
490
491static int tegra_cursor_plane_update(struct drm_plane *plane,
492 struct drm_crtc *crtc,
493 struct drm_framebuffer *fb, int crtc_x,
494 int crtc_y, unsigned int crtc_w,
495 unsigned int crtc_h, uint32_t src_x,
496 uint32_t src_y, uint32_t src_w,
497 uint32_t src_h)
498{
499 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
500 struct tegra_dc *dc = to_tegra_dc(crtc);
501 u32 value = CURSOR_CLIP_DISPLAY;
502
503 /* scaling not supported for cursor */
504 if ((src_w >> 16 != crtc_w) || (src_h >> 16 != crtc_h))
505 return -EINVAL;
506
507 /* only square cursors supported */
508 if (src_w != src_h)
509 return -EINVAL;
510
511 switch (crtc_w) {
512 case 32:
513 value |= CURSOR_SIZE_32x32;
514 break;
515
516 case 64:
517 value |= CURSOR_SIZE_64x64;
518 break;
519
520 case 128:
521 value |= CURSOR_SIZE_128x128;
522 break;
523
524 case 256:
525 value |= CURSOR_SIZE_256x256;
526 break;
527
528 default:
529 return -EINVAL;
530 }
531
532 value |= (bo->paddr >> 10) & 0x3fffff;
533 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
534
535#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
536 value = (bo->paddr >> 32) & 0x3;
537 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
538#endif
539
540 /* enable cursor and set blend mode */
541 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
542 value |= CURSOR_ENABLE;
543 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
544
545 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
546 value &= ~CURSOR_DST_BLEND_MASK;
547 value &= ~CURSOR_SRC_BLEND_MASK;
548 value |= CURSOR_MODE_NORMAL;
549 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
550 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
551 value |= CURSOR_ALPHA;
552 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
553
554 /* position the cursor */
555 value = (crtc_y & 0x3fff) << 16 | (crtc_x & 0x3fff);
556 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
557
558 /* apply changes */
559 tegra_dc_cursor_commit(dc);
560 tegra_dc_commit(dc);
561
562 return 0;
563}
564
565static int tegra_cursor_plane_disable(struct drm_plane *plane)
566{
567 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
568 u32 value;
569
570 if (!plane->crtc)
571 return 0;
572
573 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
574 value &= ~CURSOR_ENABLE;
575 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
576
577 tegra_dc_cursor_commit(dc);
578 tegra_dc_commit(dc);
579
580 return 0;
581}
582
583static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
584 .update_plane = tegra_cursor_plane_update,
585 .disable_plane = tegra_cursor_plane_disable,
586 .destroy = tegra_plane_destroy,
587};
588
589static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
590 struct tegra_dc *dc)
591{
592 struct tegra_plane *plane;
593 unsigned int num_formats;
594 const u32 *formats;
595 int err;
596
597 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
598 if (!plane)
599 return ERR_PTR(-ENOMEM);
600
601 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
602 formats = tegra_cursor_plane_formats;
603
604 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
605 &tegra_cursor_plane_funcs, formats,
606 num_formats, DRM_PLANE_TYPE_CURSOR);
607 if (err < 0) {
608 kfree(plane);
609 return ERR_PTR(err);
610 }
611
612 return &plane->base;
613}
614
615static int tegra_overlay_plane_update(struct drm_plane *plane,
616 struct drm_crtc *crtc,
617 struct drm_framebuffer *fb, int crtc_x,
618 int crtc_y, unsigned int crtc_w,
619 unsigned int crtc_h, uint32_t src_x,
620 uint32_t src_y, uint32_t src_w,
621 uint32_t src_h)
Thierry Redingf34bc782012-11-04 21:47:13 +0100622{
623 struct tegra_plane *p = to_tegra_plane(plane);
624 struct tegra_dc *dc = to_tegra_dc(crtc);
625 struct tegra_dc_window window;
626 unsigned int i;
Thierry Redingc134f012014-06-03 14:48:12 +0200627 int err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100628
629 memset(&window, 0, sizeof(window));
630 window.src.x = src_x >> 16;
631 window.src.y = src_y >> 16;
632 window.src.w = src_w >> 16;
633 window.src.h = src_h >> 16;
634 window.dst.x = crtc_x;
635 window.dst.y = crtc_y;
636 window.dst.w = crtc_w;
637 window.dst.h = crtc_h;
Thierry Redingf9253902014-01-29 20:31:17 +0100638 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
Thierry Redingf34bc782012-11-04 21:47:13 +0100639 window.bits_per_pixel = fb->bits_per_pixel;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200640 window.bottom_up = tegra_fb_is_bottom_up(fb);
Thierry Redingc134f012014-06-03 14:48:12 +0200641
642 err = tegra_fb_get_tiling(fb, &window.tiling);
643 if (err < 0)
644 return err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100645
646 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
Arto Merilainende2ba662013-03-22 16:34:08 +0200647 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingf34bc782012-11-04 21:47:13 +0100648
Arto Merilainende2ba662013-03-22 16:34:08 +0200649 window.base[i] = bo->paddr + fb->offsets[i];
Thierry Redingf34bc782012-11-04 21:47:13 +0100650
651 /*
652 * Tegra doesn't support different strides for U and V planes
653 * so we display a warning if the user tries to display a
654 * framebuffer with such a configuration.
655 */
656 if (i >= 2) {
657 if (fb->pitches[i] != window.stride[1])
658 DRM_ERROR("unsupported UV-plane configuration\n");
659 } else {
660 window.stride[i] = fb->pitches[i];
661 }
662 }
663
664 return tegra_dc_setup_window(dc, p->index, &window);
665}
666
Thierry Redingc7679302014-10-21 13:51:53 +0200667static void tegra_overlay_plane_destroy(struct drm_plane *plane)
Thierry Redingf34bc782012-11-04 21:47:13 +0100668{
Thierry Redingc7679302014-10-21 13:51:53 +0200669 tegra_window_plane_disable(plane);
670 tegra_plane_destroy(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100671}
672
Thierry Redingc7679302014-10-21 13:51:53 +0200673static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
674 .update_plane = tegra_overlay_plane_update,
675 .disable_plane = tegra_window_plane_disable,
676 .destroy = tegra_overlay_plane_destroy,
Thierry Redingf34bc782012-11-04 21:47:13 +0100677};
678
Thierry Redingc7679302014-10-21 13:51:53 +0200679static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100680 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100681 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100682 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100683 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100684 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100685 DRM_FORMAT_YUV420,
686 DRM_FORMAT_YUV422,
687};
688
Thierry Redingc7679302014-10-21 13:51:53 +0200689static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
690 struct tegra_dc *dc,
691 unsigned int index)
692{
693 struct tegra_plane *plane;
694 unsigned int num_formats;
695 const u32 *formats;
696 int err;
697
698 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
699 if (!plane)
700 return ERR_PTR(-ENOMEM);
701
702 plane->index = index;
703
704 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
705 formats = tegra_overlay_plane_formats;
706
707 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
708 &tegra_overlay_plane_funcs, formats,
709 num_formats, DRM_PLANE_TYPE_OVERLAY);
710 if (err < 0) {
711 kfree(plane);
712 return ERR_PTR(err);
713 }
714
715 return &plane->base;
716}
717
Thierry Redingf34bc782012-11-04 21:47:13 +0100718static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
719{
Thierry Redingc7679302014-10-21 13:51:53 +0200720 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100721 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100722
723 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200724 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
725 if (IS_ERR(plane))
726 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100727 }
728
729 return 0;
730}
731
Thierry Reding23fb4742012-11-28 11:38:24 +0100732static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
733 struct drm_framebuffer *fb)
734{
Arto Merilainende2ba662013-03-22 16:34:08 +0200735 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200736 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200737 struct tegra_bo_tiling tiling;
Sean Paul93396d02014-11-19 13:04:49 -0500738 unsigned long value, flags;
Thierry Redingf9253902014-01-29 20:31:17 +0100739 unsigned int format, swap;
Thierry Redingc134f012014-06-03 14:48:12 +0200740 int err;
741
742 err = tegra_fb_get_tiling(fb, &tiling);
743 if (err < 0)
744 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100745
Sean Paul93396d02014-11-19 13:04:49 -0500746 spin_lock_irqsave(&dc->lock, flags);
747
Thierry Reding23fb4742012-11-28 11:38:24 +0100748 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
749
750 value = fb->offsets[0] + y * fb->pitches[0] +
751 x * fb->bits_per_pixel / 8;
752
Arto Merilainende2ba662013-03-22 16:34:08 +0200753 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100754 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100755
756 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200757 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100758 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100759
Thierry Redingc134f012014-06-03 14:48:12 +0200760 if (dc->soc->supports_block_linear) {
761 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200762
Thierry Redingc134f012014-06-03 14:48:12 +0200763 switch (tiling.mode) {
764 case TEGRA_BO_TILING_MODE_PITCH:
765 value = DC_WINBUF_SURFACE_KIND_PITCH;
766 break;
767
768 case TEGRA_BO_TILING_MODE_TILED:
769 value = DC_WINBUF_SURFACE_KIND_TILED;
770 break;
771
772 case TEGRA_BO_TILING_MODE_BLOCK:
773 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
774 DC_WINBUF_SURFACE_KIND_BLOCK;
775 break;
776 }
777
778 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
779 } else {
780 switch (tiling.mode) {
781 case TEGRA_BO_TILING_MODE_PITCH:
782 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
783 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
784 break;
785
786 case TEGRA_BO_TILING_MODE_TILED:
787 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
788 DC_WIN_BUFFER_ADDR_MODE_TILE;
789 break;
790
791 case TEGRA_BO_TILING_MODE_BLOCK:
792 DRM_ERROR("hardware doesn't support block linear mode\n");
Sean Paul93396d02014-11-19 13:04:49 -0500793 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc134f012014-06-03 14:48:12 +0200794 return -EINVAL;
795 }
796
797 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
798 }
Thierry Reding773af772013-10-04 22:34:01 +0200799
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200800 /* make sure bottom-up buffers are properly displayed */
801 if (tegra_fb_is_bottom_up(fb)) {
802 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100803 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200804 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
805
806 v_offset += fb->height - 1;
807 } else {
808 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100809 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200810 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
811 }
812
813 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
814 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
815
Thierry Reding23fb4742012-11-28 11:38:24 +0100816 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding205d48e2014-10-21 13:41:46 +0200817 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
Thierry Reding23fb4742012-11-28 11:38:24 +0100818 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
819
Sean Paul93396d02014-11-19 13:04:49 -0500820 spin_unlock_irqrestore(&dc->lock, flags);
821
Thierry Reding23fb4742012-11-28 11:38:24 +0100822 return 0;
823}
824
Thierry Reding6e5ff992012-11-28 11:45:47 +0100825void tegra_dc_enable_vblank(struct tegra_dc *dc)
826{
827 unsigned long value, flags;
828
829 spin_lock_irqsave(&dc->lock, flags);
830
831 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
832 value |= VBLANK_INT;
833 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
834
835 spin_unlock_irqrestore(&dc->lock, flags);
836}
837
838void tegra_dc_disable_vblank(struct tegra_dc *dc)
839{
840 unsigned long value, flags;
841
842 spin_lock_irqsave(&dc->lock, flags);
843
844 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
845 value &= ~VBLANK_INT;
846 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
847
848 spin_unlock_irqrestore(&dc->lock, flags);
849}
850
Thierry Reding3c03c462012-11-28 12:00:18 +0100851static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
852{
853 struct drm_device *drm = dc->base.dev;
854 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100855 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200856 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100857
Thierry Reding6b59cc12014-12-16 16:33:27 +0100858 spin_lock_irqsave(&drm->event_lock, flags);
859
860 if (!dc->event) {
861 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +0100862 return;
Thierry Reding6b59cc12014-12-16 16:33:27 +0100863 }
Thierry Reding3c03c462012-11-28 12:00:18 +0100864
Matt Roperf4510a22014-04-01 15:22:40 -0700865 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100866
Sean Paul93396d02014-11-19 13:04:49 -0500867 spin_lock_irqsave(&dc->lock, flags);
868
Thierry Reding3c03c462012-11-28 12:00:18 +0100869 /* check if new start address has been latched */
Sean Paul93396d02014-11-19 13:04:49 -0500870 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
Thierry Reding3c03c462012-11-28 12:00:18 +0100871 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
872 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
873 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
874
Sean Paul93396d02014-11-19 13:04:49 -0500875 spin_unlock_irqrestore(&dc->lock, flags);
876
Matt Roperf4510a22014-04-01 15:22:40 -0700877 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Redinged7dae52014-12-16 16:03:13 +0100878 drm_crtc_send_vblank_event(crtc, dc->event);
879 drm_crtc_vblank_put(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100880 dc->event = NULL;
Thierry Reding3c03c462012-11-28 12:00:18 +0100881 }
Thierry Reding6b59cc12014-12-16 16:33:27 +0100882
883 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +0100884}
885
886void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
887{
888 struct tegra_dc *dc = to_tegra_dc(crtc);
889 struct drm_device *drm = crtc->dev;
890 unsigned long flags;
891
892 spin_lock_irqsave(&drm->event_lock, flags);
893
894 if (dc->event && dc->event->base.file_priv == file) {
895 dc->event->base.destroy(&dc->event->base);
Thierry Redinged7dae52014-12-16 16:03:13 +0100896 drm_crtc_vblank_put(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100897 dc->event = NULL;
898 }
899
900 spin_unlock_irqrestore(&drm->event_lock, flags);
901}
902
903static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000904 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100905{
Thierry Redinged7dae52014-12-16 16:03:13 +0100906 unsigned int pipe = drm_crtc_index(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100907 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100908
909 if (dc->event)
910 return -EBUSY;
911
912 if (event) {
Thierry Redinged7dae52014-12-16 16:03:13 +0100913 event->pipe = pipe;
Thierry Reding3c03c462012-11-28 12:00:18 +0100914 dc->event = event;
Thierry Redinged7dae52014-12-16 16:03:13 +0100915 drm_crtc_vblank_get(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100916 }
917
918 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700919 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100920
921 return 0;
922}
923
Thierry Redingf002abc2013-10-14 14:06:02 +0200924static void tegra_dc_destroy(struct drm_crtc *crtc)
925{
926 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +0200927}
928
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000929static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding3c03c462012-11-28 12:00:18 +0100930 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000931 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +0200932 .destroy = tegra_dc_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000933};
934
Thierry Redingf34bc782012-11-04 21:47:13 +0100935static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000936{
Thierry Redingf002abc2013-10-14 14:06:02 +0200937 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100938 struct drm_device *drm = crtc->dev;
939 struct drm_plane *plane;
940
Daniel Vetter2b4c3662014-04-23 15:15:32 +0200941 drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
Thierry Redingf34bc782012-11-04 21:47:13 +0100942 if (plane->crtc == crtc) {
Thierry Redingc7679302014-10-21 13:51:53 +0200943 tegra_window_plane_disable(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100944 plane->crtc = NULL;
945
946 if (plane->fb) {
947 drm_framebuffer_unreference(plane->fb);
948 plane->fb = NULL;
949 }
950 }
951 }
Thierry Redingf002abc2013-10-14 14:06:02 +0200952
Thierry Reding8ff64c12014-10-08 14:48:51 +0200953 drm_crtc_vblank_off(crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200954 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000955}
956
957static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
958 const struct drm_display_mode *mode,
959 struct drm_display_mode *adjusted)
960{
961 return true;
962}
963
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000964static int tegra_dc_set_timings(struct tegra_dc *dc,
965 struct drm_display_mode *mode)
966{
Thierry Reding0444c0f2014-04-16 09:22:38 +0200967 unsigned int h_ref_to_sync = 1;
968 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000969 unsigned long value;
970
971 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
972
973 value = (v_ref_to_sync << 16) | h_ref_to_sync;
974 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
975
976 value = ((mode->vsync_end - mode->vsync_start) << 16) |
977 ((mode->hsync_end - mode->hsync_start) << 0);
978 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
979
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000980 value = ((mode->vtotal - mode->vsync_end) << 16) |
981 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +0000982 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
983
984 value = ((mode->vsync_start - mode->vdisplay) << 16) |
985 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000986 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
987
988 value = (mode->vdisplay << 16) | mode->hdisplay;
989 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
990
991 return 0;
992}
993
994static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100995 struct drm_display_mode *mode)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000996{
Thierry Reding91eded92014-03-26 13:32:21 +0100997 unsigned long pclk = mode->clock * 1000;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000998 struct tegra_dc *dc = to_tegra_dc(crtc);
999 struct tegra_output *output = NULL;
1000 struct drm_encoder *encoder;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001001 unsigned int div;
1002 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001003 long err;
1004
1005 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
1006 if (encoder->crtc == crtc) {
1007 output = encoder_to_output(encoder);
1008 break;
1009 }
1010
1011 if (!output)
1012 return -ENODEV;
1013
1014 /*
Thierry Reding91eded92014-03-26 13:32:21 +01001015 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
1016 * respectively, each of which divides the base pll_d by 2.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001017 */
Thierry Reding91eded92014-03-26 13:32:21 +01001018 err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001019 if (err < 0) {
1020 dev_err(dc->dev, "failed to setup clock: %ld\n", err);
1021 return err;
1022 }
1023
Thierry Reding91eded92014-03-26 13:32:21 +01001024 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001025
1026 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1027 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001028
1029 return 0;
1030}
1031
1032static int tegra_crtc_mode_set(struct drm_crtc *crtc,
1033 struct drm_display_mode *mode,
1034 struct drm_display_mode *adjusted,
1035 int x, int y, struct drm_framebuffer *old_fb)
1036{
Matt Roperf4510a22014-04-01 15:22:40 -07001037 struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001038 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001039 struct tegra_dc_window window;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001040 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001041 int err;
1042
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001043 err = tegra_crtc_setup_clk(crtc, mode);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001044 if (err) {
1045 dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
1046 return err;
1047 }
1048
1049 /* program display mode */
1050 tegra_dc_set_timings(dc, mode);
1051
Thierry Reding42d06592014-12-08 15:45:39 +01001052 if (dc->soc->supports_border_color)
1053 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1054
Thierry Reding8620fc62013-12-12 11:03:59 +01001055 /* interlacing isn't supported yet, so disable it */
1056 if (dc->soc->supports_interlacing) {
1057 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1058 value &= ~INTERLACE_ENABLE;
1059 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1060 }
1061
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001062 /* setup window parameters */
Thierry Redingf34bc782012-11-04 21:47:13 +01001063 memset(&window, 0, sizeof(window));
1064 window.src.x = 0;
1065 window.src.y = 0;
1066 window.src.w = mode->hdisplay;
1067 window.src.h = mode->vdisplay;
1068 window.dst.x = 0;
1069 window.dst.y = 0;
1070 window.dst.w = mode->hdisplay;
1071 window.dst.h = mode->vdisplay;
Thierry Redingf9253902014-01-29 20:31:17 +01001072 window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
1073 &window.swap);
Matt Roperf4510a22014-04-01 15:22:40 -07001074 window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
1075 window.stride[0] = crtc->primary->fb->pitches[0];
Arto Merilainende2ba662013-03-22 16:34:08 +02001076 window.base[0] = bo->paddr;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001077
Thierry Redingf34bc782012-11-04 21:47:13 +01001078 err = tegra_dc_setup_window(dc, 0, &window);
1079 if (err < 0)
1080 dev_err(dc->dev, "failed to enable root plane\n");
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001081
1082 return 0;
1083}
1084
Thierry Reding23fb4742012-11-28 11:38:24 +01001085static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1086 struct drm_framebuffer *old_fb)
1087{
1088 struct tegra_dc *dc = to_tegra_dc(crtc);
1089
Matt Roperf4510a22014-04-01 15:22:40 -07001090 return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
Thierry Reding23fb4742012-11-28 11:38:24 +01001091}
1092
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001093static void tegra_crtc_prepare(struct drm_crtc *crtc)
1094{
1095 struct tegra_dc *dc = to_tegra_dc(crtc);
1096 unsigned int syncpt;
1097 unsigned long value;
1098
Thierry Reding8ff64c12014-10-08 14:48:51 +02001099 drm_crtc_vblank_off(crtc);
1100
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001101 /* hardware initialization */
Stephen Warrenca480802013-11-06 16:20:54 -07001102 reset_control_deassert(dc->rst);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001103 usleep_range(10000, 20000);
1104
1105 if (dc->pipe)
1106 syncpt = SYNCPT_VBLANK1;
1107 else
1108 syncpt = SYNCPT_VBLANK0;
1109
1110 /* initialize display controller */
1111 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1112 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1113
1114 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1115 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1116
1117 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1118 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1119 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1120
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001121 /* initialize timer */
1122 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1123 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1124 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1125
1126 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1127 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1128 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1129
1130 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001131 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +01001132
1133 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1134 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001135}
1136
1137static void tegra_crtc_commit(struct drm_crtc *crtc)
1138{
1139 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001140
Thierry Reding8ff64c12014-10-08 14:48:51 +02001141 drm_crtc_vblank_on(crtc);
Thierry Reding205d48e2014-10-21 13:41:46 +02001142 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001143}
1144
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001145static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +01001146 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001147 .mode_fixup = tegra_crtc_mode_fixup,
1148 .mode_set = tegra_crtc_mode_set,
Thierry Reding23fb4742012-11-28 11:38:24 +01001149 .mode_set_base = tegra_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001150 .prepare = tegra_crtc_prepare,
1151 .commit = tegra_crtc_commit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001152};
1153
Thierry Reding6e5ff992012-11-28 11:45:47 +01001154static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001155{
1156 struct tegra_dc *dc = data;
1157 unsigned long status;
1158
1159 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1160 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1161
1162 if (status & FRAME_END_INT) {
1163 /*
1164 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1165 */
1166 }
1167
1168 if (status & VBLANK_INT) {
1169 /*
1170 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1171 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001172 drm_crtc_handle_vblank(&dc->base);
Thierry Reding3c03c462012-11-28 12:00:18 +01001173 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001174 }
1175
1176 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1177 /*
1178 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1179 */
1180 }
1181
1182 return IRQ_HANDLED;
1183}
1184
1185static int tegra_dc_show_regs(struct seq_file *s, void *data)
1186{
1187 struct drm_info_node *node = s->private;
1188 struct tegra_dc *dc = node->info_ent->data;
1189
1190#define DUMP_REG(name) \
Thierry Reding03a60562014-10-21 13:48:48 +02001191 seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001192 tegra_dc_readl(dc, name))
1193
1194 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1195 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1196 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1197 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1198 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1199 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1200 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1201 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1202 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1203 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1204 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1205 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1206 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1207 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1208 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1209 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1210 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1211 DUMP_REG(DC_CMD_INT_STATUS);
1212 DUMP_REG(DC_CMD_INT_MASK);
1213 DUMP_REG(DC_CMD_INT_ENABLE);
1214 DUMP_REG(DC_CMD_INT_TYPE);
1215 DUMP_REG(DC_CMD_INT_POLARITY);
1216 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1217 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1218 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1219 DUMP_REG(DC_CMD_STATE_ACCESS);
1220 DUMP_REG(DC_CMD_STATE_CONTROL);
1221 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1222 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1223 DUMP_REG(DC_COM_CRC_CONTROL);
1224 DUMP_REG(DC_COM_CRC_CHECKSUM);
1225 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1226 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1227 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1228 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1229 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1230 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1231 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1232 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1233 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1234 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1235 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1236 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1237 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1238 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1239 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1240 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1241 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1242 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1243 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1244 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1245 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1246 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1247 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1248 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1249 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1250 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1251 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1252 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1253 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1254 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1255 DUMP_REG(DC_COM_SPI_CONTROL);
1256 DUMP_REG(DC_COM_SPI_START_BYTE);
1257 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1258 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1259 DUMP_REG(DC_COM_HSPI_CS_DC);
1260 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1261 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1262 DUMP_REG(DC_COM_GPIO_CTRL);
1263 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1264 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1265 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1266 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1267 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1268 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1269 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1270 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1271 DUMP_REG(DC_DISP_REF_TO_SYNC);
1272 DUMP_REG(DC_DISP_SYNC_WIDTH);
1273 DUMP_REG(DC_DISP_BACK_PORCH);
1274 DUMP_REG(DC_DISP_ACTIVE);
1275 DUMP_REG(DC_DISP_FRONT_PORCH);
1276 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1277 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1278 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1279 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1280 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1281 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1282 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1283 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1284 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1285 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1286 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1287 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1288 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1289 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1290 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1291 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1292 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1293 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1294 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1295 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1296 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1297 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1298 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1299 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1300 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1301 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1302 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1303 DUMP_REG(DC_DISP_M0_CONTROL);
1304 DUMP_REG(DC_DISP_M1_CONTROL);
1305 DUMP_REG(DC_DISP_DI_CONTROL);
1306 DUMP_REG(DC_DISP_PP_CONTROL);
1307 DUMP_REG(DC_DISP_PP_SELECT_A);
1308 DUMP_REG(DC_DISP_PP_SELECT_B);
1309 DUMP_REG(DC_DISP_PP_SELECT_C);
1310 DUMP_REG(DC_DISP_PP_SELECT_D);
1311 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1312 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1313 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1314 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1315 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1316 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1317 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1318 DUMP_REG(DC_DISP_BORDER_COLOR);
1319 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1320 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1321 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1322 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1323 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1324 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1325 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1326 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1327 DUMP_REG(DC_DISP_CURSOR_POSITION);
1328 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1329 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1330 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1331 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1332 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1333 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1334 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1335 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1336 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1337 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1338 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1339 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1340 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1341 DUMP_REG(DC_DISP_SD_CONTROL);
1342 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1343 DUMP_REG(DC_DISP_SD_LUT(0));
1344 DUMP_REG(DC_DISP_SD_LUT(1));
1345 DUMP_REG(DC_DISP_SD_LUT(2));
1346 DUMP_REG(DC_DISP_SD_LUT(3));
1347 DUMP_REG(DC_DISP_SD_LUT(4));
1348 DUMP_REG(DC_DISP_SD_LUT(5));
1349 DUMP_REG(DC_DISP_SD_LUT(6));
1350 DUMP_REG(DC_DISP_SD_LUT(7));
1351 DUMP_REG(DC_DISP_SD_LUT(8));
1352 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1353 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1354 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1355 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1356 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1357 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1358 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1359 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1360 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1361 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1362 DUMP_REG(DC_DISP_SD_BL_TF(0));
1363 DUMP_REG(DC_DISP_SD_BL_TF(1));
1364 DUMP_REG(DC_DISP_SD_BL_TF(2));
1365 DUMP_REG(DC_DISP_SD_BL_TF(3));
1366 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1367 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1368 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001369 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1370 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001371 DUMP_REG(DC_WIN_WIN_OPTIONS);
1372 DUMP_REG(DC_WIN_BYTE_SWAP);
1373 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1374 DUMP_REG(DC_WIN_COLOR_DEPTH);
1375 DUMP_REG(DC_WIN_POSITION);
1376 DUMP_REG(DC_WIN_SIZE);
1377 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1378 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1379 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1380 DUMP_REG(DC_WIN_DDA_INC);
1381 DUMP_REG(DC_WIN_LINE_STRIDE);
1382 DUMP_REG(DC_WIN_BUF_STRIDE);
1383 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1384 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1385 DUMP_REG(DC_WIN_DV_CONTROL);
1386 DUMP_REG(DC_WIN_BLEND_NOKEY);
1387 DUMP_REG(DC_WIN_BLEND_1WIN);
1388 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1389 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001390 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001391 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1392 DUMP_REG(DC_WINBUF_START_ADDR);
1393 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1394 DUMP_REG(DC_WINBUF_START_ADDR_U);
1395 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1396 DUMP_REG(DC_WINBUF_START_ADDR_V);
1397 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1398 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1399 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1400 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1401 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1402 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1403 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1404 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1405 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1406
1407#undef DUMP_REG
1408
1409 return 0;
1410}
1411
1412static struct drm_info_list debugfs_files[] = {
1413 { "regs", tegra_dc_show_regs, 0, NULL },
1414};
1415
1416static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1417{
1418 unsigned int i;
1419 char *name;
1420 int err;
1421
1422 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1423 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1424 kfree(name);
1425
1426 if (!dc->debugfs)
1427 return -ENOMEM;
1428
1429 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1430 GFP_KERNEL);
1431 if (!dc->debugfs_files) {
1432 err = -ENOMEM;
1433 goto remove;
1434 }
1435
1436 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1437 dc->debugfs_files[i].data = dc;
1438
1439 err = drm_debugfs_create_files(dc->debugfs_files,
1440 ARRAY_SIZE(debugfs_files),
1441 dc->debugfs, minor);
1442 if (err < 0)
1443 goto free;
1444
1445 dc->minor = minor;
1446
1447 return 0;
1448
1449free:
1450 kfree(dc->debugfs_files);
1451 dc->debugfs_files = NULL;
1452remove:
1453 debugfs_remove(dc->debugfs);
1454 dc->debugfs = NULL;
1455
1456 return err;
1457}
1458
1459static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1460{
1461 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1462 dc->minor);
1463 dc->minor = NULL;
1464
1465 kfree(dc->debugfs_files);
1466 dc->debugfs_files = NULL;
1467
1468 debugfs_remove(dc->debugfs);
1469 dc->debugfs = NULL;
1470
1471 return 0;
1472}
1473
Thierry Reding53fa7f72013-09-24 15:35:40 +02001474static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001475{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001476 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001477 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001478 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001479 struct drm_plane *primary = NULL;
1480 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001481 int err;
1482
Thierry Redingdf06b752014-06-26 21:41:53 +02001483 if (tegra->domain) {
1484 err = iommu_attach_device(tegra->domain, dc->dev);
1485 if (err < 0) {
1486 dev_err(dc->dev, "failed to attach to domain: %d\n",
1487 err);
1488 return err;
1489 }
1490
1491 dc->domain = tegra->domain;
1492 }
1493
Thierry Redingc7679302014-10-21 13:51:53 +02001494 primary = tegra_dc_primary_plane_create(drm, dc);
1495 if (IS_ERR(primary)) {
1496 err = PTR_ERR(primary);
1497 goto cleanup;
1498 }
1499
1500 if (dc->soc->supports_cursor) {
1501 cursor = tegra_dc_cursor_plane_create(drm, dc);
1502 if (IS_ERR(cursor)) {
1503 err = PTR_ERR(cursor);
1504 goto cleanup;
1505 }
1506 }
1507
1508 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1509 &tegra_crtc_funcs);
1510 if (err < 0)
1511 goto cleanup;
1512
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001513 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1514 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1515
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001516 /*
1517 * Keep track of the minimum pitch alignment across all display
1518 * controllers.
1519 */
1520 if (dc->soc->pitch_align > tegra->pitch_align)
1521 tegra->pitch_align = dc->soc->pitch_align;
1522
Thierry Reding9910f5c2014-05-22 09:57:15 +02001523 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001524 if (err < 0 && err != -ENODEV) {
1525 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001526 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001527 }
1528
Thierry Reding9910f5c2014-05-22 09:57:15 +02001529 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001530 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001531 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001532
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001533 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001534 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001535 if (err < 0)
1536 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1537 }
1538
Thierry Reding6e5ff992012-11-28 11:45:47 +01001539 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001540 dev_name(dc->dev), dc);
1541 if (err < 0) {
1542 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1543 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001544 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001545 }
1546
1547 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001548
1549cleanup:
1550 if (cursor)
1551 drm_plane_cleanup(cursor);
1552
1553 if (primary)
1554 drm_plane_cleanup(primary);
1555
1556 if (tegra->domain) {
1557 iommu_detach_device(tegra->domain, dc->dev);
1558 dc->domain = NULL;
1559 }
1560
1561 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001562}
1563
Thierry Reding53fa7f72013-09-24 15:35:40 +02001564static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001565{
Thierry Reding776dc382013-10-14 14:43:22 +02001566 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001567 int err;
1568
1569 devm_free_irq(dc->dev, dc->irq, dc);
1570
1571 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1572 err = tegra_dc_debugfs_exit(dc);
1573 if (err < 0)
1574 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1575 }
1576
1577 err = tegra_dc_rgb_exit(dc);
1578 if (err) {
1579 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1580 return err;
1581 }
1582
Thierry Redingdf06b752014-06-26 21:41:53 +02001583 if (dc->domain) {
1584 iommu_detach_device(dc->domain, dc->dev);
1585 dc->domain = NULL;
1586 }
1587
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001588 return 0;
1589}
1590
1591static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001592 .init = tegra_dc_init,
1593 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001594};
1595
Thierry Reding8620fc62013-12-12 11:03:59 +01001596static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001597 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001598 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001599 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001600 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001601 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001602 .has_powergate = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001603};
1604
1605static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001606 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001607 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001608 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001609 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001610 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001611 .has_powergate = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001612};
1613
1614static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001615 .supports_border_color = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001616 .supports_interlacing = false,
1617 .supports_cursor = false,
1618 .supports_block_linear = false,
1619 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001620 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001621};
1622
1623static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001624 .supports_border_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001625 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001626 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001627 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001628 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001629 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001630};
1631
1632static const struct of_device_id tegra_dc_of_match[] = {
1633 {
1634 .compatible = "nvidia,tegra124-dc",
1635 .data = &tegra124_dc_soc_info,
1636 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001637 .compatible = "nvidia,tegra114-dc",
1638 .data = &tegra114_dc_soc_info,
1639 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001640 .compatible = "nvidia,tegra30-dc",
1641 .data = &tegra30_dc_soc_info,
1642 }, {
1643 .compatible = "nvidia,tegra20-dc",
1644 .data = &tegra20_dc_soc_info,
1645 }, {
1646 /* sentinel */
1647 }
1648};
Stephen Warrenef707282014-06-18 16:21:55 -06001649MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001650
Thierry Reding13411dd2014-01-09 17:08:36 +01001651static int tegra_dc_parse_dt(struct tegra_dc *dc)
1652{
1653 struct device_node *np;
1654 u32 value = 0;
1655 int err;
1656
1657 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1658 if (err < 0) {
1659 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1660
1661 /*
1662 * If the nvidia,head property isn't present, try to find the
1663 * correct head number by looking up the position of this
1664 * display controller's node within the device tree. Assuming
1665 * that the nodes are ordered properly in the DTS file and
1666 * that the translation into a flattened device tree blob
1667 * preserves that ordering this will actually yield the right
1668 * head number.
1669 *
1670 * If those assumptions don't hold, this will still work for
1671 * cases where only a single display controller is used.
1672 */
1673 for_each_matching_node(np, tegra_dc_of_match) {
1674 if (np == dc->dev->of_node)
1675 break;
1676
1677 value++;
1678 }
1679 }
1680
1681 dc->pipe = value;
1682
1683 return 0;
1684}
1685
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001686static int tegra_dc_probe(struct platform_device *pdev)
1687{
Thierry Reding8620fc62013-12-12 11:03:59 +01001688 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001689 struct resource *regs;
1690 struct tegra_dc *dc;
1691 int err;
1692
1693 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1694 if (!dc)
1695 return -ENOMEM;
1696
Thierry Reding8620fc62013-12-12 11:03:59 +01001697 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1698 if (!id)
1699 return -ENODEV;
1700
Thierry Reding6e5ff992012-11-28 11:45:47 +01001701 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001702 INIT_LIST_HEAD(&dc->list);
1703 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001704 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001705
Thierry Reding13411dd2014-01-09 17:08:36 +01001706 err = tegra_dc_parse_dt(dc);
1707 if (err < 0)
1708 return err;
1709
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001710 dc->clk = devm_clk_get(&pdev->dev, NULL);
1711 if (IS_ERR(dc->clk)) {
1712 dev_err(&pdev->dev, "failed to get clock\n");
1713 return PTR_ERR(dc->clk);
1714 }
1715
Stephen Warrenca480802013-11-06 16:20:54 -07001716 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1717 if (IS_ERR(dc->rst)) {
1718 dev_err(&pdev->dev, "failed to get reset\n");
1719 return PTR_ERR(dc->rst);
1720 }
1721
Thierry Reding9c012702014-07-07 15:32:53 +02001722 if (dc->soc->has_powergate) {
1723 if (dc->pipe == 0)
1724 dc->powergate = TEGRA_POWERGATE_DIS;
1725 else
1726 dc->powergate = TEGRA_POWERGATE_DISB;
1727
1728 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
1729 dc->rst);
1730 if (err < 0) {
1731 dev_err(&pdev->dev, "failed to power partition: %d\n",
1732 err);
1733 return err;
1734 }
1735 } else {
1736 err = clk_prepare_enable(dc->clk);
1737 if (err < 0) {
1738 dev_err(&pdev->dev, "failed to enable clock: %d\n",
1739 err);
1740 return err;
1741 }
1742
1743 err = reset_control_deassert(dc->rst);
1744 if (err < 0) {
1745 dev_err(&pdev->dev, "failed to deassert reset: %d\n",
1746 err);
1747 return err;
1748 }
1749 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001750
1751 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001752 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1753 if (IS_ERR(dc->regs))
1754 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001755
1756 dc->irq = platform_get_irq(pdev, 0);
1757 if (dc->irq < 0) {
1758 dev_err(&pdev->dev, "failed to get IRQ\n");
1759 return -ENXIO;
1760 }
1761
Thierry Reding776dc382013-10-14 14:43:22 +02001762 INIT_LIST_HEAD(&dc->client.list);
1763 dc->client.ops = &dc_client_ops;
1764 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001765
1766 err = tegra_dc_rgb_probe(dc);
1767 if (err < 0 && err != -ENODEV) {
1768 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1769 return err;
1770 }
1771
Thierry Reding776dc382013-10-14 14:43:22 +02001772 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001773 if (err < 0) {
1774 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1775 err);
1776 return err;
1777 }
1778
1779 platform_set_drvdata(pdev, dc);
1780
1781 return 0;
1782}
1783
1784static int tegra_dc_remove(struct platform_device *pdev)
1785{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001786 struct tegra_dc *dc = platform_get_drvdata(pdev);
1787 int err;
1788
Thierry Reding776dc382013-10-14 14:43:22 +02001789 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001790 if (err < 0) {
1791 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1792 err);
1793 return err;
1794 }
1795
Thierry Reding59d29c02013-10-14 14:26:42 +02001796 err = tegra_dc_rgb_remove(dc);
1797 if (err < 0) {
1798 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1799 return err;
1800 }
1801
Thierry Reding5482d752014-07-11 08:39:03 +02001802 reset_control_assert(dc->rst);
Thierry Reding9c012702014-07-07 15:32:53 +02001803
1804 if (dc->soc->has_powergate)
1805 tegra_powergate_power_off(dc->powergate);
1806
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001807 clk_disable_unprepare(dc->clk);
1808
1809 return 0;
1810}
1811
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001812struct platform_driver tegra_dc_driver = {
1813 .driver = {
1814 .name = "tegra-dc",
1815 .owner = THIS_MODULE,
1816 .of_match_table = tegra_dc_of_match,
1817 },
1818 .probe = tegra_dc_probe,
1819 .remove = tegra_dc_remove,
1820};