blob: 8fb815dde969d934ceb508f820195142d563a906 [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 Redingf34bc782012-11-04 21:47:13 +0100335static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
336 struct drm_framebuffer *fb, int crtc_x,
337 int crtc_y, unsigned int crtc_w,
338 unsigned int crtc_h, uint32_t src_x,
339 uint32_t src_y, uint32_t src_w, uint32_t src_h)
340{
341 struct tegra_plane *p = to_tegra_plane(plane);
342 struct tegra_dc *dc = to_tegra_dc(crtc);
343 struct tegra_dc_window window;
344 unsigned int i;
Thierry Redingc134f012014-06-03 14:48:12 +0200345 int err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100346
347 memset(&window, 0, sizeof(window));
348 window.src.x = src_x >> 16;
349 window.src.y = src_y >> 16;
350 window.src.w = src_w >> 16;
351 window.src.h = src_h >> 16;
352 window.dst.x = crtc_x;
353 window.dst.y = crtc_y;
354 window.dst.w = crtc_w;
355 window.dst.h = crtc_h;
Thierry Redingf9253902014-01-29 20:31:17 +0100356 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
Thierry Redingf34bc782012-11-04 21:47:13 +0100357 window.bits_per_pixel = fb->bits_per_pixel;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200358 window.bottom_up = tegra_fb_is_bottom_up(fb);
Thierry Redingc134f012014-06-03 14:48:12 +0200359
360 err = tegra_fb_get_tiling(fb, &window.tiling);
361 if (err < 0)
362 return err;
Thierry Redingf34bc782012-11-04 21:47:13 +0100363
364 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
Arto Merilainende2ba662013-03-22 16:34:08 +0200365 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingf34bc782012-11-04 21:47:13 +0100366
Arto Merilainende2ba662013-03-22 16:34:08 +0200367 window.base[i] = bo->paddr + fb->offsets[i];
Thierry Redingf34bc782012-11-04 21:47:13 +0100368
369 /*
370 * Tegra doesn't support different strides for U and V planes
371 * so we display a warning if the user tries to display a
372 * framebuffer with such a configuration.
373 */
374 if (i >= 2) {
375 if (fb->pitches[i] != window.stride[1])
376 DRM_ERROR("unsupported UV-plane configuration\n");
377 } else {
378 window.stride[i] = fb->pitches[i];
379 }
380 }
381
382 return tegra_dc_setup_window(dc, p->index, &window);
383}
384
385static int tegra_plane_disable(struct drm_plane *plane)
386{
387 struct tegra_dc *dc = to_tegra_dc(plane->crtc);
388 struct tegra_plane *p = to_tegra_plane(plane);
389 unsigned long value;
390
Thierry Reding2678aeb2013-03-18 11:09:13 +0100391 if (!plane->crtc)
392 return 0;
393
Thierry Redingf34bc782012-11-04 21:47:13 +0100394 value = WINDOW_A_SELECT << p->index;
395 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
396
397 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
398 value &= ~WIN_ENABLE;
399 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
400
Thierry Reding205d48e2014-10-21 13:41:46 +0200401 tegra_dc_window_commit(dc, p->index);
Thierry Redingf34bc782012-11-04 21:47:13 +0100402
403 return 0;
404}
405
406static void tegra_plane_destroy(struct drm_plane *plane)
407{
Thierry Redingf002abc2013-10-14 14:06:02 +0200408 struct tegra_plane *p = to_tegra_plane(plane);
409
Thierry Redingf34bc782012-11-04 21:47:13 +0100410 tegra_plane_disable(plane);
411 drm_plane_cleanup(plane);
Thierry Redingf002abc2013-10-14 14:06:02 +0200412 kfree(p);
Thierry Redingf34bc782012-11-04 21:47:13 +0100413}
414
415static const struct drm_plane_funcs tegra_plane_funcs = {
416 .update_plane = tegra_plane_update,
417 .disable_plane = tegra_plane_disable,
418 .destroy = tegra_plane_destroy,
419};
420
421static const uint32_t plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100422 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100423 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100424 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100425 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100426 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100427 DRM_FORMAT_YUV420,
428 DRM_FORMAT_YUV422,
429};
430
431static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
432{
433 unsigned int i;
434 int err = 0;
435
436 for (i = 0; i < 2; i++) {
437 struct tegra_plane *plane;
438
Thierry Redingf002abc2013-10-14 14:06:02 +0200439 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
Thierry Redingf34bc782012-11-04 21:47:13 +0100440 if (!plane)
441 return -ENOMEM;
442
443 plane->index = 1 + i;
444
445 err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
446 &tegra_plane_funcs, plane_formats,
447 ARRAY_SIZE(plane_formats), false);
Thierry Redingf002abc2013-10-14 14:06:02 +0200448 if (err < 0) {
449 kfree(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100450 return err;
Thierry Redingf002abc2013-10-14 14:06:02 +0200451 }
Thierry Redingf34bc782012-11-04 21:47:13 +0100452 }
453
454 return 0;
455}
456
Thierry Reding23fb4742012-11-28 11:38:24 +0100457static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
458 struct drm_framebuffer *fb)
459{
Arto Merilainende2ba662013-03-22 16:34:08 +0200460 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200461 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200462 struct tegra_bo_tiling tiling;
Thierry Redingf9253902014-01-29 20:31:17 +0100463 unsigned int format, swap;
Thierry Reding23fb4742012-11-28 11:38:24 +0100464 unsigned long value;
Thierry Redingc134f012014-06-03 14:48:12 +0200465 int err;
466
467 err = tegra_fb_get_tiling(fb, &tiling);
468 if (err < 0)
469 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100470
471 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
472
473 value = fb->offsets[0] + y * fb->pitches[0] +
474 x * fb->bits_per_pixel / 8;
475
Arto Merilainende2ba662013-03-22 16:34:08 +0200476 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100477 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100478
479 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200480 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100481 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100482
Thierry Redingc134f012014-06-03 14:48:12 +0200483 if (dc->soc->supports_block_linear) {
484 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200485
Thierry Redingc134f012014-06-03 14:48:12 +0200486 switch (tiling.mode) {
487 case TEGRA_BO_TILING_MODE_PITCH:
488 value = DC_WINBUF_SURFACE_KIND_PITCH;
489 break;
490
491 case TEGRA_BO_TILING_MODE_TILED:
492 value = DC_WINBUF_SURFACE_KIND_TILED;
493 break;
494
495 case TEGRA_BO_TILING_MODE_BLOCK:
496 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
497 DC_WINBUF_SURFACE_KIND_BLOCK;
498 break;
499 }
500
501 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
502 } else {
503 switch (tiling.mode) {
504 case TEGRA_BO_TILING_MODE_PITCH:
505 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
506 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
507 break;
508
509 case TEGRA_BO_TILING_MODE_TILED:
510 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
511 DC_WIN_BUFFER_ADDR_MODE_TILE;
512 break;
513
514 case TEGRA_BO_TILING_MODE_BLOCK:
515 DRM_ERROR("hardware doesn't support block linear mode\n");
516 return -EINVAL;
517 }
518
519 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
520 }
Thierry Reding773af772013-10-04 22:34:01 +0200521
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200522 /* make sure bottom-up buffers are properly displayed */
523 if (tegra_fb_is_bottom_up(fb)) {
524 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100525 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200526 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
527
528 v_offset += fb->height - 1;
529 } else {
530 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100531 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200532 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
533 }
534
535 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
536 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
537
Thierry Reding23fb4742012-11-28 11:38:24 +0100538 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding205d48e2014-10-21 13:41:46 +0200539 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
Thierry Reding23fb4742012-11-28 11:38:24 +0100540 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
541
542 return 0;
543}
544
Thierry Reding6e5ff992012-11-28 11:45:47 +0100545void tegra_dc_enable_vblank(struct tegra_dc *dc)
546{
547 unsigned long value, flags;
548
549 spin_lock_irqsave(&dc->lock, flags);
550
551 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
552 value |= VBLANK_INT;
553 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
554
555 spin_unlock_irqrestore(&dc->lock, flags);
556}
557
558void tegra_dc_disable_vblank(struct tegra_dc *dc)
559{
560 unsigned long value, flags;
561
562 spin_lock_irqsave(&dc->lock, flags);
563
564 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
565 value &= ~VBLANK_INT;
566 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
567
568 spin_unlock_irqrestore(&dc->lock, flags);
569}
570
Thierry Redinge6876512013-12-20 13:58:33 +0100571static int tegra_dc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file,
572 uint32_t handle, uint32_t width,
573 uint32_t height, int32_t hot_x, int32_t hot_y)
574{
575 unsigned long value = CURSOR_CLIP_DISPLAY;
576 struct tegra_dc *dc = to_tegra_dc(crtc);
577 struct drm_gem_object *gem;
578 struct tegra_bo *bo = NULL;
579
580 if (!dc->soc->supports_cursor)
581 return -ENXIO;
582
583 if (width != height)
584 return -EINVAL;
585
586 switch (width) {
587 case 32:
588 value |= CURSOR_SIZE_32x32;
589 break;
590
591 case 64:
592 value |= CURSOR_SIZE_64x64;
593 break;
594
595 case 128:
596 value |= CURSOR_SIZE_128x128;
597
598 case 256:
599 value |= CURSOR_SIZE_256x256;
600 break;
601
602 default:
603 return -EINVAL;
604 }
605
606 if (handle) {
607 gem = drm_gem_object_lookup(crtc->dev, file, handle);
608 if (!gem)
609 return -ENOENT;
610
611 bo = to_tegra_bo(gem);
612 }
613
614 if (bo) {
615 unsigned long addr = (bo->paddr & 0xfffffc00) >> 10;
616#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
617 unsigned long high = (bo->paddr & 0xfffffffc) >> 32;
618#endif
619
620 tegra_dc_writel(dc, value | addr, DC_DISP_CURSOR_START_ADDR);
621
622#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
623 tegra_dc_writel(dc, high, DC_DISP_CURSOR_START_ADDR_HI);
624#endif
625
626 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
627 value |= CURSOR_ENABLE;
628 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
629
630 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
631 value &= ~CURSOR_DST_BLEND_MASK;
632 value &= ~CURSOR_SRC_BLEND_MASK;
633 value |= CURSOR_MODE_NORMAL;
634 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
635 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
636 value |= CURSOR_ALPHA;
637 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
638 } else {
639 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
640 value &= ~CURSOR_ENABLE;
641 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
642 }
643
Thierry Reding205d48e2014-10-21 13:41:46 +0200644 tegra_dc_cursor_commit(dc);
645 tegra_dc_commit(dc);
Thierry Redinge6876512013-12-20 13:58:33 +0100646
647 return 0;
648}
649
650static int tegra_dc_cursor_move(struct drm_crtc *crtc, int x, int y)
651{
652 struct tegra_dc *dc = to_tegra_dc(crtc);
653 unsigned long value;
654
655 if (!dc->soc->supports_cursor)
656 return -ENXIO;
657
658 value = ((y & 0x3fff) << 16) | (x & 0x3fff);
659 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
660
Thierry Reding205d48e2014-10-21 13:41:46 +0200661 tegra_dc_cursor_commit(dc);
Thierry Redinge6876512013-12-20 13:58:33 +0100662 /* XXX: only required on generations earlier than Tegra124? */
Thierry Reding205d48e2014-10-21 13:41:46 +0200663 tegra_dc_commit(dc);
Thierry Redinge6876512013-12-20 13:58:33 +0100664
665 return 0;
666}
667
Thierry Reding3c03c462012-11-28 12:00:18 +0100668static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
669{
670 struct drm_device *drm = dc->base.dev;
671 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100672 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200673 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100674
675 if (!dc->event)
676 return;
677
Matt Roperf4510a22014-04-01 15:22:40 -0700678 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100679
680 /* check if new start address has been latched */
681 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
682 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
683 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
684
Matt Roperf4510a22014-04-01 15:22:40 -0700685 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Reding3c03c462012-11-28 12:00:18 +0100686 spin_lock_irqsave(&drm->event_lock, flags);
687 drm_send_vblank_event(drm, dc->pipe, dc->event);
688 drm_vblank_put(drm, dc->pipe);
689 dc->event = NULL;
690 spin_unlock_irqrestore(&drm->event_lock, flags);
691 }
692}
693
694void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
695{
696 struct tegra_dc *dc = to_tegra_dc(crtc);
697 struct drm_device *drm = crtc->dev;
698 unsigned long flags;
699
700 spin_lock_irqsave(&drm->event_lock, flags);
701
702 if (dc->event && dc->event->base.file_priv == file) {
703 dc->event->base.destroy(&dc->event->base);
704 drm_vblank_put(drm, dc->pipe);
705 dc->event = NULL;
706 }
707
708 spin_unlock_irqrestore(&drm->event_lock, flags);
709}
710
711static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000712 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100713{
714 struct tegra_dc *dc = to_tegra_dc(crtc);
715 struct drm_device *drm = crtc->dev;
716
717 if (dc->event)
718 return -EBUSY;
719
720 if (event) {
721 event->pipe = dc->pipe;
722 dc->event = event;
723 drm_vblank_get(drm, dc->pipe);
724 }
725
726 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700727 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100728
729 return 0;
730}
731
Thierry Redingf002abc2013-10-14 14:06:02 +0200732static void drm_crtc_clear(struct drm_crtc *crtc)
733{
734 memset(crtc, 0, sizeof(*crtc));
735}
736
737static void tegra_dc_destroy(struct drm_crtc *crtc)
738{
739 drm_crtc_cleanup(crtc);
740 drm_crtc_clear(crtc);
741}
742
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000743static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Redinge6876512013-12-20 13:58:33 +0100744 .cursor_set2 = tegra_dc_cursor_set2,
745 .cursor_move = tegra_dc_cursor_move,
Thierry Reding3c03c462012-11-28 12:00:18 +0100746 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000747 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +0200748 .destroy = tegra_dc_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000749};
750
Thierry Redingf34bc782012-11-04 21:47:13 +0100751static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000752{
Thierry Redingf002abc2013-10-14 14:06:02 +0200753 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100754 struct drm_device *drm = crtc->dev;
755 struct drm_plane *plane;
756
Daniel Vetter2b4c3662014-04-23 15:15:32 +0200757 drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
Thierry Redingf34bc782012-11-04 21:47:13 +0100758 if (plane->crtc == crtc) {
759 tegra_plane_disable(plane);
760 plane->crtc = NULL;
761
762 if (plane->fb) {
763 drm_framebuffer_unreference(plane->fb);
764 plane->fb = NULL;
765 }
766 }
767 }
Thierry Redingf002abc2013-10-14 14:06:02 +0200768
769 drm_vblank_off(drm, dc->pipe);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000770}
771
772static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
773 const struct drm_display_mode *mode,
774 struct drm_display_mode *adjusted)
775{
776 return true;
777}
778
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000779static int tegra_dc_set_timings(struct tegra_dc *dc,
780 struct drm_display_mode *mode)
781{
Thierry Reding0444c0f2014-04-16 09:22:38 +0200782 unsigned int h_ref_to_sync = 1;
783 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000784 unsigned long value;
785
786 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
787
788 value = (v_ref_to_sync << 16) | h_ref_to_sync;
789 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
790
791 value = ((mode->vsync_end - mode->vsync_start) << 16) |
792 ((mode->hsync_end - mode->hsync_start) << 0);
793 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
794
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000795 value = ((mode->vtotal - mode->vsync_end) << 16) |
796 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +0000797 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
798
799 value = ((mode->vsync_start - mode->vdisplay) << 16) |
800 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000801 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
802
803 value = (mode->vdisplay << 16) | mode->hdisplay;
804 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
805
806 return 0;
807}
808
809static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100810 struct drm_display_mode *mode)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000811{
Thierry Reding91eded92014-03-26 13:32:21 +0100812 unsigned long pclk = mode->clock * 1000;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000813 struct tegra_dc *dc = to_tegra_dc(crtc);
814 struct tegra_output *output = NULL;
815 struct drm_encoder *encoder;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100816 unsigned int div;
817 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000818 long err;
819
820 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
821 if (encoder->crtc == crtc) {
822 output = encoder_to_output(encoder);
823 break;
824 }
825
826 if (!output)
827 return -ENODEV;
828
829 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100830 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
831 * respectively, each of which divides the base pll_d by 2.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000832 */
Thierry Reding91eded92014-03-26 13:32:21 +0100833 err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000834 if (err < 0) {
835 dev_err(dc->dev, "failed to setup clock: %ld\n", err);
836 return err;
837 }
838
Thierry Reding91eded92014-03-26 13:32:21 +0100839 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100840
841 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
842 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000843
844 return 0;
845}
846
847static int tegra_crtc_mode_set(struct drm_crtc *crtc,
848 struct drm_display_mode *mode,
849 struct drm_display_mode *adjusted,
850 int x, int y, struct drm_framebuffer *old_fb)
851{
Matt Roperf4510a22014-04-01 15:22:40 -0700852 struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000853 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingf34bc782012-11-04 21:47:13 +0100854 struct tegra_dc_window window;
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100855 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000856 int err;
857
Thierry Reding6e5ff992012-11-28 11:45:47 +0100858 drm_vblank_pre_modeset(crtc->dev, dc->pipe);
859
Thierry Redingdbb3f2f2014-03-26 12:32:14 +0100860 err = tegra_crtc_setup_clk(crtc, mode);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000861 if (err) {
862 dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
863 return err;
864 }
865
866 /* program display mode */
867 tegra_dc_set_timings(dc, mode);
868
Thierry Reding8620fc62013-12-12 11:03:59 +0100869 /* interlacing isn't supported yet, so disable it */
870 if (dc->soc->supports_interlacing) {
871 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
872 value &= ~INTERLACE_ENABLE;
873 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
874 }
875
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000876 /* setup window parameters */
Thierry Redingf34bc782012-11-04 21:47:13 +0100877 memset(&window, 0, sizeof(window));
878 window.src.x = 0;
879 window.src.y = 0;
880 window.src.w = mode->hdisplay;
881 window.src.h = mode->vdisplay;
882 window.dst.x = 0;
883 window.dst.y = 0;
884 window.dst.w = mode->hdisplay;
885 window.dst.h = mode->vdisplay;
Thierry Redingf9253902014-01-29 20:31:17 +0100886 window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
887 &window.swap);
Matt Roperf4510a22014-04-01 15:22:40 -0700888 window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
889 window.stride[0] = crtc->primary->fb->pitches[0];
Arto Merilainende2ba662013-03-22 16:34:08 +0200890 window.base[0] = bo->paddr;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000891
Thierry Redingf34bc782012-11-04 21:47:13 +0100892 err = tegra_dc_setup_window(dc, 0, &window);
893 if (err < 0)
894 dev_err(dc->dev, "failed to enable root plane\n");
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000895
896 return 0;
897}
898
Thierry Reding23fb4742012-11-28 11:38:24 +0100899static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
900 struct drm_framebuffer *old_fb)
901{
902 struct tegra_dc *dc = to_tegra_dc(crtc);
903
Matt Roperf4510a22014-04-01 15:22:40 -0700904 return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
Thierry Reding23fb4742012-11-28 11:38:24 +0100905}
906
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000907static void tegra_crtc_prepare(struct drm_crtc *crtc)
908{
909 struct tegra_dc *dc = to_tegra_dc(crtc);
910 unsigned int syncpt;
911 unsigned long value;
912
913 /* hardware initialization */
Stephen Warrenca480802013-11-06 16:20:54 -0700914 reset_control_deassert(dc->rst);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000915 usleep_range(10000, 20000);
916
917 if (dc->pipe)
918 syncpt = SYNCPT_VBLANK1;
919 else
920 syncpt = SYNCPT_VBLANK0;
921
922 /* initialize display controller */
923 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
924 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
925
926 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
927 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
928
929 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
930 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
931 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
932
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000933 /* initialize timer */
934 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
935 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
936 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
937
938 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
939 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
940 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
941
942 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000943 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +0100944
945 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
946 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000947}
948
949static void tegra_crtc_commit(struct drm_crtc *crtc)
950{
951 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000952
Thierry Reding6e5ff992012-11-28 11:45:47 +0100953 drm_vblank_post_modeset(crtc->dev, dc->pipe);
Thierry Reding205d48e2014-10-21 13:41:46 +0200954 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000955}
956
957static void tegra_crtc_load_lut(struct drm_crtc *crtc)
958{
959}
960
961static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +0100962 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000963 .mode_fixup = tegra_crtc_mode_fixup,
964 .mode_set = tegra_crtc_mode_set,
Thierry Reding23fb4742012-11-28 11:38:24 +0100965 .mode_set_base = tegra_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000966 .prepare = tegra_crtc_prepare,
967 .commit = tegra_crtc_commit,
968 .load_lut = tegra_crtc_load_lut,
969};
970
Thierry Reding6e5ff992012-11-28 11:45:47 +0100971static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000972{
973 struct tegra_dc *dc = data;
974 unsigned long status;
975
976 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
977 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
978
979 if (status & FRAME_END_INT) {
980 /*
981 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
982 */
983 }
984
985 if (status & VBLANK_INT) {
986 /*
987 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
988 */
989 drm_handle_vblank(dc->base.dev, dc->pipe);
Thierry Reding3c03c462012-11-28 12:00:18 +0100990 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000991 }
992
993 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
994 /*
995 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
996 */
997 }
998
999 return IRQ_HANDLED;
1000}
1001
1002static int tegra_dc_show_regs(struct seq_file *s, void *data)
1003{
1004 struct drm_info_node *node = s->private;
1005 struct tegra_dc *dc = node->info_ent->data;
1006
1007#define DUMP_REG(name) \
1008 seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
1009 tegra_dc_readl(dc, name))
1010
1011 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1012 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1013 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1014 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1015 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1016 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1017 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1018 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1019 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1020 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1021 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1022 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1023 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1024 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1025 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1026 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1027 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1028 DUMP_REG(DC_CMD_INT_STATUS);
1029 DUMP_REG(DC_CMD_INT_MASK);
1030 DUMP_REG(DC_CMD_INT_ENABLE);
1031 DUMP_REG(DC_CMD_INT_TYPE);
1032 DUMP_REG(DC_CMD_INT_POLARITY);
1033 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1034 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1035 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1036 DUMP_REG(DC_CMD_STATE_ACCESS);
1037 DUMP_REG(DC_CMD_STATE_CONTROL);
1038 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1039 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1040 DUMP_REG(DC_COM_CRC_CONTROL);
1041 DUMP_REG(DC_COM_CRC_CHECKSUM);
1042 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1043 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1044 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1045 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1046 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1047 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1048 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1049 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1050 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1051 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1052 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1053 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1054 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1055 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1056 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1057 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1058 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1059 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1060 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1061 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1062 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1063 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1064 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1065 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1066 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1067 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1068 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1069 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1070 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1071 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1072 DUMP_REG(DC_COM_SPI_CONTROL);
1073 DUMP_REG(DC_COM_SPI_START_BYTE);
1074 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1075 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1076 DUMP_REG(DC_COM_HSPI_CS_DC);
1077 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1078 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1079 DUMP_REG(DC_COM_GPIO_CTRL);
1080 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1081 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1082 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1083 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1084 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1085 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1086 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1087 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1088 DUMP_REG(DC_DISP_REF_TO_SYNC);
1089 DUMP_REG(DC_DISP_SYNC_WIDTH);
1090 DUMP_REG(DC_DISP_BACK_PORCH);
1091 DUMP_REG(DC_DISP_ACTIVE);
1092 DUMP_REG(DC_DISP_FRONT_PORCH);
1093 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1094 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1095 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1096 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1097 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1098 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1099 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1100 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1101 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1102 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1103 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1104 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1105 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1106 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1107 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1108 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1109 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1110 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1111 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1112 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1113 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1114 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1115 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1116 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1117 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1118 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1119 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1120 DUMP_REG(DC_DISP_M0_CONTROL);
1121 DUMP_REG(DC_DISP_M1_CONTROL);
1122 DUMP_REG(DC_DISP_DI_CONTROL);
1123 DUMP_REG(DC_DISP_PP_CONTROL);
1124 DUMP_REG(DC_DISP_PP_SELECT_A);
1125 DUMP_REG(DC_DISP_PP_SELECT_B);
1126 DUMP_REG(DC_DISP_PP_SELECT_C);
1127 DUMP_REG(DC_DISP_PP_SELECT_D);
1128 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1129 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1130 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1131 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1132 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1133 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1134 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1135 DUMP_REG(DC_DISP_BORDER_COLOR);
1136 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1137 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1138 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1139 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1140 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1141 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1142 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1143 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1144 DUMP_REG(DC_DISP_CURSOR_POSITION);
1145 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1146 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1147 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1148 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1149 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1150 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1151 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1152 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1153 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1154 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1155 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1156 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1157 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1158 DUMP_REG(DC_DISP_SD_CONTROL);
1159 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1160 DUMP_REG(DC_DISP_SD_LUT(0));
1161 DUMP_REG(DC_DISP_SD_LUT(1));
1162 DUMP_REG(DC_DISP_SD_LUT(2));
1163 DUMP_REG(DC_DISP_SD_LUT(3));
1164 DUMP_REG(DC_DISP_SD_LUT(4));
1165 DUMP_REG(DC_DISP_SD_LUT(5));
1166 DUMP_REG(DC_DISP_SD_LUT(6));
1167 DUMP_REG(DC_DISP_SD_LUT(7));
1168 DUMP_REG(DC_DISP_SD_LUT(8));
1169 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1170 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1171 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1172 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1173 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1174 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1175 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1176 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1177 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1178 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1179 DUMP_REG(DC_DISP_SD_BL_TF(0));
1180 DUMP_REG(DC_DISP_SD_BL_TF(1));
1181 DUMP_REG(DC_DISP_SD_BL_TF(2));
1182 DUMP_REG(DC_DISP_SD_BL_TF(3));
1183 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1184 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1185 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001186 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1187 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001188 DUMP_REG(DC_WIN_WIN_OPTIONS);
1189 DUMP_REG(DC_WIN_BYTE_SWAP);
1190 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1191 DUMP_REG(DC_WIN_COLOR_DEPTH);
1192 DUMP_REG(DC_WIN_POSITION);
1193 DUMP_REG(DC_WIN_SIZE);
1194 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1195 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1196 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1197 DUMP_REG(DC_WIN_DDA_INC);
1198 DUMP_REG(DC_WIN_LINE_STRIDE);
1199 DUMP_REG(DC_WIN_BUF_STRIDE);
1200 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1201 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1202 DUMP_REG(DC_WIN_DV_CONTROL);
1203 DUMP_REG(DC_WIN_BLEND_NOKEY);
1204 DUMP_REG(DC_WIN_BLEND_1WIN);
1205 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1206 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001207 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001208 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1209 DUMP_REG(DC_WINBUF_START_ADDR);
1210 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1211 DUMP_REG(DC_WINBUF_START_ADDR_U);
1212 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1213 DUMP_REG(DC_WINBUF_START_ADDR_V);
1214 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1215 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1216 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1217 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1218 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1219 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1220 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1221 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1222 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1223
1224#undef DUMP_REG
1225
1226 return 0;
1227}
1228
1229static struct drm_info_list debugfs_files[] = {
1230 { "regs", tegra_dc_show_regs, 0, NULL },
1231};
1232
1233static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1234{
1235 unsigned int i;
1236 char *name;
1237 int err;
1238
1239 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1240 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1241 kfree(name);
1242
1243 if (!dc->debugfs)
1244 return -ENOMEM;
1245
1246 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1247 GFP_KERNEL);
1248 if (!dc->debugfs_files) {
1249 err = -ENOMEM;
1250 goto remove;
1251 }
1252
1253 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1254 dc->debugfs_files[i].data = dc;
1255
1256 err = drm_debugfs_create_files(dc->debugfs_files,
1257 ARRAY_SIZE(debugfs_files),
1258 dc->debugfs, minor);
1259 if (err < 0)
1260 goto free;
1261
1262 dc->minor = minor;
1263
1264 return 0;
1265
1266free:
1267 kfree(dc->debugfs_files);
1268 dc->debugfs_files = NULL;
1269remove:
1270 debugfs_remove(dc->debugfs);
1271 dc->debugfs = NULL;
1272
1273 return err;
1274}
1275
1276static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1277{
1278 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1279 dc->minor);
1280 dc->minor = NULL;
1281
1282 kfree(dc->debugfs_files);
1283 dc->debugfs_files = NULL;
1284
1285 debugfs_remove(dc->debugfs);
1286 dc->debugfs = NULL;
1287
1288 return 0;
1289}
1290
Thierry Reding53fa7f72013-09-24 15:35:40 +02001291static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001292{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001293 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001294 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001295 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001296 int err;
1297
Thierry Redingdf06b752014-06-26 21:41:53 +02001298 if (tegra->domain) {
1299 err = iommu_attach_device(tegra->domain, dc->dev);
1300 if (err < 0) {
1301 dev_err(dc->dev, "failed to attach to domain: %d\n",
1302 err);
1303 return err;
1304 }
1305
1306 dc->domain = tegra->domain;
1307 }
1308
Thierry Reding9910f5c2014-05-22 09:57:15 +02001309 drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001310 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1311 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1312
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001313 /*
1314 * Keep track of the minimum pitch alignment across all display
1315 * controllers.
1316 */
1317 if (dc->soc->pitch_align > tegra->pitch_align)
1318 tegra->pitch_align = dc->soc->pitch_align;
1319
Thierry Reding9910f5c2014-05-22 09:57:15 +02001320 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001321 if (err < 0 && err != -ENODEV) {
1322 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
1323 return err;
1324 }
1325
Thierry Reding9910f5c2014-05-22 09:57:15 +02001326 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001327 if (err < 0)
1328 return err;
1329
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001330 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001331 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001332 if (err < 0)
1333 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1334 }
1335
Thierry Reding6e5ff992012-11-28 11:45:47 +01001336 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001337 dev_name(dc->dev), dc);
1338 if (err < 0) {
1339 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1340 err);
1341 return err;
1342 }
1343
1344 return 0;
1345}
1346
Thierry Reding53fa7f72013-09-24 15:35:40 +02001347static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001348{
Thierry Reding776dc382013-10-14 14:43:22 +02001349 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001350 int err;
1351
1352 devm_free_irq(dc->dev, dc->irq, dc);
1353
1354 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1355 err = tegra_dc_debugfs_exit(dc);
1356 if (err < 0)
1357 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1358 }
1359
1360 err = tegra_dc_rgb_exit(dc);
1361 if (err) {
1362 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1363 return err;
1364 }
1365
Thierry Redingdf06b752014-06-26 21:41:53 +02001366 if (dc->domain) {
1367 iommu_detach_device(dc->domain, dc->dev);
1368 dc->domain = NULL;
1369 }
1370
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001371 return 0;
1372}
1373
1374static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001375 .init = tegra_dc_init,
1376 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001377};
1378
Thierry Reding8620fc62013-12-12 11:03:59 +01001379static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
1380 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001381 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001382 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001383 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001384 .has_powergate = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001385};
1386
1387static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
1388 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001389 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001390 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001391 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001392 .has_powergate = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001393};
1394
1395static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
1396 .supports_interlacing = false,
1397 .supports_cursor = false,
1398 .supports_block_linear = false,
1399 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001400 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001401};
1402
1403static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
1404 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001405 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001406 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001407 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001408 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001409};
1410
1411static const struct of_device_id tegra_dc_of_match[] = {
1412 {
1413 .compatible = "nvidia,tegra124-dc",
1414 .data = &tegra124_dc_soc_info,
1415 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001416 .compatible = "nvidia,tegra114-dc",
1417 .data = &tegra114_dc_soc_info,
1418 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001419 .compatible = "nvidia,tegra30-dc",
1420 .data = &tegra30_dc_soc_info,
1421 }, {
1422 .compatible = "nvidia,tegra20-dc",
1423 .data = &tegra20_dc_soc_info,
1424 }, {
1425 /* sentinel */
1426 }
1427};
Stephen Warrenef707282014-06-18 16:21:55 -06001428MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001429
Thierry Reding13411dd2014-01-09 17:08:36 +01001430static int tegra_dc_parse_dt(struct tegra_dc *dc)
1431{
1432 struct device_node *np;
1433 u32 value = 0;
1434 int err;
1435
1436 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1437 if (err < 0) {
1438 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1439
1440 /*
1441 * If the nvidia,head property isn't present, try to find the
1442 * correct head number by looking up the position of this
1443 * display controller's node within the device tree. Assuming
1444 * that the nodes are ordered properly in the DTS file and
1445 * that the translation into a flattened device tree blob
1446 * preserves that ordering this will actually yield the right
1447 * head number.
1448 *
1449 * If those assumptions don't hold, this will still work for
1450 * cases where only a single display controller is used.
1451 */
1452 for_each_matching_node(np, tegra_dc_of_match) {
1453 if (np == dc->dev->of_node)
1454 break;
1455
1456 value++;
1457 }
1458 }
1459
1460 dc->pipe = value;
1461
1462 return 0;
1463}
1464
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001465static int tegra_dc_probe(struct platform_device *pdev)
1466{
Thierry Reding8620fc62013-12-12 11:03:59 +01001467 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001468 struct resource *regs;
1469 struct tegra_dc *dc;
1470 int err;
1471
1472 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1473 if (!dc)
1474 return -ENOMEM;
1475
Thierry Reding8620fc62013-12-12 11:03:59 +01001476 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1477 if (!id)
1478 return -ENODEV;
1479
Thierry Reding6e5ff992012-11-28 11:45:47 +01001480 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001481 INIT_LIST_HEAD(&dc->list);
1482 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001483 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001484
Thierry Reding13411dd2014-01-09 17:08:36 +01001485 err = tegra_dc_parse_dt(dc);
1486 if (err < 0)
1487 return err;
1488
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001489 dc->clk = devm_clk_get(&pdev->dev, NULL);
1490 if (IS_ERR(dc->clk)) {
1491 dev_err(&pdev->dev, "failed to get clock\n");
1492 return PTR_ERR(dc->clk);
1493 }
1494
Stephen Warrenca480802013-11-06 16:20:54 -07001495 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1496 if (IS_ERR(dc->rst)) {
1497 dev_err(&pdev->dev, "failed to get reset\n");
1498 return PTR_ERR(dc->rst);
1499 }
1500
Thierry Reding9c012702014-07-07 15:32:53 +02001501 if (dc->soc->has_powergate) {
1502 if (dc->pipe == 0)
1503 dc->powergate = TEGRA_POWERGATE_DIS;
1504 else
1505 dc->powergate = TEGRA_POWERGATE_DISB;
1506
1507 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
1508 dc->rst);
1509 if (err < 0) {
1510 dev_err(&pdev->dev, "failed to power partition: %d\n",
1511 err);
1512 return err;
1513 }
1514 } else {
1515 err = clk_prepare_enable(dc->clk);
1516 if (err < 0) {
1517 dev_err(&pdev->dev, "failed to enable clock: %d\n",
1518 err);
1519 return err;
1520 }
1521
1522 err = reset_control_deassert(dc->rst);
1523 if (err < 0) {
1524 dev_err(&pdev->dev, "failed to deassert reset: %d\n",
1525 err);
1526 return err;
1527 }
1528 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001529
1530 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001531 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1532 if (IS_ERR(dc->regs))
1533 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001534
1535 dc->irq = platform_get_irq(pdev, 0);
1536 if (dc->irq < 0) {
1537 dev_err(&pdev->dev, "failed to get IRQ\n");
1538 return -ENXIO;
1539 }
1540
Thierry Reding776dc382013-10-14 14:43:22 +02001541 INIT_LIST_HEAD(&dc->client.list);
1542 dc->client.ops = &dc_client_ops;
1543 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001544
1545 err = tegra_dc_rgb_probe(dc);
1546 if (err < 0 && err != -ENODEV) {
1547 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1548 return err;
1549 }
1550
Thierry Reding776dc382013-10-14 14:43:22 +02001551 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001552 if (err < 0) {
1553 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1554 err);
1555 return err;
1556 }
1557
1558 platform_set_drvdata(pdev, dc);
1559
1560 return 0;
1561}
1562
1563static int tegra_dc_remove(struct platform_device *pdev)
1564{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001565 struct tegra_dc *dc = platform_get_drvdata(pdev);
1566 int err;
1567
Thierry Reding776dc382013-10-14 14:43:22 +02001568 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001569 if (err < 0) {
1570 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1571 err);
1572 return err;
1573 }
1574
Thierry Reding59d29c02013-10-14 14:26:42 +02001575 err = tegra_dc_rgb_remove(dc);
1576 if (err < 0) {
1577 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1578 return err;
1579 }
1580
Thierry Reding5482d752014-07-11 08:39:03 +02001581 reset_control_assert(dc->rst);
Thierry Reding9c012702014-07-07 15:32:53 +02001582
1583 if (dc->soc->has_powergate)
1584 tegra_powergate_power_off(dc->powergate);
1585
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001586 clk_disable_unprepare(dc->clk);
1587
1588 return 0;
1589}
1590
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001591struct platform_driver tegra_dc_driver = {
1592 .driver = {
1593 .name = "tegra-dc",
1594 .owner = THIS_MODULE,
1595 .of_match_table = tegra_dc_of_match,
1596 },
1597 .probe = tegra_dc_probe,
1598 .remove = tegra_dc_remove,
1599};