blob: 6cee248601a97243737fce8e7b7ffa742fd497f7 [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 Redingf34bc782012-11-04 21:47:13 +010027struct tegra_plane {
28 struct drm_plane base;
29 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000030};
31
Thierry Redingf34bc782012-11-04 21:47:13 +010032static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
33{
34 return container_of(plane, struct tegra_plane, base);
35}
36
Thierry Redingca915b12014-12-08 16:14:45 +010037struct tegra_dc_state {
38 struct drm_crtc_state base;
39
40 struct clk *clk;
41 unsigned long pclk;
42 unsigned int div;
Thierry Reding47802b02014-11-26 12:28:39 +010043
44 u32 planes;
Thierry Redingca915b12014-12-08 16:14:45 +010045};
46
47static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
48{
49 if (state)
50 return container_of(state, struct tegra_dc_state, base);
51
52 return NULL;
53}
54
Thierry Reding8f604f82014-11-28 13:14:55 +010055struct tegra_plane_state {
56 struct drm_plane_state base;
57
58 struct tegra_bo_tiling tiling;
59 u32 format;
60 u32 swap;
61};
62
63static inline struct tegra_plane_state *
64to_tegra_plane_state(struct drm_plane_state *state)
65{
66 if (state)
67 return container_of(state, struct tegra_plane_state, base);
68
69 return NULL;
70}
71
Thierry Reding791ddb12015-07-28 21:27:05 +020072static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
73{
74 stats->frames = 0;
75 stats->vblank = 0;
76 stats->underflow = 0;
77 stats->overflow = 0;
78}
79
Thierry Redingd700ba72014-12-08 15:50:04 +010080/*
Thierry Reding86df2562014-12-08 16:03:53 +010081 * Reads the active copy of a register. This takes the dc->lock spinlock to
82 * prevent races with the VBLANK processing which also needs access to the
83 * active copy of some registers.
84 */
85static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
86{
87 unsigned long flags;
88 u32 value;
89
90 spin_lock_irqsave(&dc->lock, flags);
91
92 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
93 value = tegra_dc_readl(dc, offset);
94 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
95
96 spin_unlock_irqrestore(&dc->lock, flags);
97 return value;
98}
99
100/*
Thierry Redingd700ba72014-12-08 15:50:04 +0100101 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
102 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
103 * Latching happens mmediately if the display controller is in STOP mode or
104 * on the next frame boundary otherwise.
105 *
106 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
107 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
108 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
109 * into the ACTIVE copy, either immediately if the display controller is in
110 * STOP mode, or at the next frame boundary otherwise.
111 */
Thierry Reding62b9e062014-11-21 17:33:33 +0100112void tegra_dc_commit(struct tegra_dc *dc)
Thierry Reding205d48e2014-10-21 13:41:46 +0200113{
114 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
115 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
116}
117
Thierry Reding8f604f82014-11-28 13:14:55 +0100118static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
Thierry Reding10288ee2014-03-14 09:54:58 +0100119{
120 /* assume no swapping of fetched data */
121 if (swap)
122 *swap = BYTE_SWAP_NOSWAP;
123
Thierry Reding8f604f82014-11-28 13:14:55 +0100124 switch (fourcc) {
Thierry Reding10288ee2014-03-14 09:54:58 +0100125 case DRM_FORMAT_XBGR8888:
Thierry Reding8f604f82014-11-28 13:14:55 +0100126 *format = WIN_COLOR_DEPTH_R8G8B8A8;
127 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100128
129 case DRM_FORMAT_XRGB8888:
Thierry Reding8f604f82014-11-28 13:14:55 +0100130 *format = WIN_COLOR_DEPTH_B8G8R8A8;
131 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100132
133 case DRM_FORMAT_RGB565:
Thierry Reding8f604f82014-11-28 13:14:55 +0100134 *format = WIN_COLOR_DEPTH_B5G6R5;
135 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100136
137 case DRM_FORMAT_UYVY:
Thierry Reding8f604f82014-11-28 13:14:55 +0100138 *format = WIN_COLOR_DEPTH_YCbCr422;
139 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100140
141 case DRM_FORMAT_YUYV:
142 if (swap)
143 *swap = BYTE_SWAP_SWAP2;
144
Thierry Reding8f604f82014-11-28 13:14:55 +0100145 *format = WIN_COLOR_DEPTH_YCbCr422;
146 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100147
148 case DRM_FORMAT_YUV420:
Thierry Reding8f604f82014-11-28 13:14:55 +0100149 *format = WIN_COLOR_DEPTH_YCbCr420P;
150 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100151
152 case DRM_FORMAT_YUV422:
Thierry Reding8f604f82014-11-28 13:14:55 +0100153 *format = WIN_COLOR_DEPTH_YCbCr422P;
154 break;
Thierry Reding10288ee2014-03-14 09:54:58 +0100155
156 default:
Thierry Reding8f604f82014-11-28 13:14:55 +0100157 return -EINVAL;
Thierry Reding10288ee2014-03-14 09:54:58 +0100158 }
159
Thierry Reding8f604f82014-11-28 13:14:55 +0100160 return 0;
Thierry Reding10288ee2014-03-14 09:54:58 +0100161}
162
163static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
164{
165 switch (format) {
166 case WIN_COLOR_DEPTH_YCbCr422:
167 case WIN_COLOR_DEPTH_YUV422:
168 if (planar)
169 *planar = false;
170
171 return true;
172
173 case WIN_COLOR_DEPTH_YCbCr420P:
174 case WIN_COLOR_DEPTH_YUV420P:
175 case WIN_COLOR_DEPTH_YCbCr422P:
176 case WIN_COLOR_DEPTH_YUV422P:
177 case WIN_COLOR_DEPTH_YCbCr422R:
178 case WIN_COLOR_DEPTH_YUV422R:
179 case WIN_COLOR_DEPTH_YCbCr422RA:
180 case WIN_COLOR_DEPTH_YUV422RA:
181 if (planar)
182 *planar = true;
183
184 return true;
185 }
186
Thierry Redingfb35c6b2014-12-08 15:55:28 +0100187 if (planar)
188 *planar = false;
189
Thierry Reding10288ee2014-03-14 09:54:58 +0100190 return false;
191}
192
193static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
194 unsigned int bpp)
195{
196 fixed20_12 outf = dfixed_init(out);
197 fixed20_12 inf = dfixed_init(in);
198 u32 dda_inc;
199 int max;
200
201 if (v)
202 max = 15;
203 else {
204 switch (bpp) {
205 case 2:
206 max = 8;
207 break;
208
209 default:
210 WARN_ON_ONCE(1);
211 /* fallthrough */
212 case 4:
213 max = 4;
214 break;
215 }
216 }
217
218 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
219 inf.full -= dfixed_const(1);
220
221 dda_inc = dfixed_div(inf, outf);
222 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
223
224 return dda_inc;
225}
226
227static inline u32 compute_initial_dda(unsigned int in)
228{
229 fixed20_12 inf = dfixed_init(in);
230 return dfixed_frac(inf);
231}
232
Thierry Reding4aa3df72014-11-24 16:27:13 +0100233static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
234 const struct tegra_dc_window *window)
Thierry Reding10288ee2014-03-14 09:54:58 +0100235{
236 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
Sean Paul93396d02014-11-19 13:04:49 -0500237 unsigned long value, flags;
Thierry Reding10288ee2014-03-14 09:54:58 +0100238 bool yuv, planar;
239
240 /*
241 * For YUV planar modes, the number of bytes per pixel takes into
242 * account only the luma component and therefore is 1.
243 */
244 yuv = tegra_dc_format_is_yuv(window->format, &planar);
245 if (!yuv)
246 bpp = window->bits_per_pixel / 8;
247 else
248 bpp = planar ? 1 : 2;
249
Sean Paul93396d02014-11-19 13:04:49 -0500250 spin_lock_irqsave(&dc->lock, flags);
251
Thierry Reding10288ee2014-03-14 09:54:58 +0100252 value = WINDOW_A_SELECT << index;
253 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
254
255 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
256 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
257
258 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
259 tegra_dc_writel(dc, value, DC_WIN_POSITION);
260
261 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
262 tegra_dc_writel(dc, value, DC_WIN_SIZE);
263
264 h_offset = window->src.x * bpp;
265 v_offset = window->src.y;
266 h_size = window->src.w * bpp;
267 v_size = window->src.h;
268
269 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
270 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
271
272 /*
273 * For DDA computations the number of bytes per pixel for YUV planar
274 * modes needs to take into account all Y, U and V components.
275 */
276 if (yuv && planar)
277 bpp = 2;
278
279 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
280 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
281
282 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
283 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
284
285 h_dda = compute_initial_dda(window->src.x);
286 v_dda = compute_initial_dda(window->src.y);
287
288 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
289 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
290
291 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
292 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
293
294 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
295
296 if (yuv && planar) {
297 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
298 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
299 value = window->stride[1] << 16 | window->stride[0];
300 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
301 } else {
302 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
303 }
304
305 if (window->bottom_up)
306 v_offset += window->src.h - 1;
307
308 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
309 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
310
Thierry Redingc134f012014-06-03 14:48:12 +0200311 if (dc->soc->supports_block_linear) {
312 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100313
Thierry Redingc134f012014-06-03 14:48:12 +0200314 switch (window->tiling.mode) {
315 case TEGRA_BO_TILING_MODE_PITCH:
316 value = DC_WINBUF_SURFACE_KIND_PITCH;
317 break;
318
319 case TEGRA_BO_TILING_MODE_TILED:
320 value = DC_WINBUF_SURFACE_KIND_TILED;
321 break;
322
323 case TEGRA_BO_TILING_MODE_BLOCK:
324 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
325 DC_WINBUF_SURFACE_KIND_BLOCK;
326 break;
327 }
328
329 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
330 } else {
331 switch (window->tiling.mode) {
332 case TEGRA_BO_TILING_MODE_PITCH:
333 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
334 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
335 break;
336
337 case TEGRA_BO_TILING_MODE_TILED:
338 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
339 DC_WIN_BUFFER_ADDR_MODE_TILE;
340 break;
341
342 case TEGRA_BO_TILING_MODE_BLOCK:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100343 /*
344 * No need to handle this here because ->atomic_check
345 * will already have filtered it out.
346 */
347 break;
Thierry Redingc134f012014-06-03 14:48:12 +0200348 }
349
350 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
351 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100352
353 value = WIN_ENABLE;
354
355 if (yuv) {
356 /* setup default colorspace conversion coefficients */
357 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
358 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
359 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
360 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
361 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
362 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
363 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
364 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
365
366 value |= CSC_ENABLE;
367 } else if (window->bits_per_pixel < 24) {
368 value |= COLOR_EXPAND;
369 }
370
371 if (window->bottom_up)
372 value |= V_DIRECTION;
373
374 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
375
376 /*
377 * Disable blending and assume Window A is the bottom-most window,
378 * Window C is the top-most window and Window B is in the middle.
379 */
380 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
381 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
382
383 switch (index) {
384 case 0:
385 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
386 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
387 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
388 break;
389
390 case 1:
391 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
392 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
393 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
394 break;
395
396 case 2:
397 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
398 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
399 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
400 break;
401 }
402
Sean Paul93396d02014-11-19 13:04:49 -0500403 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc7679302014-10-21 13:51:53 +0200404}
405
406static void tegra_plane_destroy(struct drm_plane *plane)
407{
408 struct tegra_plane *p = to_tegra_plane(plane);
409
410 drm_plane_cleanup(plane);
411 kfree(p);
412}
413
414static const u32 tegra_primary_plane_formats[] = {
415 DRM_FORMAT_XBGR8888,
416 DRM_FORMAT_XRGB8888,
417 DRM_FORMAT_RGB565,
418};
419
Thierry Reding4aa3df72014-11-24 16:27:13 +0100420static void tegra_primary_plane_destroy(struct drm_plane *plane)
Thierry Redingc7679302014-10-21 13:51:53 +0200421{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100422 tegra_plane_destroy(plane);
423}
424
Thierry Reding8f604f82014-11-28 13:14:55 +0100425static void tegra_plane_reset(struct drm_plane *plane)
426{
427 struct tegra_plane_state *state;
428
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100429 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +0200430 __drm_atomic_helper_plane_destroy_state(plane->state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100431
432 kfree(plane->state);
433 plane->state = NULL;
434
435 state = kzalloc(sizeof(*state), GFP_KERNEL);
436 if (state) {
437 plane->state = &state->base;
438 plane->state->plane = plane;
439 }
440}
441
442static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
443{
444 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
445 struct tegra_plane_state *copy;
446
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100447 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Reding8f604f82014-11-28 13:14:55 +0100448 if (!copy)
449 return NULL;
450
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100451 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
452 copy->tiling = state->tiling;
453 copy->format = state->format;
454 copy->swap = state->swap;
Thierry Reding8f604f82014-11-28 13:14:55 +0100455
456 return &copy->base;
457}
458
459static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
460 struct drm_plane_state *state)
461{
Daniel Vetter2f701692016-05-09 16:34:10 +0200462 __drm_atomic_helper_plane_destroy_state(state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100463 kfree(state);
464}
465
Thierry Reding4aa3df72014-11-24 16:27:13 +0100466static const struct drm_plane_funcs tegra_primary_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100467 .update_plane = drm_atomic_helper_update_plane,
468 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100469 .destroy = tegra_primary_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100470 .reset = tegra_plane_reset,
471 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
472 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100473};
474
Thierry Reding47802b02014-11-26 12:28:39 +0100475static int tegra_plane_state_add(struct tegra_plane *plane,
476 struct drm_plane_state *state)
477{
478 struct drm_crtc_state *crtc_state;
479 struct tegra_dc_state *tegra;
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300480 struct drm_rect clip;
481 int err;
Thierry Reding47802b02014-11-26 12:28:39 +0100482
483 /* Propagate errors from allocation or locking failures. */
484 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
485 if (IS_ERR(crtc_state))
486 return PTR_ERR(crtc_state);
487
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300488 clip.x1 = 0;
489 clip.y1 = 0;
490 clip.x2 = crtc_state->mode.hdisplay;
491 clip.y2 = crtc_state->mode.vdisplay;
492
493 /* Check plane state for visibility and calculate clipping bounds */
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200494 err = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
495 0, INT_MAX, true, true);
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300496 if (err < 0)
497 return err;
498
Thierry Reding47802b02014-11-26 12:28:39 +0100499 tegra = to_dc_state(crtc_state);
500
501 tegra->planes |= WIN_A_ACT_REQ << plane->index;
502
503 return 0;
504}
505
Thierry Reding4aa3df72014-11-24 16:27:13 +0100506static int tegra_plane_atomic_check(struct drm_plane *plane,
507 struct drm_plane_state *state)
508{
Thierry Reding8f604f82014-11-28 13:14:55 +0100509 struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
510 struct tegra_bo_tiling *tiling = &plane_state->tiling;
Thierry Reding47802b02014-11-26 12:28:39 +0100511 struct tegra_plane *tegra = to_tegra_plane(plane);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100512 struct tegra_dc *dc = to_tegra_dc(state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200513 int err;
514
Thierry Reding4aa3df72014-11-24 16:27:13 +0100515 /* no need for further checks if the plane is being disabled */
516 if (!state->crtc)
517 return 0;
518
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200519 err = tegra_dc_format(state->fb->format->format, &plane_state->format,
Thierry Reding8f604f82014-11-28 13:14:55 +0100520 &plane_state->swap);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100521 if (err < 0)
522 return err;
523
Thierry Reding8f604f82014-11-28 13:14:55 +0100524 err = tegra_fb_get_tiling(state->fb, tiling);
525 if (err < 0)
526 return err;
527
528 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
Thierry Reding4aa3df72014-11-24 16:27:13 +0100529 !dc->soc->supports_block_linear) {
530 DRM_ERROR("hardware doesn't support block linear mode\n");
531 return -EINVAL;
532 }
533
534 /*
535 * Tegra doesn't support different strides for U and V planes so we
536 * error out if the user tries to display a framebuffer with such a
537 * configuration.
538 */
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200539 if (state->fb->format->num_planes > 2) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100540 if (state->fb->pitches[2] != state->fb->pitches[1]) {
541 DRM_ERROR("unsupported UV-plane configuration\n");
542 return -EINVAL;
543 }
544 }
545
Thierry Reding47802b02014-11-26 12:28:39 +0100546 err = tegra_plane_state_add(tegra, state);
547 if (err < 0)
548 return err;
549
Thierry Reding4aa3df72014-11-24 16:27:13 +0100550 return 0;
551}
552
Thierry Redinga4bfa092017-08-30 17:34:10 +0200553static void tegra_plane_atomic_disable(struct drm_plane *plane,
554 struct drm_plane_state *old_state)
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300555{
Thierry Redinga4bfa092017-08-30 17:34:10 +0200556 struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
557 struct tegra_plane *p = to_tegra_plane(plane);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300558 unsigned long flags;
559 u32 value;
560
Thierry Redinga4bfa092017-08-30 17:34:10 +0200561 /* rien ne va plus */
562 if (!old_state || !old_state->crtc)
563 return;
564
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300565 spin_lock_irqsave(&dc->lock, flags);
566
Thierry Redinga4bfa092017-08-30 17:34:10 +0200567 value = WINDOW_A_SELECT << p->index;
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300568 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
569
570 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
571 value &= ~WIN_ENABLE;
572 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
573
574 spin_unlock_irqrestore(&dc->lock, flags);
575}
576
Thierry Reding4aa3df72014-11-24 16:27:13 +0100577static void tegra_plane_atomic_update(struct drm_plane *plane,
578 struct drm_plane_state *old_state)
579{
Thierry Reding8f604f82014-11-28 13:14:55 +0100580 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100581 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
582 struct drm_framebuffer *fb = plane->state->fb;
583 struct tegra_plane *p = to_tegra_plane(plane);
584 struct tegra_dc_window window;
585 unsigned int i;
Thierry Reding4aa3df72014-11-24 16:27:13 +0100586
587 /* rien ne va plus */
588 if (!plane->state->crtc || !plane->state->fb)
589 return;
590
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300591 if (!plane->state->visible)
Thierry Redinga4bfa092017-08-30 17:34:10 +0200592 return tegra_plane_atomic_disable(plane, old_state);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300593
Thierry Redingc7679302014-10-21 13:51:53 +0200594 memset(&window, 0, sizeof(window));
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300595 window.src.x = plane->state->src.x1 >> 16;
596 window.src.y = plane->state->src.y1 >> 16;
597 window.src.w = drm_rect_width(&plane->state->src) >> 16;
598 window.src.h = drm_rect_height(&plane->state->src) >> 16;
599 window.dst.x = plane->state->dst.x1;
600 window.dst.y = plane->state->dst.y1;
601 window.dst.w = drm_rect_width(&plane->state->dst);
602 window.dst.h = drm_rect_height(&plane->state->dst);
Ville Syrjälä272725c2016-12-14 23:32:20 +0200603 window.bits_per_pixel = fb->format->cpp[0] * 8;
Thierry Redingc7679302014-10-21 13:51:53 +0200604 window.bottom_up = tegra_fb_is_bottom_up(fb);
605
Thierry Reding8f604f82014-11-28 13:14:55 +0100606 /* copy from state */
607 window.tiling = state->tiling;
608 window.format = state->format;
609 window.swap = state->swap;
Thierry Redingc7679302014-10-21 13:51:53 +0200610
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200611 for (i = 0; i < fb->format->num_planes; i++) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100612 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingc7679302014-10-21 13:51:53 +0200613
Thierry Reding4aa3df72014-11-24 16:27:13 +0100614 window.base[i] = bo->paddr + fb->offsets[i];
Dmitry Osipenko08ee0172016-08-21 11:57:58 +0300615
616 /*
617 * Tegra uses a shared stride for UV planes. Framebuffers are
618 * already checked for this in the tegra_plane_atomic_check()
619 * function, so it's safe to ignore the V-plane pitch here.
620 */
621 if (i < 2)
622 window.stride[i] = fb->pitches[i];
Thierry Reding4aa3df72014-11-24 16:27:13 +0100623 }
Thierry Redingc7679302014-10-21 13:51:53 +0200624
Thierry Reding4aa3df72014-11-24 16:27:13 +0100625 tegra_dc_setup_window(dc, p->index, &window);
Thierry Redingc7679302014-10-21 13:51:53 +0200626}
627
Thierry Redinga4bfa092017-08-30 17:34:10 +0200628static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100629 .atomic_check = tegra_plane_atomic_check,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100630 .atomic_disable = tegra_plane_atomic_disable,
Thierry Redinga4bfa092017-08-30 17:34:10 +0200631 .atomic_update = tegra_plane_atomic_update,
Thierry Redingc7679302014-10-21 13:51:53 +0200632};
633
634static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
635 struct tegra_dc *dc)
636{
Thierry Reding518e6222014-12-16 18:04:08 +0100637 /*
638 * Ideally this would use drm_crtc_mask(), but that would require the
639 * CRTC to already be in the mode_config's list of CRTCs. However, it
640 * will only be added to that list in the drm_crtc_init_with_planes()
641 * (in tegra_dc_init()), which in turn requires registration of these
642 * planes. So we have ourselves a nice little chicken and egg problem
643 * here.
644 *
645 * We work around this by manually creating the mask from the number
646 * of CRTCs that have been registered, and should therefore always be
647 * the same as drm_crtc_index() after registration.
648 */
649 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Redingc7679302014-10-21 13:51:53 +0200650 struct tegra_plane *plane;
651 unsigned int num_formats;
652 const u32 *formats;
653 int err;
654
655 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
656 if (!plane)
657 return ERR_PTR(-ENOMEM);
658
659 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
660 formats = tegra_primary_plane_formats;
661
Thierry Reding518e6222014-12-16 18:04:08 +0100662 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc7679302014-10-21 13:51:53 +0200663 &tegra_primary_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700664 num_formats, NULL,
665 DRM_PLANE_TYPE_PRIMARY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200666 if (err < 0) {
667 kfree(plane);
668 return ERR_PTR(err);
669 }
670
Thierry Redinga4bfa092017-08-30 17:34:10 +0200671 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100672
Thierry Redingc7679302014-10-21 13:51:53 +0200673 return &plane->base;
674}
675
676static const u32 tegra_cursor_plane_formats[] = {
677 DRM_FORMAT_RGBA8888,
678};
679
Thierry Reding4aa3df72014-11-24 16:27:13 +0100680static int tegra_cursor_atomic_check(struct drm_plane *plane,
681 struct drm_plane_state *state)
Thierry Redingc7679302014-10-21 13:51:53 +0200682{
Thierry Reding47802b02014-11-26 12:28:39 +0100683 struct tegra_plane *tegra = to_tegra_plane(plane);
684 int err;
685
Thierry Reding4aa3df72014-11-24 16:27:13 +0100686 /* no need for further checks if the plane is being disabled */
687 if (!state->crtc)
688 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200689
690 /* scaling not supported for cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100691 if ((state->src_w >> 16 != state->crtc_w) ||
692 (state->src_h >> 16 != state->crtc_h))
Thierry Redingc7679302014-10-21 13:51:53 +0200693 return -EINVAL;
694
695 /* only square cursors supported */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100696 if (state->src_w != state->src_h)
Thierry Redingc7679302014-10-21 13:51:53 +0200697 return -EINVAL;
698
Thierry Reding4aa3df72014-11-24 16:27:13 +0100699 if (state->crtc_w != 32 && state->crtc_w != 64 &&
700 state->crtc_w != 128 && state->crtc_w != 256)
701 return -EINVAL;
702
Thierry Reding47802b02014-11-26 12:28:39 +0100703 err = tegra_plane_state_add(tegra, state);
704 if (err < 0)
705 return err;
706
Thierry Reding4aa3df72014-11-24 16:27:13 +0100707 return 0;
708}
709
710static void tegra_cursor_atomic_update(struct drm_plane *plane,
711 struct drm_plane_state *old_state)
712{
713 struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
714 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
715 struct drm_plane_state *state = plane->state;
716 u32 value = CURSOR_CLIP_DISPLAY;
717
718 /* rien ne va plus */
719 if (!plane->state->crtc || !plane->state->fb)
720 return;
721
722 switch (state->crtc_w) {
Thierry Redingc7679302014-10-21 13:51:53 +0200723 case 32:
724 value |= CURSOR_SIZE_32x32;
725 break;
726
727 case 64:
728 value |= CURSOR_SIZE_64x64;
729 break;
730
731 case 128:
732 value |= CURSOR_SIZE_128x128;
733 break;
734
735 case 256:
736 value |= CURSOR_SIZE_256x256;
737 break;
738
739 default:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100740 WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
741 state->crtc_h);
742 return;
Thierry Redingc7679302014-10-21 13:51:53 +0200743 }
744
745 value |= (bo->paddr >> 10) & 0x3fffff;
746 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
747
748#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
749 value = (bo->paddr >> 32) & 0x3;
750 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
751#endif
752
753 /* enable cursor and set blend mode */
754 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
755 value |= CURSOR_ENABLE;
756 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
757
758 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
759 value &= ~CURSOR_DST_BLEND_MASK;
760 value &= ~CURSOR_SRC_BLEND_MASK;
761 value |= CURSOR_MODE_NORMAL;
762 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
763 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
764 value |= CURSOR_ALPHA;
765 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
766
767 /* position the cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100768 value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
Thierry Redingc7679302014-10-21 13:51:53 +0200769 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
Thierry Redingc7679302014-10-21 13:51:53 +0200770}
771
Thierry Reding4aa3df72014-11-24 16:27:13 +0100772static void tegra_cursor_atomic_disable(struct drm_plane *plane,
773 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200774{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100775 struct tegra_dc *dc;
Thierry Redingc7679302014-10-21 13:51:53 +0200776 u32 value;
777
Thierry Reding4aa3df72014-11-24 16:27:13 +0100778 /* rien ne va plus */
779 if (!old_state || !old_state->crtc)
780 return;
781
782 dc = to_tegra_dc(old_state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200783
784 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
785 value &= ~CURSOR_ENABLE;
786 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc7679302014-10-21 13:51:53 +0200787}
788
789static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100790 .update_plane = drm_atomic_helper_update_plane,
791 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Redingc7679302014-10-21 13:51:53 +0200792 .destroy = tegra_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100793 .reset = tegra_plane_reset,
794 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
795 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100796};
797
798static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100799 .atomic_check = tegra_cursor_atomic_check,
800 .atomic_update = tegra_cursor_atomic_update,
801 .atomic_disable = tegra_cursor_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200802};
803
804static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
805 struct tegra_dc *dc)
806{
807 struct tegra_plane *plane;
808 unsigned int num_formats;
809 const u32 *formats;
810 int err;
811
812 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
813 if (!plane)
814 return ERR_PTR(-ENOMEM);
815
Thierry Reding47802b02014-11-26 12:28:39 +0100816 /*
Thierry Redinga1df3b22015-07-21 16:42:30 +0200817 * This index is kind of fake. The cursor isn't a regular plane, but
818 * its update and activation request bits in DC_CMD_STATE_CONTROL do
819 * use the same programming. Setting this fake index here allows the
820 * code in tegra_add_plane_state() to do the right thing without the
821 * need to special-casing the cursor plane.
Thierry Reding47802b02014-11-26 12:28:39 +0100822 */
823 plane->index = 6;
824
Thierry Redingc7679302014-10-21 13:51:53 +0200825 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
826 formats = tegra_cursor_plane_formats;
827
828 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
829 &tegra_cursor_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700830 num_formats, NULL,
831 DRM_PLANE_TYPE_CURSOR, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200832 if (err < 0) {
833 kfree(plane);
834 return ERR_PTR(err);
835 }
836
Thierry Reding4aa3df72014-11-24 16:27:13 +0100837 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
838
Thierry Redingc7679302014-10-21 13:51:53 +0200839 return &plane->base;
840}
841
Thierry Redingc7679302014-10-21 13:51:53 +0200842static void tegra_overlay_plane_destroy(struct drm_plane *plane)
Thierry Redingf34bc782012-11-04 21:47:13 +0100843{
Thierry Redingc7679302014-10-21 13:51:53 +0200844 tegra_plane_destroy(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100845}
846
Thierry Redingc7679302014-10-21 13:51:53 +0200847static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100848 .update_plane = drm_atomic_helper_update_plane,
849 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Redingc7679302014-10-21 13:51:53 +0200850 .destroy = tegra_overlay_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100851 .reset = tegra_plane_reset,
852 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
853 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Redingf34bc782012-11-04 21:47:13 +0100854};
855
Thierry Redingc7679302014-10-21 13:51:53 +0200856static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100857 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100858 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100859 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100860 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100861 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100862 DRM_FORMAT_YUV420,
863 DRM_FORMAT_YUV422,
864};
865
Thierry Redingc7679302014-10-21 13:51:53 +0200866static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
867 struct tegra_dc *dc,
868 unsigned int index)
869{
870 struct tegra_plane *plane;
871 unsigned int num_formats;
872 const u32 *formats;
873 int err;
874
875 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
876 if (!plane)
877 return ERR_PTR(-ENOMEM);
878
879 plane->index = index;
880
881 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
882 formats = tegra_overlay_plane_formats;
883
884 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
885 &tegra_overlay_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700886 num_formats, NULL,
887 DRM_PLANE_TYPE_OVERLAY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200888 if (err < 0) {
889 kfree(plane);
890 return ERR_PTR(err);
891 }
892
Thierry Redinga4bfa092017-08-30 17:34:10 +0200893 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100894
Thierry Redingc7679302014-10-21 13:51:53 +0200895 return &plane->base;
896}
897
Thierry Redingf34bc782012-11-04 21:47:13 +0100898static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
899{
Thierry Redingc7679302014-10-21 13:51:53 +0200900 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100901 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100902
903 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200904 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
905 if (IS_ERR(plane))
906 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100907 }
908
909 return 0;
910}
911
Thierry Redingf002abc2013-10-14 14:06:02 +0200912static void tegra_dc_destroy(struct drm_crtc *crtc)
913{
914 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +0200915}
916
Thierry Redingca915b12014-12-08 16:14:45 +0100917static void tegra_crtc_reset(struct drm_crtc *crtc)
918{
919 struct tegra_dc_state *state;
920
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100921 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200922 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100923
Thierry Redingca915b12014-12-08 16:14:45 +0100924 kfree(crtc->state);
925 crtc->state = NULL;
926
927 state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Reding332bbe72015-01-28 15:03:31 +0100928 if (state) {
Thierry Redingca915b12014-12-08 16:14:45 +0100929 crtc->state = &state->base;
Thierry Reding332bbe72015-01-28 15:03:31 +0100930 crtc->state->crtc = crtc;
931 }
Thierry Reding31930d42015-07-02 17:04:06 +0200932
933 drm_crtc_vblank_reset(crtc);
Thierry Redingca915b12014-12-08 16:14:45 +0100934}
935
936static struct drm_crtc_state *
937tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
938{
939 struct tegra_dc_state *state = to_dc_state(crtc->state);
940 struct tegra_dc_state *copy;
941
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100942 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Redingca915b12014-12-08 16:14:45 +0100943 if (!copy)
944 return NULL;
945
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100946 __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
947 copy->clk = state->clk;
948 copy->pclk = state->pclk;
949 copy->div = state->div;
950 copy->planes = state->planes;
Thierry Redingca915b12014-12-08 16:14:45 +0100951
952 return &copy->base;
953}
954
955static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
956 struct drm_crtc_state *state)
957{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200958 __drm_atomic_helper_crtc_destroy_state(state);
Thierry Redingca915b12014-12-08 16:14:45 +0100959 kfree(state);
960}
961
Thierry Redingb95800e2017-11-08 13:40:54 +0100962#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
963
964static const struct debugfs_reg32 tegra_dc_regs[] = {
965 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
966 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
967 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
968 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
969 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
970 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
971 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
972 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
973 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
974 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
975 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
976 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
977 DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
978 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
979 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
980 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
981 DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
982 DEBUGFS_REG32(DC_CMD_INT_STATUS),
983 DEBUGFS_REG32(DC_CMD_INT_MASK),
984 DEBUGFS_REG32(DC_CMD_INT_ENABLE),
985 DEBUGFS_REG32(DC_CMD_INT_TYPE),
986 DEBUGFS_REG32(DC_CMD_INT_POLARITY),
987 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
988 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
989 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
990 DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
991 DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
992 DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
993 DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
994 DEBUGFS_REG32(DC_COM_CRC_CONTROL),
995 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
996 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
997 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
998 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
999 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1000 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1001 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1002 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1003 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1004 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1005 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1006 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1007 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1008 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1009 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1010 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1011 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1012 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1013 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1014 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1015 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1016 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1017 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1018 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1019 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1020 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1021 DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1022 DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1023 DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1024 DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1025 DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1026 DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1027 DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1028 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1029 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1030 DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1031 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1032 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1033 DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1034 DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1035 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1036 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1037 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1038 DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1039 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1040 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1041 DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1042 DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1043 DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1044 DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1045 DEBUGFS_REG32(DC_DISP_ACTIVE),
1046 DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1047 DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1048 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1049 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1050 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1051 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1052 DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1053 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1054 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1055 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1056 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1057 DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1058 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1059 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1060 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1061 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1062 DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1063 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1064 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1065 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1066 DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1067 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1068 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1069 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1070 DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1071 DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1072 DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1073 DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1074 DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1075 DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1076 DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1077 DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1078 DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1079 DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1080 DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1081 DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1082 DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1083 DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1084 DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1085 DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1086 DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1087 DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1088 DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1089 DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1090 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1091 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1092 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1093 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1094 DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1095 DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1096 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1097 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1098 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1099 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1100 DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1101 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1102 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1103 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1104 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1105 DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1106 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1107 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1108 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1109 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1110 DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1111 DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1112 DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1113 DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1114 DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1115 DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1116 DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1117 DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1118 DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1119 DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1120 DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1121 DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1122 DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1123 DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1124 DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1125 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1126 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1127 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1128 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1129 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1130 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1131 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1132 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1133 DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1134 DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1135 DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1136 DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1137 DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1138 DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1139 DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1140 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1141 DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1142 DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1143 DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1144 DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1145 DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1146 DEBUGFS_REG32(DC_WIN_POSITION),
1147 DEBUGFS_REG32(DC_WIN_SIZE),
1148 DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1149 DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1150 DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1151 DEBUGFS_REG32(DC_WIN_DDA_INC),
1152 DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1153 DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1154 DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1155 DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1156 DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1157 DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1158 DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1159 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1160 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1161 DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1162 DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1163 DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1164 DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1165 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1166 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1167 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1168 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1169 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1170 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1171 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1172 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1173 DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1174 DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1175 DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1176 DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1177};
1178
1179static int tegra_dc_show_regs(struct seq_file *s, void *data)
1180{
1181 struct drm_info_node *node = s->private;
1182 struct tegra_dc *dc = node->info_ent->data;
1183 unsigned int i;
1184 int err = 0;
1185
1186 drm_modeset_lock(&dc->base.mutex, NULL);
1187
1188 if (!dc->base.state->active) {
1189 err = -EBUSY;
1190 goto unlock;
1191 }
1192
1193 for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1194 unsigned int offset = tegra_dc_regs[i].offset;
1195
1196 seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1197 offset, tegra_dc_readl(dc, offset));
1198 }
1199
1200unlock:
1201 drm_modeset_unlock(&dc->base.mutex);
1202 return err;
1203}
1204
1205static int tegra_dc_show_crc(struct seq_file *s, void *data)
1206{
1207 struct drm_info_node *node = s->private;
1208 struct tegra_dc *dc = node->info_ent->data;
1209 int err = 0;
1210 u32 value;
1211
1212 drm_modeset_lock(&dc->base.mutex, NULL);
1213
1214 if (!dc->base.state->active) {
1215 err = -EBUSY;
1216 goto unlock;
1217 }
1218
1219 value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1220 tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1221 tegra_dc_commit(dc);
1222
1223 drm_crtc_wait_one_vblank(&dc->base);
1224 drm_crtc_wait_one_vblank(&dc->base);
1225
1226 value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1227 seq_printf(s, "%08x\n", value);
1228
1229 tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1230
1231unlock:
1232 drm_modeset_unlock(&dc->base.mutex);
1233 return err;
1234}
1235
1236static int tegra_dc_show_stats(struct seq_file *s, void *data)
1237{
1238 struct drm_info_node *node = s->private;
1239 struct tegra_dc *dc = node->info_ent->data;
1240
1241 seq_printf(s, "frames: %lu\n", dc->stats.frames);
1242 seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1243 seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1244 seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1245
1246 return 0;
1247}
1248
1249static struct drm_info_list debugfs_files[] = {
1250 { "regs", tegra_dc_show_regs, 0, NULL },
1251 { "crc", tegra_dc_show_crc, 0, NULL },
1252 { "stats", tegra_dc_show_stats, 0, NULL },
1253};
1254
1255static int tegra_dc_late_register(struct drm_crtc *crtc)
1256{
1257 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1258 struct drm_minor *minor = crtc->dev->primary;
1259 struct dentry *root = crtc->debugfs_entry;
1260 struct tegra_dc *dc = to_tegra_dc(crtc);
1261 int err;
1262
1263 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1264 GFP_KERNEL);
1265 if (!dc->debugfs_files)
1266 return -ENOMEM;
1267
1268 for (i = 0; i < count; i++)
1269 dc->debugfs_files[i].data = dc;
1270
1271 err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1272 if (err < 0)
1273 goto free;
1274
1275 return 0;
1276
1277free:
1278 kfree(dc->debugfs_files);
1279 dc->debugfs_files = NULL;
1280
1281 return err;
1282}
1283
1284static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1285{
1286 unsigned int count = ARRAY_SIZE(debugfs_files);
1287 struct drm_minor *minor = crtc->dev->primary;
1288 struct tegra_dc *dc = to_tegra_dc(crtc);
1289
1290 drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1291 kfree(dc->debugfs_files);
1292 dc->debugfs_files = NULL;
1293}
1294
Thierry Redingc49c81e2017-11-08 13:32:05 +01001295static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1296{
1297 struct tegra_dc *dc = to_tegra_dc(crtc);
1298
1299 if (dc->syncpt)
1300 return host1x_syncpt_read(dc->syncpt);
1301
1302 /* fallback to software emulated VBLANK counter */
1303 return drm_crtc_vblank_count(&dc->base);
1304}
1305
1306static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1307{
1308 struct tegra_dc *dc = to_tegra_dc(crtc);
1309 unsigned long value, flags;
1310
1311 spin_lock_irqsave(&dc->lock, flags);
1312
1313 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1314 value |= VBLANK_INT;
1315 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1316
1317 spin_unlock_irqrestore(&dc->lock, flags);
1318
1319 return 0;
1320}
1321
1322static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1323{
1324 struct tegra_dc *dc = to_tegra_dc(crtc);
1325 unsigned long value, flags;
1326
1327 spin_lock_irqsave(&dc->lock, flags);
1328
1329 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1330 value &= ~VBLANK_INT;
1331 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1332
1333 spin_unlock_irqrestore(&dc->lock, flags);
1334}
1335
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001336static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding1503ca42014-11-24 17:41:23 +01001337 .page_flip = drm_atomic_helper_page_flip,
Thierry Reding74f48792014-11-24 17:08:20 +01001338 .set_config = drm_atomic_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +02001339 .destroy = tegra_dc_destroy,
Thierry Redingca915b12014-12-08 16:14:45 +01001340 .reset = tegra_crtc_reset,
1341 .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1342 .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
Thierry Redingb95800e2017-11-08 13:40:54 +01001343 .late_register = tegra_dc_late_register,
1344 .early_unregister = tegra_dc_early_unregister,
Shawn Guo10437d92017-02-07 17:16:32 +08001345 .get_vblank_counter = tegra_dc_get_vblank_counter,
1346 .enable_vblank = tegra_dc_enable_vblank,
1347 .disable_vblank = tegra_dc_disable_vblank,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001348};
1349
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001350static int tegra_dc_set_timings(struct tegra_dc *dc,
1351 struct drm_display_mode *mode)
1352{
Thierry Reding0444c0f2014-04-16 09:22:38 +02001353 unsigned int h_ref_to_sync = 1;
1354 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001355 unsigned long value;
1356
1357 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1358
1359 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1360 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1361
1362 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1363 ((mode->hsync_end - mode->hsync_start) << 0);
1364 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1365
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001366 value = ((mode->vtotal - mode->vsync_end) << 16) |
1367 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +00001368 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1369
1370 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1371 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001372 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1373
1374 value = (mode->vdisplay << 16) | mode->hdisplay;
1375 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1376
1377 return 0;
1378}
1379
Thierry Reding9d910b62015-01-28 15:25:54 +01001380/**
1381 * tegra_dc_state_setup_clock - check clock settings and store them in atomic
1382 * state
1383 * @dc: display controller
1384 * @crtc_state: CRTC atomic state
1385 * @clk: parent clock for display controller
1386 * @pclk: pixel clock
1387 * @div: shift clock divider
1388 *
1389 * Returns:
1390 * 0 on success or a negative error-code on failure.
1391 */
Thierry Redingca915b12014-12-08 16:14:45 +01001392int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1393 struct drm_crtc_state *crtc_state,
1394 struct clk *clk, unsigned long pclk,
1395 unsigned int div)
1396{
1397 struct tegra_dc_state *state = to_dc_state(crtc_state);
1398
Thierry Redingd2982742015-01-22 08:48:25 +01001399 if (!clk_has_parent(dc->clk, clk))
1400 return -EINVAL;
1401
Thierry Redingca915b12014-12-08 16:14:45 +01001402 state->clk = clk;
1403 state->pclk = pclk;
1404 state->div = div;
1405
1406 return 0;
1407}
1408
Thierry Reding76d59ed2014-12-19 15:09:16 +01001409static void tegra_dc_commit_state(struct tegra_dc *dc,
1410 struct tegra_dc_state *state)
1411{
1412 u32 value;
1413 int err;
1414
1415 err = clk_set_parent(dc->clk, state->clk);
1416 if (err < 0)
1417 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1418
1419 /*
1420 * Outputs may not want to change the parent clock rate. This is only
1421 * relevant to Tegra20 where only a single display PLL is available.
1422 * Since that PLL would typically be used for HDMI, an internal LVDS
1423 * panel would need to be driven by some other clock such as PLL_P
1424 * which is shared with other peripherals. Changing the clock rate
1425 * should therefore be avoided.
1426 */
1427 if (state->pclk > 0) {
1428 err = clk_set_rate(state->clk, state->pclk);
1429 if (err < 0)
1430 dev_err(dc->dev,
1431 "failed to set clock rate to %lu Hz\n",
1432 state->pclk);
1433 }
1434
1435 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
1436 state->div);
1437 DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
1438
1439 value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
1440 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Reding39e08af2017-08-30 17:38:39 +02001441
1442 err = clk_set_rate(dc->clk, state->pclk);
1443 if (err < 0)
1444 dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1445 dc->clk, state->pclk, err);
Thierry Reding76d59ed2014-12-19 15:09:16 +01001446}
1447
Thierry Reding003fc842015-08-03 13:16:26 +02001448static void tegra_dc_stop(struct tegra_dc *dc)
1449{
1450 u32 value;
1451
1452 /* stop the display controller */
1453 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1454 value &= ~DISP_CTRL_MODE_MASK;
1455 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1456
1457 tegra_dc_commit(dc);
1458}
1459
1460static bool tegra_dc_idle(struct tegra_dc *dc)
1461{
1462 u32 value;
1463
1464 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1465
1466 return (value & DISP_CTRL_MODE_MASK) == 0;
1467}
1468
1469static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1470{
1471 timeout = jiffies + msecs_to_jiffies(timeout);
1472
1473 while (time_before(jiffies, timeout)) {
1474 if (tegra_dc_idle(dc))
1475 return 0;
1476
1477 usleep_range(1000, 2000);
1478 }
1479
1480 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1481 return -ETIMEDOUT;
1482}
1483
Laurent Pinchart64581712017-06-30 12:36:45 +03001484static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1485 struct drm_crtc_state *old_state)
Thierry Reding003fc842015-08-03 13:16:26 +02001486{
1487 struct tegra_dc *dc = to_tegra_dc(crtc);
1488 u32 value;
1489
1490 if (!tegra_dc_idle(dc)) {
1491 tegra_dc_stop(dc);
1492
1493 /*
1494 * Ignore the return value, there isn't anything useful to do
1495 * in case this fails.
1496 */
1497 tegra_dc_wait_idle(dc, 100);
1498 }
1499
1500 /*
1501 * This should really be part of the RGB encoder driver, but clearing
1502 * these bits has the side-effect of stopping the display controller.
1503 * When that happens no VBLANK interrupts will be raised. At the same
1504 * time the encoder is disabled before the display controller, so the
1505 * above code is always going to timeout waiting for the controller
1506 * to go idle.
1507 *
1508 * Given the close coupling between the RGB encoder and the display
1509 * controller doing it here is still kind of okay. None of the other
1510 * encoder drivers require these bits to be cleared.
1511 *
1512 * XXX: Perhaps given that the display controller is switched off at
1513 * this point anyway maybe clearing these bits isn't even useful for
1514 * the RGB encoder?
1515 */
1516 if (dc->rgb) {
1517 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1518 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1519 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1520 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1521 }
1522
1523 tegra_dc_stats_reset(&dc->stats);
1524 drm_crtc_vblank_off(crtc);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001525
1526 pm_runtime_put_sync(dc->dev);
Thierry Reding003fc842015-08-03 13:16:26 +02001527}
1528
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001529static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1530 struct drm_crtc_state *old_state)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001531{
Thierry Reding4aa3df72014-11-24 16:27:13 +01001532 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Thierry Reding76d59ed2014-12-19 15:09:16 +01001533 struct tegra_dc_state *state = to_dc_state(crtc->state);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001534 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001535 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001536
Thierry Reding33a8eb82015-08-03 13:20:49 +02001537 pm_runtime_get_sync(dc->dev);
1538
1539 /* initialize display controller */
1540 if (dc->syncpt) {
1541 u32 syncpt = host1x_syncpt_id(dc->syncpt);
1542
1543 value = SYNCPT_CNTRL_NO_STALL;
1544 tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1545
1546 value = SYNCPT_VSYNC_ENABLE | syncpt;
1547 tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
1548 }
1549
1550 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1551 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1552 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1553
1554 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1555 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1556 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1557
1558 /* initialize timer */
1559 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1560 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1561 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1562
1563 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1564 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1565 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1566
1567 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1568 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1569 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1570
1571 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1572 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1573 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1574
Thierry Reding7116e9a2017-11-13 11:20:48 +01001575 if (dc->soc->supports_background_color)
1576 tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
1577 else
Thierry Reding33a8eb82015-08-03 13:20:49 +02001578 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1579
1580 /* apply PLL and pixel clock changes */
Thierry Reding76d59ed2014-12-19 15:09:16 +01001581 tegra_dc_commit_state(dc, state);
1582
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001583 /* program display mode */
1584 tegra_dc_set_timings(dc, mode);
1585
Thierry Reding8620fc62013-12-12 11:03:59 +01001586 /* interlacing isn't supported yet, so disable it */
1587 if (dc->soc->supports_interlacing) {
1588 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1589 value &= ~INTERLACE_ENABLE;
1590 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1591 }
Thierry Reding666cb872014-12-08 16:32:47 +01001592
1593 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1594 value &= ~DISP_CTRL_MODE_MASK;
1595 value |= DISP_CTRL_MODE_C_DISPLAY;
1596 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1597
1598 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1599 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1600 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1601 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1602
1603 tegra_dc_commit(dc);
Thierry Reding23fb4742012-11-28 11:38:24 +01001604
Thierry Reding8ff64c12014-10-08 14:48:51 +02001605 drm_crtc_vblank_on(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001606}
1607
Thierry Reding4aa3df72014-11-24 16:27:13 +01001608static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1609 struct drm_crtc_state *state)
1610{
1611 return 0;
1612}
1613
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001614static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1615 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001616{
Thierry Reding1503ca42014-11-24 17:41:23 +01001617 struct tegra_dc *dc = to_tegra_dc(crtc);
1618
1619 if (crtc->state->event) {
1620 crtc->state->event->pipe = drm_crtc_index(crtc);
1621
1622 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1623
1624 dc->event = crtc->state->event;
1625 crtc->state->event = NULL;
1626 }
Thierry Reding4aa3df72014-11-24 16:27:13 +01001627}
1628
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001629static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1630 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001631{
Thierry Reding47802b02014-11-26 12:28:39 +01001632 struct tegra_dc_state *state = to_dc_state(crtc->state);
1633 struct tegra_dc *dc = to_tegra_dc(crtc);
1634
1635 tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
1636 tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
Thierry Reding4aa3df72014-11-24 16:27:13 +01001637}
1638
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001639static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +01001640 .atomic_check = tegra_crtc_atomic_check,
1641 .atomic_begin = tegra_crtc_atomic_begin,
1642 .atomic_flush = tegra_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001643 .atomic_enable = tegra_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +03001644 .atomic_disable = tegra_crtc_atomic_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001645};
1646
Thierry Redingc49c81e2017-11-08 13:32:05 +01001647static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
1648{
1649 struct drm_device *drm = dc->base.dev;
1650 struct drm_crtc *crtc = &dc->base;
1651 unsigned long flags, base;
1652 struct tegra_bo *bo;
1653
1654 spin_lock_irqsave(&drm->event_lock, flags);
1655
1656 if (!dc->event) {
1657 spin_unlock_irqrestore(&drm->event_lock, flags);
1658 return;
1659 }
1660
1661 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
1662
1663 spin_lock(&dc->lock);
1664
1665 /* check if new start address has been latched */
1666 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
1667 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
1668 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
1669 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
1670
1671 spin_unlock(&dc->lock);
1672
1673 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
1674 drm_crtc_send_vblank_event(crtc, dc->event);
1675 drm_crtc_vblank_put(crtc);
1676 dc->event = NULL;
1677 }
1678
1679 spin_unlock_irqrestore(&drm->event_lock, flags);
1680}
1681
Thierry Reding6e5ff992012-11-28 11:45:47 +01001682static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001683{
1684 struct tegra_dc *dc = data;
1685 unsigned long status;
1686
1687 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1688 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1689
1690 if (status & FRAME_END_INT) {
1691 /*
1692 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1693 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001694 dc->stats.frames++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001695 }
1696
1697 if (status & VBLANK_INT) {
1698 /*
1699 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1700 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001701 drm_crtc_handle_vblank(&dc->base);
Thierry Reding3c03c462012-11-28 12:00:18 +01001702 tegra_dc_finish_page_flip(dc);
Thierry Reding791ddb12015-07-28 21:27:05 +02001703 dc->stats.vblank++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001704 }
1705
1706 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1707 /*
1708 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1709 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001710 dc->stats.underflow++;
1711 }
1712
1713 if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1714 /*
1715 dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1716 */
1717 dc->stats.overflow++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001718 }
1719
1720 return IRQ_HANDLED;
1721}
1722
Thierry Reding53fa7f72013-09-24 15:35:40 +02001723static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001724{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001725 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001726 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
Thierry Reding776dc382013-10-14 14:43:22 +02001727 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001728 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001729 struct drm_plane *primary = NULL;
1730 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001731 int err;
1732
Thierry Reding617dd7c2017-08-30 12:48:31 +02001733 dc->syncpt = host1x_syncpt_request(client, flags);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001734 if (!dc->syncpt)
1735 dev_warn(dc->dev, "failed to allocate syncpoint\n");
1736
Thierry Redingdf06b752014-06-26 21:41:53 +02001737 if (tegra->domain) {
1738 err = iommu_attach_device(tegra->domain, dc->dev);
1739 if (err < 0) {
1740 dev_err(dc->dev, "failed to attach to domain: %d\n",
1741 err);
1742 return err;
1743 }
1744
1745 dc->domain = tegra->domain;
1746 }
1747
Thierry Redingc7679302014-10-21 13:51:53 +02001748 primary = tegra_dc_primary_plane_create(drm, dc);
1749 if (IS_ERR(primary)) {
1750 err = PTR_ERR(primary);
1751 goto cleanup;
1752 }
1753
1754 if (dc->soc->supports_cursor) {
1755 cursor = tegra_dc_cursor_plane_create(drm, dc);
1756 if (IS_ERR(cursor)) {
1757 err = PTR_ERR(cursor);
1758 goto cleanup;
1759 }
1760 }
1761
1762 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001763 &tegra_crtc_funcs, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +02001764 if (err < 0)
1765 goto cleanup;
1766
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001767 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1768
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001769 /*
1770 * Keep track of the minimum pitch alignment across all display
1771 * controllers.
1772 */
1773 if (dc->soc->pitch_align > tegra->pitch_align)
1774 tegra->pitch_align = dc->soc->pitch_align;
1775
Thierry Reding9910f5c2014-05-22 09:57:15 +02001776 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001777 if (err < 0 && err != -ENODEV) {
1778 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001779 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001780 }
1781
Thierry Reding9910f5c2014-05-22 09:57:15 +02001782 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001783 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001784 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001785
Thierry Reding6e5ff992012-11-28 11:45:47 +01001786 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001787 dev_name(dc->dev), dc);
1788 if (err < 0) {
1789 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1790 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001791 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001792 }
1793
1794 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001795
1796cleanup:
1797 if (cursor)
1798 drm_plane_cleanup(cursor);
1799
1800 if (primary)
1801 drm_plane_cleanup(primary);
1802
1803 if (tegra->domain) {
1804 iommu_detach_device(tegra->domain, dc->dev);
1805 dc->domain = NULL;
1806 }
1807
1808 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001809}
1810
Thierry Reding53fa7f72013-09-24 15:35:40 +02001811static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001812{
Thierry Reding776dc382013-10-14 14:43:22 +02001813 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001814 int err;
1815
1816 devm_free_irq(dc->dev, dc->irq, dc);
1817
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001818 err = tegra_dc_rgb_exit(dc);
1819 if (err) {
1820 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1821 return err;
1822 }
1823
Thierry Redingdf06b752014-06-26 21:41:53 +02001824 if (dc->domain) {
1825 iommu_detach_device(dc->domain, dc->dev);
1826 dc->domain = NULL;
1827 }
1828
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001829 host1x_syncpt_free(dc->syncpt);
1830
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001831 return 0;
1832}
1833
1834static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001835 .init = tegra_dc_init,
1836 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001837};
1838
Thierry Reding8620fc62013-12-12 11:03:59 +01001839static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001840 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001841 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001842 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001843 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001844 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001845 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001846 .broken_reset = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001847};
1848
1849static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001850 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001851 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001852 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001853 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001854 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001855 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001856 .broken_reset = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001857};
1858
1859static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001860 .supports_background_color = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001861 .supports_interlacing = false,
1862 .supports_cursor = false,
1863 .supports_block_linear = false,
1864 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001865 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001866 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001867};
1868
1869static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001870 .supports_background_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001871 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001872 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001873 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001874 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001875 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001876 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001877};
1878
Thierry Reding5b4f5162015-03-27 10:31:58 +01001879static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001880 .supports_background_color = true,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001881 .supports_interlacing = true,
1882 .supports_cursor = true,
1883 .supports_block_linear = true,
1884 .pitch_align = 64,
1885 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001886 .broken_reset = false,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001887};
1888
Thierry Reding8620fc62013-12-12 11:03:59 +01001889static const struct of_device_id tegra_dc_of_match[] = {
1890 {
Thierry Reding5b4f5162015-03-27 10:31:58 +01001891 .compatible = "nvidia,tegra210-dc",
1892 .data = &tegra210_dc_soc_info,
1893 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001894 .compatible = "nvidia,tegra124-dc",
1895 .data = &tegra124_dc_soc_info,
1896 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001897 .compatible = "nvidia,tegra114-dc",
1898 .data = &tegra114_dc_soc_info,
1899 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001900 .compatible = "nvidia,tegra30-dc",
1901 .data = &tegra30_dc_soc_info,
1902 }, {
1903 .compatible = "nvidia,tegra20-dc",
1904 .data = &tegra20_dc_soc_info,
1905 }, {
1906 /* sentinel */
1907 }
1908};
Stephen Warrenef707282014-06-18 16:21:55 -06001909MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001910
Thierry Reding13411dd2014-01-09 17:08:36 +01001911static int tegra_dc_parse_dt(struct tegra_dc *dc)
1912{
1913 struct device_node *np;
1914 u32 value = 0;
1915 int err;
1916
1917 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1918 if (err < 0) {
1919 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1920
1921 /*
1922 * If the nvidia,head property isn't present, try to find the
1923 * correct head number by looking up the position of this
1924 * display controller's node within the device tree. Assuming
1925 * that the nodes are ordered properly in the DTS file and
1926 * that the translation into a flattened device tree blob
1927 * preserves that ordering this will actually yield the right
1928 * head number.
1929 *
1930 * If those assumptions don't hold, this will still work for
1931 * cases where only a single display controller is used.
1932 */
1933 for_each_matching_node(np, tegra_dc_of_match) {
Julia Lawallcf6b1742015-10-24 16:42:31 +02001934 if (np == dc->dev->of_node) {
1935 of_node_put(np);
Thierry Reding13411dd2014-01-09 17:08:36 +01001936 break;
Julia Lawallcf6b1742015-10-24 16:42:31 +02001937 }
Thierry Reding13411dd2014-01-09 17:08:36 +01001938
1939 value++;
1940 }
1941 }
1942
1943 dc->pipe = value;
1944
1945 return 0;
1946}
1947
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001948static int tegra_dc_probe(struct platform_device *pdev)
1949{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001950 struct resource *regs;
1951 struct tegra_dc *dc;
1952 int err;
1953
1954 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1955 if (!dc)
1956 return -ENOMEM;
1957
Thierry Redingb9ff7ae2017-08-21 16:35:17 +02001958 dc->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding8620fc62013-12-12 11:03:59 +01001959
Thierry Reding6e5ff992012-11-28 11:45:47 +01001960 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001961 INIT_LIST_HEAD(&dc->list);
1962 dc->dev = &pdev->dev;
1963
Thierry Reding13411dd2014-01-09 17:08:36 +01001964 err = tegra_dc_parse_dt(dc);
1965 if (err < 0)
1966 return err;
1967
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001968 dc->clk = devm_clk_get(&pdev->dev, NULL);
1969 if (IS_ERR(dc->clk)) {
1970 dev_err(&pdev->dev, "failed to get clock\n");
1971 return PTR_ERR(dc->clk);
1972 }
1973
Stephen Warrenca480802013-11-06 16:20:54 -07001974 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1975 if (IS_ERR(dc->rst)) {
1976 dev_err(&pdev->dev, "failed to get reset\n");
1977 return PTR_ERR(dc->rst);
1978 }
1979
Thierry Redinga2f2f742017-08-30 17:41:00 +02001980 /* assert reset and disable clock */
1981 if (!dc->soc->broken_reset) {
1982 err = clk_prepare_enable(dc->clk);
1983 if (err < 0)
1984 return err;
1985
1986 usleep_range(2000, 4000);
1987
1988 err = reset_control_assert(dc->rst);
1989 if (err < 0)
1990 return err;
1991
1992 usleep_range(2000, 4000);
1993
1994 clk_disable_unprepare(dc->clk);
1995 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02001996
Thierry Reding9c012702014-07-07 15:32:53 +02001997 if (dc->soc->has_powergate) {
1998 if (dc->pipe == 0)
1999 dc->powergate = TEGRA_POWERGATE_DIS;
2000 else
2001 dc->powergate = TEGRA_POWERGATE_DISB;
2002
Thierry Reding33a8eb82015-08-03 13:20:49 +02002003 tegra_powergate_power_off(dc->powergate);
Thierry Reding9c012702014-07-07 15:32:53 +02002004 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002005
2006 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01002007 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
2008 if (IS_ERR(dc->regs))
2009 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002010
2011 dc->irq = platform_get_irq(pdev, 0);
2012 if (dc->irq < 0) {
2013 dev_err(&pdev->dev, "failed to get IRQ\n");
2014 return -ENXIO;
2015 }
2016
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002017 err = tegra_dc_rgb_probe(dc);
2018 if (err < 0 && err != -ENODEV) {
2019 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
2020 return err;
2021 }
2022
Thierry Reding33a8eb82015-08-03 13:20:49 +02002023 platform_set_drvdata(pdev, dc);
2024 pm_runtime_enable(&pdev->dev);
2025
2026 INIT_LIST_HEAD(&dc->client.list);
2027 dc->client.ops = &dc_client_ops;
2028 dc->client.dev = &pdev->dev;
2029
Thierry Reding776dc382013-10-14 14:43:22 +02002030 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002031 if (err < 0) {
2032 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2033 err);
2034 return err;
2035 }
2036
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002037 return 0;
2038}
2039
2040static int tegra_dc_remove(struct platform_device *pdev)
2041{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002042 struct tegra_dc *dc = platform_get_drvdata(pdev);
2043 int err;
2044
Thierry Reding776dc382013-10-14 14:43:22 +02002045 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002046 if (err < 0) {
2047 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2048 err);
2049 return err;
2050 }
2051
Thierry Reding59d29c02013-10-14 14:26:42 +02002052 err = tegra_dc_rgb_remove(dc);
2053 if (err < 0) {
2054 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
2055 return err;
2056 }
2057
Thierry Reding33a8eb82015-08-03 13:20:49 +02002058 pm_runtime_disable(&pdev->dev);
2059
2060 return 0;
2061}
2062
2063#ifdef CONFIG_PM
2064static int tegra_dc_suspend(struct device *dev)
2065{
2066 struct tegra_dc *dc = dev_get_drvdata(dev);
2067 int err;
2068
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002069 if (!dc->soc->broken_reset) {
2070 err = reset_control_assert(dc->rst);
2071 if (err < 0) {
2072 dev_err(dev, "failed to assert reset: %d\n", err);
2073 return err;
2074 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002075 }
Thierry Reding9c012702014-07-07 15:32:53 +02002076
2077 if (dc->soc->has_powergate)
2078 tegra_powergate_power_off(dc->powergate);
2079
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002080 clk_disable_unprepare(dc->clk);
2081
2082 return 0;
2083}
2084
Thierry Reding33a8eb82015-08-03 13:20:49 +02002085static int tegra_dc_resume(struct device *dev)
2086{
2087 struct tegra_dc *dc = dev_get_drvdata(dev);
2088 int err;
2089
2090 if (dc->soc->has_powergate) {
2091 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2092 dc->rst);
2093 if (err < 0) {
2094 dev_err(dev, "failed to power partition: %d\n", err);
2095 return err;
2096 }
2097 } else {
2098 err = clk_prepare_enable(dc->clk);
2099 if (err < 0) {
2100 dev_err(dev, "failed to enable clock: %d\n", err);
2101 return err;
2102 }
2103
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002104 if (!dc->soc->broken_reset) {
2105 err = reset_control_deassert(dc->rst);
2106 if (err < 0) {
2107 dev_err(dev,
2108 "failed to deassert reset: %d\n", err);
2109 return err;
2110 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002111 }
2112 }
2113
2114 return 0;
2115}
2116#endif
2117
2118static const struct dev_pm_ops tegra_dc_pm_ops = {
2119 SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
2120};
2121
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002122struct platform_driver tegra_dc_driver = {
2123 .driver = {
2124 .name = "tegra-dc",
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002125 .of_match_table = tegra_dc_of_match,
Thierry Reding33a8eb82015-08-03 13:20:49 +02002126 .pm = &tegra_dc_pm_ops,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002127 },
2128 .probe = tegra_dc_probe,
2129 .remove = tegra_dc_remove,
2130};