blob: 2b5e052fb8d393925393d51bac1f28f65eafcc6b [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>
Thierry Redingb9ff7ae2017-08-21 16:35:17 +020013#include <linux/of_device.h>
Thierry Reding33a8eb82015-08-03 13:20:49 +020014#include <linux/pm_runtime.h>
Stephen Warrenca480802013-11-06 16:20:54 -070015#include <linux/reset.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000016
Thierry Reding9c012702014-07-07 15:32:53 +020017#include <soc/tegra/pmc.h>
18
Arto Merilainende2ba662013-03-22 16:34:08 +020019#include "dc.h"
20#include "drm.h"
21#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000022
Thierry Reding9d441892014-11-24 17:02:53 +010023#include <drm/drm_atomic.h>
Thierry Reding4aa3df72014-11-24 16:27:13 +010024#include <drm/drm_atomic_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010025#include <drm/drm_plane_helper.h>
26
Thierry Reding8620fc62013-12-12 11:03:59 +010027struct tegra_dc_soc_info {
Thierry Reding42d06592014-12-08 15:45:39 +010028 bool supports_border_color;
Thierry Reding8620fc62013-12-12 11:03:59 +010029 bool supports_interlacing;
Thierry Redinge6876512013-12-20 13:58:33 +010030 bool supports_cursor;
Thierry Redingc134f012014-06-03 14:48:12 +020031 bool supports_block_linear;
Thierry Redingd1f3e1e2014-07-11 08:29:14 +020032 unsigned int pitch_align;
Thierry Reding9c012702014-07-07 15:32:53 +020033 bool has_powergate;
Dmitry Osipenko6ac15712017-06-15 02:18:29 +030034 bool broken_reset;
Thierry Reding8620fc62013-12-12 11:03:59 +010035};
36
Thierry Redingf34bc782012-11-04 21:47:13 +010037struct tegra_plane {
38 struct drm_plane base;
39 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000040};
41
Thierry Redingf34bc782012-11-04 21:47:13 +010042static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
43{
44 return container_of(plane, struct tegra_plane, base);
45}
46
Thierry Redingca915b12014-12-08 16:14:45 +010047struct tegra_dc_state {
48 struct drm_crtc_state base;
49
50 struct clk *clk;
51 unsigned long pclk;
52 unsigned int div;
Thierry Reding47802b02014-11-26 12:28:39 +010053
54 u32 planes;
Thierry Redingca915b12014-12-08 16:14:45 +010055};
56
57static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
58{
59 if (state)
60 return container_of(state, struct tegra_dc_state, base);
61
62 return NULL;
63}
64
Thierry Reding8f604f82014-11-28 13:14:55 +010065struct tegra_plane_state {
66 struct drm_plane_state base;
67
68 struct tegra_bo_tiling tiling;
69 u32 format;
70 u32 swap;
71};
72
73static inline struct tegra_plane_state *
74to_tegra_plane_state(struct drm_plane_state *state)
75{
76 if (state)
77 return container_of(state, struct tegra_plane_state, base);
78
79 return NULL;
80}
81
Thierry Reding791ddb12015-07-28 21:27:05 +020082static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
83{
84 stats->frames = 0;
85 stats->vblank = 0;
86 stats->underflow = 0;
87 stats->overflow = 0;
88}
89
Thierry Redingd700ba72014-12-08 15:50:04 +010090/*
Thierry Reding86df2562014-12-08 16:03:53 +010091 * Reads the active copy of a register. This takes the dc->lock spinlock to
92 * prevent races with the VBLANK processing which also needs access to the
93 * active copy of some registers.
94 */
95static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
96{
97 unsigned long flags;
98 u32 value;
99
100 spin_lock_irqsave(&dc->lock, flags);
101
102 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
103 value = tegra_dc_readl(dc, offset);
104 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
105
106 spin_unlock_irqrestore(&dc->lock, flags);
107 return value;
108}
109
110/*
Thierry Redingd700ba72014-12-08 15:50:04 +0100111 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
112 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
113 * Latching happens mmediately if the display controller is in STOP mode or
114 * on the next frame boundary otherwise.
115 *
116 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
117 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
118 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
119 * into the ACTIVE copy, either immediately if the display controller is in
120 * STOP mode, or at the next frame boundary otherwise.
121 */
Thierry Reding62b9e062014-11-21 17:33:33 +0100122void tegra_dc_commit(struct tegra_dc *dc)
Thierry Reding205d48e2014-10-21 13:41:46 +0200123{
124 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
125 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
126}
127
Thierry Reding8f604f82014-11-28 13:14:55 +0100128static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
Thierry Reding10288ee2014-03-14 09:54:58 +0100129{
130 /* assume no swapping of fetched data */
131 if (swap)
132 *swap = BYTE_SWAP_NOSWAP;
133
Thierry Reding8f604f82014-11-28 13:14:55 +0100134 switch (fourcc) {
Thierry Reding10288ee2014-03-14 09:54:58 +0100135 case DRM_FORMAT_XBGR8888:
Thierry Reding8f604f82014-11-28 13:14:55 +0100136 *format = WIN_COLOR_DEPTH_R8G8B8A8;
137 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100138
139 case DRM_FORMAT_XRGB8888:
Thierry Reding8f604f82014-11-28 13:14:55 +0100140 *format = WIN_COLOR_DEPTH_B8G8R8A8;
141 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100142
143 case DRM_FORMAT_RGB565:
Thierry Reding8f604f82014-11-28 13:14:55 +0100144 *format = WIN_COLOR_DEPTH_B5G6R5;
145 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100146
147 case DRM_FORMAT_UYVY:
Thierry Reding8f604f82014-11-28 13:14:55 +0100148 *format = WIN_COLOR_DEPTH_YCbCr422;
149 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100150
151 case DRM_FORMAT_YUYV:
152 if (swap)
153 *swap = BYTE_SWAP_SWAP2;
154
Thierry Reding8f604f82014-11-28 13:14:55 +0100155 *format = WIN_COLOR_DEPTH_YCbCr422;
156 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100157
158 case DRM_FORMAT_YUV420:
Thierry Reding8f604f82014-11-28 13:14:55 +0100159 *format = WIN_COLOR_DEPTH_YCbCr420P;
160 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100161
162 case DRM_FORMAT_YUV422:
Thierry Reding8f604f82014-11-28 13:14:55 +0100163 *format = WIN_COLOR_DEPTH_YCbCr422P;
164 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100165
166 default:
Thierry Reding8f604f82014-11-28 13:14:55 +0100167 return -EINVAL;
Thierry Reding10288ee2014-03-14 09:54:58 +0100168 }
169
Thierry Reding8f604f82014-11-28 13:14:55 +0100170 return 0;
Thierry Reding10288ee2014-03-14 09:54:58 +0100171}
172
173static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
174{
175 switch (format) {
176 case WIN_COLOR_DEPTH_YCbCr422:
177 case WIN_COLOR_DEPTH_YUV422:
178 if (planar)
179 *planar = false;
180
181 return true;
182
183 case WIN_COLOR_DEPTH_YCbCr420P:
184 case WIN_COLOR_DEPTH_YUV420P:
185 case WIN_COLOR_DEPTH_YCbCr422P:
186 case WIN_COLOR_DEPTH_YUV422P:
187 case WIN_COLOR_DEPTH_YCbCr422R:
188 case WIN_COLOR_DEPTH_YUV422R:
189 case WIN_COLOR_DEPTH_YCbCr422RA:
190 case WIN_COLOR_DEPTH_YUV422RA:
191 if (planar)
192 *planar = true;
193
194 return true;
195 }
196
Thierry Redingfb35c6b2014-12-08 15:55:28 +0100197 if (planar)
198 *planar = false;
199
Thierry Reding10288ee2014-03-14 09:54:58 +0100200 return false;
201}
202
203static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
204 unsigned int bpp)
205{
206 fixed20_12 outf = dfixed_init(out);
207 fixed20_12 inf = dfixed_init(in);
208 u32 dda_inc;
209 int max;
210
211 if (v)
212 max = 15;
213 else {
214 switch (bpp) {
215 case 2:
216 max = 8;
217 break;
218
219 default:
220 WARN_ON_ONCE(1);
221 /* fallthrough */
222 case 4:
223 max = 4;
224 break;
225 }
226 }
227
228 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
229 inf.full -= dfixed_const(1);
230
231 dda_inc = dfixed_div(inf, outf);
232 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
233
234 return dda_inc;
235}
236
237static inline u32 compute_initial_dda(unsigned int in)
238{
239 fixed20_12 inf = dfixed_init(in);
240 return dfixed_frac(inf);
241}
242
Thierry Reding4aa3df72014-11-24 16:27:13 +0100243static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
244 const struct tegra_dc_window *window)
Thierry Reding10288ee2014-03-14 09:54:58 +0100245{
246 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
Sean Paul93396d02014-11-19 13:04:49 -0500247 unsigned long value, flags;
Thierry Reding10288ee2014-03-14 09:54:58 +0100248 bool yuv, planar;
249
250 /*
251 * For YUV planar modes, the number of bytes per pixel takes into
252 * account only the luma component and therefore is 1.
253 */
254 yuv = tegra_dc_format_is_yuv(window->format, &planar);
255 if (!yuv)
256 bpp = window->bits_per_pixel / 8;
257 else
258 bpp = planar ? 1 : 2;
259
Sean Paul93396d02014-11-19 13:04:49 -0500260 spin_lock_irqsave(&dc->lock, flags);
261
Thierry Reding10288ee2014-03-14 09:54:58 +0100262 value = WINDOW_A_SELECT << index;
263 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
264
265 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
266 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
267
268 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
269 tegra_dc_writel(dc, value, DC_WIN_POSITION);
270
271 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
272 tegra_dc_writel(dc, value, DC_WIN_SIZE);
273
274 h_offset = window->src.x * bpp;
275 v_offset = window->src.y;
276 h_size = window->src.w * bpp;
277 v_size = window->src.h;
278
279 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
280 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
281
282 /*
283 * For DDA computations the number of bytes per pixel for YUV planar
284 * modes needs to take into account all Y, U and V components.
285 */
286 if (yuv && planar)
287 bpp = 2;
288
289 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
290 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
291
292 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
293 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
294
295 h_dda = compute_initial_dda(window->src.x);
296 v_dda = compute_initial_dda(window->src.y);
297
298 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
299 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
300
301 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
302 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
303
304 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
305
306 if (yuv && planar) {
307 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
308 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
309 value = window->stride[1] << 16 | window->stride[0];
310 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
311 } else {
312 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
313 }
314
315 if (window->bottom_up)
316 v_offset += window->src.h - 1;
317
318 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
319 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
320
Thierry Redingc134f012014-06-03 14:48:12 +0200321 if (dc->soc->supports_block_linear) {
322 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100323
Thierry Redingc134f012014-06-03 14:48:12 +0200324 switch (window->tiling.mode) {
325 case TEGRA_BO_TILING_MODE_PITCH:
326 value = DC_WINBUF_SURFACE_KIND_PITCH;
327 break;
328
329 case TEGRA_BO_TILING_MODE_TILED:
330 value = DC_WINBUF_SURFACE_KIND_TILED;
331 break;
332
333 case TEGRA_BO_TILING_MODE_BLOCK:
334 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
335 DC_WINBUF_SURFACE_KIND_BLOCK;
336 break;
337 }
338
339 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
340 } else {
341 switch (window->tiling.mode) {
342 case TEGRA_BO_TILING_MODE_PITCH:
343 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
344 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
345 break;
346
347 case TEGRA_BO_TILING_MODE_TILED:
348 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
349 DC_WIN_BUFFER_ADDR_MODE_TILE;
350 break;
351
352 case TEGRA_BO_TILING_MODE_BLOCK:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100353 /*
354 * No need to handle this here because ->atomic_check
355 * will already have filtered it out.
356 */
357 break;
Thierry Redingc134f012014-06-03 14:48:12 +0200358 }
359
360 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
361 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100362
363 value = WIN_ENABLE;
364
365 if (yuv) {
366 /* setup default colorspace conversion coefficients */
367 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
368 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
369 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
370 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
371 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
372 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
373 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
374 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
375
376 value |= CSC_ENABLE;
377 } else if (window->bits_per_pixel < 24) {
378 value |= COLOR_EXPAND;
379 }
380
381 if (window->bottom_up)
382 value |= V_DIRECTION;
383
384 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
385
386 /*
387 * Disable blending and assume Window A is the bottom-most window,
388 * Window C is the top-most window and Window B is in the middle.
389 */
390 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
391 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
392
393 switch (index) {
394 case 0:
395 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
396 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
397 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
398 break;
399
400 case 1:
401 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
402 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
403 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
404 break;
405
406 case 2:
407 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
408 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
409 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
410 break;
411 }
412
Sean Paul93396d02014-11-19 13:04:49 -0500413 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc7679302014-10-21 13:51:53 +0200414}
415
416static void tegra_plane_destroy(struct drm_plane *plane)
417{
418 struct tegra_plane *p = to_tegra_plane(plane);
419
420 drm_plane_cleanup(plane);
421 kfree(p);
422}
423
424static const u32 tegra_primary_plane_formats[] = {
425 DRM_FORMAT_XBGR8888,
426 DRM_FORMAT_XRGB8888,
427 DRM_FORMAT_RGB565,
428};
429
Thierry Reding4aa3df72014-11-24 16:27:13 +0100430static void tegra_primary_plane_destroy(struct drm_plane *plane)
Thierry Redingc7679302014-10-21 13:51:53 +0200431{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100432 tegra_plane_destroy(plane);
433}
434
Thierry Reding8f604f82014-11-28 13:14:55 +0100435static void tegra_plane_reset(struct drm_plane *plane)
436{
437 struct tegra_plane_state *state;
438
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100439 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +0200440 __drm_atomic_helper_plane_destroy_state(plane->state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100441
442 kfree(plane->state);
443 plane->state = NULL;
444
445 state = kzalloc(sizeof(*state), GFP_KERNEL);
446 if (state) {
447 plane->state = &state->base;
448 plane->state->plane = plane;
449 }
450}
451
452static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
453{
454 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
455 struct tegra_plane_state *copy;
456
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100457 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Reding8f604f82014-11-28 13:14:55 +0100458 if (!copy)
459 return NULL;
460
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100461 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
462 copy->tiling = state->tiling;
463 copy->format = state->format;
464 copy->swap = state->swap;
Thierry Reding8f604f82014-11-28 13:14:55 +0100465
466 return &copy->base;
467}
468
469static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
470 struct drm_plane_state *state)
471{
Daniel Vetter2f701692016-05-09 16:34:10 +0200472 __drm_atomic_helper_plane_destroy_state(state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100473 kfree(state);
474}
475
Thierry Reding4aa3df72014-11-24 16:27:13 +0100476static const struct drm_plane_funcs tegra_primary_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100477 .update_plane = drm_atomic_helper_update_plane,
478 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100479 .destroy = tegra_primary_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100480 .reset = tegra_plane_reset,
481 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
482 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100483};
484
Thierry Reding47802b02014-11-26 12:28:39 +0100485static int tegra_plane_state_add(struct tegra_plane *plane,
486 struct drm_plane_state *state)
487{
488 struct drm_crtc_state *crtc_state;
489 struct tegra_dc_state *tegra;
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300490 struct drm_rect clip;
491 int err;
Thierry Reding47802b02014-11-26 12:28:39 +0100492
493 /* Propagate errors from allocation or locking failures. */
494 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
495 if (IS_ERR(crtc_state))
496 return PTR_ERR(crtc_state);
497
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300498 clip.x1 = 0;
499 clip.y1 = 0;
500 clip.x2 = crtc_state->mode.hdisplay;
501 clip.y2 = crtc_state->mode.vdisplay;
502
503 /* Check plane state for visibility and calculate clipping bounds */
504 err = drm_plane_helper_check_state(state, &clip, 0, INT_MAX,
505 true, true);
506 if (err < 0)
507 return err;
508
Thierry Reding47802b02014-11-26 12:28:39 +0100509 tegra = to_dc_state(crtc_state);
510
511 tegra->planes |= WIN_A_ACT_REQ << plane->index;
512
513 return 0;
514}
515
Thierry Reding4aa3df72014-11-24 16:27:13 +0100516static int tegra_plane_atomic_check(struct drm_plane *plane,
517 struct drm_plane_state *state)
518{
Thierry Reding8f604f82014-11-28 13:14:55 +0100519 struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
520 struct tegra_bo_tiling *tiling = &plane_state->tiling;
Thierry Reding47802b02014-11-26 12:28:39 +0100521 struct tegra_plane *tegra = to_tegra_plane(plane);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100522 struct tegra_dc *dc = to_tegra_dc(state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200523 int err;
524
Thierry Reding4aa3df72014-11-24 16:27:13 +0100525 /* no need for further checks if the plane is being disabled */
526 if (!state->crtc)
527 return 0;
528
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200529 err = tegra_dc_format(state->fb->format->format, &plane_state->format,
Thierry Reding8f604f82014-11-28 13:14:55 +0100530 &plane_state->swap);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100531 if (err < 0)
532 return err;
533
Thierry Reding8f604f82014-11-28 13:14:55 +0100534 err = tegra_fb_get_tiling(state->fb, tiling);
535 if (err < 0)
536 return err;
537
538 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
Thierry Reding4aa3df72014-11-24 16:27:13 +0100539 !dc->soc->supports_block_linear) {
540 DRM_ERROR("hardware doesn't support block linear mode\n");
541 return -EINVAL;
542 }
543
544 /*
545 * Tegra doesn't support different strides for U and V planes so we
546 * error out if the user tries to display a framebuffer with such a
547 * configuration.
548 */
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200549 if (state->fb->format->num_planes > 2) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100550 if (state->fb->pitches[2] != state->fb->pitches[1]) {
551 DRM_ERROR("unsupported UV-plane configuration\n");
552 return -EINVAL;
553 }
554 }
555
Thierry Reding47802b02014-11-26 12:28:39 +0100556 err = tegra_plane_state_add(tegra, state);
557 if (err < 0)
558 return err;
559
Thierry Reding4aa3df72014-11-24 16:27:13 +0100560 return 0;
561}
562
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300563static void tegra_dc_disable_window(struct tegra_dc *dc, int index)
564{
565 unsigned long flags;
566 u32 value;
567
568 spin_lock_irqsave(&dc->lock, flags);
569
570 value = WINDOW_A_SELECT << index;
571 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
572
573 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
574 value &= ~WIN_ENABLE;
575 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
576
577 spin_unlock_irqrestore(&dc->lock, flags);
578}
579
Thierry Reding4aa3df72014-11-24 16:27:13 +0100580static void tegra_plane_atomic_update(struct drm_plane *plane,
581 struct drm_plane_state *old_state)
582{
Thierry Reding8f604f82014-11-28 13:14:55 +0100583 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100584 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
585 struct drm_framebuffer *fb = plane->state->fb;
586 struct tegra_plane *p = to_tegra_plane(plane);
587 struct tegra_dc_window window;
588 unsigned int i;
Thierry Reding4aa3df72014-11-24 16:27:13 +0100589
590 /* rien ne va plus */
591 if (!plane->state->crtc || !plane->state->fb)
592 return;
593
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300594 if (!plane->state->visible)
595 return tegra_dc_disable_window(dc, p->index);
596
Thierry Redingc7679302014-10-21 13:51:53 +0200597 memset(&window, 0, sizeof(window));
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300598 window.src.x = plane->state->src.x1 >> 16;
599 window.src.y = plane->state->src.y1 >> 16;
600 window.src.w = drm_rect_width(&plane->state->src) >> 16;
601 window.src.h = drm_rect_height(&plane->state->src) >> 16;
602 window.dst.x = plane->state->dst.x1;
603 window.dst.y = plane->state->dst.y1;
604 window.dst.w = drm_rect_width(&plane->state->dst);
605 window.dst.h = drm_rect_height(&plane->state->dst);
Ville Syrjälä272725c2016-12-14 23:32:20 +0200606 window.bits_per_pixel = fb->format->cpp[0] * 8;
Thierry Redingc7679302014-10-21 13:51:53 +0200607 window.bottom_up = tegra_fb_is_bottom_up(fb);
608
Thierry Reding8f604f82014-11-28 13:14:55 +0100609 /* copy from state */
610 window.tiling = state->tiling;
611 window.format = state->format;
612 window.swap = state->swap;
Thierry Redingc7679302014-10-21 13:51:53 +0200613
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200614 for (i = 0; i < fb->format->num_planes; i++) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100615 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingc7679302014-10-21 13:51:53 +0200616
Thierry Reding4aa3df72014-11-24 16:27:13 +0100617 window.base[i] = bo->paddr + fb->offsets[i];
Dmitry Osipenko08ee0172016-08-21 11:57:58 +0300618
619 /*
620 * Tegra uses a shared stride for UV planes. Framebuffers are
621 * already checked for this in the tegra_plane_atomic_check()
622 * function, so it's safe to ignore the V-plane pitch here.
623 */
624 if (i < 2)
625 window.stride[i] = fb->pitches[i];
Thierry Reding4aa3df72014-11-24 16:27:13 +0100626 }
Thierry Redingc7679302014-10-21 13:51:53 +0200627
Thierry Reding4aa3df72014-11-24 16:27:13 +0100628 tegra_dc_setup_window(dc, p->index, &window);
Thierry Redingc7679302014-10-21 13:51:53 +0200629}
630
Thierry Reding4aa3df72014-11-24 16:27:13 +0100631static void tegra_plane_atomic_disable(struct drm_plane *plane,
632 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200633{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100634 struct tegra_plane *p = to_tegra_plane(plane);
635 struct tegra_dc *dc;
Thierry Reding4aa3df72014-11-24 16:27:13 +0100636
637 /* rien ne va plus */
638 if (!old_state || !old_state->crtc)
639 return;
640
641 dc = to_tegra_dc(old_state->crtc);
642
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300643 tegra_dc_disable_window(dc, p->index);
Thierry Redingc7679302014-10-21 13:51:53 +0200644}
645
Thierry Reding4aa3df72014-11-24 16:27:13 +0100646static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100647 .atomic_check = tegra_plane_atomic_check,
648 .atomic_update = tegra_plane_atomic_update,
649 .atomic_disable = tegra_plane_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200650};
651
652static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
653 struct tegra_dc *dc)
654{
Thierry Reding518e6222014-12-16 18:04:08 +0100655 /*
656 * Ideally this would use drm_crtc_mask(), but that would require the
657 * CRTC to already be in the mode_config's list of CRTCs. However, it
658 * will only be added to that list in the drm_crtc_init_with_planes()
659 * (in tegra_dc_init()), which in turn requires registration of these
660 * planes. So we have ourselves a nice little chicken and egg problem
661 * here.
662 *
663 * We work around this by manually creating the mask from the number
664 * of CRTCs that have been registered, and should therefore always be
665 * the same as drm_crtc_index() after registration.
666 */
667 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Redingc7679302014-10-21 13:51:53 +0200668 struct tegra_plane *plane;
669 unsigned int num_formats;
670 const u32 *formats;
671 int err;
672
673 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
674 if (!plane)
675 return ERR_PTR(-ENOMEM);
676
677 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
678 formats = tegra_primary_plane_formats;
679
Thierry Reding518e6222014-12-16 18:04:08 +0100680 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc7679302014-10-21 13:51:53 +0200681 &tegra_primary_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700682 num_formats, NULL,
683 DRM_PLANE_TYPE_PRIMARY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200684 if (err < 0) {
685 kfree(plane);
686 return ERR_PTR(err);
687 }
688
Thierry Reding4aa3df72014-11-24 16:27:13 +0100689 drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
690
Thierry Redingc7679302014-10-21 13:51:53 +0200691 return &plane->base;
692}
693
694static const u32 tegra_cursor_plane_formats[] = {
695 DRM_FORMAT_RGBA8888,
696};
697
Thierry Reding4aa3df72014-11-24 16:27:13 +0100698static int tegra_cursor_atomic_check(struct drm_plane *plane,
699 struct drm_plane_state *state)
Thierry Redingc7679302014-10-21 13:51:53 +0200700{
Thierry Reding47802b02014-11-26 12:28:39 +0100701 struct tegra_plane *tegra = to_tegra_plane(plane);
702 int err;
703
Thierry Reding4aa3df72014-11-24 16:27:13 +0100704 /* no need for further checks if the plane is being disabled */
705 if (!state->crtc)
706 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200707
708 /* scaling not supported for cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100709 if ((state->src_w >> 16 != state->crtc_w) ||
710 (state->src_h >> 16 != state->crtc_h))
Thierry Redingc7679302014-10-21 13:51:53 +0200711 return -EINVAL;
712
713 /* only square cursors supported */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100714 if (state->src_w != state->src_h)
Thierry Redingc7679302014-10-21 13:51:53 +0200715 return -EINVAL;
716
Thierry Reding4aa3df72014-11-24 16:27:13 +0100717 if (state->crtc_w != 32 && state->crtc_w != 64 &&
718 state->crtc_w != 128 && state->crtc_w != 256)
719 return -EINVAL;
720
Thierry Reding47802b02014-11-26 12:28:39 +0100721 err = tegra_plane_state_add(tegra, state);
722 if (err < 0)
723 return err;
724
Thierry Reding4aa3df72014-11-24 16:27:13 +0100725 return 0;
726}
727
728static void tegra_cursor_atomic_update(struct drm_plane *plane,
729 struct drm_plane_state *old_state)
730{
731 struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
732 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
733 struct drm_plane_state *state = plane->state;
734 u32 value = CURSOR_CLIP_DISPLAY;
735
736 /* rien ne va plus */
737 if (!plane->state->crtc || !plane->state->fb)
738 return;
739
740 switch (state->crtc_w) {
Thierry Redingc7679302014-10-21 13:51:53 +0200741 case 32:
742 value |= CURSOR_SIZE_32x32;
743 break;
744
745 case 64:
746 value |= CURSOR_SIZE_64x64;
747 break;
748
749 case 128:
750 value |= CURSOR_SIZE_128x128;
751 break;
752
753 case 256:
754 value |= CURSOR_SIZE_256x256;
755 break;
756
757 default:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100758 WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
759 state->crtc_h);
760 return;
Thierry Redingc7679302014-10-21 13:51:53 +0200761 }
762
763 value |= (bo->paddr >> 10) & 0x3fffff;
764 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
765
766#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
767 value = (bo->paddr >> 32) & 0x3;
768 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
769#endif
770
771 /* enable cursor and set blend mode */
772 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
773 value |= CURSOR_ENABLE;
774 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
775
776 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
777 value &= ~CURSOR_DST_BLEND_MASK;
778 value &= ~CURSOR_SRC_BLEND_MASK;
779 value |= CURSOR_MODE_NORMAL;
780 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
781 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
782 value |= CURSOR_ALPHA;
783 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
784
785 /* position the cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100786 value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
Thierry Redingc7679302014-10-21 13:51:53 +0200787 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
Thierry Redingc7679302014-10-21 13:51:53 +0200788}
789
Thierry Reding4aa3df72014-11-24 16:27:13 +0100790static void tegra_cursor_atomic_disable(struct drm_plane *plane,
791 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200792{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100793 struct tegra_dc *dc;
Thierry Redingc7679302014-10-21 13:51:53 +0200794 u32 value;
795
Thierry Reding4aa3df72014-11-24 16:27:13 +0100796 /* rien ne va plus */
797 if (!old_state || !old_state->crtc)
798 return;
799
800 dc = to_tegra_dc(old_state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200801
802 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
803 value &= ~CURSOR_ENABLE;
804 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc7679302014-10-21 13:51:53 +0200805}
806
807static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100808 .update_plane = drm_atomic_helper_update_plane,
809 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Redingc7679302014-10-21 13:51:53 +0200810 .destroy = tegra_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100811 .reset = tegra_plane_reset,
812 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
813 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100814};
815
816static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100817 .atomic_check = tegra_cursor_atomic_check,
818 .atomic_update = tegra_cursor_atomic_update,
819 .atomic_disable = tegra_cursor_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200820};
821
822static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
823 struct tegra_dc *dc)
824{
825 struct tegra_plane *plane;
826 unsigned int num_formats;
827 const u32 *formats;
828 int err;
829
830 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
831 if (!plane)
832 return ERR_PTR(-ENOMEM);
833
Thierry Reding47802b02014-11-26 12:28:39 +0100834 /*
Thierry Redinga1df3b22015-07-21 16:42:30 +0200835 * This index is kind of fake. The cursor isn't a regular plane, but
836 * its update and activation request bits in DC_CMD_STATE_CONTROL do
837 * use the same programming. Setting this fake index here allows the
838 * code in tegra_add_plane_state() to do the right thing without the
839 * need to special-casing the cursor plane.
Thierry Reding47802b02014-11-26 12:28:39 +0100840 */
841 plane->index = 6;
842
Thierry Redingc7679302014-10-21 13:51:53 +0200843 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
844 formats = tegra_cursor_plane_formats;
845
846 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
847 &tegra_cursor_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700848 num_formats, NULL,
849 DRM_PLANE_TYPE_CURSOR, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200850 if (err < 0) {
851 kfree(plane);
852 return ERR_PTR(err);
853 }
854
Thierry Reding4aa3df72014-11-24 16:27:13 +0100855 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
856
Thierry Redingc7679302014-10-21 13:51:53 +0200857 return &plane->base;
858}
859
Thierry Redingc7679302014-10-21 13:51:53 +0200860static void tegra_overlay_plane_destroy(struct drm_plane *plane)
Thierry Redingf34bc782012-11-04 21:47:13 +0100861{
Thierry Redingc7679302014-10-21 13:51:53 +0200862 tegra_plane_destroy(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100863}
864
Thierry Redingc7679302014-10-21 13:51:53 +0200865static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100866 .update_plane = drm_atomic_helper_update_plane,
867 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Redingc7679302014-10-21 13:51:53 +0200868 .destroy = tegra_overlay_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100869 .reset = tegra_plane_reset,
870 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
871 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Redingf34bc782012-11-04 21:47:13 +0100872};
873
Thierry Redingc7679302014-10-21 13:51:53 +0200874static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100875 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100876 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100877 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100878 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100879 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100880 DRM_FORMAT_YUV420,
881 DRM_FORMAT_YUV422,
882};
883
Thierry Reding4aa3df72014-11-24 16:27:13 +0100884static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100885 .atomic_check = tegra_plane_atomic_check,
886 .atomic_update = tegra_plane_atomic_update,
887 .atomic_disable = tegra_plane_atomic_disable,
888};
889
Thierry Redingc7679302014-10-21 13:51:53 +0200890static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
891 struct tegra_dc *dc,
892 unsigned int index)
893{
894 struct tegra_plane *plane;
895 unsigned int num_formats;
896 const u32 *formats;
897 int err;
898
899 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
900 if (!plane)
901 return ERR_PTR(-ENOMEM);
902
903 plane->index = index;
904
905 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
906 formats = tegra_overlay_plane_formats;
907
908 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
909 &tegra_overlay_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700910 num_formats, NULL,
911 DRM_PLANE_TYPE_OVERLAY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200912 if (err < 0) {
913 kfree(plane);
914 return ERR_PTR(err);
915 }
916
Thierry Reding4aa3df72014-11-24 16:27:13 +0100917 drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
918
Thierry Redingc7679302014-10-21 13:51:53 +0200919 return &plane->base;
920}
921
Thierry Redingf34bc782012-11-04 21:47:13 +0100922static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
923{
Thierry Redingc7679302014-10-21 13:51:53 +0200924 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100925 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100926
927 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200928 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
929 if (IS_ERR(plane))
930 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100931 }
932
933 return 0;
934}
935
Shawn Guo10437d92017-02-07 17:16:32 +0800936static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
Thierry Reding42e9ce02015-01-28 14:43:05 +0100937{
Shawn Guo10437d92017-02-07 17:16:32 +0800938 struct tegra_dc *dc = to_tegra_dc(crtc);
939
Thierry Reding42e9ce02015-01-28 14:43:05 +0100940 if (dc->syncpt)
941 return host1x_syncpt_read(dc->syncpt);
942
943 /* fallback to software emulated VBLANK counter */
944 return drm_crtc_vblank_count(&dc->base);
945}
946
Shawn Guo10437d92017-02-07 17:16:32 +0800947static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
Thierry Reding6e5ff992012-11-28 11:45:47 +0100948{
Shawn Guo10437d92017-02-07 17:16:32 +0800949 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding6e5ff992012-11-28 11:45:47 +0100950 unsigned long value, flags;
951
952 spin_lock_irqsave(&dc->lock, flags);
953
954 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
955 value |= VBLANK_INT;
956 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
957
958 spin_unlock_irqrestore(&dc->lock, flags);
Shawn Guo10437d92017-02-07 17:16:32 +0800959
960 return 0;
Thierry Reding6e5ff992012-11-28 11:45:47 +0100961}
962
Shawn Guo10437d92017-02-07 17:16:32 +0800963static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
Thierry Reding6e5ff992012-11-28 11:45:47 +0100964{
Shawn Guo10437d92017-02-07 17:16:32 +0800965 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding6e5ff992012-11-28 11:45:47 +0100966 unsigned long value, flags;
967
968 spin_lock_irqsave(&dc->lock, flags);
969
970 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
971 value &= ~VBLANK_INT;
972 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
973
974 spin_unlock_irqrestore(&dc->lock, flags);
975}
976
Thierry Reding3c03c462012-11-28 12:00:18 +0100977static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
978{
979 struct drm_device *drm = dc->base.dev;
980 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100981 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200982 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100983
Thierry Reding6b59cc12014-12-16 16:33:27 +0100984 spin_lock_irqsave(&drm->event_lock, flags);
985
986 if (!dc->event) {
987 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +0100988 return;
Thierry Reding6b59cc12014-12-16 16:33:27 +0100989 }
Thierry Reding3c03c462012-11-28 12:00:18 +0100990
Matt Roperf4510a22014-04-01 15:22:40 -0700991 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100992
Dan Carpenter8643bc62015-01-07 14:01:26 +0300993 spin_lock(&dc->lock);
Sean Paul93396d02014-11-19 13:04:49 -0500994
Thierry Reding3c03c462012-11-28 12:00:18 +0100995 /* check if new start address has been latched */
Sean Paul93396d02014-11-19 13:04:49 -0500996 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
Thierry Reding3c03c462012-11-28 12:00:18 +0100997 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
998 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
999 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
1000
Dan Carpenter8643bc62015-01-07 14:01:26 +03001001 spin_unlock(&dc->lock);
Sean Paul93396d02014-11-19 13:04:49 -05001002
Matt Roperf4510a22014-04-01 15:22:40 -07001003 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Redinged7dae52014-12-16 16:03:13 +01001004 drm_crtc_send_vblank_event(crtc, dc->event);
1005 drm_crtc_vblank_put(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +01001006 dc->event = NULL;
Thierry Reding3c03c462012-11-28 12:00:18 +01001007 }
Thierry Reding6b59cc12014-12-16 16:33:27 +01001008
1009 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +01001010}
1011
Thierry Redingf002abc2013-10-14 14:06:02 +02001012static void tegra_dc_destroy(struct drm_crtc *crtc)
1013{
1014 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +02001015}
1016
Thierry Redingca915b12014-12-08 16:14:45 +01001017static void tegra_crtc_reset(struct drm_crtc *crtc)
1018{
1019 struct tegra_dc_state *state;
1020
Thierry Reding3b59b7ac2015-01-28 15:01:22 +01001021 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02001022 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Thierry Reding3b59b7ac2015-01-28 15:01:22 +01001023
Thierry Redingca915b12014-12-08 16:14:45 +01001024 kfree(crtc->state);
1025 crtc->state = NULL;
1026
1027 state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Reding332bbe72015-01-28 15:03:31 +01001028 if (state) {
Thierry Redingca915b12014-12-08 16:14:45 +01001029 crtc->state = &state->base;
Thierry Reding332bbe72015-01-28 15:03:31 +01001030 crtc->state->crtc = crtc;
1031 }
Thierry Reding31930d42015-07-02 17:04:06 +02001032
1033 drm_crtc_vblank_reset(crtc);
Thierry Redingca915b12014-12-08 16:14:45 +01001034}
1035
1036static struct drm_crtc_state *
1037tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1038{
1039 struct tegra_dc_state *state = to_dc_state(crtc->state);
1040 struct tegra_dc_state *copy;
1041
Thierry Reding3b59b7ac2015-01-28 15:01:22 +01001042 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Redingca915b12014-12-08 16:14:45 +01001043 if (!copy)
1044 return NULL;
1045
Thierry Reding3b59b7ac2015-01-28 15:01:22 +01001046 __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
1047 copy->clk = state->clk;
1048 copy->pclk = state->pclk;
1049 copy->div = state->div;
1050 copy->planes = state->planes;
Thierry Redingca915b12014-12-08 16:14:45 +01001051
1052 return &copy->base;
1053}
1054
1055static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1056 struct drm_crtc_state *state)
1057{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02001058 __drm_atomic_helper_crtc_destroy_state(state);
Thierry Redingca915b12014-12-08 16:14:45 +01001059 kfree(state);
1060}
1061
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001062static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding1503ca42014-11-24 17:41:23 +01001063 .page_flip = drm_atomic_helper_page_flip,
Thierry Reding74f48792014-11-24 17:08:20 +01001064 .set_config = drm_atomic_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +02001065 .destroy = tegra_dc_destroy,
Thierry Redingca915b12014-12-08 16:14:45 +01001066 .reset = tegra_crtc_reset,
1067 .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1068 .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
Shawn Guo10437d92017-02-07 17:16:32 +08001069 .get_vblank_counter = tegra_dc_get_vblank_counter,
1070 .enable_vblank = tegra_dc_enable_vblank,
1071 .disable_vblank = tegra_dc_disable_vblank,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001072};
1073
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001074static int tegra_dc_set_timings(struct tegra_dc *dc,
1075 struct drm_display_mode *mode)
1076{
Thierry Reding0444c0f2014-04-16 09:22:38 +02001077 unsigned int h_ref_to_sync = 1;
1078 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001079 unsigned long value;
1080
1081 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1082
1083 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1084 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1085
1086 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1087 ((mode->hsync_end - mode->hsync_start) << 0);
1088 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1089
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001090 value = ((mode->vtotal - mode->vsync_end) << 16) |
1091 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +00001092 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1093
1094 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1095 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001096 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1097
1098 value = (mode->vdisplay << 16) | mode->hdisplay;
1099 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1100
1101 return 0;
1102}
1103
Thierry Reding9d910b62015-01-28 15:25:54 +01001104/**
1105 * tegra_dc_state_setup_clock - check clock settings and store them in atomic
1106 * state
1107 * @dc: display controller
1108 * @crtc_state: CRTC atomic state
1109 * @clk: parent clock for display controller
1110 * @pclk: pixel clock
1111 * @div: shift clock divider
1112 *
1113 * Returns:
1114 * 0 on success or a negative error-code on failure.
1115 */
Thierry Redingca915b12014-12-08 16:14:45 +01001116int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1117 struct drm_crtc_state *crtc_state,
1118 struct clk *clk, unsigned long pclk,
1119 unsigned int div)
1120{
1121 struct tegra_dc_state *state = to_dc_state(crtc_state);
1122
Thierry Redingd2982742015-01-22 08:48:25 +01001123 if (!clk_has_parent(dc->clk, clk))
1124 return -EINVAL;
1125
Thierry Redingca915b12014-12-08 16:14:45 +01001126 state->clk = clk;
1127 state->pclk = pclk;
1128 state->div = div;
1129
1130 return 0;
1131}
1132
Thierry Reding76d59ed2014-12-19 15:09:16 +01001133static void tegra_dc_commit_state(struct tegra_dc *dc,
1134 struct tegra_dc_state *state)
1135{
1136 u32 value;
1137 int err;
1138
1139 err = clk_set_parent(dc->clk, state->clk);
1140 if (err < 0)
1141 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1142
1143 /*
1144 * Outputs may not want to change the parent clock rate. This is only
1145 * relevant to Tegra20 where only a single display PLL is available.
1146 * Since that PLL would typically be used for HDMI, an internal LVDS
1147 * panel would need to be driven by some other clock such as PLL_P
1148 * which is shared with other peripherals. Changing the clock rate
1149 * should therefore be avoided.
1150 */
1151 if (state->pclk > 0) {
1152 err = clk_set_rate(state->clk, state->pclk);
1153 if (err < 0)
1154 dev_err(dc->dev,
1155 "failed to set clock rate to %lu Hz\n",
1156 state->pclk);
1157 }
1158
1159 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
1160 state->div);
1161 DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
1162
1163 value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
1164 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1165}
1166
Thierry Reding003fc842015-08-03 13:16:26 +02001167static void tegra_dc_stop(struct tegra_dc *dc)
1168{
1169 u32 value;
1170
1171 /* stop the display controller */
1172 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1173 value &= ~DISP_CTRL_MODE_MASK;
1174 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1175
1176 tegra_dc_commit(dc);
1177}
1178
1179static bool tegra_dc_idle(struct tegra_dc *dc)
1180{
1181 u32 value;
1182
1183 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1184
1185 return (value & DISP_CTRL_MODE_MASK) == 0;
1186}
1187
1188static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1189{
1190 timeout = jiffies + msecs_to_jiffies(timeout);
1191
1192 while (time_before(jiffies, timeout)) {
1193 if (tegra_dc_idle(dc))
1194 return 0;
1195
1196 usleep_range(1000, 2000);
1197 }
1198
1199 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1200 return -ETIMEDOUT;
1201}
1202
Laurent Pinchart64581712017-06-30 12:36:45 +03001203static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1204 struct drm_crtc_state *old_state)
Thierry Reding003fc842015-08-03 13:16:26 +02001205{
1206 struct tegra_dc *dc = to_tegra_dc(crtc);
1207 u32 value;
1208
1209 if (!tegra_dc_idle(dc)) {
1210 tegra_dc_stop(dc);
1211
1212 /*
1213 * Ignore the return value, there isn't anything useful to do
1214 * in case this fails.
1215 */
1216 tegra_dc_wait_idle(dc, 100);
1217 }
1218
1219 /*
1220 * This should really be part of the RGB encoder driver, but clearing
1221 * these bits has the side-effect of stopping the display controller.
1222 * When that happens no VBLANK interrupts will be raised. At the same
1223 * time the encoder is disabled before the display controller, so the
1224 * above code is always going to timeout waiting for the controller
1225 * to go idle.
1226 *
1227 * Given the close coupling between the RGB encoder and the display
1228 * controller doing it here is still kind of okay. None of the other
1229 * encoder drivers require these bits to be cleared.
1230 *
1231 * XXX: Perhaps given that the display controller is switched off at
1232 * this point anyway maybe clearing these bits isn't even useful for
1233 * the RGB encoder?
1234 */
1235 if (dc->rgb) {
1236 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1237 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1238 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1239 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1240 }
1241
1242 tegra_dc_stats_reset(&dc->stats);
1243 drm_crtc_vblank_off(crtc);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001244
1245 pm_runtime_put_sync(dc->dev);
Thierry Reding003fc842015-08-03 13:16:26 +02001246}
1247
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001248static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1249 struct drm_crtc_state *old_state)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001250{
Thierry Reding4aa3df72014-11-24 16:27:13 +01001251 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Thierry Reding76d59ed2014-12-19 15:09:16 +01001252 struct tegra_dc_state *state = to_dc_state(crtc->state);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001253 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001254 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001255
Thierry Reding33a8eb82015-08-03 13:20:49 +02001256 pm_runtime_get_sync(dc->dev);
1257
1258 /* initialize display controller */
1259 if (dc->syncpt) {
1260 u32 syncpt = host1x_syncpt_id(dc->syncpt);
1261
1262 value = SYNCPT_CNTRL_NO_STALL;
1263 tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1264
1265 value = SYNCPT_VSYNC_ENABLE | syncpt;
1266 tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
1267 }
1268
1269 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1270 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1271 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1272
1273 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1274 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1275 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1276
1277 /* initialize timer */
1278 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1279 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1280 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1281
1282 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1283 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1284 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1285
1286 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1287 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1288 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1289
1290 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1291 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1292 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1293
1294 if (dc->soc->supports_border_color)
1295 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1296
1297 /* apply PLL and pixel clock changes */
Thierry Reding76d59ed2014-12-19 15:09:16 +01001298 tegra_dc_commit_state(dc, state);
1299
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001300 /* program display mode */
1301 tegra_dc_set_timings(dc, mode);
1302
Thierry Reding8620fc62013-12-12 11:03:59 +01001303 /* interlacing isn't supported yet, so disable it */
1304 if (dc->soc->supports_interlacing) {
1305 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1306 value &= ~INTERLACE_ENABLE;
1307 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1308 }
Thierry Reding666cb872014-12-08 16:32:47 +01001309
1310 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1311 value &= ~DISP_CTRL_MODE_MASK;
1312 value |= DISP_CTRL_MODE_C_DISPLAY;
1313 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1314
1315 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1316 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1317 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1318 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1319
1320 tegra_dc_commit(dc);
Thierry Reding23fb4742012-11-28 11:38:24 +01001321
Thierry Reding8ff64c12014-10-08 14:48:51 +02001322 drm_crtc_vblank_on(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001323}
1324
Thierry Reding4aa3df72014-11-24 16:27:13 +01001325static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1326 struct drm_crtc_state *state)
1327{
1328 return 0;
1329}
1330
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001331static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1332 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001333{
Thierry Reding1503ca42014-11-24 17:41:23 +01001334 struct tegra_dc *dc = to_tegra_dc(crtc);
1335
1336 if (crtc->state->event) {
1337 crtc->state->event->pipe = drm_crtc_index(crtc);
1338
1339 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1340
1341 dc->event = crtc->state->event;
1342 crtc->state->event = NULL;
1343 }
Thierry Reding4aa3df72014-11-24 16:27:13 +01001344}
1345
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001346static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1347 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001348{
Thierry Reding47802b02014-11-26 12:28:39 +01001349 struct tegra_dc_state *state = to_dc_state(crtc->state);
1350 struct tegra_dc *dc = to_tegra_dc(crtc);
1351
1352 tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
1353 tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
Thierry Reding4aa3df72014-11-24 16:27:13 +01001354}
1355
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001356static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +01001357 .atomic_check = tegra_crtc_atomic_check,
1358 .atomic_begin = tegra_crtc_atomic_begin,
1359 .atomic_flush = tegra_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001360 .atomic_enable = tegra_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +03001361 .atomic_disable = tegra_crtc_atomic_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001362};
1363
Thierry Reding6e5ff992012-11-28 11:45:47 +01001364static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001365{
1366 struct tegra_dc *dc = data;
1367 unsigned long status;
1368
1369 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1370 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1371
1372 if (status & FRAME_END_INT) {
1373 /*
1374 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1375 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001376 dc->stats.frames++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001377 }
1378
1379 if (status & VBLANK_INT) {
1380 /*
1381 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1382 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001383 drm_crtc_handle_vblank(&dc->base);
Thierry Reding3c03c462012-11-28 12:00:18 +01001384 tegra_dc_finish_page_flip(dc);
Thierry Reding791ddb12015-07-28 21:27:05 +02001385 dc->stats.vblank++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001386 }
1387
1388 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1389 /*
1390 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1391 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001392 dc->stats.underflow++;
1393 }
1394
1395 if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1396 /*
1397 dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1398 */
1399 dc->stats.overflow++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001400 }
1401
1402 return IRQ_HANDLED;
1403}
1404
1405static int tegra_dc_show_regs(struct seq_file *s, void *data)
1406{
1407 struct drm_info_node *node = s->private;
1408 struct tegra_dc *dc = node->info_ent->data;
Thierry Reding003fc842015-08-03 13:16:26 +02001409 int err = 0;
1410
Daniel Vetter99612b22017-03-22 22:50:46 +01001411 drm_modeset_lock(&dc->base.mutex, NULL);
Thierry Reding003fc842015-08-03 13:16:26 +02001412
1413 if (!dc->base.state->active) {
1414 err = -EBUSY;
1415 goto unlock;
1416 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001417
1418#define DUMP_REG(name) \
Thierry Reding03a60562014-10-21 13:48:48 +02001419 seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001420 tegra_dc_readl(dc, name))
1421
1422 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1423 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1424 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1425 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1426 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1427 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1428 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1429 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1430 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1431 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1432 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1433 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1434 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1435 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1436 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1437 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1438 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1439 DUMP_REG(DC_CMD_INT_STATUS);
1440 DUMP_REG(DC_CMD_INT_MASK);
1441 DUMP_REG(DC_CMD_INT_ENABLE);
1442 DUMP_REG(DC_CMD_INT_TYPE);
1443 DUMP_REG(DC_CMD_INT_POLARITY);
1444 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1445 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1446 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1447 DUMP_REG(DC_CMD_STATE_ACCESS);
1448 DUMP_REG(DC_CMD_STATE_CONTROL);
1449 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1450 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1451 DUMP_REG(DC_COM_CRC_CONTROL);
1452 DUMP_REG(DC_COM_CRC_CHECKSUM);
1453 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1454 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1455 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1456 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1457 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1458 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1459 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1460 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1461 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1462 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1463 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1464 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1465 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1466 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1467 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1468 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1469 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1470 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1471 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1472 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1473 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1474 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1475 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1476 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1477 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1478 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1479 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1480 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1481 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1482 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1483 DUMP_REG(DC_COM_SPI_CONTROL);
1484 DUMP_REG(DC_COM_SPI_START_BYTE);
1485 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1486 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1487 DUMP_REG(DC_COM_HSPI_CS_DC);
1488 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1489 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1490 DUMP_REG(DC_COM_GPIO_CTRL);
1491 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1492 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1493 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1494 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1495 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1496 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1497 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1498 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1499 DUMP_REG(DC_DISP_REF_TO_SYNC);
1500 DUMP_REG(DC_DISP_SYNC_WIDTH);
1501 DUMP_REG(DC_DISP_BACK_PORCH);
1502 DUMP_REG(DC_DISP_ACTIVE);
1503 DUMP_REG(DC_DISP_FRONT_PORCH);
1504 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1505 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1506 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1507 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1508 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1509 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1510 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1511 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1512 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1513 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1514 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1515 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1516 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1517 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1518 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1519 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1520 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1521 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1522 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1523 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1524 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1525 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1526 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1527 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1528 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1529 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1530 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1531 DUMP_REG(DC_DISP_M0_CONTROL);
1532 DUMP_REG(DC_DISP_M1_CONTROL);
1533 DUMP_REG(DC_DISP_DI_CONTROL);
1534 DUMP_REG(DC_DISP_PP_CONTROL);
1535 DUMP_REG(DC_DISP_PP_SELECT_A);
1536 DUMP_REG(DC_DISP_PP_SELECT_B);
1537 DUMP_REG(DC_DISP_PP_SELECT_C);
1538 DUMP_REG(DC_DISP_PP_SELECT_D);
1539 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1540 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1541 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1542 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1543 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1544 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1545 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1546 DUMP_REG(DC_DISP_BORDER_COLOR);
1547 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1548 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1549 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1550 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1551 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1552 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1553 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1554 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1555 DUMP_REG(DC_DISP_CURSOR_POSITION);
1556 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1557 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1558 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1559 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1560 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1561 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1562 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1563 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1564 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1565 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1566 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1567 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1568 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1569 DUMP_REG(DC_DISP_SD_CONTROL);
1570 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1571 DUMP_REG(DC_DISP_SD_LUT(0));
1572 DUMP_REG(DC_DISP_SD_LUT(1));
1573 DUMP_REG(DC_DISP_SD_LUT(2));
1574 DUMP_REG(DC_DISP_SD_LUT(3));
1575 DUMP_REG(DC_DISP_SD_LUT(4));
1576 DUMP_REG(DC_DISP_SD_LUT(5));
1577 DUMP_REG(DC_DISP_SD_LUT(6));
1578 DUMP_REG(DC_DISP_SD_LUT(7));
1579 DUMP_REG(DC_DISP_SD_LUT(8));
1580 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1581 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1582 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1583 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1584 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1585 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1586 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1587 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1588 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1589 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1590 DUMP_REG(DC_DISP_SD_BL_TF(0));
1591 DUMP_REG(DC_DISP_SD_BL_TF(1));
1592 DUMP_REG(DC_DISP_SD_BL_TF(2));
1593 DUMP_REG(DC_DISP_SD_BL_TF(3));
1594 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1595 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1596 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001597 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1598 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001599 DUMP_REG(DC_WIN_WIN_OPTIONS);
1600 DUMP_REG(DC_WIN_BYTE_SWAP);
1601 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1602 DUMP_REG(DC_WIN_COLOR_DEPTH);
1603 DUMP_REG(DC_WIN_POSITION);
1604 DUMP_REG(DC_WIN_SIZE);
1605 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1606 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1607 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1608 DUMP_REG(DC_WIN_DDA_INC);
1609 DUMP_REG(DC_WIN_LINE_STRIDE);
1610 DUMP_REG(DC_WIN_BUF_STRIDE);
1611 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1612 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1613 DUMP_REG(DC_WIN_DV_CONTROL);
1614 DUMP_REG(DC_WIN_BLEND_NOKEY);
1615 DUMP_REG(DC_WIN_BLEND_1WIN);
1616 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1617 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001618 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001619 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1620 DUMP_REG(DC_WINBUF_START_ADDR);
1621 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1622 DUMP_REG(DC_WINBUF_START_ADDR_U);
1623 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1624 DUMP_REG(DC_WINBUF_START_ADDR_V);
1625 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1626 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1627 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1628 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1629 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1630 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1631 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1632 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1633 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1634
1635#undef DUMP_REG
1636
Thierry Reding003fc842015-08-03 13:16:26 +02001637unlock:
Daniel Vetter99612b22017-03-22 22:50:46 +01001638 drm_modeset_unlock(&dc->base.mutex);
Thierry Reding003fc842015-08-03 13:16:26 +02001639 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001640}
1641
Thierry Reding6ca1f622015-04-01 14:59:40 +02001642static int tegra_dc_show_crc(struct seq_file *s, void *data)
1643{
1644 struct drm_info_node *node = s->private;
1645 struct tegra_dc *dc = node->info_ent->data;
Thierry Reding003fc842015-08-03 13:16:26 +02001646 int err = 0;
Thierry Reding6ca1f622015-04-01 14:59:40 +02001647 u32 value;
1648
Daniel Vetter99612b22017-03-22 22:50:46 +01001649 drm_modeset_lock(&dc->base.mutex, NULL);
Thierry Reding003fc842015-08-03 13:16:26 +02001650
1651 if (!dc->base.state->active) {
1652 err = -EBUSY;
1653 goto unlock;
1654 }
1655
Thierry Reding6ca1f622015-04-01 14:59:40 +02001656 value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1657 tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1658 tegra_dc_commit(dc);
1659
1660 drm_crtc_wait_one_vblank(&dc->base);
1661 drm_crtc_wait_one_vblank(&dc->base);
1662
1663 value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1664 seq_printf(s, "%08x\n", value);
1665
1666 tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1667
Thierry Reding003fc842015-08-03 13:16:26 +02001668unlock:
Daniel Vetter99612b22017-03-22 22:50:46 +01001669 drm_modeset_unlock(&dc->base.mutex);
Thierry Reding003fc842015-08-03 13:16:26 +02001670 return err;
Thierry Reding6ca1f622015-04-01 14:59:40 +02001671}
1672
Thierry Reding791ddb12015-07-28 21:27:05 +02001673static int tegra_dc_show_stats(struct seq_file *s, void *data)
1674{
1675 struct drm_info_node *node = s->private;
1676 struct tegra_dc *dc = node->info_ent->data;
1677
1678 seq_printf(s, "frames: %lu\n", dc->stats.frames);
1679 seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1680 seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1681 seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1682
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001683 return 0;
1684}
1685
1686static struct drm_info_list debugfs_files[] = {
1687 { "regs", tegra_dc_show_regs, 0, NULL },
Thierry Reding6ca1f622015-04-01 14:59:40 +02001688 { "crc", tegra_dc_show_crc, 0, NULL },
Thierry Reding791ddb12015-07-28 21:27:05 +02001689 { "stats", tegra_dc_show_stats, 0, NULL },
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001690};
1691
1692static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1693{
1694 unsigned int i;
1695 char *name;
1696 int err;
1697
1698 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1699 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1700 kfree(name);
1701
1702 if (!dc->debugfs)
1703 return -ENOMEM;
1704
1705 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1706 GFP_KERNEL);
1707 if (!dc->debugfs_files) {
1708 err = -ENOMEM;
1709 goto remove;
1710 }
1711
1712 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1713 dc->debugfs_files[i].data = dc;
1714
1715 err = drm_debugfs_create_files(dc->debugfs_files,
1716 ARRAY_SIZE(debugfs_files),
1717 dc->debugfs, minor);
1718 if (err < 0)
1719 goto free;
1720
1721 dc->minor = minor;
1722
1723 return 0;
1724
1725free:
1726 kfree(dc->debugfs_files);
1727 dc->debugfs_files = NULL;
1728remove:
1729 debugfs_remove(dc->debugfs);
1730 dc->debugfs = NULL;
1731
1732 return err;
1733}
1734
1735static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1736{
1737 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1738 dc->minor);
1739 dc->minor = NULL;
1740
1741 kfree(dc->debugfs_files);
1742 dc->debugfs_files = NULL;
1743
1744 debugfs_remove(dc->debugfs);
1745 dc->debugfs = NULL;
1746
1747 return 0;
1748}
1749
Thierry Reding53fa7f72013-09-24 15:35:40 +02001750static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001751{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001752 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001753 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
Thierry Reding776dc382013-10-14 14:43:22 +02001754 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001755 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001756 struct drm_plane *primary = NULL;
1757 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001758 int err;
1759
Thierry Reding617dd7c2017-08-30 12:48:31 +02001760 dc->syncpt = host1x_syncpt_request(client, flags);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001761 if (!dc->syncpt)
1762 dev_warn(dc->dev, "failed to allocate syncpoint\n");
1763
Thierry Redingdf06b752014-06-26 21:41:53 +02001764 if (tegra->domain) {
1765 err = iommu_attach_device(tegra->domain, dc->dev);
1766 if (err < 0) {
1767 dev_err(dc->dev, "failed to attach to domain: %d\n",
1768 err);
1769 return err;
1770 }
1771
1772 dc->domain = tegra->domain;
1773 }
1774
Thierry Redingc7679302014-10-21 13:51:53 +02001775 primary = tegra_dc_primary_plane_create(drm, dc);
1776 if (IS_ERR(primary)) {
1777 err = PTR_ERR(primary);
1778 goto cleanup;
1779 }
1780
1781 if (dc->soc->supports_cursor) {
1782 cursor = tegra_dc_cursor_plane_create(drm, dc);
1783 if (IS_ERR(cursor)) {
1784 err = PTR_ERR(cursor);
1785 goto cleanup;
1786 }
1787 }
1788
1789 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001790 &tegra_crtc_funcs, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +02001791 if (err < 0)
1792 goto cleanup;
1793
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001794 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1795
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001796 /*
1797 * Keep track of the minimum pitch alignment across all display
1798 * controllers.
1799 */
1800 if (dc->soc->pitch_align > tegra->pitch_align)
1801 tegra->pitch_align = dc->soc->pitch_align;
1802
Thierry Reding9910f5c2014-05-22 09:57:15 +02001803 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001804 if (err < 0 && err != -ENODEV) {
1805 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001806 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001807 }
1808
Thierry Reding9910f5c2014-05-22 09:57:15 +02001809 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001810 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001811 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001812
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001813 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001814 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001815 if (err < 0)
1816 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1817 }
1818
Thierry Reding6e5ff992012-11-28 11:45:47 +01001819 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001820 dev_name(dc->dev), dc);
1821 if (err < 0) {
1822 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1823 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001824 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001825 }
1826
1827 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001828
1829cleanup:
1830 if (cursor)
1831 drm_plane_cleanup(cursor);
1832
1833 if (primary)
1834 drm_plane_cleanup(primary);
1835
1836 if (tegra->domain) {
1837 iommu_detach_device(tegra->domain, dc->dev);
1838 dc->domain = NULL;
1839 }
1840
1841 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001842}
1843
Thierry Reding53fa7f72013-09-24 15:35:40 +02001844static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001845{
Thierry Reding776dc382013-10-14 14:43:22 +02001846 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001847 int err;
1848
1849 devm_free_irq(dc->dev, dc->irq, dc);
1850
1851 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1852 err = tegra_dc_debugfs_exit(dc);
1853 if (err < 0)
1854 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1855 }
1856
1857 err = tegra_dc_rgb_exit(dc);
1858 if (err) {
1859 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1860 return err;
1861 }
1862
Thierry Redingdf06b752014-06-26 21:41:53 +02001863 if (dc->domain) {
1864 iommu_detach_device(dc->domain, dc->dev);
1865 dc->domain = NULL;
1866 }
1867
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001868 host1x_syncpt_free(dc->syncpt);
1869
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001870 return 0;
1871}
1872
1873static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001874 .init = tegra_dc_init,
1875 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001876};
1877
Thierry Reding8620fc62013-12-12 11:03:59 +01001878static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001879 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001880 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001881 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001882 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001883 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001884 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001885 .broken_reset = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001886};
1887
1888static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001889 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001890 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001891 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001892 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001893 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001894 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001895 .broken_reset = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001896};
1897
1898static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001899 .supports_border_color = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001900 .supports_interlacing = false,
1901 .supports_cursor = false,
1902 .supports_block_linear = false,
1903 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001904 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001905 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001906};
1907
1908static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001909 .supports_border_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001910 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001911 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001912 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001913 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001914 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001915 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001916};
1917
Thierry Reding5b4f5162015-03-27 10:31:58 +01001918static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
1919 .supports_border_color = false,
1920 .supports_interlacing = true,
1921 .supports_cursor = true,
1922 .supports_block_linear = true,
1923 .pitch_align = 64,
1924 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001925 .broken_reset = false,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001926};
1927
Thierry Reding8620fc62013-12-12 11:03:59 +01001928static const struct of_device_id tegra_dc_of_match[] = {
1929 {
Thierry Reding5b4f5162015-03-27 10:31:58 +01001930 .compatible = "nvidia,tegra210-dc",
1931 .data = &tegra210_dc_soc_info,
1932 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001933 .compatible = "nvidia,tegra124-dc",
1934 .data = &tegra124_dc_soc_info,
1935 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001936 .compatible = "nvidia,tegra114-dc",
1937 .data = &tegra114_dc_soc_info,
1938 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001939 .compatible = "nvidia,tegra30-dc",
1940 .data = &tegra30_dc_soc_info,
1941 }, {
1942 .compatible = "nvidia,tegra20-dc",
1943 .data = &tegra20_dc_soc_info,
1944 }, {
1945 /* sentinel */
1946 }
1947};
Stephen Warrenef707282014-06-18 16:21:55 -06001948MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001949
Thierry Reding13411dd2014-01-09 17:08:36 +01001950static int tegra_dc_parse_dt(struct tegra_dc *dc)
1951{
1952 struct device_node *np;
1953 u32 value = 0;
1954 int err;
1955
1956 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1957 if (err < 0) {
1958 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1959
1960 /*
1961 * If the nvidia,head property isn't present, try to find the
1962 * correct head number by looking up the position of this
1963 * display controller's node within the device tree. Assuming
1964 * that the nodes are ordered properly in the DTS file and
1965 * that the translation into a flattened device tree blob
1966 * preserves that ordering this will actually yield the right
1967 * head number.
1968 *
1969 * If those assumptions don't hold, this will still work for
1970 * cases where only a single display controller is used.
1971 */
1972 for_each_matching_node(np, tegra_dc_of_match) {
Julia Lawallcf6b1742015-10-24 16:42:31 +02001973 if (np == dc->dev->of_node) {
1974 of_node_put(np);
Thierry Reding13411dd2014-01-09 17:08:36 +01001975 break;
Julia Lawallcf6b1742015-10-24 16:42:31 +02001976 }
Thierry Reding13411dd2014-01-09 17:08:36 +01001977
1978 value++;
1979 }
1980 }
1981
1982 dc->pipe = value;
1983
1984 return 0;
1985}
1986
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001987static int tegra_dc_probe(struct platform_device *pdev)
1988{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001989 struct resource *regs;
1990 struct tegra_dc *dc;
1991 int err;
1992
1993 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1994 if (!dc)
1995 return -ENOMEM;
1996
Thierry Redingb9ff7ae2017-08-21 16:35:17 +02001997 dc->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding8620fc62013-12-12 11:03:59 +01001998
Thierry Reding6e5ff992012-11-28 11:45:47 +01001999 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002000 INIT_LIST_HEAD(&dc->list);
2001 dc->dev = &pdev->dev;
2002
Thierry Reding13411dd2014-01-09 17:08:36 +01002003 err = tegra_dc_parse_dt(dc);
2004 if (err < 0)
2005 return err;
2006
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002007 dc->clk = devm_clk_get(&pdev->dev, NULL);
2008 if (IS_ERR(dc->clk)) {
2009 dev_err(&pdev->dev, "failed to get clock\n");
2010 return PTR_ERR(dc->clk);
2011 }
2012
Stephen Warrenca480802013-11-06 16:20:54 -07002013 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
2014 if (IS_ERR(dc->rst)) {
2015 dev_err(&pdev->dev, "failed to get reset\n");
2016 return PTR_ERR(dc->rst);
2017 }
2018
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002019 if (!dc->soc->broken_reset)
2020 reset_control_assert(dc->rst);
Thierry Reding33a8eb82015-08-03 13:20:49 +02002021
Thierry Reding9c012702014-07-07 15:32:53 +02002022 if (dc->soc->has_powergate) {
2023 if (dc->pipe == 0)
2024 dc->powergate = TEGRA_POWERGATE_DIS;
2025 else
2026 dc->powergate = TEGRA_POWERGATE_DISB;
2027
Thierry Reding33a8eb82015-08-03 13:20:49 +02002028 tegra_powergate_power_off(dc->powergate);
Thierry Reding9c012702014-07-07 15:32:53 +02002029 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002030
2031 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01002032 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2033 if (IS_ERR(dc->regs))
2034 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002035
2036 dc->irq = platform_get_irq(pdev, 0);
2037 if (dc->irq < 0) {
2038 dev_err(&pdev->dev, "failed to get IRQ\n");
2039 return -ENXIO;
2040 }
2041
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002042 err = tegra_dc_rgb_probe(dc);
2043 if (err < 0 && err != -ENODEV) {
2044 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2045 return err;
2046 }
2047
Thierry Reding33a8eb82015-08-03 13:20:49 +02002048 platform_set_drvdata(pdev, dc);
2049 pm_runtime_enable(&pdev->dev);
2050
2051 INIT_LIST_HEAD(&dc->client.list);
2052 dc->client.ops = &dc_client_ops;
2053 dc->client.dev = &pdev->dev;
2054
Thierry Reding776dc382013-10-14 14:43:22 +02002055 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002056 if (err < 0) {
2057 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2058 err);
2059 return err;
2060 }
2061
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002062 return 0;
2063}
2064
2065static int tegra_dc_remove(struct platform_device *pdev)
2066{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002067 struct tegra_dc *dc = platform_get_drvdata(pdev);
2068 int err;
2069
Thierry Reding776dc382013-10-14 14:43:22 +02002070 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002071 if (err < 0) {
2072 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2073 err);
2074 return err;
2075 }
2076
Thierry Reding59d29c02013-10-14 14:26:42 +02002077 err = tegra_dc_rgb_remove(dc);
2078 if (err < 0) {
2079 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
2080 return err;
2081 }
2082
Thierry Reding33a8eb82015-08-03 13:20:49 +02002083 pm_runtime_disable(&pdev->dev);
2084
2085 return 0;
2086}
2087
2088#ifdef CONFIG_PM
2089static int tegra_dc_suspend(struct device *dev)
2090{
2091 struct tegra_dc *dc = dev_get_drvdata(dev);
2092 int err;
2093
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002094 if (!dc->soc->broken_reset) {
2095 err = reset_control_assert(dc->rst);
2096 if (err < 0) {
2097 dev_err(dev, "failed to assert reset: %d\n", err);
2098 return err;
2099 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002100 }
Thierry Reding9c012702014-07-07 15:32:53 +02002101
2102 if (dc->soc->has_powergate)
2103 tegra_powergate_power_off(dc->powergate);
2104
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002105 clk_disable_unprepare(dc->clk);
2106
2107 return 0;
2108}
2109
Thierry Reding33a8eb82015-08-03 13:20:49 +02002110static int tegra_dc_resume(struct device *dev)
2111{
2112 struct tegra_dc *dc = dev_get_drvdata(dev);
2113 int err;
2114
2115 if (dc->soc->has_powergate) {
2116 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2117 dc->rst);
2118 if (err < 0) {
2119 dev_err(dev, "failed to power partition: %d\n", err);
2120 return err;
2121 }
2122 } else {
2123 err = clk_prepare_enable(dc->clk);
2124 if (err < 0) {
2125 dev_err(dev, "failed to enable clock: %d\n", err);
2126 return err;
2127 }
2128
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002129 if (!dc->soc->broken_reset) {
2130 err = reset_control_deassert(dc->rst);
2131 if (err < 0) {
2132 dev_err(dev,
2133 "failed to deassert reset: %d\n", err);
2134 return err;
2135 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002136 }
2137 }
2138
2139 return 0;
2140}
2141#endif
2142
2143static const struct dev_pm_ops tegra_dc_pm_ops = {
2144 SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
2145};
2146
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002147struct platform_driver tegra_dc_driver = {
2148 .driver = {
2149 .name = "tegra-dc",
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002150 .of_match_table = tegra_dc_of_match,
Thierry Reding33a8eb82015-08-03 13:20:49 +02002151 .pm = &tegra_dc_pm_ops,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002152 },
2153 .probe = tegra_dc_probe,
2154 .remove = tegra_dc_remove,
2155};