blob: 5f138b7c81bff2e35716be2675bc1eb8cbb594d2 [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 Reding10288ee2014-03-14 09:54:58 +010039static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
40{
41 /* assume no swapping of fetched data */
42 if (swap)
43 *swap = BYTE_SWAP_NOSWAP;
44
45 switch (format) {
46 case DRM_FORMAT_XBGR8888:
47 return WIN_COLOR_DEPTH_R8G8B8A8;
48
49 case DRM_FORMAT_XRGB8888:
50 return WIN_COLOR_DEPTH_B8G8R8A8;
51
52 case DRM_FORMAT_RGB565:
53 return WIN_COLOR_DEPTH_B5G6R5;
54
55 case DRM_FORMAT_UYVY:
56 return WIN_COLOR_DEPTH_YCbCr422;
57
58 case DRM_FORMAT_YUYV:
59 if (swap)
60 *swap = BYTE_SWAP_SWAP2;
61
62 return WIN_COLOR_DEPTH_YCbCr422;
63
64 case DRM_FORMAT_YUV420:
65 return WIN_COLOR_DEPTH_YCbCr420P;
66
67 case DRM_FORMAT_YUV422:
68 return WIN_COLOR_DEPTH_YCbCr422P;
69
70 default:
71 break;
72 }
73
74 WARN(1, "unsupported pixel format %u, using default\n", format);
75 return WIN_COLOR_DEPTH_B8G8R8A8;
76}
77
78static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
79{
80 switch (format) {
81 case WIN_COLOR_DEPTH_YCbCr422:
82 case WIN_COLOR_DEPTH_YUV422:
83 if (planar)
84 *planar = false;
85
86 return true;
87
88 case WIN_COLOR_DEPTH_YCbCr420P:
89 case WIN_COLOR_DEPTH_YUV420P:
90 case WIN_COLOR_DEPTH_YCbCr422P:
91 case WIN_COLOR_DEPTH_YUV422P:
92 case WIN_COLOR_DEPTH_YCbCr422R:
93 case WIN_COLOR_DEPTH_YUV422R:
94 case WIN_COLOR_DEPTH_YCbCr422RA:
95 case WIN_COLOR_DEPTH_YUV422RA:
96 if (planar)
97 *planar = true;
98
99 return true;
100 }
101
102 return false;
103}
104
105static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
106 unsigned int bpp)
107{
108 fixed20_12 outf = dfixed_init(out);
109 fixed20_12 inf = dfixed_init(in);
110 u32 dda_inc;
111 int max;
112
113 if (v)
114 max = 15;
115 else {
116 switch (bpp) {
117 case 2:
118 max = 8;
119 break;
120
121 default:
122 WARN_ON_ONCE(1);
123 /* fallthrough */
124 case 4:
125 max = 4;
126 break;
127 }
128 }
129
130 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
131 inf.full -= dfixed_const(1);
132
133 dda_inc = dfixed_div(inf, outf);
134 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
135
136 return dda_inc;
137}
138
139static inline u32 compute_initial_dda(unsigned int in)
140{
141 fixed20_12 inf = dfixed_init(in);
142 return dfixed_frac(inf);
143}
144
145static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
146 const struct tegra_dc_window *window)
147{
148 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
149 unsigned long value;
150 bool yuv, planar;
151
152 /*
153 * For YUV planar modes, the number of bytes per pixel takes into
154 * account only the luma component and therefore is 1.
155 */
156 yuv = tegra_dc_format_is_yuv(window->format, &planar);
157 if (!yuv)
158 bpp = window->bits_per_pixel / 8;
159 else
160 bpp = planar ? 1 : 2;
161
162 value = WINDOW_A_SELECT << index;
163 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
164
165 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
166 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
167
168 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
169 tegra_dc_writel(dc, value, DC_WIN_POSITION);
170
171 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
172 tegra_dc_writel(dc, value, DC_WIN_SIZE);
173
174 h_offset = window->src.x * bpp;
175 v_offset = window->src.y;
176 h_size = window->src.w * bpp;
177 v_size = window->src.h;
178
179 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
180 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
181
182 /*
183 * For DDA computations the number of bytes per pixel for YUV planar
184 * modes needs to take into account all Y, U and V components.
185 */
186 if (yuv && planar)
187 bpp = 2;
188
189 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
190 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
191
192 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
193 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
194
195 h_dda = compute_initial_dda(window->src.x);
196 v_dda = compute_initial_dda(window->src.y);
197
198 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
199 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
200
201 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
202 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
203
204 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
205
206 if (yuv && planar) {
207 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
208 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
209 value = window->stride[1] << 16 | window->stride[0];
210 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
211 } else {
212 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
213 }
214
215 if (window->bottom_up)
216 v_offset += window->src.h - 1;
217
218 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
219 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
220
Thierry Redingc134f012014-06-03 14:48:12 +0200221 if (dc->soc->supports_block_linear) {
222 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100223
Thierry Redingc134f012014-06-03 14:48:12 +0200224 switch (window->tiling.mode) {
225 case TEGRA_BO_TILING_MODE_PITCH:
226 value = DC_WINBUF_SURFACE_KIND_PITCH;
227 break;
228
229 case TEGRA_BO_TILING_MODE_TILED:
230 value = DC_WINBUF_SURFACE_KIND_TILED;
231 break;
232
233 case TEGRA_BO_TILING_MODE_BLOCK:
234 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
235 DC_WINBUF_SURFACE_KIND_BLOCK;
236 break;
237 }
238
239 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
240 } else {
241 switch (window->tiling.mode) {
242 case TEGRA_BO_TILING_MODE_PITCH:
243 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
244 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
245 break;
246
247 case TEGRA_BO_TILING_MODE_TILED:
248 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
249 DC_WIN_BUFFER_ADDR_MODE_TILE;
250 break;
251
252 case TEGRA_BO_TILING_MODE_BLOCK:
253 DRM_ERROR("hardware doesn't support block linear mode\n");
254 return -EINVAL;
255 }
256
257 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
258 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100259
260 value = WIN_ENABLE;
261
262 if (yuv) {
263 /* setup default colorspace conversion coefficients */
264 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
265 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
266 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
267 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
268 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
269 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
270 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
271 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
272
273 value |= CSC_ENABLE;
274 } else if (window->bits_per_pixel < 24) {
275 value |= COLOR_EXPAND;
276 }
277
278 if (window->bottom_up)
279 value |= V_DIRECTION;
280
281 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
282
283 /*
284 * Disable blending and assume Window A is the bottom-most window,
285 * Window C is the top-most window and Window B is in the middle.
286 */
287 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
288 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
289
290 switch (index) {
291 case 0:
292 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
293 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
294 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
295 break;
296
297 case 1:
298 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
299 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
300 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
301 break;
302
303 case 2:
304 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
305 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
306 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
307 break;
308 }
309
310 tegra_dc_writel(dc, WIN_A_UPDATE << index, DC_CMD_STATE_CONTROL);
311 tegra_dc_writel(dc, WIN_A_ACT_REQ << index, DC_CMD_STATE_CONTROL);
312
313 return 0;
314}
315
Thierry Redingf34bc782012-11-04 21:47:13 +0100316static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
317 struct drm_framebuffer *fb, int crtc_x,
318 int crtc_y, unsigned int crtc_w,
319 unsigned int crtc_h, uint32_t src_x,
320 uint32_t src_y, uint32_t src_w, uint32_t src_h)
321{
322 struct tegra_plane *p = to_tegra_plane(plane);
323 struct tegra_dc *dc = to_tegra_dc(crtc);
324 struct tegra_dc_window window;
325 unsigned int i;
Thierry Redingc134f012014-06-03 14:48:12 +0200326 int err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100327
328 memset(&window, 0, sizeof(window));
329 window.src.x = src_x >> 16;
330 window.src.y = src_y >> 16;
331 window.src.w = src_w >> 16;
332 window.src.h = src_h >> 16;
333 window.dst.x = crtc_x;
334 window.dst.y = crtc_y;
335 window.dst.w = crtc_w;
336 window.dst.h = crtc_h;
Thierry Redingf9253902014-01-29 20:31:17 +0100337 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
Thierry Redingf34bc782012-11-04 21:47:13 +0100338 window.bits_per_pixel = fb->bits_per_pixel;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200339 window.bottom_up = tegra_fb_is_bottom_up(fb);
Thierry Redingc134f012014-06-03 14:48:12 +0200340
341 err = tegra_fb_get_tiling(fb, &window.tiling);
342 if (err < 0)
343 return err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100344
345 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
Arto Merilainende2ba662013-03-22 16:34:08 +0200346 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingf34bc782012-11-04 21:47:13 +0100347
Arto Merilainende2ba662013-03-22 16:34:08 +0200348 window.base[i] = bo->paddr + fb->offsets[i];
Thierry Redingf34bc782012-11-04 21:47:13 +0100349
350 /*
351 * Tegra doesn't support different strides for U and V planes
352 * so we display a warning if the user tries to display a
353 * framebuffer with such a configuration.
354 */
355 if (i >= 2) {
356 if (fb->pitches[i] != window.stride[1])
357 DRM_ERROR("unsupported UV-plane configuration\n");
358 } else {
359 window.stride[i] = fb->pitches[i];
360 }
361 }
362
363 return tegra_dc_setup_window(dc, p->index, &window);
364}
365
366static int tegra_plane_disable(struct drm_plane *plane)
367{
368 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
369 struct tegra_plane *p = to_tegra_plane(plane);
370 unsigned long value;
371
Thierry Reding2678aeb2013-03-18 11:09:13 +0100372 if (!plane->crtc)
373 return 0;
374
Thierry Redingf34bc782012-11-04 21:47:13 +0100375 value = WINDOW_A_SELECT << p->index;
376 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
377
378 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
379 value &= ~WIN_ENABLE;
380 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
381
382 tegra_dc_writel(dc, WIN_A_UPDATE << p->index, DC_CMD_STATE_CONTROL);
383 tegra_dc_writel(dc, WIN_A_ACT_REQ << p->index, DC_CMD_STATE_CONTROL);
384
385 return 0;
386}
387
388static void tegra_plane_destroy(struct drm_plane *plane)
389{
Thierry Redingf002abc2013-10-14 14:06:02 +0200390 struct tegra_plane *p = to_tegra_plane(plane);
391
Thierry Redingf34bc782012-11-04 21:47:13 +0100392 tegra_plane_disable(plane);
393 drm_plane_cleanup(plane);
Thierry Redingf002abc2013-10-14 14:06:02 +0200394 kfree(p);
Thierry Redingf34bc782012-11-04 21:47:13 +0100395}
396
397static const struct drm_plane_funcs tegra_plane_funcs = {
398 .update_plane = tegra_plane_update,
399 .disable_plane = tegra_plane_disable,
400 .destroy = tegra_plane_destroy,
401};
402
403static const uint32_t plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100404 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100405 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100406 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100407 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100408 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100409 DRM_FORMAT_YUV420,
410 DRM_FORMAT_YUV422,
411};
412
413static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
414{
415 unsigned int i;
416 int err = 0;
417
418 for (i = 0; i < 2; i++) {
419 struct tegra_plane *plane;
420
Thierry Redingf002abc2013-10-14 14:06:02 +0200421 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
Thierry Redingf34bc782012-11-04 21:47:13 +0100422 if (!plane)
423 return -ENOMEM;
424
425 plane->index = 1 + i;
426
427 err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
428 &tegra_plane_funcs, plane_formats,
429 ARRAY_SIZE(plane_formats), false);
Thierry Redingf002abc2013-10-14 14:06:02 +0200430 if (err < 0) {
431 kfree(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100432 return err;
Thierry Redingf002abc2013-10-14 14:06:02 +0200433 }
Thierry Redingf34bc782012-11-04 21:47:13 +0100434 }
435
436 return 0;
437}
438
Thierry Reding23fb4742012-11-28 11:38:24 +0100439static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
440 struct drm_framebuffer *fb)
441{
Arto Merilainende2ba662013-03-22 16:34:08 +0200442 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200443 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200444 struct tegra_bo_tiling tiling;
Thierry Redingf9253902014-01-29 20:31:17 +0100445 unsigned int format, swap;
Thierry Reding23fb4742012-11-28 11:38:24 +0100446 unsigned long value;
Thierry Redingc134f012014-06-03 14:48:12 +0200447 int err;
448
449 err = tegra_fb_get_tiling(fb, &tiling);
450 if (err < 0)
451 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100452
453 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
454
455 value = fb->offsets[0] + y * fb->pitches[0] +
456 x * fb->bits_per_pixel / 8;
457
Arto Merilainende2ba662013-03-22 16:34:08 +0200458 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100459 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100460
461 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200462 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100463 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100464
Thierry Redingc134f012014-06-03 14:48:12 +0200465 if (dc->soc->supports_block_linear) {
466 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200467
Thierry Redingc134f012014-06-03 14:48:12 +0200468 switch (tiling.mode) {
469 case TEGRA_BO_TILING_MODE_PITCH:
470 value = DC_WINBUF_SURFACE_KIND_PITCH;
471 break;
472
473 case TEGRA_BO_TILING_MODE_TILED:
474 value = DC_WINBUF_SURFACE_KIND_TILED;
475 break;
476
477 case TEGRA_BO_TILING_MODE_BLOCK:
478 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
479 DC_WINBUF_SURFACE_KIND_BLOCK;
480 break;
481 }
482
483 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
484 } else {
485 switch (tiling.mode) {
486 case TEGRA_BO_TILING_MODE_PITCH:
487 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
488 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
489 break;
490
491 case TEGRA_BO_TILING_MODE_TILED:
492 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
493 DC_WIN_BUFFER_ADDR_MODE_TILE;
494 break;
495
496 case TEGRA_BO_TILING_MODE_BLOCK:
497 DRM_ERROR("hardware doesn't support block linear mode\n");
498 return -EINVAL;
499 }
500
501 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
502 }
Thierry Reding773af772013-10-04 22:34:01 +0200503
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200504 /* make sure bottom-up buffers are properly displayed */
505 if (tegra_fb_is_bottom_up(fb)) {
506 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100507 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200508 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
509
510 v_offset += fb->height - 1;
511 } else {
512 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100513 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200514 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
515 }
516
517 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
518 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
519
Thierry Reding23fb4742012-11-28 11:38:24 +0100520 value = GENERAL_UPDATE | WIN_A_UPDATE;
521 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
522
523 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
524 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
525
526 return 0;
527}
528
Thierry Reding6e5ff992012-11-28 11:45:47 +0100529void tegra_dc_enable_vblank(struct tegra_dc *dc)
530{
531 unsigned long value, flags;
532
533 spin_lock_irqsave(&dc->lock, flags);
534
535 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
536 value |= VBLANK_INT;
537 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
538
539 spin_unlock_irqrestore(&dc->lock, flags);
540}
541
542void tegra_dc_disable_vblank(struct tegra_dc *dc)
543{
544 unsigned long value, flags;
545
546 spin_lock_irqsave(&dc->lock, flags);
547
548 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
549 value &= ~VBLANK_INT;
550 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
551
552 spin_unlock_irqrestore(&dc->lock, flags);
553}
554
Thierry Redinge6876512013-12-20 13:58:33 +0100555static int tegra_dc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file,
556 uint32_t handle, uint32_t width,
557 uint32_t height, int32_t hot_x, int32_t hot_y)
558{
559 unsigned long value = CURSOR_CLIP_DISPLAY;
560 struct tegra_dc *dc = to_tegra_dc(crtc);
561 struct drm_gem_object *gem;
562 struct tegra_bo *bo = NULL;
563
564 if (!dc->soc->supports_cursor)
565 return -ENXIO;
566
567 if (width != height)
568 return -EINVAL;
569
570 switch (width) {
571 case 32:
572 value |= CURSOR_SIZE_32x32;
573 break;
574
575 case 64:
576 value |= CURSOR_SIZE_64x64;
577 break;
578
579 case 128:
580 value |= CURSOR_SIZE_128x128;
581
582 case 256:
583 value |= CURSOR_SIZE_256x256;
584 break;
585
586 default:
587 return -EINVAL;
588 }
589
590 if (handle) {
591 gem = drm_gem_object_lookup(crtc->dev, file, handle);
592 if (!gem)
593 return -ENOENT;
594
595 bo = to_tegra_bo(gem);
596 }
597
598 if (bo) {
599 unsigned long addr = (bo->paddr & 0xfffffc00) >> 10;
600#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
601 unsigned long high = (bo->paddr & 0xfffffffc) >> 32;
602#endif
603
604 tegra_dc_writel(dc, value | addr, DC_DISP_CURSOR_START_ADDR);
605
606#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
607 tegra_dc_writel(dc, high, DC_DISP_CURSOR_START_ADDR_HI);
608#endif
609
610 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
611 value |= CURSOR_ENABLE;
612 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
613
614 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
615 value &= ~CURSOR_DST_BLEND_MASK;
616 value &= ~CURSOR_SRC_BLEND_MASK;
617 value |= CURSOR_MODE_NORMAL;
618 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
619 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
620 value |= CURSOR_ALPHA;
621 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
622 } else {
623 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
624 value &= ~CURSOR_ENABLE;
625 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
626 }
627
628 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
629 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
630
631 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
632 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
633
634 return 0;
635}
636
637static int tegra_dc_cursor_move(struct drm_crtc *crtc, int x, int y)
638{
639 struct tegra_dc *dc = to_tegra_dc(crtc);
640 unsigned long value;
641
642 if (!dc->soc->supports_cursor)
643 return -ENXIO;
644
645 value = ((y & 0x3fff) << 16) | (x & 0x3fff);
646 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
647
648 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
649 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
650
651 /* XXX: only required on generations earlier than Tegra124? */
652 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
653 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
654
655 return 0;
656}
657
Thierry Reding3c03c462012-11-28 12:00:18 +0100658static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
659{
660 struct drm_device *drm = dc->base.dev;
661 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100662 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200663 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100664
665 if (!dc->event)
666 return;
667
Matt Roperf4510a22014-04-01 15:22:40 -0700668 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100669
670 /* check if new start address has been latched */
671 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
672 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
673 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
674
Matt Roperf4510a22014-04-01 15:22:40 -0700675 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Reding3c03c462012-11-28 12:00:18 +0100676 spin_lock_irqsave(&drm->event_lock, flags);
677 drm_send_vblank_event(drm, dc->pipe, dc->event);
678 drm_vblank_put(drm, dc->pipe);
679 dc->event = NULL;
680 spin_unlock_irqrestore(&drm->event_lock, flags);
681 }
682}
683
684void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
685{
686 struct tegra_dc *dc = to_tegra_dc(crtc);
687 struct drm_device *drm = crtc->dev;
688 unsigned long flags;
689
690 spin_lock_irqsave(&drm->event_lock, flags);
691
692 if (dc->event && dc->event->base.file_priv == file) {
693 dc->event->base.destroy(&dc->event->base);
694 drm_vblank_put(drm, dc->pipe);
695 dc->event = NULL;
696 }
697
698 spin_unlock_irqrestore(&drm->event_lock, flags);
699}
700
701static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000702 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100703{
704 struct tegra_dc *dc = to_tegra_dc(crtc);
705 struct drm_device *drm = crtc->dev;
706
707 if (dc->event)
708 return -EBUSY;
709
710 if (event) {
711 event->pipe = dc->pipe;
712 dc->event = event;
713 drm_vblank_get(drm, dc->pipe);
714 }
715
716 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700717 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100718
719 return 0;
720}
721
Thierry Redingf002abc2013-10-14 14:06:02 +0200722static void drm_crtc_clear(struct drm_crtc *crtc)
723{
724 memset(crtc, 0, sizeof(*crtc));
725}
726
727static void tegra_dc_destroy(struct drm_crtc *crtc)
728{
729 drm_crtc_cleanup(crtc);
730 drm_crtc_clear(crtc);
731}
732
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000733static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Redinge6876512013-12-20 13:58:33 +0100734 .cursor_set2 = tegra_dc_cursor_set2,
735 .cursor_move = tegra_dc_cursor_move,
Thierry Reding3c03c462012-11-28 12:00:18 +0100736 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000737 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +0200738 .destroy = tegra_dc_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000739};
740
Thierry Redingf34bc782012-11-04 21:47:13 +0100741static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000742{
Thierry Redingf002abc2013-10-14 14:06:02 +0200743 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100744 struct drm_device *drm = crtc->dev;
745 struct drm_plane *plane;
746
Daniel Vetter2b4c3662014-04-23 15:15:32 +0200747 drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
Thierry Redingf34bc782012-11-04 21:47:13 +0100748 if (plane->crtc == crtc) {
749 tegra_plane_disable(plane);
750 plane->crtc = NULL;
751
752 if (plane->fb) {
753 drm_framebuffer_unreference(plane->fb);
754 plane->fb = NULL;
755 }
756 }
757 }
Thierry Redingf002abc2013-10-14 14:06:02 +0200758
759 drm_vblank_off(drm, dc->pipe);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000760}
761
762static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
763 const struct drm_display_mode *mode,
764 struct drm_display_mode *adjusted)
765{
766 return true;
767}
768
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000769static int tegra_dc_set_timings(struct tegra_dc *dc,
770 struct drm_display_mode *mode)
771{
Thierry Reding0444c0f2014-04-16 09:22:38 +0200772 unsigned int h_ref_to_sync = 1;
773 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000774 unsigned long value;
775
776 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
777
778 value = (v_ref_to_sync << 16) | h_ref_to_sync;
779 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
780
781 value = ((mode->vsync_end - mode->vsync_start) << 16) |
782 ((mode->hsync_end - mode->hsync_start) << 0);
783 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
784
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000785 value = ((mode->vtotal - mode->vsync_end) << 16) |
786 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +0000787 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
788
789 value = ((mode->vsync_start - mode->vdisplay) << 16) |
790 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000791 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
792
793 value = (mode->vdisplay << 16) | mode->hdisplay;
794 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
795
796 return 0;
797}
798
799static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100800 struct drm_display_mode *mode)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000801{
Thierry Reding91eded92014-03-26 13:32:21 +0100802 unsigned long pclk = mode->clock * 1000;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000803 struct tegra_dc *dc = to_tegra_dc(crtc);
804 struct tegra_output *output = NULL;
805 struct drm_encoder *encoder;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100806 unsigned int div;
807 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000808 long err;
809
810 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
811 if (encoder->crtc == crtc) {
812 output = encoder_to_output(encoder);
813 break;
814 }
815
816 if (!output)
817 return -ENODEV;
818
819 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100820 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
821 * respectively, each of which divides the base pll_d by 2.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000822 */
Thierry Reding91eded92014-03-26 13:32:21 +0100823 err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000824 if (err < 0) {
825 dev_err(dc->dev, "failed to setup clock: %ld\n", err);
826 return err;
827 }
828
Thierry Reding91eded92014-03-26 13:32:21 +0100829 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100830
831 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
832 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000833
834 return 0;
835}
836
837static int tegra_crtc_mode_set(struct drm_crtc *crtc,
838 struct drm_display_mode *mode,
839 struct drm_display_mode *adjusted,
840 int x, int y, struct drm_framebuffer *old_fb)
841{
Matt Roperf4510a22014-04-01 15:22:40 -0700842 struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000843 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100844 struct tegra_dc_window window;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100845 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000846 int err;
847
Thierry Reding6e5ff992012-11-28 11:45:47 +0100848 drm_vblank_pre_modeset(crtc->dev, dc->pipe);
849
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100850 err = tegra_crtc_setup_clk(crtc, mode);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000851 if (err) {
852 dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
853 return err;
854 }
855
856 /* program display mode */
857 tegra_dc_set_timings(dc, mode);
858
Thierry Reding8620fc62013-12-12 11:03:59 +0100859 /* interlacing isn't supported yet, so disable it */
860 if (dc->soc->supports_interlacing) {
861 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
862 value &= ~INTERLACE_ENABLE;
863 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
864 }
865
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000866 /* setup window parameters */
Thierry Redingf34bc782012-11-04 21:47:13 +0100867 memset(&window, 0, sizeof(window));
868 window.src.x = 0;
869 window.src.y = 0;
870 window.src.w = mode->hdisplay;
871 window.src.h = mode->vdisplay;
872 window.dst.x = 0;
873 window.dst.y = 0;
874 window.dst.w = mode->hdisplay;
875 window.dst.h = mode->vdisplay;
Thierry Redingf9253902014-01-29 20:31:17 +0100876 window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
877 &window.swap);
Matt Roperf4510a22014-04-01 15:22:40 -0700878 window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
879 window.stride[0] = crtc->primary->fb->pitches[0];
Arto Merilainende2ba662013-03-22 16:34:08 +0200880 window.base[0] = bo->paddr;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000881
Thierry Redingf34bc782012-11-04 21:47:13 +0100882 err = tegra_dc_setup_window(dc, 0, &window);
883 if (err < 0)
884 dev_err(dc->dev, "failed to enable root plane\n");
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000885
886 return 0;
887}
888
Thierry Reding23fb4742012-11-28 11:38:24 +0100889static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
890 struct drm_framebuffer *old_fb)
891{
892 struct tegra_dc *dc = to_tegra_dc(crtc);
893
Matt Roperf4510a22014-04-01 15:22:40 -0700894 return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
Thierry Reding23fb4742012-11-28 11:38:24 +0100895}
896
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000897static void tegra_crtc_prepare(struct drm_crtc *crtc)
898{
899 struct tegra_dc *dc = to_tegra_dc(crtc);
900 unsigned int syncpt;
901 unsigned long value;
902
903 /* hardware initialization */
Stephen Warrenca480802013-11-06 16:20:54 -0700904 reset_control_deassert(dc->rst);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000905 usleep_range(10000, 20000);
906
907 if (dc->pipe)
908 syncpt = SYNCPT_VBLANK1;
909 else
910 syncpt = SYNCPT_VBLANK0;
911
912 /* initialize display controller */
913 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
914 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
915
916 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
917 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
918
919 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
920 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
921 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
922
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000923 /* initialize timer */
924 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
925 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
926 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
927
928 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
929 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
930 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
931
932 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000933 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +0100934
935 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
936 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000937}
938
939static void tegra_crtc_commit(struct drm_crtc *crtc)
940{
941 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000942 unsigned long value;
943
Thierry Reding3b9e71e2013-01-15 12:21:36 +0100944 value = GENERAL_UPDATE | WIN_A_UPDATE;
945 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000946
Thierry Reding3b9e71e2013-01-15 12:21:36 +0100947 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding6e5ff992012-11-28 11:45:47 +0100948 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000949
Thierry Reding6e5ff992012-11-28 11:45:47 +0100950 drm_vblank_post_modeset(crtc->dev, dc->pipe);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000951}
952
953static void tegra_crtc_load_lut(struct drm_crtc *crtc)
954{
955}
956
957static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +0100958 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000959 .mode_fixup = tegra_crtc_mode_fixup,
960 .mode_set = tegra_crtc_mode_set,
Thierry Reding23fb4742012-11-28 11:38:24 +0100961 .mode_set_base = tegra_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000962 .prepare = tegra_crtc_prepare,
963 .commit = tegra_crtc_commit,
964 .load_lut = tegra_crtc_load_lut,
965};
966
Thierry Reding6e5ff992012-11-28 11:45:47 +0100967static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000968{
969 struct tegra_dc *dc = data;
970 unsigned long status;
971
972 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
973 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
974
975 if (status & FRAME_END_INT) {
976 /*
977 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
978 */
979 }
980
981 if (status & VBLANK_INT) {
982 /*
983 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
984 */
985 drm_handle_vblank(dc->base.dev, dc->pipe);
Thierry Reding3c03c462012-11-28 12:00:18 +0100986 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000987 }
988
989 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
990 /*
991 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
992 */
993 }
994
995 return IRQ_HANDLED;
996}
997
998static int tegra_dc_show_regs(struct seq_file *s, void *data)
999{
1000 struct drm_info_node *node = s->private;
1001 struct tegra_dc *dc = node->info_ent->data;
1002
1003#define DUMP_REG(name) \
1004 seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
1005 tegra_dc_readl(dc, name))
1006
1007 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1008 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1009 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1010 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1011 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1012 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1013 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1014 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1015 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1016 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1017 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1018 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1019 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1020 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1021 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1022 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1023 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1024 DUMP_REG(DC_CMD_INT_STATUS);
1025 DUMP_REG(DC_CMD_INT_MASK);
1026 DUMP_REG(DC_CMD_INT_ENABLE);
1027 DUMP_REG(DC_CMD_INT_TYPE);
1028 DUMP_REG(DC_CMD_INT_POLARITY);
1029 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1030 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1031 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1032 DUMP_REG(DC_CMD_STATE_ACCESS);
1033 DUMP_REG(DC_CMD_STATE_CONTROL);
1034 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1035 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1036 DUMP_REG(DC_COM_CRC_CONTROL);
1037 DUMP_REG(DC_COM_CRC_CHECKSUM);
1038 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1039 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1040 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1041 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1042 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1043 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1044 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1045 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1046 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1047 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1048 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1049 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1050 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1051 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1052 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1053 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1054 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1055 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1056 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1057 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1058 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1059 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1060 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1061 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1062 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1063 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1064 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1065 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1066 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1067 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1068 DUMP_REG(DC_COM_SPI_CONTROL);
1069 DUMP_REG(DC_COM_SPI_START_BYTE);
1070 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1071 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1072 DUMP_REG(DC_COM_HSPI_CS_DC);
1073 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1074 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1075 DUMP_REG(DC_COM_GPIO_CTRL);
1076 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1077 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1078 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1079 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1080 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1081 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1082 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1083 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1084 DUMP_REG(DC_DISP_REF_TO_SYNC);
1085 DUMP_REG(DC_DISP_SYNC_WIDTH);
1086 DUMP_REG(DC_DISP_BACK_PORCH);
1087 DUMP_REG(DC_DISP_ACTIVE);
1088 DUMP_REG(DC_DISP_FRONT_PORCH);
1089 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1090 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1091 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1092 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1093 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1094 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1095 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1096 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1097 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1098 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1099 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1100 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1101 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1102 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1103 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1104 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1105 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1106 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1107 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1108 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1109 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1110 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1111 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1112 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1113 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1114 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1115 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1116 DUMP_REG(DC_DISP_M0_CONTROL);
1117 DUMP_REG(DC_DISP_M1_CONTROL);
1118 DUMP_REG(DC_DISP_DI_CONTROL);
1119 DUMP_REG(DC_DISP_PP_CONTROL);
1120 DUMP_REG(DC_DISP_PP_SELECT_A);
1121 DUMP_REG(DC_DISP_PP_SELECT_B);
1122 DUMP_REG(DC_DISP_PP_SELECT_C);
1123 DUMP_REG(DC_DISP_PP_SELECT_D);
1124 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1125 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1126 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1127 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1128 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1129 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1130 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1131 DUMP_REG(DC_DISP_BORDER_COLOR);
1132 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1133 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1134 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1135 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1136 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1137 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1138 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1139 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1140 DUMP_REG(DC_DISP_CURSOR_POSITION);
1141 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1142 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1143 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1144 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1145 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1146 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1147 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1148 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1149 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1150 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1151 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1152 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1153 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1154 DUMP_REG(DC_DISP_SD_CONTROL);
1155 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1156 DUMP_REG(DC_DISP_SD_LUT(0));
1157 DUMP_REG(DC_DISP_SD_LUT(1));
1158 DUMP_REG(DC_DISP_SD_LUT(2));
1159 DUMP_REG(DC_DISP_SD_LUT(3));
1160 DUMP_REG(DC_DISP_SD_LUT(4));
1161 DUMP_REG(DC_DISP_SD_LUT(5));
1162 DUMP_REG(DC_DISP_SD_LUT(6));
1163 DUMP_REG(DC_DISP_SD_LUT(7));
1164 DUMP_REG(DC_DISP_SD_LUT(8));
1165 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1166 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1167 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1168 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1169 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1170 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1171 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1172 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1173 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1174 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1175 DUMP_REG(DC_DISP_SD_BL_TF(0));
1176 DUMP_REG(DC_DISP_SD_BL_TF(1));
1177 DUMP_REG(DC_DISP_SD_BL_TF(2));
1178 DUMP_REG(DC_DISP_SD_BL_TF(3));
1179 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1180 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1181 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001182 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1183 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001184 DUMP_REG(DC_WIN_WIN_OPTIONS);
1185 DUMP_REG(DC_WIN_BYTE_SWAP);
1186 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1187 DUMP_REG(DC_WIN_COLOR_DEPTH);
1188 DUMP_REG(DC_WIN_POSITION);
1189 DUMP_REG(DC_WIN_SIZE);
1190 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1191 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1192 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1193 DUMP_REG(DC_WIN_DDA_INC);
1194 DUMP_REG(DC_WIN_LINE_STRIDE);
1195 DUMP_REG(DC_WIN_BUF_STRIDE);
1196 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1197 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1198 DUMP_REG(DC_WIN_DV_CONTROL);
1199 DUMP_REG(DC_WIN_BLEND_NOKEY);
1200 DUMP_REG(DC_WIN_BLEND_1WIN);
1201 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1202 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001203 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001204 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1205 DUMP_REG(DC_WINBUF_START_ADDR);
1206 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1207 DUMP_REG(DC_WINBUF_START_ADDR_U);
1208 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1209 DUMP_REG(DC_WINBUF_START_ADDR_V);
1210 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1211 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1212 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1213 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1214 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1215 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1216 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1217 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1218 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1219
1220#undef DUMP_REG
1221
1222 return 0;
1223}
1224
1225static struct drm_info_list debugfs_files[] = {
1226 { "regs", tegra_dc_show_regs, 0, NULL },
1227};
1228
1229static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1230{
1231 unsigned int i;
1232 char *name;
1233 int err;
1234
1235 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1236 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1237 kfree(name);
1238
1239 if (!dc->debugfs)
1240 return -ENOMEM;
1241
1242 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1243 GFP_KERNEL);
1244 if (!dc->debugfs_files) {
1245 err = -ENOMEM;
1246 goto remove;
1247 }
1248
1249 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1250 dc->debugfs_files[i].data = dc;
1251
1252 err = drm_debugfs_create_files(dc->debugfs_files,
1253 ARRAY_SIZE(debugfs_files),
1254 dc->debugfs, minor);
1255 if (err < 0)
1256 goto free;
1257
1258 dc->minor = minor;
1259
1260 return 0;
1261
1262free:
1263 kfree(dc->debugfs_files);
1264 dc->debugfs_files = NULL;
1265remove:
1266 debugfs_remove(dc->debugfs);
1267 dc->debugfs = NULL;
1268
1269 return err;
1270}
1271
1272static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1273{
1274 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1275 dc->minor);
1276 dc->minor = NULL;
1277
1278 kfree(dc->debugfs_files);
1279 dc->debugfs_files = NULL;
1280
1281 debugfs_remove(dc->debugfs);
1282 dc->debugfs = NULL;
1283
1284 return 0;
1285}
1286
Thierry Reding53fa7f72013-09-24 15:35:40 +02001287static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001288{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001289 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001290 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001291 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001292 int err;
1293
Thierry Redingdf06b752014-06-26 21:41:53 +02001294 if (tegra->domain) {
1295 err = iommu_attach_device(tegra->domain, dc->dev);
1296 if (err < 0) {
1297 dev_err(dc->dev, "failed to attach to domain: %d\n",
1298 err);
1299 return err;
1300 }
1301
1302 dc->domain = tegra->domain;
1303 }
1304
Thierry Reding9910f5c2014-05-22 09:57:15 +02001305 drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001306 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1307 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1308
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001309 /*
1310 * Keep track of the minimum pitch alignment across all display
1311 * controllers.
1312 */
1313 if (dc->soc->pitch_align > tegra->pitch_align)
1314 tegra->pitch_align = dc->soc->pitch_align;
1315
Thierry Reding9910f5c2014-05-22 09:57:15 +02001316 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001317 if (err < 0 && err != -ENODEV) {
1318 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1319 return err;
1320 }
1321
Thierry Reding9910f5c2014-05-22 09:57:15 +02001322 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001323 if (err < 0)
1324 return err;
1325
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001326 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001327 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001328 if (err < 0)
1329 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1330 }
1331
Thierry Reding6e5ff992012-11-28 11:45:47 +01001332 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001333 dev_name(dc->dev), dc);
1334 if (err < 0) {
1335 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1336 err);
1337 return err;
1338 }
1339
1340 return 0;
1341}
1342
Thierry Reding53fa7f72013-09-24 15:35:40 +02001343static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001344{
Thierry Reding776dc382013-10-14 14:43:22 +02001345 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001346 int err;
1347
1348 devm_free_irq(dc->dev, dc->irq, dc);
1349
1350 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1351 err = tegra_dc_debugfs_exit(dc);
1352 if (err < 0)
1353 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1354 }
1355
1356 err = tegra_dc_rgb_exit(dc);
1357 if (err) {
1358 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1359 return err;
1360 }
1361
Thierry Redingdf06b752014-06-26 21:41:53 +02001362 if (dc->domain) {
1363 iommu_detach_device(dc->domain, dc->dev);
1364 dc->domain = NULL;
1365 }
1366
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001367 return 0;
1368}
1369
1370static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001371 .init = tegra_dc_init,
1372 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001373};
1374
Thierry Reding8620fc62013-12-12 11:03:59 +01001375static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
1376 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001377 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001378 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001379 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001380 .has_powergate = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001381};
1382
1383static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
1384 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001385 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001386 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001387 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001388 .has_powergate = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001389};
1390
1391static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
1392 .supports_interlacing = false,
1393 .supports_cursor = false,
1394 .supports_block_linear = false,
1395 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001396 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001397};
1398
1399static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
1400 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001401 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001402 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001403 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001404 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001405};
1406
1407static const struct of_device_id tegra_dc_of_match[] = {
1408 {
1409 .compatible = "nvidia,tegra124-dc",
1410 .data = &tegra124_dc_soc_info,
1411 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001412 .compatible = "nvidia,tegra114-dc",
1413 .data = &tegra114_dc_soc_info,
1414 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001415 .compatible = "nvidia,tegra30-dc",
1416 .data = &tegra30_dc_soc_info,
1417 }, {
1418 .compatible = "nvidia,tegra20-dc",
1419 .data = &tegra20_dc_soc_info,
1420 }, {
1421 /* sentinel */
1422 }
1423};
Stephen Warrenef707282014-06-18 16:21:55 -06001424MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001425
Thierry Reding13411dd2014-01-09 17:08:36 +01001426static int tegra_dc_parse_dt(struct tegra_dc *dc)
1427{
1428 struct device_node *np;
1429 u32 value = 0;
1430 int err;
1431
1432 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1433 if (err < 0) {
1434 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1435
1436 /*
1437 * If the nvidia,head property isn't present, try to find the
1438 * correct head number by looking up the position of this
1439 * display controller's node within the device tree. Assuming
1440 * that the nodes are ordered properly in the DTS file and
1441 * that the translation into a flattened device tree blob
1442 * preserves that ordering this will actually yield the right
1443 * head number.
1444 *
1445 * If those assumptions don't hold, this will still work for
1446 * cases where only a single display controller is used.
1447 */
1448 for_each_matching_node(np, tegra_dc_of_match) {
1449 if (np == dc->dev->of_node)
1450 break;
1451
1452 value++;
1453 }
1454 }
1455
1456 dc->pipe = value;
1457
1458 return 0;
1459}
1460
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001461static int tegra_dc_probe(struct platform_device *pdev)
1462{
Thierry Reding8620fc62013-12-12 11:03:59 +01001463 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001464 struct resource *regs;
1465 struct tegra_dc *dc;
1466 int err;
1467
1468 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1469 if (!dc)
1470 return -ENOMEM;
1471
Thierry Reding8620fc62013-12-12 11:03:59 +01001472 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1473 if (!id)
1474 return -ENODEV;
1475
Thierry Reding6e5ff992012-11-28 11:45:47 +01001476 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001477 INIT_LIST_HEAD(&dc->list);
1478 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001479 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001480
Thierry Reding13411dd2014-01-09 17:08:36 +01001481 err = tegra_dc_parse_dt(dc);
1482 if (err < 0)
1483 return err;
1484
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001485 dc->clk = devm_clk_get(&pdev->dev, NULL);
1486 if (IS_ERR(dc->clk)) {
1487 dev_err(&pdev->dev, "failed to get clock\n");
1488 return PTR_ERR(dc->clk);
1489 }
1490
Stephen Warrenca480802013-11-06 16:20:54 -07001491 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1492 if (IS_ERR(dc->rst)) {
1493 dev_err(&pdev->dev, "failed to get reset\n");
1494 return PTR_ERR(dc->rst);
1495 }
1496
Thierry Reding9c012702014-07-07 15:32:53 +02001497 if (dc->soc->has_powergate) {
1498 if (dc->pipe == 0)
1499 dc->powergate = TEGRA_POWERGATE_DIS;
1500 else
1501 dc->powergate = TEGRA_POWERGATE_DISB;
1502
1503 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
1504 dc->rst);
1505 if (err < 0) {
1506 dev_err(&pdev->dev, "failed to power partition: %d\n",
1507 err);
1508 return err;
1509 }
1510 } else {
1511 err = clk_prepare_enable(dc->clk);
1512 if (err < 0) {
1513 dev_err(&pdev->dev, "failed to enable clock: %d\n",
1514 err);
1515 return err;
1516 }
1517
1518 err = reset_control_deassert(dc->rst);
1519 if (err < 0) {
1520 dev_err(&pdev->dev, "failed to deassert reset: %d\n",
1521 err);
1522 return err;
1523 }
1524 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001525
1526 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001527 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1528 if (IS_ERR(dc->regs))
1529 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001530
1531 dc->irq = platform_get_irq(pdev, 0);
1532 if (dc->irq < 0) {
1533 dev_err(&pdev->dev, "failed to get IRQ\n");
1534 return -ENXIO;
1535 }
1536
Thierry Reding776dc382013-10-14 14:43:22 +02001537 INIT_LIST_HEAD(&dc->client.list);
1538 dc->client.ops = &dc_client_ops;
1539 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001540
1541 err = tegra_dc_rgb_probe(dc);
1542 if (err < 0 && err != -ENODEV) {
1543 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1544 return err;
1545 }
1546
Thierry Reding776dc382013-10-14 14:43:22 +02001547 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001548 if (err < 0) {
1549 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1550 err);
1551 return err;
1552 }
1553
1554 platform_set_drvdata(pdev, dc);
1555
1556 return 0;
1557}
1558
1559static int tegra_dc_remove(struct platform_device *pdev)
1560{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001561 struct tegra_dc *dc = platform_get_drvdata(pdev);
1562 int err;
1563
Thierry Reding776dc382013-10-14 14:43:22 +02001564 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001565 if (err < 0) {
1566 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1567 err);
1568 return err;
1569 }
1570
Thierry Reding59d29c02013-10-14 14:26:42 +02001571 err = tegra_dc_rgb_remove(dc);
1572 if (err < 0) {
1573 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1574 return err;
1575 }
1576
Thierry Reding5482d752014-07-11 08:39:03 +02001577 reset_control_assert(dc->rst);
Thierry Reding9c012702014-07-07 15:32:53 +02001578
1579 if (dc->soc->has_powergate)
1580 tegra_powergate_power_off(dc->powergate);
1581
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001582 clk_disable_unprepare(dc->clk);
1583
1584 return 0;
1585}
1586
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001587struct platform_driver tegra_dc_driver = {
1588 .driver = {
1589 .name = "tegra-dc",
1590 .owner = THIS_MODULE,
1591 .of_match_table = tegra_dc_of_match,
1592 },
1593 .probe = tegra_dc_probe,
1594 .remove = tegra_dc_remove,
1595};