blob: 775e51c7a5c078d9559f3c6113fa4e220f635832 [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 Reding8f604f82014-11-28 13:14:55 +0100420static void tegra_plane_reset(struct drm_plane *plane)
421{
422 struct tegra_plane_state *state;
423
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100424 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +0200425 __drm_atomic_helper_plane_destroy_state(plane->state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100426
427 kfree(plane->state);
428 plane->state = NULL;
429
430 state = kzalloc(sizeof(*state), GFP_KERNEL);
431 if (state) {
432 plane->state = &state->base;
433 plane->state->plane = plane;
434 }
435}
436
437static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
438{
439 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
440 struct tegra_plane_state *copy;
441
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100442 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Reding8f604f82014-11-28 13:14:55 +0100443 if (!copy)
444 return NULL;
445
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100446 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
447 copy->tiling = state->tiling;
448 copy->format = state->format;
449 copy->swap = state->swap;
Thierry Reding8f604f82014-11-28 13:14:55 +0100450
451 return &copy->base;
452}
453
454static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
455 struct drm_plane_state *state)
456{
Daniel Vetter2f701692016-05-09 16:34:10 +0200457 __drm_atomic_helper_plane_destroy_state(state);
Thierry Reding8f604f82014-11-28 13:14:55 +0100458 kfree(state);
459}
460
Thierry Redingc1cb4b62017-08-30 18:04:12 +0200461static const struct drm_plane_funcs tegra_plane_funcs = {
Thierry Reding07866962014-11-24 17:08:06 +0100462 .update_plane = drm_atomic_helper_update_plane,
463 .disable_plane = drm_atomic_helper_disable_plane,
Thierry Reding6f70ec52017-08-30 18:02:51 +0200464 .destroy = tegra_plane_destroy,
Thierry Reding8f604f82014-11-28 13:14:55 +0100465 .reset = tegra_plane_reset,
466 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
467 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100468};
469
Thierry Reding47802b02014-11-26 12:28:39 +0100470static int tegra_plane_state_add(struct tegra_plane *plane,
471 struct drm_plane_state *state)
472{
473 struct drm_crtc_state *crtc_state;
474 struct tegra_dc_state *tegra;
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300475 struct drm_rect clip;
476 int err;
Thierry Reding47802b02014-11-26 12:28:39 +0100477
478 /* Propagate errors from allocation or locking failures. */
479 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
480 if (IS_ERR(crtc_state))
481 return PTR_ERR(crtc_state);
482
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300483 clip.x1 = 0;
484 clip.y1 = 0;
485 clip.x2 = crtc_state->mode.hdisplay;
486 clip.y2 = crtc_state->mode.vdisplay;
487
488 /* Check plane state for visibility and calculate clipping bounds */
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200489 err = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
490 0, INT_MAX, true, true);
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300491 if (err < 0)
492 return err;
493
Thierry Reding47802b02014-11-26 12:28:39 +0100494 tegra = to_dc_state(crtc_state);
495
496 tegra->planes |= WIN_A_ACT_REQ << plane->index;
497
498 return 0;
499}
500
Thierry Reding4aa3df72014-11-24 16:27:13 +0100501static int tegra_plane_atomic_check(struct drm_plane *plane,
502 struct drm_plane_state *state)
503{
Thierry Reding8f604f82014-11-28 13:14:55 +0100504 struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
505 struct tegra_bo_tiling *tiling = &plane_state->tiling;
Thierry Reding47802b02014-11-26 12:28:39 +0100506 struct tegra_plane *tegra = to_tegra_plane(plane);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100507 struct tegra_dc *dc = to_tegra_dc(state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200508 int err;
509
Thierry Reding4aa3df72014-11-24 16:27:13 +0100510 /* no need for further checks if the plane is being disabled */
511 if (!state->crtc)
512 return 0;
513
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200514 err = tegra_dc_format(state->fb->format->format, &plane_state->format,
Thierry Reding8f604f82014-11-28 13:14:55 +0100515 &plane_state->swap);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100516 if (err < 0)
517 return err;
518
Thierry Reding8f604f82014-11-28 13:14:55 +0100519 err = tegra_fb_get_tiling(state->fb, tiling);
520 if (err < 0)
521 return err;
522
523 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
Thierry Reding4aa3df72014-11-24 16:27:13 +0100524 !dc->soc->supports_block_linear) {
525 DRM_ERROR("hardware doesn't support block linear mode\n");
526 return -EINVAL;
527 }
528
529 /*
530 * Tegra doesn't support different strides for U and V planes so we
531 * error out if the user tries to display a framebuffer with such a
532 * configuration.
533 */
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200534 if (state->fb->format->num_planes > 2) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100535 if (state->fb->pitches[2] != state->fb->pitches[1]) {
536 DRM_ERROR("unsupported UV-plane configuration\n");
537 return -EINVAL;
538 }
539 }
540
Thierry Reding47802b02014-11-26 12:28:39 +0100541 err = tegra_plane_state_add(tegra, state);
542 if (err < 0)
543 return err;
544
Thierry Reding4aa3df72014-11-24 16:27:13 +0100545 return 0;
546}
547
Thierry Redinga4bfa092017-08-30 17:34:10 +0200548static void tegra_plane_atomic_disable(struct drm_plane *plane,
549 struct drm_plane_state *old_state)
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300550{
Thierry Redinga4bfa092017-08-30 17:34:10 +0200551 struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
552 struct tegra_plane *p = to_tegra_plane(plane);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300553 unsigned long flags;
554 u32 value;
555
Thierry Redinga4bfa092017-08-30 17:34:10 +0200556 /* rien ne va plus */
557 if (!old_state || !old_state->crtc)
558 return;
559
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300560 spin_lock_irqsave(&dc->lock, flags);
561
Thierry Redinga4bfa092017-08-30 17:34:10 +0200562 value = WINDOW_A_SELECT << p->index;
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300563 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
564
565 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
566 value &= ~WIN_ENABLE;
567 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
568
569 spin_unlock_irqrestore(&dc->lock, flags);
570}
571
Thierry Reding4aa3df72014-11-24 16:27:13 +0100572static void tegra_plane_atomic_update(struct drm_plane *plane,
573 struct drm_plane_state *old_state)
574{
Thierry Reding8f604f82014-11-28 13:14:55 +0100575 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100576 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
577 struct drm_framebuffer *fb = plane->state->fb;
578 struct tegra_plane *p = to_tegra_plane(plane);
579 struct tegra_dc_window window;
580 unsigned int i;
Thierry Reding4aa3df72014-11-24 16:27:13 +0100581
582 /* rien ne va plus */
583 if (!plane->state->crtc || !plane->state->fb)
584 return;
585
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300586 if (!plane->state->visible)
Thierry Redinga4bfa092017-08-30 17:34:10 +0200587 return tegra_plane_atomic_disable(plane, old_state);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300588
Thierry Redingc7679302014-10-21 13:51:53 +0200589 memset(&window, 0, sizeof(window));
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300590 window.src.x = plane->state->src.x1 >> 16;
591 window.src.y = plane->state->src.y1 >> 16;
592 window.src.w = drm_rect_width(&plane->state->src) >> 16;
593 window.src.h = drm_rect_height(&plane->state->src) >> 16;
594 window.dst.x = plane->state->dst.x1;
595 window.dst.y = plane->state->dst.y1;
596 window.dst.w = drm_rect_width(&plane->state->dst);
597 window.dst.h = drm_rect_height(&plane->state->dst);
Ville Syrjälä272725c2016-12-14 23:32:20 +0200598 window.bits_per_pixel = fb->format->cpp[0] * 8;
Thierry Redingc7679302014-10-21 13:51:53 +0200599 window.bottom_up = tegra_fb_is_bottom_up(fb);
600
Thierry Reding8f604f82014-11-28 13:14:55 +0100601 /* copy from state */
602 window.tiling = state->tiling;
603 window.format = state->format;
604 window.swap = state->swap;
Thierry Redingc7679302014-10-21 13:51:53 +0200605
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200606 for (i = 0; i < fb->format->num_planes; i++) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100607 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingc7679302014-10-21 13:51:53 +0200608
Thierry Reding4aa3df72014-11-24 16:27:13 +0100609 window.base[i] = bo->paddr + fb->offsets[i];
Dmitry Osipenko08ee0172016-08-21 11:57:58 +0300610
611 /*
612 * Tegra uses a shared stride for UV planes. Framebuffers are
613 * already checked for this in the tegra_plane_atomic_check()
614 * function, so it's safe to ignore the V-plane pitch here.
615 */
616 if (i < 2)
617 window.stride[i] = fb->pitches[i];
Thierry Reding4aa3df72014-11-24 16:27:13 +0100618 }
Thierry Redingc7679302014-10-21 13:51:53 +0200619
Thierry Reding4aa3df72014-11-24 16:27:13 +0100620 tegra_dc_setup_window(dc, p->index, &window);
Thierry Redingc7679302014-10-21 13:51:53 +0200621}
622
Thierry Redinga4bfa092017-08-30 17:34:10 +0200623static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100624 .atomic_check = tegra_plane_atomic_check,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100625 .atomic_disable = tegra_plane_atomic_disable,
Thierry Redinga4bfa092017-08-30 17:34:10 +0200626 .atomic_update = tegra_plane_atomic_update,
Thierry Redingc7679302014-10-21 13:51:53 +0200627};
628
629static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
630 struct tegra_dc *dc)
631{
Thierry Reding518e6222014-12-16 18:04:08 +0100632 /*
633 * Ideally this would use drm_crtc_mask(), but that would require the
634 * CRTC to already be in the mode_config's list of CRTCs. However, it
635 * will only be added to that list in the drm_crtc_init_with_planes()
636 * (in tegra_dc_init()), which in turn requires registration of these
637 * planes. So we have ourselves a nice little chicken and egg problem
638 * here.
639 *
640 * We work around this by manually creating the mask from the number
641 * of CRTCs that have been registered, and should therefore always be
642 * the same as drm_crtc_index() after registration.
643 */
644 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Redingc7679302014-10-21 13:51:53 +0200645 struct tegra_plane *plane;
646 unsigned int num_formats;
647 const u32 *formats;
648 int err;
649
650 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
651 if (!plane)
652 return ERR_PTR(-ENOMEM);
653
654 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
655 formats = tegra_primary_plane_formats;
656
Thierry Reding518e6222014-12-16 18:04:08 +0100657 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc1cb4b62017-08-30 18:04:12 +0200658 &tegra_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700659 num_formats, NULL,
660 DRM_PLANE_TYPE_PRIMARY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200661 if (err < 0) {
662 kfree(plane);
663 return ERR_PTR(err);
664 }
665
Thierry Redinga4bfa092017-08-30 17:34:10 +0200666 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100667
Thierry Redingc7679302014-10-21 13:51:53 +0200668 return &plane->base;
669}
670
671static const u32 tegra_cursor_plane_formats[] = {
672 DRM_FORMAT_RGBA8888,
673};
674
Thierry Reding4aa3df72014-11-24 16:27:13 +0100675static int tegra_cursor_atomic_check(struct drm_plane *plane,
676 struct drm_plane_state *state)
Thierry Redingc7679302014-10-21 13:51:53 +0200677{
Thierry Reding47802b02014-11-26 12:28:39 +0100678 struct tegra_plane *tegra = to_tegra_plane(plane);
679 int err;
680
Thierry Reding4aa3df72014-11-24 16:27:13 +0100681 /* no need for further checks if the plane is being disabled */
682 if (!state->crtc)
683 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200684
685 /* scaling not supported for cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100686 if ((state->src_w >> 16 != state->crtc_w) ||
687 (state->src_h >> 16 != state->crtc_h))
Thierry Redingc7679302014-10-21 13:51:53 +0200688 return -EINVAL;
689
690 /* only square cursors supported */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100691 if (state->src_w != state->src_h)
Thierry Redingc7679302014-10-21 13:51:53 +0200692 return -EINVAL;
693
Thierry Reding4aa3df72014-11-24 16:27:13 +0100694 if (state->crtc_w != 32 && state->crtc_w != 64 &&
695 state->crtc_w != 128 && state->crtc_w != 256)
696 return -EINVAL;
697
Thierry Reding47802b02014-11-26 12:28:39 +0100698 err = tegra_plane_state_add(tegra, state);
699 if (err < 0)
700 return err;
701
Thierry Reding4aa3df72014-11-24 16:27:13 +0100702 return 0;
703}
704
705static void tegra_cursor_atomic_update(struct drm_plane *plane,
706 struct drm_plane_state *old_state)
707{
708 struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
709 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
710 struct drm_plane_state *state = plane->state;
711 u32 value = CURSOR_CLIP_DISPLAY;
712
713 /* rien ne va plus */
714 if (!plane->state->crtc || !plane->state->fb)
715 return;
716
717 switch (state->crtc_w) {
Thierry Redingc7679302014-10-21 13:51:53 +0200718 case 32:
719 value |= CURSOR_SIZE_32x32;
720 break;
721
722 case 64:
723 value |= CURSOR_SIZE_64x64;
724 break;
725
726 case 128:
727 value |= CURSOR_SIZE_128x128;
728 break;
729
730 case 256:
731 value |= CURSOR_SIZE_256x256;
732 break;
733
734 default:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100735 WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
736 state->crtc_h);
737 return;
Thierry Redingc7679302014-10-21 13:51:53 +0200738 }
739
740 value |= (bo->paddr >> 10) & 0x3fffff;
741 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
742
743#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
744 value = (bo->paddr >> 32) & 0x3;
745 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
746#endif
747
748 /* enable cursor and set blend mode */
749 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
750 value |= CURSOR_ENABLE;
751 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
752
753 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
754 value &= ~CURSOR_DST_BLEND_MASK;
755 value &= ~CURSOR_SRC_BLEND_MASK;
756 value |= CURSOR_MODE_NORMAL;
757 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
758 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
759 value |= CURSOR_ALPHA;
760 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
761
762 /* position the cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100763 value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
Thierry Redingc7679302014-10-21 13:51:53 +0200764 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
Thierry Redingc7679302014-10-21 13:51:53 +0200765}
766
Thierry Reding4aa3df72014-11-24 16:27:13 +0100767static void tegra_cursor_atomic_disable(struct drm_plane *plane,
768 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200769{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100770 struct tegra_dc *dc;
Thierry Redingc7679302014-10-21 13:51:53 +0200771 u32 value;
772
Thierry Reding4aa3df72014-11-24 16:27:13 +0100773 /* rien ne va plus */
774 if (!old_state || !old_state->crtc)
775 return;
776
777 dc = to_tegra_dc(old_state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200778
779 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
780 value &= ~CURSOR_ENABLE;
781 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc7679302014-10-21 13:51:53 +0200782}
783
Thierry Reding4aa3df72014-11-24 16:27:13 +0100784static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100785 .atomic_check = tegra_cursor_atomic_check,
786 .atomic_update = tegra_cursor_atomic_update,
787 .atomic_disable = tegra_cursor_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200788};
789
790static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
791 struct tegra_dc *dc)
792{
793 struct tegra_plane *plane;
794 unsigned int num_formats;
795 const u32 *formats;
796 int err;
797
798 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
799 if (!plane)
800 return ERR_PTR(-ENOMEM);
801
Thierry Reding47802b02014-11-26 12:28:39 +0100802 /*
Thierry Redinga1df3b22015-07-21 16:42:30 +0200803 * This index is kind of fake. The cursor isn't a regular plane, but
804 * its update and activation request bits in DC_CMD_STATE_CONTROL do
805 * use the same programming. Setting this fake index here allows the
806 * code in tegra_add_plane_state() to do the right thing without the
807 * need to special-casing the cursor plane.
Thierry Reding47802b02014-11-26 12:28:39 +0100808 */
809 plane->index = 6;
810
Thierry Redingc7679302014-10-21 13:51:53 +0200811 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
812 formats = tegra_cursor_plane_formats;
813
814 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
Thierry Redingc1cb4b62017-08-30 18:04:12 +0200815 &tegra_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700816 num_formats, NULL,
817 DRM_PLANE_TYPE_CURSOR, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200818 if (err < 0) {
819 kfree(plane);
820 return ERR_PTR(err);
821 }
822
Thierry Reding4aa3df72014-11-24 16:27:13 +0100823 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
824
Thierry Redingc7679302014-10-21 13:51:53 +0200825 return &plane->base;
826}
827
Thierry Redingc7679302014-10-21 13:51:53 +0200828static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100829 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100830 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100831 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100832 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100833 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100834 DRM_FORMAT_YUV420,
835 DRM_FORMAT_YUV422,
836};
837
Thierry Redingc7679302014-10-21 13:51:53 +0200838static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
839 struct tegra_dc *dc,
840 unsigned int index)
841{
842 struct tegra_plane *plane;
843 unsigned int num_formats;
844 const u32 *formats;
845 int err;
846
847 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
848 if (!plane)
849 return ERR_PTR(-ENOMEM);
850
851 plane->index = index;
852
853 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
854 formats = tegra_overlay_plane_formats;
855
856 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
Thierry Reding301e0dd2017-08-30 18:04:12 +0200857 &tegra_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700858 num_formats, NULL,
859 DRM_PLANE_TYPE_OVERLAY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200860 if (err < 0) {
861 kfree(plane);
862 return ERR_PTR(err);
863 }
864
Thierry Redinga4bfa092017-08-30 17:34:10 +0200865 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100866
Thierry Redingc7679302014-10-21 13:51:53 +0200867 return &plane->base;
868}
869
Thierry Redingf34bc782012-11-04 21:47:13 +0100870static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
871{
Thierry Redingc7679302014-10-21 13:51:53 +0200872 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100873 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100874
875 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200876 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
877 if (IS_ERR(plane))
878 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100879 }
880
881 return 0;
882}
883
Thierry Redingf002abc2013-10-14 14:06:02 +0200884static void tegra_dc_destroy(struct drm_crtc *crtc)
885{
886 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +0200887}
888
Thierry Redingca915b12014-12-08 16:14:45 +0100889static void tegra_crtc_reset(struct drm_crtc *crtc)
890{
891 struct tegra_dc_state *state;
892
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100893 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200894 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100895
Thierry Redingca915b12014-12-08 16:14:45 +0100896 kfree(crtc->state);
897 crtc->state = NULL;
898
899 state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Reding332bbe72015-01-28 15:03:31 +0100900 if (state) {
Thierry Redingca915b12014-12-08 16:14:45 +0100901 crtc->state = &state->base;
Thierry Reding332bbe72015-01-28 15:03:31 +0100902 crtc->state->crtc = crtc;
903 }
Thierry Reding31930d42015-07-02 17:04:06 +0200904
905 drm_crtc_vblank_reset(crtc);
Thierry Redingca915b12014-12-08 16:14:45 +0100906}
907
908static struct drm_crtc_state *
909tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
910{
911 struct tegra_dc_state *state = to_dc_state(crtc->state);
912 struct tegra_dc_state *copy;
913
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100914 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Redingca915b12014-12-08 16:14:45 +0100915 if (!copy)
916 return NULL;
917
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100918 __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
919 copy->clk = state->clk;
920 copy->pclk = state->pclk;
921 copy->div = state->div;
922 copy->planes = state->planes;
Thierry Redingca915b12014-12-08 16:14:45 +0100923
924 return &copy->base;
925}
926
927static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
928 struct drm_crtc_state *state)
929{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200930 __drm_atomic_helper_crtc_destroy_state(state);
Thierry Redingca915b12014-12-08 16:14:45 +0100931 kfree(state);
932}
933
Thierry Redingb95800e2017-11-08 13:40:54 +0100934#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
935
936static const struct debugfs_reg32 tegra_dc_regs[] = {
937 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
938 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
939 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
940 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
941 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
942 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
943 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
944 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
945 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
946 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
947 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
948 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
949 DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
950 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
951 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
952 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
953 DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
954 DEBUGFS_REG32(DC_CMD_INT_STATUS),
955 DEBUGFS_REG32(DC_CMD_INT_MASK),
956 DEBUGFS_REG32(DC_CMD_INT_ENABLE),
957 DEBUGFS_REG32(DC_CMD_INT_TYPE),
958 DEBUGFS_REG32(DC_CMD_INT_POLARITY),
959 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
960 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
961 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
962 DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
963 DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
964 DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
965 DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
966 DEBUGFS_REG32(DC_COM_CRC_CONTROL),
967 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
968 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
969 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
970 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
971 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
972 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
973 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
974 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
975 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
976 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
977 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
978 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
979 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
980 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
981 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
982 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
983 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
984 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
985 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
986 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
987 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
988 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
989 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
990 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
991 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
992 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
993 DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
994 DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
995 DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
996 DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
997 DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
998 DEBUGFS_REG32(DC_COM_SPI_CONTROL),
999 DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1000 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1001 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1002 DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1003 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1004 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1005 DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1006 DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1007 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1008 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1009 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1010 DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1011 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1012 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1013 DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1014 DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1015 DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1016 DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1017 DEBUGFS_REG32(DC_DISP_ACTIVE),
1018 DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1019 DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1020 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1021 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1022 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1023 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1024 DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1025 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1026 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1027 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1028 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1029 DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1030 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1031 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1032 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1033 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1034 DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1035 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1036 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1037 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1038 DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1039 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1040 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1041 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1042 DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1043 DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1044 DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1045 DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1046 DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1047 DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1048 DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1049 DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1050 DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1051 DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1052 DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1053 DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1054 DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1055 DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1056 DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1057 DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1058 DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1059 DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1060 DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1061 DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1062 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1063 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1064 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1065 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1066 DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1067 DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1068 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1069 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1070 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1071 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1072 DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1073 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1074 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1075 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1076 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1077 DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1078 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1079 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1080 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1081 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1082 DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1083 DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1084 DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1085 DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1086 DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1087 DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1088 DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1089 DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1090 DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1091 DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1092 DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1093 DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1094 DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1095 DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1096 DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1097 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1098 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1099 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1100 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1101 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1102 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1103 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1104 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1105 DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1106 DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1107 DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1108 DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1109 DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1110 DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1111 DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1112 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1113 DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1114 DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1115 DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1116 DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1117 DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1118 DEBUGFS_REG32(DC_WIN_POSITION),
1119 DEBUGFS_REG32(DC_WIN_SIZE),
1120 DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1121 DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1122 DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1123 DEBUGFS_REG32(DC_WIN_DDA_INC),
1124 DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1125 DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1126 DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1127 DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1128 DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1129 DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1130 DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1131 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1132 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1133 DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1134 DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1135 DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1136 DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1137 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1138 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1139 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1140 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1141 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1142 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1143 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1144 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1145 DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1146 DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1147 DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1148 DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1149};
1150
1151static int tegra_dc_show_regs(struct seq_file *s, void *data)
1152{
1153 struct drm_info_node *node = s->private;
1154 struct tegra_dc *dc = node->info_ent->data;
1155 unsigned int i;
1156 int err = 0;
1157
1158 drm_modeset_lock(&dc->base.mutex, NULL);
1159
1160 if (!dc->base.state->active) {
1161 err = -EBUSY;
1162 goto unlock;
1163 }
1164
1165 for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1166 unsigned int offset = tegra_dc_regs[i].offset;
1167
1168 seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1169 offset, tegra_dc_readl(dc, offset));
1170 }
1171
1172unlock:
1173 drm_modeset_unlock(&dc->base.mutex);
1174 return err;
1175}
1176
1177static int tegra_dc_show_crc(struct seq_file *s, void *data)
1178{
1179 struct drm_info_node *node = s->private;
1180 struct tegra_dc *dc = node->info_ent->data;
1181 int err = 0;
1182 u32 value;
1183
1184 drm_modeset_lock(&dc->base.mutex, NULL);
1185
1186 if (!dc->base.state->active) {
1187 err = -EBUSY;
1188 goto unlock;
1189 }
1190
1191 value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1192 tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1193 tegra_dc_commit(dc);
1194
1195 drm_crtc_wait_one_vblank(&dc->base);
1196 drm_crtc_wait_one_vblank(&dc->base);
1197
1198 value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1199 seq_printf(s, "%08x\n", value);
1200
1201 tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1202
1203unlock:
1204 drm_modeset_unlock(&dc->base.mutex);
1205 return err;
1206}
1207
1208static int tegra_dc_show_stats(struct seq_file *s, void *data)
1209{
1210 struct drm_info_node *node = s->private;
1211 struct tegra_dc *dc = node->info_ent->data;
1212
1213 seq_printf(s, "frames: %lu\n", dc->stats.frames);
1214 seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1215 seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1216 seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1217
1218 return 0;
1219}
1220
1221static struct drm_info_list debugfs_files[] = {
1222 { "regs", tegra_dc_show_regs, 0, NULL },
1223 { "crc", tegra_dc_show_crc, 0, NULL },
1224 { "stats", tegra_dc_show_stats, 0, NULL },
1225};
1226
1227static int tegra_dc_late_register(struct drm_crtc *crtc)
1228{
1229 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1230 struct drm_minor *minor = crtc->dev->primary;
1231 struct dentry *root = crtc->debugfs_entry;
1232 struct tegra_dc *dc = to_tegra_dc(crtc);
1233 int err;
1234
1235 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1236 GFP_KERNEL);
1237 if (!dc->debugfs_files)
1238 return -ENOMEM;
1239
1240 for (i = 0; i < count; i++)
1241 dc->debugfs_files[i].data = dc;
1242
1243 err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1244 if (err < 0)
1245 goto free;
1246
1247 return 0;
1248
1249free:
1250 kfree(dc->debugfs_files);
1251 dc->debugfs_files = NULL;
1252
1253 return err;
1254}
1255
1256static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1257{
1258 unsigned int count = ARRAY_SIZE(debugfs_files);
1259 struct drm_minor *minor = crtc->dev->primary;
1260 struct tegra_dc *dc = to_tegra_dc(crtc);
1261
1262 drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1263 kfree(dc->debugfs_files);
1264 dc->debugfs_files = NULL;
1265}
1266
Thierry Redingc49c81e2017-11-08 13:32:05 +01001267static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1268{
1269 struct tegra_dc *dc = to_tegra_dc(crtc);
1270
1271 if (dc->syncpt)
1272 return host1x_syncpt_read(dc->syncpt);
1273
1274 /* fallback to software emulated VBLANK counter */
1275 return drm_crtc_vblank_count(&dc->base);
1276}
1277
1278static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1279{
1280 struct tegra_dc *dc = to_tegra_dc(crtc);
1281 unsigned long value, flags;
1282
1283 spin_lock_irqsave(&dc->lock, flags);
1284
1285 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1286 value |= VBLANK_INT;
1287 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1288
1289 spin_unlock_irqrestore(&dc->lock, flags);
1290
1291 return 0;
1292}
1293
1294static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1295{
1296 struct tegra_dc *dc = to_tegra_dc(crtc);
1297 unsigned long value, flags;
1298
1299 spin_lock_irqsave(&dc->lock, flags);
1300
1301 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1302 value &= ~VBLANK_INT;
1303 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1304
1305 spin_unlock_irqrestore(&dc->lock, flags);
1306}
1307
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001308static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding1503ca42014-11-24 17:41:23 +01001309 .page_flip = drm_atomic_helper_page_flip,
Thierry Reding74f48792014-11-24 17:08:20 +01001310 .set_config = drm_atomic_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +02001311 .destroy = tegra_dc_destroy,
Thierry Redingca915b12014-12-08 16:14:45 +01001312 .reset = tegra_crtc_reset,
1313 .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1314 .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
Thierry Redingb95800e2017-11-08 13:40:54 +01001315 .late_register = tegra_dc_late_register,
1316 .early_unregister = tegra_dc_early_unregister,
Shawn Guo10437d92017-02-07 17:16:32 +08001317 .get_vblank_counter = tegra_dc_get_vblank_counter,
1318 .enable_vblank = tegra_dc_enable_vblank,
1319 .disable_vblank = tegra_dc_disable_vblank,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001320};
1321
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001322static int tegra_dc_set_timings(struct tegra_dc *dc,
1323 struct drm_display_mode *mode)
1324{
Thierry Reding0444c0f2014-04-16 09:22:38 +02001325 unsigned int h_ref_to_sync = 1;
1326 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001327 unsigned long value;
1328
1329 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1330
1331 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1332 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1333
1334 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1335 ((mode->hsync_end - mode->hsync_start) << 0);
1336 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1337
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001338 value = ((mode->vtotal - mode->vsync_end) << 16) |
1339 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +00001340 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1341
1342 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1343 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001344 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1345
1346 value = (mode->vdisplay << 16) | mode->hdisplay;
1347 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1348
1349 return 0;
1350}
1351
Thierry Reding9d910b62015-01-28 15:25:54 +01001352/**
1353 * tegra_dc_state_setup_clock - check clock settings and store them in atomic
1354 * state
1355 * @dc: display controller
1356 * @crtc_state: CRTC atomic state
1357 * @clk: parent clock for display controller
1358 * @pclk: pixel clock
1359 * @div: shift clock divider
1360 *
1361 * Returns:
1362 * 0 on success or a negative error-code on failure.
1363 */
Thierry Redingca915b12014-12-08 16:14:45 +01001364int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1365 struct drm_crtc_state *crtc_state,
1366 struct clk *clk, unsigned long pclk,
1367 unsigned int div)
1368{
1369 struct tegra_dc_state *state = to_dc_state(crtc_state);
1370
Thierry Redingd2982742015-01-22 08:48:25 +01001371 if (!clk_has_parent(dc->clk, clk))
1372 return -EINVAL;
1373
Thierry Redingca915b12014-12-08 16:14:45 +01001374 state->clk = clk;
1375 state->pclk = pclk;
1376 state->div = div;
1377
1378 return 0;
1379}
1380
Thierry Reding76d59ed2014-12-19 15:09:16 +01001381static void tegra_dc_commit_state(struct tegra_dc *dc,
1382 struct tegra_dc_state *state)
1383{
1384 u32 value;
1385 int err;
1386
1387 err = clk_set_parent(dc->clk, state->clk);
1388 if (err < 0)
1389 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1390
1391 /*
1392 * Outputs may not want to change the parent clock rate. This is only
1393 * relevant to Tegra20 where only a single display PLL is available.
1394 * Since that PLL would typically be used for HDMI, an internal LVDS
1395 * panel would need to be driven by some other clock such as PLL_P
1396 * which is shared with other peripherals. Changing the clock rate
1397 * should therefore be avoided.
1398 */
1399 if (state->pclk > 0) {
1400 err = clk_set_rate(state->clk, state->pclk);
1401 if (err < 0)
1402 dev_err(dc->dev,
1403 "failed to set clock rate to %lu Hz\n",
1404 state->pclk);
1405 }
1406
1407 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
1408 state->div);
1409 DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
1410
1411 value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
1412 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
Thierry Reding39e08af2017-08-30 17:38:39 +02001413
1414 err = clk_set_rate(dc->clk, state->pclk);
1415 if (err < 0)
1416 dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1417 dc->clk, state->pclk, err);
Thierry Reding76d59ed2014-12-19 15:09:16 +01001418}
1419
Thierry Reding003fc842015-08-03 13:16:26 +02001420static void tegra_dc_stop(struct tegra_dc *dc)
1421{
1422 u32 value;
1423
1424 /* stop the display controller */
1425 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1426 value &= ~DISP_CTRL_MODE_MASK;
1427 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1428
1429 tegra_dc_commit(dc);
1430}
1431
1432static bool tegra_dc_idle(struct tegra_dc *dc)
1433{
1434 u32 value;
1435
1436 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1437
1438 return (value & DISP_CTRL_MODE_MASK) == 0;
1439}
1440
1441static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1442{
1443 timeout = jiffies + msecs_to_jiffies(timeout);
1444
1445 while (time_before(jiffies, timeout)) {
1446 if (tegra_dc_idle(dc))
1447 return 0;
1448
1449 usleep_range(1000, 2000);
1450 }
1451
1452 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1453 return -ETIMEDOUT;
1454}
1455
Laurent Pinchart64581712017-06-30 12:36:45 +03001456static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1457 struct drm_crtc_state *old_state)
Thierry Reding003fc842015-08-03 13:16:26 +02001458{
1459 struct tegra_dc *dc = to_tegra_dc(crtc);
1460 u32 value;
1461
1462 if (!tegra_dc_idle(dc)) {
1463 tegra_dc_stop(dc);
1464
1465 /*
1466 * Ignore the return value, there isn't anything useful to do
1467 * in case this fails.
1468 */
1469 tegra_dc_wait_idle(dc, 100);
1470 }
1471
1472 /*
1473 * This should really be part of the RGB encoder driver, but clearing
1474 * these bits has the side-effect of stopping the display controller.
1475 * When that happens no VBLANK interrupts will be raised. At the same
1476 * time the encoder is disabled before the display controller, so the
1477 * above code is always going to timeout waiting for the controller
1478 * to go idle.
1479 *
1480 * Given the close coupling between the RGB encoder and the display
1481 * controller doing it here is still kind of okay. None of the other
1482 * encoder drivers require these bits to be cleared.
1483 *
1484 * XXX: Perhaps given that the display controller is switched off at
1485 * this point anyway maybe clearing these bits isn't even useful for
1486 * the RGB encoder?
1487 */
1488 if (dc->rgb) {
1489 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1490 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1491 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1492 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1493 }
1494
1495 tegra_dc_stats_reset(&dc->stats);
1496 drm_crtc_vblank_off(crtc);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001497
Thierry Reding9d99ab62017-10-12 17:40:46 +02001498 spin_lock_irq(&crtc->dev->event_lock);
1499
1500 if (crtc->state->event) {
1501 drm_crtc_send_vblank_event(crtc, crtc->state->event);
1502 crtc->state->event = NULL;
1503 }
1504
1505 spin_unlock_irq(&crtc->dev->event_lock);
1506
Thierry Reding33a8eb82015-08-03 13:20:49 +02001507 pm_runtime_put_sync(dc->dev);
Thierry Reding003fc842015-08-03 13:16:26 +02001508}
1509
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001510static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1511 struct drm_crtc_state *old_state)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001512{
Thierry Reding4aa3df72014-11-24 16:27:13 +01001513 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Thierry Reding76d59ed2014-12-19 15:09:16 +01001514 struct tegra_dc_state *state = to_dc_state(crtc->state);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001515 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001516 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001517
Thierry Reding33a8eb82015-08-03 13:20:49 +02001518 pm_runtime_get_sync(dc->dev);
1519
1520 /* initialize display controller */
1521 if (dc->syncpt) {
1522 u32 syncpt = host1x_syncpt_id(dc->syncpt);
1523
1524 value = SYNCPT_CNTRL_NO_STALL;
1525 tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1526
1527 value = SYNCPT_VSYNC_ENABLE | syncpt;
1528 tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
1529 }
1530
1531 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1532 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1533 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1534
1535 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1536 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1537 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1538
1539 /* initialize timer */
1540 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1541 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1542 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1543
1544 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1545 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1546 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1547
1548 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1549 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1550 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1551
1552 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1553 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1554 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1555
Thierry Reding7116e9a2017-11-13 11:20:48 +01001556 if (dc->soc->supports_background_color)
1557 tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
1558 else
Thierry Reding33a8eb82015-08-03 13:20:49 +02001559 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1560
1561 /* apply PLL and pixel clock changes */
Thierry Reding76d59ed2014-12-19 15:09:16 +01001562 tegra_dc_commit_state(dc, state);
1563
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001564 /* program display mode */
1565 tegra_dc_set_timings(dc, mode);
1566
Thierry Reding8620fc62013-12-12 11:03:59 +01001567 /* interlacing isn't supported yet, so disable it */
1568 if (dc->soc->supports_interlacing) {
1569 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1570 value &= ~INTERLACE_ENABLE;
1571 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1572 }
Thierry Reding666cb872014-12-08 16:32:47 +01001573
1574 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1575 value &= ~DISP_CTRL_MODE_MASK;
1576 value |= DISP_CTRL_MODE_C_DISPLAY;
1577 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1578
1579 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1580 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1581 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1582 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1583
1584 tegra_dc_commit(dc);
Thierry Reding23fb4742012-11-28 11:38:24 +01001585
Thierry Reding8ff64c12014-10-08 14:48:51 +02001586 drm_crtc_vblank_on(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001587}
1588
Thierry Reding4aa3df72014-11-24 16:27:13 +01001589static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1590 struct drm_crtc_state *state)
1591{
1592 return 0;
1593}
1594
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001595static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1596 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001597{
Thierry Reding9d99ab62017-10-12 17:40:46 +02001598 unsigned long flags;
Thierry Reding1503ca42014-11-24 17:41:23 +01001599
1600 if (crtc->state->event) {
Thierry Reding9d99ab62017-10-12 17:40:46 +02001601 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Thierry Reding1503ca42014-11-24 17:41:23 +01001602
Thierry Reding9d99ab62017-10-12 17:40:46 +02001603 if (drm_crtc_vblank_get(crtc) != 0)
1604 drm_crtc_send_vblank_event(crtc, crtc->state->event);
1605 else
1606 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
Thierry Reding1503ca42014-11-24 17:41:23 +01001607
Thierry Reding9d99ab62017-10-12 17:40:46 +02001608 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
1609
Thierry Reding1503ca42014-11-24 17:41:23 +01001610 crtc->state->event = NULL;
1611 }
Thierry Reding4aa3df72014-11-24 16:27:13 +01001612}
1613
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001614static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1615 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001616{
Thierry Reding47802b02014-11-26 12:28:39 +01001617 struct tegra_dc_state *state = to_dc_state(crtc->state);
1618 struct tegra_dc *dc = to_tegra_dc(crtc);
1619
1620 tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
1621 tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
Thierry Reding4aa3df72014-11-24 16:27:13 +01001622}
1623
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001624static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +01001625 .atomic_check = tegra_crtc_atomic_check,
1626 .atomic_begin = tegra_crtc_atomic_begin,
1627 .atomic_flush = tegra_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001628 .atomic_enable = tegra_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +03001629 .atomic_disable = tegra_crtc_atomic_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001630};
1631
Thierry Reding6e5ff992012-11-28 11:45:47 +01001632static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001633{
1634 struct tegra_dc *dc = data;
1635 unsigned long status;
1636
1637 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1638 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1639
1640 if (status & FRAME_END_INT) {
1641 /*
1642 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1643 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001644 dc->stats.frames++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001645 }
1646
1647 if (status & VBLANK_INT) {
1648 /*
1649 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1650 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001651 drm_crtc_handle_vblank(&dc->base);
Thierry Reding791ddb12015-07-28 21:27:05 +02001652 dc->stats.vblank++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001653 }
1654
1655 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1656 /*
1657 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1658 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001659 dc->stats.underflow++;
1660 }
1661
1662 if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1663 /*
1664 dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1665 */
1666 dc->stats.overflow++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001667 }
1668
1669 return IRQ_HANDLED;
1670}
1671
Thierry Reding53fa7f72013-09-24 15:35:40 +02001672static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001673{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001674 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001675 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
Thierry Reding776dc382013-10-14 14:43:22 +02001676 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001677 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001678 struct drm_plane *primary = NULL;
1679 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001680 int err;
1681
Thierry Reding617dd7c2017-08-30 12:48:31 +02001682 dc->syncpt = host1x_syncpt_request(client, flags);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001683 if (!dc->syncpt)
1684 dev_warn(dc->dev, "failed to allocate syncpoint\n");
1685
Thierry Redingdf06b752014-06-26 21:41:53 +02001686 if (tegra->domain) {
1687 err = iommu_attach_device(tegra->domain, dc->dev);
1688 if (err < 0) {
1689 dev_err(dc->dev, "failed to attach to domain: %d\n",
1690 err);
1691 return err;
1692 }
1693
1694 dc->domain = tegra->domain;
1695 }
1696
Thierry Redingc7679302014-10-21 13:51:53 +02001697 primary = tegra_dc_primary_plane_create(drm, dc);
1698 if (IS_ERR(primary)) {
1699 err = PTR_ERR(primary);
1700 goto cleanup;
1701 }
1702
1703 if (dc->soc->supports_cursor) {
1704 cursor = tegra_dc_cursor_plane_create(drm, dc);
1705 if (IS_ERR(cursor)) {
1706 err = PTR_ERR(cursor);
1707 goto cleanup;
1708 }
1709 }
1710
1711 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001712 &tegra_crtc_funcs, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +02001713 if (err < 0)
1714 goto cleanup;
1715
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001716 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1717
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001718 /*
1719 * Keep track of the minimum pitch alignment across all display
1720 * controllers.
1721 */
1722 if (dc->soc->pitch_align > tegra->pitch_align)
1723 tegra->pitch_align = dc->soc->pitch_align;
1724
Thierry Reding9910f5c2014-05-22 09:57:15 +02001725 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001726 if (err < 0 && err != -ENODEV) {
1727 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001728 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001729 }
1730
Thierry Reding9910f5c2014-05-22 09:57:15 +02001731 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001732 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001733 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001734
Thierry Reding6e5ff992012-11-28 11:45:47 +01001735 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001736 dev_name(dc->dev), dc);
1737 if (err < 0) {
1738 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1739 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001740 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001741 }
1742
1743 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001744
1745cleanup:
1746 if (cursor)
1747 drm_plane_cleanup(cursor);
1748
1749 if (primary)
1750 drm_plane_cleanup(primary);
1751
1752 if (tegra->domain) {
1753 iommu_detach_device(tegra->domain, dc->dev);
1754 dc->domain = NULL;
1755 }
1756
1757 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001758}
1759
Thierry Reding53fa7f72013-09-24 15:35:40 +02001760static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001761{
Thierry Reding776dc382013-10-14 14:43:22 +02001762 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001763 int err;
1764
1765 devm_free_irq(dc->dev, dc->irq, dc);
1766
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001767 err = tegra_dc_rgb_exit(dc);
1768 if (err) {
1769 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1770 return err;
1771 }
1772
Thierry Redingdf06b752014-06-26 21:41:53 +02001773 if (dc->domain) {
1774 iommu_detach_device(dc->domain, dc->dev);
1775 dc->domain = NULL;
1776 }
1777
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001778 host1x_syncpt_free(dc->syncpt);
1779
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001780 return 0;
1781}
1782
1783static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001784 .init = tegra_dc_init,
1785 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001786};
1787
Thierry Reding8620fc62013-12-12 11:03:59 +01001788static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001789 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001790 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001791 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001792 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001793 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001794 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001795 .broken_reset = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001796};
1797
1798static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001799 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001800 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001801 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001802 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001803 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001804 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001805 .broken_reset = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001806};
1807
1808static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001809 .supports_background_color = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001810 .supports_interlacing = false,
1811 .supports_cursor = false,
1812 .supports_block_linear = false,
1813 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001814 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001815 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001816};
1817
1818static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001819 .supports_background_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001820 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001821 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001822 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001823 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001824 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001825 .broken_reset = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001826};
1827
Thierry Reding5b4f5162015-03-27 10:31:58 +01001828static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001829 .supports_background_color = true,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001830 .supports_interlacing = true,
1831 .supports_cursor = true,
1832 .supports_block_linear = true,
1833 .pitch_align = 64,
1834 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001835 .broken_reset = false,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001836};
1837
Thierry Reding8620fc62013-12-12 11:03:59 +01001838static const struct of_device_id tegra_dc_of_match[] = {
1839 {
Thierry Reding5b4f5162015-03-27 10:31:58 +01001840 .compatible = "nvidia,tegra210-dc",
1841 .data = &tegra210_dc_soc_info,
1842 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001843 .compatible = "nvidia,tegra124-dc",
1844 .data = &tegra124_dc_soc_info,
1845 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001846 .compatible = "nvidia,tegra114-dc",
1847 .data = &tegra114_dc_soc_info,
1848 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001849 .compatible = "nvidia,tegra30-dc",
1850 .data = &tegra30_dc_soc_info,
1851 }, {
1852 .compatible = "nvidia,tegra20-dc",
1853 .data = &tegra20_dc_soc_info,
1854 }, {
1855 /* sentinel */
1856 }
1857};
Stephen Warrenef707282014-06-18 16:21:55 -06001858MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001859
Thierry Reding13411dd2014-01-09 17:08:36 +01001860static int tegra_dc_parse_dt(struct tegra_dc *dc)
1861{
1862 struct device_node *np;
1863 u32 value = 0;
1864 int err;
1865
1866 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1867 if (err < 0) {
1868 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1869
1870 /*
1871 * If the nvidia,head property isn't present, try to find the
1872 * correct head number by looking up the position of this
1873 * display controller's node within the device tree. Assuming
1874 * that the nodes are ordered properly in the DTS file and
1875 * that the translation into a flattened device tree blob
1876 * preserves that ordering this will actually yield the right
1877 * head number.
1878 *
1879 * If those assumptions don't hold, this will still work for
1880 * cases where only a single display controller is used.
1881 */
1882 for_each_matching_node(np, tegra_dc_of_match) {
Julia Lawallcf6b1742015-10-24 16:42:31 +02001883 if (np == dc->dev->of_node) {
1884 of_node_put(np);
Thierry Reding13411dd2014-01-09 17:08:36 +01001885 break;
Julia Lawallcf6b1742015-10-24 16:42:31 +02001886 }
Thierry Reding13411dd2014-01-09 17:08:36 +01001887
1888 value++;
1889 }
1890 }
1891
1892 dc->pipe = value;
1893
1894 return 0;
1895}
1896
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001897static int tegra_dc_probe(struct platform_device *pdev)
1898{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001899 struct resource *regs;
1900 struct tegra_dc *dc;
1901 int err;
1902
1903 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1904 if (!dc)
1905 return -ENOMEM;
1906
Thierry Redingb9ff7ae2017-08-21 16:35:17 +02001907 dc->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding8620fc62013-12-12 11:03:59 +01001908
Thierry Reding6e5ff992012-11-28 11:45:47 +01001909 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001910 INIT_LIST_HEAD(&dc->list);
1911 dc->dev = &pdev->dev;
1912
Thierry Reding13411dd2014-01-09 17:08:36 +01001913 err = tegra_dc_parse_dt(dc);
1914 if (err < 0)
1915 return err;
1916
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001917 dc->clk = devm_clk_get(&pdev->dev, NULL);
1918 if (IS_ERR(dc->clk)) {
1919 dev_err(&pdev->dev, "failed to get clock\n");
1920 return PTR_ERR(dc->clk);
1921 }
1922
Stephen Warrenca480802013-11-06 16:20:54 -07001923 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1924 if (IS_ERR(dc->rst)) {
1925 dev_err(&pdev->dev, "failed to get reset\n");
1926 return PTR_ERR(dc->rst);
1927 }
1928
Thierry Redinga2f2f742017-08-30 17:41:00 +02001929 /* assert reset and disable clock */
1930 if (!dc->soc->broken_reset) {
1931 err = clk_prepare_enable(dc->clk);
1932 if (err < 0)
1933 return err;
1934
1935 usleep_range(2000, 4000);
1936
1937 err = reset_control_assert(dc->rst);
1938 if (err < 0)
1939 return err;
1940
1941 usleep_range(2000, 4000);
1942
1943 clk_disable_unprepare(dc->clk);
1944 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02001945
Thierry Reding9c012702014-07-07 15:32:53 +02001946 if (dc->soc->has_powergate) {
1947 if (dc->pipe == 0)
1948 dc->powergate = TEGRA_POWERGATE_DIS;
1949 else
1950 dc->powergate = TEGRA_POWERGATE_DISB;
1951
Thierry Reding33a8eb82015-08-03 13:20:49 +02001952 tegra_powergate_power_off(dc->powergate);
Thierry Reding9c012702014-07-07 15:32:53 +02001953 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001954
1955 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001956 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1957 if (IS_ERR(dc->regs))
1958 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001959
1960 dc->irq = platform_get_irq(pdev, 0);
1961 if (dc->irq < 0) {
1962 dev_err(&pdev->dev, "failed to get IRQ\n");
1963 return -ENXIO;
1964 }
1965
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001966 err = tegra_dc_rgb_probe(dc);
1967 if (err < 0 && err != -ENODEV) {
1968 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1969 return err;
1970 }
1971
Thierry Reding33a8eb82015-08-03 13:20:49 +02001972 platform_set_drvdata(pdev, dc);
1973 pm_runtime_enable(&pdev->dev);
1974
1975 INIT_LIST_HEAD(&dc->client.list);
1976 dc->client.ops = &dc_client_ops;
1977 dc->client.dev = &pdev->dev;
1978
Thierry Reding776dc382013-10-14 14:43:22 +02001979 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001980 if (err < 0) {
1981 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1982 err);
1983 return err;
1984 }
1985
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001986 return 0;
1987}
1988
1989static int tegra_dc_remove(struct platform_device *pdev)
1990{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001991 struct tegra_dc *dc = platform_get_drvdata(pdev);
1992 int err;
1993
Thierry Reding776dc382013-10-14 14:43:22 +02001994 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001995 if (err < 0) {
1996 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1997 err);
1998 return err;
1999 }
2000
Thierry Reding59d29c02013-10-14 14:26:42 +02002001 err = tegra_dc_rgb_remove(dc);
2002 if (err < 0) {
2003 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
2004 return err;
2005 }
2006
Thierry Reding33a8eb82015-08-03 13:20:49 +02002007 pm_runtime_disable(&pdev->dev);
2008
2009 return 0;
2010}
2011
2012#ifdef CONFIG_PM
2013static int tegra_dc_suspend(struct device *dev)
2014{
2015 struct tegra_dc *dc = dev_get_drvdata(dev);
2016 int err;
2017
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002018 if (!dc->soc->broken_reset) {
2019 err = reset_control_assert(dc->rst);
2020 if (err < 0) {
2021 dev_err(dev, "failed to assert reset: %d\n", err);
2022 return err;
2023 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002024 }
Thierry Reding9c012702014-07-07 15:32:53 +02002025
2026 if (dc->soc->has_powergate)
2027 tegra_powergate_power_off(dc->powergate);
2028
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002029 clk_disable_unprepare(dc->clk);
2030
2031 return 0;
2032}
2033
Thierry Reding33a8eb82015-08-03 13:20:49 +02002034static int tegra_dc_resume(struct device *dev)
2035{
2036 struct tegra_dc *dc = dev_get_drvdata(dev);
2037 int err;
2038
2039 if (dc->soc->has_powergate) {
2040 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2041 dc->rst);
2042 if (err < 0) {
2043 dev_err(dev, "failed to power partition: %d\n", err);
2044 return err;
2045 }
2046 } else {
2047 err = clk_prepare_enable(dc->clk);
2048 if (err < 0) {
2049 dev_err(dev, "failed to enable clock: %d\n", err);
2050 return err;
2051 }
2052
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002053 if (!dc->soc->broken_reset) {
2054 err = reset_control_deassert(dc->rst);
2055 if (err < 0) {
2056 dev_err(dev,
2057 "failed to deassert reset: %d\n", err);
2058 return err;
2059 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002060 }
2061 }
2062
2063 return 0;
2064}
2065#endif
2066
2067static const struct dev_pm_ops tegra_dc_pm_ops = {
2068 SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
2069};
2070
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002071struct platform_driver tegra_dc_driver = {
2072 .driver = {
2073 .name = "tegra-dc",
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002074 .of_match_table = tegra_dc_of_match,
Thierry Reding33a8eb82015-08-03 13:20:49 +02002075 .pm = &tegra_dc_pm_ops,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002076 },
2077 .probe = tegra_dc_probe,
2078 .remove = tegra_dc_remove,
2079};