blob: 7ef16a2409b11a55f78b4d9e454cf3a1065c5b02 [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
Thierry Reding8620fc62013-12-12 11:03:59 +010021struct tegra_dc_soc_info {
22 bool supports_interlacing;
Thierry Redinge6876512013-12-20 13:58:33 +010023 bool supports_cursor;
Thierry Redingc134f012014-06-03 14:48:12 +020024 bool supports_block_linear;
Thierry Redingd1f3e1e2014-07-11 08:29:14 +020025 unsigned int pitch_align;
Thierry Reding9c012702014-07-07 15:32:53 +020026 bool has_powergate;
Thierry Reding8620fc62013-12-12 11:03:59 +010027};
28
Thierry Redingf34bc782012-11-04 21:47:13 +010029struct tegra_plane {
30 struct drm_plane base;
31 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000032};
33
Thierry Redingf34bc782012-11-04 21:47:13 +010034static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
35{
36 return container_of(plane, struct tegra_plane, base);
37}
38
Thierry Reding205d48e2014-10-21 13:41:46 +020039static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
40{
41 u32 value = WIN_A_ACT_REQ << index;
42
43 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
44 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
45}
46
47static void tegra_dc_cursor_commit(struct tegra_dc *dc)
48{
49 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
50 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
51}
52
53static void tegra_dc_commit(struct tegra_dc *dc)
54{
55 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
56 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
57}
58
Thierry Reding10288ee2014-03-14 09:54:58 +010059static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
60{
61 /* assume no swapping of fetched data */
62 if (swap)
63 *swap = BYTE_SWAP_NOSWAP;
64
65 switch (format) {
66 case DRM_FORMAT_XBGR8888:
67 return WIN_COLOR_DEPTH_R8G8B8A8;
68
69 case DRM_FORMAT_XRGB8888:
70 return WIN_COLOR_DEPTH_B8G8R8A8;
71
72 case DRM_FORMAT_RGB565:
73 return WIN_COLOR_DEPTH_B5G6R5;
74
75 case DRM_FORMAT_UYVY:
76 return WIN_COLOR_DEPTH_YCbCr422;
77
78 case DRM_FORMAT_YUYV:
79 if (swap)
80 *swap = BYTE_SWAP_SWAP2;
81
82 return WIN_COLOR_DEPTH_YCbCr422;
83
84 case DRM_FORMAT_YUV420:
85 return WIN_COLOR_DEPTH_YCbCr420P;
86
87 case DRM_FORMAT_YUV422:
88 return WIN_COLOR_DEPTH_YCbCr422P;
89
90 default:
91 break;
92 }
93
94 WARN(1, "unsupported pixel format %u, using default\n", format);
95 return WIN_COLOR_DEPTH_B8G8R8A8;
96}
97
98static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
99{
100 switch (format) {
101 case WIN_COLOR_DEPTH_YCbCr422:
102 case WIN_COLOR_DEPTH_YUV422:
103 if (planar)
104 *planar = false;
105
106 return true;
107
108 case WIN_COLOR_DEPTH_YCbCr420P:
109 case WIN_COLOR_DEPTH_YUV420P:
110 case WIN_COLOR_DEPTH_YCbCr422P:
111 case WIN_COLOR_DEPTH_YUV422P:
112 case WIN_COLOR_DEPTH_YCbCr422R:
113 case WIN_COLOR_DEPTH_YUV422R:
114 case WIN_COLOR_DEPTH_YCbCr422RA:
115 case WIN_COLOR_DEPTH_YUV422RA:
116 if (planar)
117 *planar = true;
118
119 return true;
120 }
121
122 return false;
123}
124
125static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
126 unsigned int bpp)
127{
128 fixed20_12 outf = dfixed_init(out);
129 fixed20_12 inf = dfixed_init(in);
130 u32 dda_inc;
131 int max;
132
133 if (v)
134 max = 15;
135 else {
136 switch (bpp) {
137 case 2:
138 max = 8;
139 break;
140
141 default:
142 WARN_ON_ONCE(1);
143 /* fallthrough */
144 case 4:
145 max = 4;
146 break;
147 }
148 }
149
150 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
151 inf.full -= dfixed_const(1);
152
153 dda_inc = dfixed_div(inf, outf);
154 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
155
156 return dda_inc;
157}
158
159static inline u32 compute_initial_dda(unsigned int in)
160{
161 fixed20_12 inf = dfixed_init(in);
162 return dfixed_frac(inf);
163}
164
165static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
166 const struct tegra_dc_window *window)
167{
168 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
169 unsigned long value;
170 bool yuv, planar;
171
172 /*
173 * For YUV planar modes, the number of bytes per pixel takes into
174 * account only the luma component and therefore is 1.
175 */
176 yuv = tegra_dc_format_is_yuv(window->format, &planar);
177 if (!yuv)
178 bpp = window->bits_per_pixel / 8;
179 else
180 bpp = planar ? 1 : 2;
181
182 value = WINDOW_A_SELECT << index;
183 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
184
185 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
186 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
187
188 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
189 tegra_dc_writel(dc, value, DC_WIN_POSITION);
190
191 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
192 tegra_dc_writel(dc, value, DC_WIN_SIZE);
193
194 h_offset = window->src.x * bpp;
195 v_offset = window->src.y;
196 h_size = window->src.w * bpp;
197 v_size = window->src.h;
198
199 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
200 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
201
202 /*
203 * For DDA computations the number of bytes per pixel for YUV planar
204 * modes needs to take into account all Y, U and V components.
205 */
206 if (yuv && planar)
207 bpp = 2;
208
209 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
210 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
211
212 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
213 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
214
215 h_dda = compute_initial_dda(window->src.x);
216 v_dda = compute_initial_dda(window->src.y);
217
218 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
219 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
220
221 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
222 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
223
224 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
225
226 if (yuv && planar) {
227 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
228 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
229 value = window->stride[1] << 16 | window->stride[0];
230 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
231 } else {
232 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
233 }
234
235 if (window->bottom_up)
236 v_offset += window->src.h - 1;
237
238 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
239 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
240
Thierry Redingc134f012014-06-03 14:48:12 +0200241 if (dc->soc->supports_block_linear) {
242 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100243
Thierry Redingc134f012014-06-03 14:48:12 +0200244 switch (window->tiling.mode) {
245 case TEGRA_BO_TILING_MODE_PITCH:
246 value = DC_WINBUF_SURFACE_KIND_PITCH;
247 break;
248
249 case TEGRA_BO_TILING_MODE_TILED:
250 value = DC_WINBUF_SURFACE_KIND_TILED;
251 break;
252
253 case TEGRA_BO_TILING_MODE_BLOCK:
254 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
255 DC_WINBUF_SURFACE_KIND_BLOCK;
256 break;
257 }
258
259 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
260 } else {
261 switch (window->tiling.mode) {
262 case TEGRA_BO_TILING_MODE_PITCH:
263 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
264 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
265 break;
266
267 case TEGRA_BO_TILING_MODE_TILED:
268 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
269 DC_WIN_BUFFER_ADDR_MODE_TILE;
270 break;
271
272 case TEGRA_BO_TILING_MODE_BLOCK:
273 DRM_ERROR("hardware doesn't support block linear mode\n");
274 return -EINVAL;
275 }
276
277 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
278 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100279
280 value = WIN_ENABLE;
281
282 if (yuv) {
283 /* setup default colorspace conversion coefficients */
284 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
285 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
286 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
287 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
288 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
289 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
290 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
291 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
292
293 value |= CSC_ENABLE;
294 } else if (window->bits_per_pixel < 24) {
295 value |= COLOR_EXPAND;
296 }
297
298 if (window->bottom_up)
299 value |= V_DIRECTION;
300
301 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
302
303 /*
304 * Disable blending and assume Window A is the bottom-most window,
305 * Window C is the top-most window and Window B is in the middle.
306 */
307 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
308 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
309
310 switch (index) {
311 case 0:
312 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
313 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
314 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
315 break;
316
317 case 1:
318 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
319 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
320 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
321 break;
322
323 case 2:
324 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
325 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
326 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
327 break;
328 }
329
Thierry Reding205d48e2014-10-21 13:41:46 +0200330 tegra_dc_window_commit(dc, index);
Thierry Reding10288ee2014-03-14 09:54:58 +0100331
332 return 0;
333}
334
Thierry Redingc7679302014-10-21 13:51:53 +0200335static int tegra_window_plane_disable(struct drm_plane *plane)
336{
337 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
338 struct tegra_plane *p = to_tegra_plane(plane);
339 u32 value;
340
341 if (!plane->crtc)
342 return 0;
343
344 value = WINDOW_A_SELECT << p->index;
345 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
346
347 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
348 value &= ~WIN_ENABLE;
349 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
350
351 tegra_dc_window_commit(dc, p->index);
352
353 return 0;
354}
355
356static void tegra_plane_destroy(struct drm_plane *plane)
357{
358 struct tegra_plane *p = to_tegra_plane(plane);
359
360 drm_plane_cleanup(plane);
361 kfree(p);
362}
363
364static const u32 tegra_primary_plane_formats[] = {
365 DRM_FORMAT_XBGR8888,
366 DRM_FORMAT_XRGB8888,
367 DRM_FORMAT_RGB565,
368};
369
370static int tegra_primary_plane_update(struct drm_plane *plane,
371 struct drm_crtc *crtc,
372 struct drm_framebuffer *fb, int crtc_x,
373 int crtc_y, unsigned int crtc_w,
374 unsigned int crtc_h, uint32_t src_x,
375 uint32_t src_y, uint32_t src_w,
376 uint32_t src_h)
377{
378 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
379 struct tegra_plane *p = to_tegra_plane(plane);
380 struct tegra_dc *dc = to_tegra_dc(crtc);
381 struct tegra_dc_window window;
382 int err;
383
384 memset(&window, 0, sizeof(window));
385 window.src.x = src_x >> 16;
386 window.src.y = src_y >> 16;
387 window.src.w = src_w >> 16;
388 window.src.h = src_h >> 16;
389 window.dst.x = crtc_x;
390 window.dst.y = crtc_y;
391 window.dst.w = crtc_w;
392 window.dst.h = crtc_h;
393 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
394 window.bits_per_pixel = fb->bits_per_pixel;
395 window.bottom_up = tegra_fb_is_bottom_up(fb);
396
397 err = tegra_fb_get_tiling(fb, &window.tiling);
398 if (err < 0)
399 return err;
400
401 window.base[0] = bo->paddr + fb->offsets[0];
402 window.stride[0] = fb->pitches[0];
403
404 err = tegra_dc_setup_window(dc, p->index, &window);
405 if (err < 0)
406 return err;
407
408 return 0;
409}
410
411static void tegra_primary_plane_destroy(struct drm_plane *plane)
412{
413 tegra_window_plane_disable(plane);
414 tegra_plane_destroy(plane);
415}
416
417static const struct drm_plane_funcs tegra_primary_plane_funcs = {
418 .update_plane = tegra_primary_plane_update,
419 .disable_plane = tegra_window_plane_disable,
420 .destroy = tegra_primary_plane_destroy,
421};
422
423static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
424 struct tegra_dc *dc)
425{
426 struct tegra_plane *plane;
427 unsigned int num_formats;
428 const u32 *formats;
429 int err;
430
431 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
432 if (!plane)
433 return ERR_PTR(-ENOMEM);
434
435 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
436 formats = tegra_primary_plane_formats;
437
438 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
439 &tegra_primary_plane_funcs, formats,
440 num_formats, DRM_PLANE_TYPE_PRIMARY);
441 if (err < 0) {
442 kfree(plane);
443 return ERR_PTR(err);
444 }
445
446 return &plane->base;
447}
448
449static const u32 tegra_cursor_plane_formats[] = {
450 DRM_FORMAT_RGBA8888,
451};
452
453static int tegra_cursor_plane_update(struct drm_plane *plane,
454 struct drm_crtc *crtc,
455 struct drm_framebuffer *fb, int crtc_x,
456 int crtc_y, unsigned int crtc_w,
457 unsigned int crtc_h, uint32_t src_x,
458 uint32_t src_y, uint32_t src_w,
459 uint32_t src_h)
460{
461 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
462 struct tegra_dc *dc = to_tegra_dc(crtc);
463 u32 value = CURSOR_CLIP_DISPLAY;
464
465 /* scaling not supported for cursor */
466 if ((src_w >> 16 != crtc_w) || (src_h >> 16 != crtc_h))
467 return -EINVAL;
468
469 /* only square cursors supported */
470 if (src_w != src_h)
471 return -EINVAL;
472
473 switch (crtc_w) {
474 case 32:
475 value |= CURSOR_SIZE_32x32;
476 break;
477
478 case 64:
479 value |= CURSOR_SIZE_64x64;
480 break;
481
482 case 128:
483 value |= CURSOR_SIZE_128x128;
484 break;
485
486 case 256:
487 value |= CURSOR_SIZE_256x256;
488 break;
489
490 default:
491 return -EINVAL;
492 }
493
494 value |= (bo->paddr >> 10) & 0x3fffff;
495 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
496
497#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
498 value = (bo->paddr >> 32) & 0x3;
499 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
500#endif
501
502 /* enable cursor and set blend mode */
503 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
504 value |= CURSOR_ENABLE;
505 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
506
507 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
508 value &= ~CURSOR_DST_BLEND_MASK;
509 value &= ~CURSOR_SRC_BLEND_MASK;
510 value |= CURSOR_MODE_NORMAL;
511 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
512 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
513 value |= CURSOR_ALPHA;
514 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
515
516 /* position the cursor */
517 value = (crtc_y & 0x3fff) << 16 | (crtc_x & 0x3fff);
518 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
519
520 /* apply changes */
521 tegra_dc_cursor_commit(dc);
522 tegra_dc_commit(dc);
523
524 return 0;
525}
526
527static int tegra_cursor_plane_disable(struct drm_plane *plane)
528{
529 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
530 u32 value;
531
532 if (!plane->crtc)
533 return 0;
534
535 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
536 value &= ~CURSOR_ENABLE;
537 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
538
539 tegra_dc_cursor_commit(dc);
540 tegra_dc_commit(dc);
541
542 return 0;
543}
544
545static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
546 .update_plane = tegra_cursor_plane_update,
547 .disable_plane = tegra_cursor_plane_disable,
548 .destroy = tegra_plane_destroy,
549};
550
551static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
552 struct tegra_dc *dc)
553{
554 struct tegra_plane *plane;
555 unsigned int num_formats;
556 const u32 *formats;
557 int err;
558
559 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
560 if (!plane)
561 return ERR_PTR(-ENOMEM);
562
563 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
564 formats = tegra_cursor_plane_formats;
565
566 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
567 &tegra_cursor_plane_funcs, formats,
568 num_formats, DRM_PLANE_TYPE_CURSOR);
569 if (err < 0) {
570 kfree(plane);
571 return ERR_PTR(err);
572 }
573
574 return &plane->base;
575}
576
577static int tegra_overlay_plane_update(struct drm_plane *plane,
578 struct drm_crtc *crtc,
579 struct drm_framebuffer *fb, int crtc_x,
580 int crtc_y, unsigned int crtc_w,
581 unsigned int crtc_h, uint32_t src_x,
582 uint32_t src_y, uint32_t src_w,
583 uint32_t src_h)
Thierry Redingf34bc782012-11-04 21:47:13 +0100584{
585 struct tegra_plane *p = to_tegra_plane(plane);
586 struct tegra_dc *dc = to_tegra_dc(crtc);
587 struct tegra_dc_window window;
588 unsigned int i;
Thierry Redingc134f012014-06-03 14:48:12 +0200589 int err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100590
591 memset(&window, 0, sizeof(window));
592 window.src.x = src_x >> 16;
593 window.src.y = src_y >> 16;
594 window.src.w = src_w >> 16;
595 window.src.h = src_h >> 16;
596 window.dst.x = crtc_x;
597 window.dst.y = crtc_y;
598 window.dst.w = crtc_w;
599 window.dst.h = crtc_h;
Thierry Redingf9253902014-01-29 20:31:17 +0100600 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
Thierry Redingf34bc782012-11-04 21:47:13 +0100601 window.bits_per_pixel = fb->bits_per_pixel;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200602 window.bottom_up = tegra_fb_is_bottom_up(fb);
Thierry Redingc134f012014-06-03 14:48:12 +0200603
604 err = tegra_fb_get_tiling(fb, &window.tiling);
605 if (err < 0)
606 return err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100607
608 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
Arto Merilainende2ba662013-03-22 16:34:08 +0200609 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingf34bc782012-11-04 21:47:13 +0100610
Arto Merilainende2ba662013-03-22 16:34:08 +0200611 window.base[i] = bo->paddr + fb->offsets[i];
Thierry Redingf34bc782012-11-04 21:47:13 +0100612
613 /*
614 * Tegra doesn't support different strides for U and V planes
615 * so we display a warning if the user tries to display a
616 * framebuffer with such a configuration.
617 */
618 if (i >= 2) {
619 if (fb->pitches[i] != window.stride[1])
620 DRM_ERROR("unsupported UV-plane configuration\n");
621 } else {
622 window.stride[i] = fb->pitches[i];
623 }
624 }
625
626 return tegra_dc_setup_window(dc, p->index, &window);
627}
628
Thierry Redingc7679302014-10-21 13:51:53 +0200629static void tegra_overlay_plane_destroy(struct drm_plane *plane)
Thierry Redingf34bc782012-11-04 21:47:13 +0100630{
Thierry Redingc7679302014-10-21 13:51:53 +0200631 tegra_window_plane_disable(plane);
632 tegra_plane_destroy(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100633}
634
Thierry Redingc7679302014-10-21 13:51:53 +0200635static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
636 .update_plane = tegra_overlay_plane_update,
637 .disable_plane = tegra_window_plane_disable,
638 .destroy = tegra_overlay_plane_destroy,
Thierry Redingf34bc782012-11-04 21:47:13 +0100639};
640
Thierry Redingc7679302014-10-21 13:51:53 +0200641static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100642 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100643 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100644 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100645 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100646 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100647 DRM_FORMAT_YUV420,
648 DRM_FORMAT_YUV422,
649};
650
Thierry Redingc7679302014-10-21 13:51:53 +0200651static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
652 struct tegra_dc *dc,
653 unsigned int index)
654{
655 struct tegra_plane *plane;
656 unsigned int num_formats;
657 const u32 *formats;
658 int err;
659
660 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
661 if (!plane)
662 return ERR_PTR(-ENOMEM);
663
664 plane->index = index;
665
666 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
667 formats = tegra_overlay_plane_formats;
668
669 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
670 &tegra_overlay_plane_funcs, formats,
671 num_formats, DRM_PLANE_TYPE_OVERLAY);
672 if (err < 0) {
673 kfree(plane);
674 return ERR_PTR(err);
675 }
676
677 return &plane->base;
678}
679
Thierry Redingf34bc782012-11-04 21:47:13 +0100680static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
681{
Thierry Redingc7679302014-10-21 13:51:53 +0200682 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100683 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100684
685 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200686 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
687 if (IS_ERR(plane))
688 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100689 }
690
691 return 0;
692}
693
Thierry Reding23fb4742012-11-28 11:38:24 +0100694static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
695 struct drm_framebuffer *fb)
696{
Arto Merilainende2ba662013-03-22 16:34:08 +0200697 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200698 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200699 struct tegra_bo_tiling tiling;
Thierry Redingf9253902014-01-29 20:31:17 +0100700 unsigned int format, swap;
Thierry Reding23fb4742012-11-28 11:38:24 +0100701 unsigned long value;
Thierry Redingc134f012014-06-03 14:48:12 +0200702 int err;
703
704 err = tegra_fb_get_tiling(fb, &tiling);
705 if (err < 0)
706 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100707
708 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
709
710 value = fb->offsets[0] + y * fb->pitches[0] +
711 x * fb->bits_per_pixel / 8;
712
Arto Merilainende2ba662013-03-22 16:34:08 +0200713 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100714 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100715
716 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200717 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100718 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100719
Thierry Redingc134f012014-06-03 14:48:12 +0200720 if (dc->soc->supports_block_linear) {
721 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200722
Thierry Redingc134f012014-06-03 14:48:12 +0200723 switch (tiling.mode) {
724 case TEGRA_BO_TILING_MODE_PITCH:
725 value = DC_WINBUF_SURFACE_KIND_PITCH;
726 break;
727
728 case TEGRA_BO_TILING_MODE_TILED:
729 value = DC_WINBUF_SURFACE_KIND_TILED;
730 break;
731
732 case TEGRA_BO_TILING_MODE_BLOCK:
733 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
734 DC_WINBUF_SURFACE_KIND_BLOCK;
735 break;
736 }
737
738 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
739 } else {
740 switch (tiling.mode) {
741 case TEGRA_BO_TILING_MODE_PITCH:
742 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
743 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
744 break;
745
746 case TEGRA_BO_TILING_MODE_TILED:
747 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
748 DC_WIN_BUFFER_ADDR_MODE_TILE;
749 break;
750
751 case TEGRA_BO_TILING_MODE_BLOCK:
752 DRM_ERROR("hardware doesn't support block linear mode\n");
753 return -EINVAL;
754 }
755
756 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
757 }
Thierry Reding773af772013-10-04 22:34:01 +0200758
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200759 /* make sure bottom-up buffers are properly displayed */
760 if (tegra_fb_is_bottom_up(fb)) {
761 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100762 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200763 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
764
765 v_offset += fb->height - 1;
766 } else {
767 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100768 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200769 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
770 }
771
772 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
773 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
774
Thierry Reding23fb4742012-11-28 11:38:24 +0100775 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding205d48e2014-10-21 13:41:46 +0200776 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
Thierry Reding23fb4742012-11-28 11:38:24 +0100777 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
778
779 return 0;
780}
781
Thierry Reding6e5ff992012-11-28 11:45:47 +0100782void tegra_dc_enable_vblank(struct tegra_dc *dc)
783{
784 unsigned long value, flags;
785
786 spin_lock_irqsave(&dc->lock, flags);
787
788 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
789 value |= VBLANK_INT;
790 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
791
792 spin_unlock_irqrestore(&dc->lock, flags);
793}
794
795void tegra_dc_disable_vblank(struct tegra_dc *dc)
796{
797 unsigned long value, flags;
798
799 spin_lock_irqsave(&dc->lock, flags);
800
801 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
802 value &= ~VBLANK_INT;
803 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
804
805 spin_unlock_irqrestore(&dc->lock, flags);
806}
807
Thierry Reding3c03c462012-11-28 12:00:18 +0100808static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
809{
810 struct drm_device *drm = dc->base.dev;
811 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100812 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200813 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100814
815 if (!dc->event)
816 return;
817
Matt Roperf4510a22014-04-01 15:22:40 -0700818 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100819
820 /* check if new start address has been latched */
821 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
822 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
823 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
824
Matt Roperf4510a22014-04-01 15:22:40 -0700825 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Reding3c03c462012-11-28 12:00:18 +0100826 spin_lock_irqsave(&drm->event_lock, flags);
827 drm_send_vblank_event(drm, dc->pipe, dc->event);
828 drm_vblank_put(drm, dc->pipe);
829 dc->event = NULL;
830 spin_unlock_irqrestore(&drm->event_lock, flags);
831 }
832}
833
834void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
835{
836 struct tegra_dc *dc = to_tegra_dc(crtc);
837 struct drm_device *drm = crtc->dev;
838 unsigned long flags;
839
840 spin_lock_irqsave(&drm->event_lock, flags);
841
842 if (dc->event && dc->event->base.file_priv == file) {
843 dc->event->base.destroy(&dc->event->base);
844 drm_vblank_put(drm, dc->pipe);
845 dc->event = NULL;
846 }
847
848 spin_unlock_irqrestore(&drm->event_lock, flags);
849}
850
851static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000852 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100853{
854 struct tegra_dc *dc = to_tegra_dc(crtc);
855 struct drm_device *drm = crtc->dev;
856
857 if (dc->event)
858 return -EBUSY;
859
860 if (event) {
861 event->pipe = dc->pipe;
862 dc->event = event;
863 drm_vblank_get(drm, dc->pipe);
864 }
865
866 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700867 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100868
869 return 0;
870}
871
Thierry Redingf002abc2013-10-14 14:06:02 +0200872static void drm_crtc_clear(struct drm_crtc *crtc)
873{
874 memset(crtc, 0, sizeof(*crtc));
875}
876
877static void tegra_dc_destroy(struct drm_crtc *crtc)
878{
879 drm_crtc_cleanup(crtc);
880 drm_crtc_clear(crtc);
881}
882
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000883static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding3c03c462012-11-28 12:00:18 +0100884 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000885 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +0200886 .destroy = tegra_dc_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000887};
888
Thierry Redingf34bc782012-11-04 21:47:13 +0100889static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000890{
Thierry Redingf002abc2013-10-14 14:06:02 +0200891 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100892 struct drm_device *drm = crtc->dev;
893 struct drm_plane *plane;
894
Daniel Vetter2b4c3662014-04-23 15:15:32 +0200895 drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
Thierry Redingf34bc782012-11-04 21:47:13 +0100896 if (plane->crtc == crtc) {
Thierry Redingc7679302014-10-21 13:51:53 +0200897 tegra_window_plane_disable(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100898 plane->crtc = NULL;
899
900 if (plane->fb) {
901 drm_framebuffer_unreference(plane->fb);
902 plane->fb = NULL;
903 }
904 }
905 }
Thierry Redingf002abc2013-10-14 14:06:02 +0200906
907 drm_vblank_off(drm, dc->pipe);
Thierry Redingc7679302014-10-21 13:51:53 +0200908 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000909}
910
911static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
912 const struct drm_display_mode *mode,
913 struct drm_display_mode *adjusted)
914{
915 return true;
916}
917
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000918static int tegra_dc_set_timings(struct tegra_dc *dc,
919 struct drm_display_mode *mode)
920{
Thierry Reding0444c0f2014-04-16 09:22:38 +0200921 unsigned int h_ref_to_sync = 1;
922 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000923 unsigned long value;
924
925 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
926
927 value = (v_ref_to_sync << 16) | h_ref_to_sync;
928 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
929
930 value = ((mode->vsync_end - mode->vsync_start) << 16) |
931 ((mode->hsync_end - mode->hsync_start) << 0);
932 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
933
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000934 value = ((mode->vtotal - mode->vsync_end) << 16) |
935 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +0000936 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
937
938 value = ((mode->vsync_start - mode->vdisplay) << 16) |
939 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000940 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
941
942 value = (mode->vdisplay << 16) | mode->hdisplay;
943 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
944
945 return 0;
946}
947
948static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100949 struct drm_display_mode *mode)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000950{
Thierry Reding91eded92014-03-26 13:32:21 +0100951 unsigned long pclk = mode->clock * 1000;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000952 struct tegra_dc *dc = to_tegra_dc(crtc);
953 struct tegra_output *output = NULL;
954 struct drm_encoder *encoder;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100955 unsigned int div;
956 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000957 long err;
958
959 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
960 if (encoder->crtc == crtc) {
961 output = encoder_to_output(encoder);
962 break;
963 }
964
965 if (!output)
966 return -ENODEV;
967
968 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100969 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
970 * respectively, each of which divides the base pll_d by 2.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000971 */
Thierry Reding91eded92014-03-26 13:32:21 +0100972 err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000973 if (err < 0) {
974 dev_err(dc->dev, "failed to setup clock: %ld\n", err);
975 return err;
976 }
977
Thierry Reding91eded92014-03-26 13:32:21 +0100978 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100979
980 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
981 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000982
983 return 0;
984}
985
986static int tegra_crtc_mode_set(struct drm_crtc *crtc,
987 struct drm_display_mode *mode,
988 struct drm_display_mode *adjusted,
989 int x, int y, struct drm_framebuffer *old_fb)
990{
Matt Roperf4510a22014-04-01 15:22:40 -0700991 struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000992 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100993 struct tegra_dc_window window;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100994 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000995 int err;
996
Thierry Reding6e5ff992012-11-28 11:45:47 +0100997 drm_vblank_pre_modeset(crtc->dev, dc->pipe);
998
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100999 err = tegra_crtc_setup_clk(crtc, mode);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001000 if (err) {
1001 dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
1002 return err;
1003 }
1004
1005 /* program display mode */
1006 tegra_dc_set_timings(dc, mode);
1007
Thierry Reding8620fc62013-12-12 11:03:59 +01001008 /* interlacing isn't supported yet, so disable it */
1009 if (dc->soc->supports_interlacing) {
1010 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1011 value &= ~INTERLACE_ENABLE;
1012 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1013 }
1014
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001015 /* setup window parameters */
Thierry Redingf34bc782012-11-04 21:47:13 +01001016 memset(&window, 0, sizeof(window));
1017 window.src.x = 0;
1018 window.src.y = 0;
1019 window.src.w = mode->hdisplay;
1020 window.src.h = mode->vdisplay;
1021 window.dst.x = 0;
1022 window.dst.y = 0;
1023 window.dst.w = mode->hdisplay;
1024 window.dst.h = mode->vdisplay;
Thierry Redingf9253902014-01-29 20:31:17 +01001025 window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
1026 &window.swap);
Matt Roperf4510a22014-04-01 15:22:40 -07001027 window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
1028 window.stride[0] = crtc->primary->fb->pitches[0];
Arto Merilainende2ba662013-03-22 16:34:08 +02001029 window.base[0] = bo->paddr;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001030
Thierry Redingf34bc782012-11-04 21:47:13 +01001031 err = tegra_dc_setup_window(dc, 0, &window);
1032 if (err < 0)
1033 dev_err(dc->dev, "failed to enable root plane\n");
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001034
1035 return 0;
1036}
1037
Thierry Reding23fb4742012-11-28 11:38:24 +01001038static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1039 struct drm_framebuffer *old_fb)
1040{
1041 struct tegra_dc *dc = to_tegra_dc(crtc);
1042
Matt Roperf4510a22014-04-01 15:22:40 -07001043 return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
Thierry Reding23fb4742012-11-28 11:38:24 +01001044}
1045
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001046static void tegra_crtc_prepare(struct drm_crtc *crtc)
1047{
1048 struct tegra_dc *dc = to_tegra_dc(crtc);
1049 unsigned int syncpt;
1050 unsigned long value;
1051
1052 /* hardware initialization */
Stephen Warrenca480802013-11-06 16:20:54 -07001053 reset_control_deassert(dc->rst);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001054 usleep_range(10000, 20000);
1055
1056 if (dc->pipe)
1057 syncpt = SYNCPT_VBLANK1;
1058 else
1059 syncpt = SYNCPT_VBLANK0;
1060
1061 /* initialize display controller */
1062 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1063 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1064
1065 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1066 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1067
1068 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1069 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1070 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1071
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001072 /* initialize timer */
1073 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1074 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1075 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1076
1077 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1078 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1079 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1080
1081 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001082 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +01001083
1084 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1085 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001086}
1087
1088static void tegra_crtc_commit(struct drm_crtc *crtc)
1089{
1090 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001091
Thierry Reding6e5ff992012-11-28 11:45:47 +01001092 drm_vblank_post_modeset(crtc->dev, dc->pipe);
Thierry Reding205d48e2014-10-21 13:41:46 +02001093 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001094}
1095
1096static void tegra_crtc_load_lut(struct drm_crtc *crtc)
1097{
1098}
1099
1100static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +01001101 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001102 .mode_fixup = tegra_crtc_mode_fixup,
1103 .mode_set = tegra_crtc_mode_set,
Thierry Reding23fb4742012-11-28 11:38:24 +01001104 .mode_set_base = tegra_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001105 .prepare = tegra_crtc_prepare,
1106 .commit = tegra_crtc_commit,
1107 .load_lut = tegra_crtc_load_lut,
1108};
1109
Thierry Reding6e5ff992012-11-28 11:45:47 +01001110static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001111{
1112 struct tegra_dc *dc = data;
1113 unsigned long status;
1114
1115 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1116 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1117
1118 if (status & FRAME_END_INT) {
1119 /*
1120 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1121 */
1122 }
1123
1124 if (status & VBLANK_INT) {
1125 /*
1126 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1127 */
1128 drm_handle_vblank(dc->base.dev, dc->pipe);
Thierry Reding3c03c462012-11-28 12:00:18 +01001129 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001130 }
1131
1132 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1133 /*
1134 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1135 */
1136 }
1137
1138 return IRQ_HANDLED;
1139}
1140
1141static int tegra_dc_show_regs(struct seq_file *s, void *data)
1142{
1143 struct drm_info_node *node = s->private;
1144 struct tegra_dc *dc = node->info_ent->data;
1145
1146#define DUMP_REG(name) \
Thierry Reding03a60562014-10-21 13:48:48 +02001147 seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001148 tegra_dc_readl(dc, name))
1149
1150 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1151 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1152 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1153 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1154 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1155 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1156 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1157 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1158 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1159 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1160 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1161 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1162 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1163 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1164 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1165 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1166 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1167 DUMP_REG(DC_CMD_INT_STATUS);
1168 DUMP_REG(DC_CMD_INT_MASK);
1169 DUMP_REG(DC_CMD_INT_ENABLE);
1170 DUMP_REG(DC_CMD_INT_TYPE);
1171 DUMP_REG(DC_CMD_INT_POLARITY);
1172 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1173 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1174 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1175 DUMP_REG(DC_CMD_STATE_ACCESS);
1176 DUMP_REG(DC_CMD_STATE_CONTROL);
1177 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1178 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1179 DUMP_REG(DC_COM_CRC_CONTROL);
1180 DUMP_REG(DC_COM_CRC_CHECKSUM);
1181 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1182 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1183 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1184 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1185 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1186 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1187 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1188 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1189 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1190 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1191 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1192 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1193 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1194 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1195 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1196 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1197 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1198 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1199 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1200 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1201 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1202 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1203 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1204 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1205 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1206 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1207 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1208 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1209 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1210 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1211 DUMP_REG(DC_COM_SPI_CONTROL);
1212 DUMP_REG(DC_COM_SPI_START_BYTE);
1213 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1214 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1215 DUMP_REG(DC_COM_HSPI_CS_DC);
1216 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1217 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1218 DUMP_REG(DC_COM_GPIO_CTRL);
1219 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1220 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1221 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1222 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1223 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1224 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1225 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1226 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1227 DUMP_REG(DC_DISP_REF_TO_SYNC);
1228 DUMP_REG(DC_DISP_SYNC_WIDTH);
1229 DUMP_REG(DC_DISP_BACK_PORCH);
1230 DUMP_REG(DC_DISP_ACTIVE);
1231 DUMP_REG(DC_DISP_FRONT_PORCH);
1232 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1233 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1234 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1235 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1236 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1237 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1238 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1239 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1240 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1241 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1242 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1243 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1244 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1245 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1246 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1247 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1248 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1249 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1250 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1251 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1252 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1253 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1254 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1255 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1256 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1257 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1258 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1259 DUMP_REG(DC_DISP_M0_CONTROL);
1260 DUMP_REG(DC_DISP_M1_CONTROL);
1261 DUMP_REG(DC_DISP_DI_CONTROL);
1262 DUMP_REG(DC_DISP_PP_CONTROL);
1263 DUMP_REG(DC_DISP_PP_SELECT_A);
1264 DUMP_REG(DC_DISP_PP_SELECT_B);
1265 DUMP_REG(DC_DISP_PP_SELECT_C);
1266 DUMP_REG(DC_DISP_PP_SELECT_D);
1267 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1268 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1269 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1270 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1271 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1272 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1273 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1274 DUMP_REG(DC_DISP_BORDER_COLOR);
1275 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1276 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1277 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1278 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1279 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1280 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1281 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1282 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1283 DUMP_REG(DC_DISP_CURSOR_POSITION);
1284 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1285 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1286 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1287 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1288 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1289 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1290 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1291 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1292 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1293 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1294 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1295 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1296 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1297 DUMP_REG(DC_DISP_SD_CONTROL);
1298 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1299 DUMP_REG(DC_DISP_SD_LUT(0));
1300 DUMP_REG(DC_DISP_SD_LUT(1));
1301 DUMP_REG(DC_DISP_SD_LUT(2));
1302 DUMP_REG(DC_DISP_SD_LUT(3));
1303 DUMP_REG(DC_DISP_SD_LUT(4));
1304 DUMP_REG(DC_DISP_SD_LUT(5));
1305 DUMP_REG(DC_DISP_SD_LUT(6));
1306 DUMP_REG(DC_DISP_SD_LUT(7));
1307 DUMP_REG(DC_DISP_SD_LUT(8));
1308 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1309 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1310 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1311 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1312 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1313 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1314 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1315 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1316 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1317 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1318 DUMP_REG(DC_DISP_SD_BL_TF(0));
1319 DUMP_REG(DC_DISP_SD_BL_TF(1));
1320 DUMP_REG(DC_DISP_SD_BL_TF(2));
1321 DUMP_REG(DC_DISP_SD_BL_TF(3));
1322 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1323 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1324 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001325 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1326 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001327 DUMP_REG(DC_WIN_WIN_OPTIONS);
1328 DUMP_REG(DC_WIN_BYTE_SWAP);
1329 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1330 DUMP_REG(DC_WIN_COLOR_DEPTH);
1331 DUMP_REG(DC_WIN_POSITION);
1332 DUMP_REG(DC_WIN_SIZE);
1333 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1334 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1335 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1336 DUMP_REG(DC_WIN_DDA_INC);
1337 DUMP_REG(DC_WIN_LINE_STRIDE);
1338 DUMP_REG(DC_WIN_BUF_STRIDE);
1339 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1340 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1341 DUMP_REG(DC_WIN_DV_CONTROL);
1342 DUMP_REG(DC_WIN_BLEND_NOKEY);
1343 DUMP_REG(DC_WIN_BLEND_1WIN);
1344 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1345 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001346 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001347 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1348 DUMP_REG(DC_WINBUF_START_ADDR);
1349 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1350 DUMP_REG(DC_WINBUF_START_ADDR_U);
1351 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1352 DUMP_REG(DC_WINBUF_START_ADDR_V);
1353 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1354 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1355 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1356 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1357 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1358 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1359 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1360 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1361 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1362
1363#undef DUMP_REG
1364
1365 return 0;
1366}
1367
1368static struct drm_info_list debugfs_files[] = {
1369 { "regs", tegra_dc_show_regs, 0, NULL },
1370};
1371
1372static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1373{
1374 unsigned int i;
1375 char *name;
1376 int err;
1377
1378 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1379 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1380 kfree(name);
1381
1382 if (!dc->debugfs)
1383 return -ENOMEM;
1384
1385 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1386 GFP_KERNEL);
1387 if (!dc->debugfs_files) {
1388 err = -ENOMEM;
1389 goto remove;
1390 }
1391
1392 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1393 dc->debugfs_files[i].data = dc;
1394
1395 err = drm_debugfs_create_files(dc->debugfs_files,
1396 ARRAY_SIZE(debugfs_files),
1397 dc->debugfs, minor);
1398 if (err < 0)
1399 goto free;
1400
1401 dc->minor = minor;
1402
1403 return 0;
1404
1405free:
1406 kfree(dc->debugfs_files);
1407 dc->debugfs_files = NULL;
1408remove:
1409 debugfs_remove(dc->debugfs);
1410 dc->debugfs = NULL;
1411
1412 return err;
1413}
1414
1415static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1416{
1417 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1418 dc->minor);
1419 dc->minor = NULL;
1420
1421 kfree(dc->debugfs_files);
1422 dc->debugfs_files = NULL;
1423
1424 debugfs_remove(dc->debugfs);
1425 dc->debugfs = NULL;
1426
1427 return 0;
1428}
1429
Thierry Reding53fa7f72013-09-24 15:35:40 +02001430static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001431{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001432 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001433 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001434 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001435 struct drm_plane *primary = NULL;
1436 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001437 int err;
1438
Thierry Redingdf06b752014-06-26 21:41:53 +02001439 if (tegra->domain) {
1440 err = iommu_attach_device(tegra->domain, dc->dev);
1441 if (err < 0) {
1442 dev_err(dc->dev, "failed to attach to domain: %d\n",
1443 err);
1444 return err;
1445 }
1446
1447 dc->domain = tegra->domain;
1448 }
1449
Thierry Redingc7679302014-10-21 13:51:53 +02001450 primary = tegra_dc_primary_plane_create(drm, dc);
1451 if (IS_ERR(primary)) {
1452 err = PTR_ERR(primary);
1453 goto cleanup;
1454 }
1455
1456 if (dc->soc->supports_cursor) {
1457 cursor = tegra_dc_cursor_plane_create(drm, dc);
1458 if (IS_ERR(cursor)) {
1459 err = PTR_ERR(cursor);
1460 goto cleanup;
1461 }
1462 }
1463
1464 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1465 &tegra_crtc_funcs);
1466 if (err < 0)
1467 goto cleanup;
1468
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001469 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1470 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1471
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001472 /*
1473 * Keep track of the minimum pitch alignment across all display
1474 * controllers.
1475 */
1476 if (dc->soc->pitch_align > tegra->pitch_align)
1477 tegra->pitch_align = dc->soc->pitch_align;
1478
Thierry Reding9910f5c2014-05-22 09:57:15 +02001479 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001480 if (err < 0 && err != -ENODEV) {
1481 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001482 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001483 }
1484
Thierry Reding9910f5c2014-05-22 09:57:15 +02001485 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001486 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001487 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001488
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001489 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001490 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001491 if (err < 0)
1492 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1493 }
1494
Thierry Reding6e5ff992012-11-28 11:45:47 +01001495 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001496 dev_name(dc->dev), dc);
1497 if (err < 0) {
1498 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1499 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001500 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001501 }
1502
1503 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001504
1505cleanup:
1506 if (cursor)
1507 drm_plane_cleanup(cursor);
1508
1509 if (primary)
1510 drm_plane_cleanup(primary);
1511
1512 if (tegra->domain) {
1513 iommu_detach_device(tegra->domain, dc->dev);
1514 dc->domain = NULL;
1515 }
1516
1517 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001518}
1519
Thierry Reding53fa7f72013-09-24 15:35:40 +02001520static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001521{
Thierry Reding776dc382013-10-14 14:43:22 +02001522 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001523 int err;
1524
1525 devm_free_irq(dc->dev, dc->irq, dc);
1526
1527 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1528 err = tegra_dc_debugfs_exit(dc);
1529 if (err < 0)
1530 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1531 }
1532
1533 err = tegra_dc_rgb_exit(dc);
1534 if (err) {
1535 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1536 return err;
1537 }
1538
Thierry Redingdf06b752014-06-26 21:41:53 +02001539 if (dc->domain) {
1540 iommu_detach_device(dc->domain, dc->dev);
1541 dc->domain = NULL;
1542 }
1543
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001544 return 0;
1545}
1546
1547static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001548 .init = tegra_dc_init,
1549 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001550};
1551
Thierry Reding8620fc62013-12-12 11:03:59 +01001552static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
1553 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001554 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001555 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001556 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001557 .has_powergate = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001558};
1559
1560static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
1561 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001562 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001563 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001564 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001565 .has_powergate = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001566};
1567
1568static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
1569 .supports_interlacing = false,
1570 .supports_cursor = false,
1571 .supports_block_linear = false,
1572 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001573 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001574};
1575
1576static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
1577 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001578 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001579 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001580 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001581 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001582};
1583
1584static const struct of_device_id tegra_dc_of_match[] = {
1585 {
1586 .compatible = "nvidia,tegra124-dc",
1587 .data = &tegra124_dc_soc_info,
1588 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001589 .compatible = "nvidia,tegra114-dc",
1590 .data = &tegra114_dc_soc_info,
1591 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001592 .compatible = "nvidia,tegra30-dc",
1593 .data = &tegra30_dc_soc_info,
1594 }, {
1595 .compatible = "nvidia,tegra20-dc",
1596 .data = &tegra20_dc_soc_info,
1597 }, {
1598 /* sentinel */
1599 }
1600};
Stephen Warrenef707282014-06-18 16:21:55 -06001601MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001602
Thierry Reding13411dd2014-01-09 17:08:36 +01001603static int tegra_dc_parse_dt(struct tegra_dc *dc)
1604{
1605 struct device_node *np;
1606 u32 value = 0;
1607 int err;
1608
1609 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1610 if (err < 0) {
1611 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1612
1613 /*
1614 * If the nvidia,head property isn't present, try to find the
1615 * correct head number by looking up the position of this
1616 * display controller's node within the device tree. Assuming
1617 * that the nodes are ordered properly in the DTS file and
1618 * that the translation into a flattened device tree blob
1619 * preserves that ordering this will actually yield the right
1620 * head number.
1621 *
1622 * If those assumptions don't hold, this will still work for
1623 * cases where only a single display controller is used.
1624 */
1625 for_each_matching_node(np, tegra_dc_of_match) {
1626 if (np == dc->dev->of_node)
1627 break;
1628
1629 value++;
1630 }
1631 }
1632
1633 dc->pipe = value;
1634
1635 return 0;
1636}
1637
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001638static int tegra_dc_probe(struct platform_device *pdev)
1639{
Thierry Reding8620fc62013-12-12 11:03:59 +01001640 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001641 struct resource *regs;
1642 struct tegra_dc *dc;
1643 int err;
1644
1645 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1646 if (!dc)
1647 return -ENOMEM;
1648
Thierry Reding8620fc62013-12-12 11:03:59 +01001649 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1650 if (!id)
1651 return -ENODEV;
1652
Thierry Reding6e5ff992012-11-28 11:45:47 +01001653 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001654 INIT_LIST_HEAD(&dc->list);
1655 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001656 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001657
Thierry Reding13411dd2014-01-09 17:08:36 +01001658 err = tegra_dc_parse_dt(dc);
1659 if (err < 0)
1660 return err;
1661
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001662 dc->clk = devm_clk_get(&pdev->dev, NULL);
1663 if (IS_ERR(dc->clk)) {
1664 dev_err(&pdev->dev, "failed to get clock\n");
1665 return PTR_ERR(dc->clk);
1666 }
1667
Stephen Warrenca480802013-11-06 16:20:54 -07001668 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1669 if (IS_ERR(dc->rst)) {
1670 dev_err(&pdev->dev, "failed to get reset\n");
1671 return PTR_ERR(dc->rst);
1672 }
1673
Thierry Reding9c012702014-07-07 15:32:53 +02001674 if (dc->soc->has_powergate) {
1675 if (dc->pipe == 0)
1676 dc->powergate = TEGRA_POWERGATE_DIS;
1677 else
1678 dc->powergate = TEGRA_POWERGATE_DISB;
1679
1680 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
1681 dc->rst);
1682 if (err < 0) {
1683 dev_err(&pdev->dev, "failed to power partition: %d\n",
1684 err);
1685 return err;
1686 }
1687 } else {
1688 err = clk_prepare_enable(dc->clk);
1689 if (err < 0) {
1690 dev_err(&pdev->dev, "failed to enable clock: %d\n",
1691 err);
1692 return err;
1693 }
1694
1695 err = reset_control_deassert(dc->rst);
1696 if (err < 0) {
1697 dev_err(&pdev->dev, "failed to deassert reset: %d\n",
1698 err);
1699 return err;
1700 }
1701 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001702
1703 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001704 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1705 if (IS_ERR(dc->regs))
1706 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001707
1708 dc->irq = platform_get_irq(pdev, 0);
1709 if (dc->irq < 0) {
1710 dev_err(&pdev->dev, "failed to get IRQ\n");
1711 return -ENXIO;
1712 }
1713
Thierry Reding776dc382013-10-14 14:43:22 +02001714 INIT_LIST_HEAD(&dc->client.list);
1715 dc->client.ops = &dc_client_ops;
1716 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001717
1718 err = tegra_dc_rgb_probe(dc);
1719 if (err < 0 && err != -ENODEV) {
1720 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1721 return err;
1722 }
1723
Thierry Reding776dc382013-10-14 14:43:22 +02001724 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001725 if (err < 0) {
1726 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1727 err);
1728 return err;
1729 }
1730
1731 platform_set_drvdata(pdev, dc);
1732
1733 return 0;
1734}
1735
1736static int tegra_dc_remove(struct platform_device *pdev)
1737{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001738 struct tegra_dc *dc = platform_get_drvdata(pdev);
1739 int err;
1740
Thierry Reding776dc382013-10-14 14:43:22 +02001741 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001742 if (err < 0) {
1743 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1744 err);
1745 return err;
1746 }
1747
Thierry Reding59d29c02013-10-14 14:26:42 +02001748 err = tegra_dc_rgb_remove(dc);
1749 if (err < 0) {
1750 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1751 return err;
1752 }
1753
Thierry Reding5482d752014-07-11 08:39:03 +02001754 reset_control_assert(dc->rst);
Thierry Reding9c012702014-07-07 15:32:53 +02001755
1756 if (dc->soc->has_powergate)
1757 tegra_powergate_power_off(dc->powergate);
1758
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001759 clk_disable_unprepare(dc->clk);
1760
1761 return 0;
1762}
1763
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001764struct platform_driver tegra_dc_driver = {
1765 .driver = {
1766 .name = "tegra-dc",
1767 .owner = THIS_MODULE,
1768 .of_match_table = tegra_dc_of_match,
1769 },
1770 .probe = tegra_dc_probe,
1771 .remove = tegra_dc_remove,
1772};