blob: 906752d8662253e35834c2cddc6a9072f245eae3 [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 Reding47307952017-08-30 17:42:54 +020022#include "hub.h"
Thierry Reding5acd3512017-11-10 15:27:25 +010023#include "plane.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000024
Thierry Reding9d441892014-11-24 17:02:53 +010025#include <drm/drm_atomic.h>
Thierry Reding4aa3df72014-11-24 16:27:13 +010026#include <drm/drm_atomic_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010027#include <drm/drm_plane_helper.h>
28
Thierry Reding791ddb12015-07-28 21:27:05 +020029static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
30{
31 stats->frames = 0;
32 stats->vblank = 0;
33 stats->underflow = 0;
34 stats->overflow = 0;
35}
36
Thierry Redingd700ba72014-12-08 15:50:04 +010037/*
Thierry Reding86df2562014-12-08 16:03:53 +010038 * Reads the active copy of a register. This takes the dc->lock spinlock to
39 * prevent races with the VBLANK processing which also needs access to the
40 * active copy of some registers.
41 */
42static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
43{
44 unsigned long flags;
45 u32 value;
46
47 spin_lock_irqsave(&dc->lock, flags);
48
49 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
50 value = tegra_dc_readl(dc, offset);
51 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
52
53 spin_unlock_irqrestore(&dc->lock, flags);
54 return value;
55}
56
Thierry Redingc57997b2017-10-12 19:12:57 +020057bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
58{
59 struct device_node *np = dc->dev->of_node;
60 struct of_phandle_iterator it;
61 int err;
62
63 of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
64 if (it.node == dev->of_node)
65 return true;
66
67 return false;
68}
69
Thierry Reding86df2562014-12-08 16:03:53 +010070/*
Thierry Redingd700ba72014-12-08 15:50:04 +010071 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
72 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
73 * Latching happens mmediately if the display controller is in STOP mode or
74 * on the next frame boundary otherwise.
75 *
76 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
77 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
78 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
79 * into the ACTIVE copy, either immediately if the display controller is in
80 * STOP mode, or at the next frame boundary otherwise.
81 */
Thierry Reding62b9e062014-11-21 17:33:33 +010082void tegra_dc_commit(struct tegra_dc *dc)
Thierry Reding205d48e2014-10-21 13:41:46 +020083{
84 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
85 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
86}
87
Thierry Reding10288ee2014-03-14 09:54:58 +010088static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
89 unsigned int bpp)
90{
91 fixed20_12 outf = dfixed_init(out);
92 fixed20_12 inf = dfixed_init(in);
93 u32 dda_inc;
94 int max;
95
96 if (v)
97 max = 15;
98 else {
99 switch (bpp) {
100 case 2:
101 max = 8;
102 break;
103
104 default:
105 WARN_ON_ONCE(1);
106 /* fallthrough */
107 case 4:
108 max = 4;
109 break;
110 }
111 }
112
113 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
114 inf.full -= dfixed_const(1);
115
116 dda_inc = dfixed_div(inf, outf);
117 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
118
119 return dda_inc;
120}
121
122static inline u32 compute_initial_dda(unsigned int in)
123{
124 fixed20_12 inf = dfixed_init(in);
125 return dfixed_frac(inf);
126}
127
Thierry Reding4aa3df72014-11-24 16:27:13 +0100128static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
129 const struct tegra_dc_window *window)
Thierry Reding10288ee2014-03-14 09:54:58 +0100130{
131 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
Sean Paul93396d02014-11-19 13:04:49 -0500132 unsigned long value, flags;
Thierry Reding10288ee2014-03-14 09:54:58 +0100133 bool yuv, planar;
134
135 /*
136 * For YUV planar modes, the number of bytes per pixel takes into
137 * account only the luma component and therefore is 1.
138 */
Thierry Reding5acd3512017-11-10 15:27:25 +0100139 yuv = tegra_plane_format_is_yuv(window->format, &planar);
Thierry Reding10288ee2014-03-14 09:54:58 +0100140 if (!yuv)
141 bpp = window->bits_per_pixel / 8;
142 else
143 bpp = planar ? 1 : 2;
144
Sean Paul93396d02014-11-19 13:04:49 -0500145 spin_lock_irqsave(&dc->lock, flags);
146
Thierry Reding10288ee2014-03-14 09:54:58 +0100147 value = WINDOW_A_SELECT << index;
148 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
149
150 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
151 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
152
153 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
154 tegra_dc_writel(dc, value, DC_WIN_POSITION);
155
156 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
157 tegra_dc_writel(dc, value, DC_WIN_SIZE);
158
159 h_offset = window->src.x * bpp;
160 v_offset = window->src.y;
161 h_size = window->src.w * bpp;
162 v_size = window->src.h;
163
164 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
165 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
166
167 /*
168 * For DDA computations the number of bytes per pixel for YUV planar
169 * modes needs to take into account all Y, U and V components.
170 */
171 if (yuv && planar)
172 bpp = 2;
173
174 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
175 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
176
177 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
178 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
179
180 h_dda = compute_initial_dda(window->src.x);
181 v_dda = compute_initial_dda(window->src.y);
182
183 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
184 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
185
186 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
187 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
188
189 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
190
191 if (yuv && planar) {
192 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
193 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
194 value = window->stride[1] << 16 | window->stride[0];
195 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
196 } else {
197 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
198 }
199
200 if (window->bottom_up)
201 v_offset += window->src.h - 1;
202
203 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
204 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
205
Thierry Redingc134f012014-06-03 14:48:12 +0200206 if (dc->soc->supports_block_linear) {
207 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100208
Thierry Redingc134f012014-06-03 14:48:12 +0200209 switch (window->tiling.mode) {
210 case TEGRA_BO_TILING_MODE_PITCH:
211 value = DC_WINBUF_SURFACE_KIND_PITCH;
212 break;
213
214 case TEGRA_BO_TILING_MODE_TILED:
215 value = DC_WINBUF_SURFACE_KIND_TILED;
216 break;
217
218 case TEGRA_BO_TILING_MODE_BLOCK:
219 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
220 DC_WINBUF_SURFACE_KIND_BLOCK;
221 break;
222 }
223
224 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
225 } else {
226 switch (window->tiling.mode) {
227 case TEGRA_BO_TILING_MODE_PITCH:
228 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
229 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
230 break;
231
232 case TEGRA_BO_TILING_MODE_TILED:
233 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
234 DC_WIN_BUFFER_ADDR_MODE_TILE;
235 break;
236
237 case TEGRA_BO_TILING_MODE_BLOCK:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100238 /*
239 * No need to handle this here because ->atomic_check
240 * will already have filtered it out.
241 */
242 break;
Thierry Redingc134f012014-06-03 14:48:12 +0200243 }
244
245 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
246 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100247
248 value = WIN_ENABLE;
249
250 if (yuv) {
251 /* setup default colorspace conversion coefficients */
252 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
253 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
254 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
255 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
256 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
257 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
258 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
259 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
260
261 value |= CSC_ENABLE;
262 } else if (window->bits_per_pixel < 24) {
263 value |= COLOR_EXPAND;
264 }
265
266 if (window->bottom_up)
267 value |= V_DIRECTION;
268
269 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
270
271 /*
272 * Disable blending and assume Window A is the bottom-most window,
273 * Window C is the top-most window and Window B is in the middle.
274 */
275 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
276 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
277
278 switch (index) {
279 case 0:
280 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
281 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
282 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
283 break;
284
285 case 1:
286 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
287 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
288 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
289 break;
290
291 case 2:
292 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
293 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
294 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
295 break;
296 }
297
Sean Paul93396d02014-11-19 13:04:49 -0500298 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc7679302014-10-21 13:51:53 +0200299}
300
Thierry Redingc7679302014-10-21 13:51:53 +0200301static const u32 tegra_primary_plane_formats[] = {
302 DRM_FORMAT_XBGR8888,
Thierry Reding7772fda2017-10-12 17:30:55 +0200303 DRM_FORMAT_ABGR8888,
Thierry Redingc7679302014-10-21 13:51:53 +0200304 DRM_FORMAT_XRGB8888,
Thierry Reding7772fda2017-10-12 17:30:55 +0200305 DRM_FORMAT_ARGB8888,
Thierry Redingc7679302014-10-21 13:51:53 +0200306 DRM_FORMAT_RGB565,
307};
308
Thierry Reding4aa3df72014-11-24 16:27:13 +0100309static int tegra_plane_atomic_check(struct drm_plane *plane,
310 struct drm_plane_state *state)
311{
Thierry Reding8f604f82014-11-28 13:14:55 +0100312 struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
313 struct tegra_bo_tiling *tiling = &plane_state->tiling;
Thierry Reding47802b02014-11-26 12:28:39 +0100314 struct tegra_plane *tegra = to_tegra_plane(plane);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100315 struct tegra_dc *dc = to_tegra_dc(state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200316 int err;
317
Thierry Reding4aa3df72014-11-24 16:27:13 +0100318 /* no need for further checks if the plane is being disabled */
319 if (!state->crtc)
320 return 0;
321
Thierry Reding5acd3512017-11-10 15:27:25 +0100322 err = tegra_plane_format(state->fb->format->format,
323 &plane_state->format,
324 &plane_state->swap);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100325 if (err < 0)
326 return err;
327
Thierry Reding8f604f82014-11-28 13:14:55 +0100328 err = tegra_fb_get_tiling(state->fb, tiling);
329 if (err < 0)
330 return err;
331
332 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
Thierry Reding4aa3df72014-11-24 16:27:13 +0100333 !dc->soc->supports_block_linear) {
334 DRM_ERROR("hardware doesn't support block linear mode\n");
335 return -EINVAL;
336 }
337
338 /*
339 * Tegra doesn't support different strides for U and V planes so we
340 * error out if the user tries to display a framebuffer with such a
341 * configuration.
342 */
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200343 if (state->fb->format->num_planes > 2) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100344 if (state->fb->pitches[2] != state->fb->pitches[1]) {
345 DRM_ERROR("unsupported UV-plane configuration\n");
346 return -EINVAL;
347 }
348 }
349
Thierry Reding47802b02014-11-26 12:28:39 +0100350 err = tegra_plane_state_add(tegra, state);
351 if (err < 0)
352 return err;
353
Thierry Reding4aa3df72014-11-24 16:27:13 +0100354 return 0;
355}
356
Thierry Redinga4bfa092017-08-30 17:34:10 +0200357static void tegra_plane_atomic_disable(struct drm_plane *plane,
358 struct drm_plane_state *old_state)
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300359{
Thierry Redinga4bfa092017-08-30 17:34:10 +0200360 struct tegra_dc *dc = to_tegra_dc(old_state->crtc);
361 struct tegra_plane *p = to_tegra_plane(plane);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300362 unsigned long flags;
363 u32 value;
364
Thierry Redinga4bfa092017-08-30 17:34:10 +0200365 /* rien ne va plus */
366 if (!old_state || !old_state->crtc)
367 return;
368
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300369 spin_lock_irqsave(&dc->lock, flags);
370
Thierry Redinga4bfa092017-08-30 17:34:10 +0200371 value = WINDOW_A_SELECT << p->index;
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300372 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
373
374 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
375 value &= ~WIN_ENABLE;
376 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
377
378 spin_unlock_irqrestore(&dc->lock, flags);
379}
380
Thierry Reding4aa3df72014-11-24 16:27:13 +0100381static void tegra_plane_atomic_update(struct drm_plane *plane,
382 struct drm_plane_state *old_state)
383{
Thierry Reding8f604f82014-11-28 13:14:55 +0100384 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100385 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
386 struct drm_framebuffer *fb = plane->state->fb;
387 struct tegra_plane *p = to_tegra_plane(plane);
388 struct tegra_dc_window window;
389 unsigned int i;
Thierry Reding4aa3df72014-11-24 16:27:13 +0100390
391 /* rien ne va plus */
392 if (!plane->state->crtc || !plane->state->fb)
393 return;
394
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300395 if (!plane->state->visible)
Thierry Redinga4bfa092017-08-30 17:34:10 +0200396 return tegra_plane_atomic_disable(plane, old_state);
Dmitry Osipenko80d3eef2017-06-15 02:18:31 +0300397
Thierry Redingc7679302014-10-21 13:51:53 +0200398 memset(&window, 0, sizeof(window));
Dmitry Osipenko7d205852017-06-15 02:18:30 +0300399 window.src.x = plane->state->src.x1 >> 16;
400 window.src.y = plane->state->src.y1 >> 16;
401 window.src.w = drm_rect_width(&plane->state->src) >> 16;
402 window.src.h = drm_rect_height(&plane->state->src) >> 16;
403 window.dst.x = plane->state->dst.x1;
404 window.dst.y = plane->state->dst.y1;
405 window.dst.w = drm_rect_width(&plane->state->dst);
406 window.dst.h = drm_rect_height(&plane->state->dst);
Ville Syrjälä272725c2016-12-14 23:32:20 +0200407 window.bits_per_pixel = fb->format->cpp[0] * 8;
Thierry Redingc7679302014-10-21 13:51:53 +0200408 window.bottom_up = tegra_fb_is_bottom_up(fb);
409
Thierry Reding8f604f82014-11-28 13:14:55 +0100410 /* copy from state */
411 window.tiling = state->tiling;
412 window.format = state->format;
413 window.swap = state->swap;
Thierry Redingc7679302014-10-21 13:51:53 +0200414
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200415 for (i = 0; i < fb->format->num_planes; i++) {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100416 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingc7679302014-10-21 13:51:53 +0200417
Thierry Reding4aa3df72014-11-24 16:27:13 +0100418 window.base[i] = bo->paddr + fb->offsets[i];
Dmitry Osipenko08ee0172016-08-21 11:57:58 +0300419
420 /*
421 * Tegra uses a shared stride for UV planes. Framebuffers are
422 * already checked for this in the tegra_plane_atomic_check()
423 * function, so it's safe to ignore the V-plane pitch here.
424 */
425 if (i < 2)
426 window.stride[i] = fb->pitches[i];
Thierry Reding4aa3df72014-11-24 16:27:13 +0100427 }
Thierry Redingc7679302014-10-21 13:51:53 +0200428
Thierry Reding4aa3df72014-11-24 16:27:13 +0100429 tegra_dc_setup_window(dc, p->index, &window);
Thierry Redingc7679302014-10-21 13:51:53 +0200430}
431
Thierry Redinga4bfa092017-08-30 17:34:10 +0200432static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100433 .atomic_check = tegra_plane_atomic_check,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100434 .atomic_disable = tegra_plane_atomic_disable,
Thierry Redinga4bfa092017-08-30 17:34:10 +0200435 .atomic_update = tegra_plane_atomic_update,
Thierry Redingc7679302014-10-21 13:51:53 +0200436};
437
Thierry Reding47307952017-08-30 17:42:54 +0200438static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
439 struct tegra_dc *dc)
Thierry Redingc7679302014-10-21 13:51:53 +0200440{
Thierry Reding518e6222014-12-16 18:04:08 +0100441 /*
442 * Ideally this would use drm_crtc_mask(), but that would require the
443 * CRTC to already be in the mode_config's list of CRTCs. However, it
444 * will only be added to that list in the drm_crtc_init_with_planes()
445 * (in tegra_dc_init()), which in turn requires registration of these
446 * planes. So we have ourselves a nice little chicken and egg problem
447 * here.
448 *
449 * We work around this by manually creating the mask from the number
450 * of CRTCs that have been registered, and should therefore always be
451 * the same as drm_crtc_index() after registration.
452 */
453 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Reding47307952017-08-30 17:42:54 +0200454 enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
Thierry Redingc7679302014-10-21 13:51:53 +0200455 struct tegra_plane *plane;
456 unsigned int num_formats;
457 const u32 *formats;
458 int err;
459
460 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
461 if (!plane)
462 return ERR_PTR(-ENOMEM);
463
464 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
465 formats = tegra_primary_plane_formats;
466
Thierry Redingc4755fb2017-11-13 11:08:13 +0100467 /*
468 * XXX compute offset so that we can directly access windows.
469 *
470 * Always use window A as primary window.
471 */
472 plane->offset = 0;
473 plane->index = 0;
474 plane->depth = 255;
475
Thierry Reding518e6222014-12-16 18:04:08 +0100476 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc1cb4b62017-08-30 18:04:12 +0200477 &tegra_plane_funcs, formats,
Thierry Reding47307952017-08-30 17:42:54 +0200478 num_formats, NULL, type, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200479 if (err < 0) {
480 kfree(plane);
481 return ERR_PTR(err);
482 }
483
Thierry Redinga4bfa092017-08-30 17:34:10 +0200484 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100485
Thierry Redingc7679302014-10-21 13:51:53 +0200486 return &plane->base;
487}
488
489static const u32 tegra_cursor_plane_formats[] = {
490 DRM_FORMAT_RGBA8888,
491};
492
Thierry Reding4aa3df72014-11-24 16:27:13 +0100493static int tegra_cursor_atomic_check(struct drm_plane *plane,
494 struct drm_plane_state *state)
Thierry Redingc7679302014-10-21 13:51:53 +0200495{
Thierry Reding47802b02014-11-26 12:28:39 +0100496 struct tegra_plane *tegra = to_tegra_plane(plane);
497 int err;
498
Thierry Reding4aa3df72014-11-24 16:27:13 +0100499 /* no need for further checks if the plane is being disabled */
500 if (!state->crtc)
501 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200502
503 /* scaling not supported for cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100504 if ((state->src_w >> 16 != state->crtc_w) ||
505 (state->src_h >> 16 != state->crtc_h))
Thierry Redingc7679302014-10-21 13:51:53 +0200506 return -EINVAL;
507
508 /* only square cursors supported */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100509 if (state->src_w != state->src_h)
Thierry Redingc7679302014-10-21 13:51:53 +0200510 return -EINVAL;
511
Thierry Reding4aa3df72014-11-24 16:27:13 +0100512 if (state->crtc_w != 32 && state->crtc_w != 64 &&
513 state->crtc_w != 128 && state->crtc_w != 256)
514 return -EINVAL;
515
Thierry Reding47802b02014-11-26 12:28:39 +0100516 err = tegra_plane_state_add(tegra, state);
517 if (err < 0)
518 return err;
519
Thierry Reding4aa3df72014-11-24 16:27:13 +0100520 return 0;
521}
522
523static void tegra_cursor_atomic_update(struct drm_plane *plane,
524 struct drm_plane_state *old_state)
525{
526 struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
527 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
528 struct drm_plane_state *state = plane->state;
529 u32 value = CURSOR_CLIP_DISPLAY;
530
531 /* rien ne va plus */
532 if (!plane->state->crtc || !plane->state->fb)
533 return;
534
535 switch (state->crtc_w) {
Thierry Redingc7679302014-10-21 13:51:53 +0200536 case 32:
537 value |= CURSOR_SIZE_32x32;
538 break;
539
540 case 64:
541 value |= CURSOR_SIZE_64x64;
542 break;
543
544 case 128:
545 value |= CURSOR_SIZE_128x128;
546 break;
547
548 case 256:
549 value |= CURSOR_SIZE_256x256;
550 break;
551
552 default:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100553 WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
554 state->crtc_h);
555 return;
Thierry Redingc7679302014-10-21 13:51:53 +0200556 }
557
558 value |= (bo->paddr >> 10) & 0x3fffff;
559 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
560
561#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
562 value = (bo->paddr >> 32) & 0x3;
563 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
564#endif
565
566 /* enable cursor and set blend mode */
567 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
568 value |= CURSOR_ENABLE;
569 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
570
571 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
572 value &= ~CURSOR_DST_BLEND_MASK;
573 value &= ~CURSOR_SRC_BLEND_MASK;
574 value |= CURSOR_MODE_NORMAL;
575 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
576 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
577 value |= CURSOR_ALPHA;
578 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
579
580 /* position the cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100581 value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
Thierry Redingc7679302014-10-21 13:51:53 +0200582 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
Thierry Redingc7679302014-10-21 13:51:53 +0200583}
584
Thierry Reding4aa3df72014-11-24 16:27:13 +0100585static void tegra_cursor_atomic_disable(struct drm_plane *plane,
586 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200587{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100588 struct tegra_dc *dc;
Thierry Redingc7679302014-10-21 13:51:53 +0200589 u32 value;
590
Thierry Reding4aa3df72014-11-24 16:27:13 +0100591 /* rien ne va plus */
592 if (!old_state || !old_state->crtc)
593 return;
594
595 dc = to_tegra_dc(old_state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200596
597 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
598 value &= ~CURSOR_ENABLE;
599 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc7679302014-10-21 13:51:53 +0200600}
601
Thierry Reding4aa3df72014-11-24 16:27:13 +0100602static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100603 .atomic_check = tegra_cursor_atomic_check,
604 .atomic_update = tegra_cursor_atomic_update,
605 .atomic_disable = tegra_cursor_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200606};
607
608static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
609 struct tegra_dc *dc)
610{
611 struct tegra_plane *plane;
612 unsigned int num_formats;
613 const u32 *formats;
614 int err;
615
616 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
617 if (!plane)
618 return ERR_PTR(-ENOMEM);
619
Thierry Reding47802b02014-11-26 12:28:39 +0100620 /*
Thierry Redinga1df3b22015-07-21 16:42:30 +0200621 * This index is kind of fake. The cursor isn't a regular plane, but
622 * its update and activation request bits in DC_CMD_STATE_CONTROL do
623 * use the same programming. Setting this fake index here allows the
624 * code in tegra_add_plane_state() to do the right thing without the
625 * need to special-casing the cursor plane.
Thierry Reding47802b02014-11-26 12:28:39 +0100626 */
627 plane->index = 6;
628
Thierry Redingc7679302014-10-21 13:51:53 +0200629 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
630 formats = tegra_cursor_plane_formats;
631
632 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
Thierry Redingc1cb4b62017-08-30 18:04:12 +0200633 &tegra_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700634 num_formats, NULL,
635 DRM_PLANE_TYPE_CURSOR, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200636 if (err < 0) {
637 kfree(plane);
638 return ERR_PTR(err);
639 }
640
Thierry Reding4aa3df72014-11-24 16:27:13 +0100641 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
642
Thierry Redingc7679302014-10-21 13:51:53 +0200643 return &plane->base;
644}
645
Thierry Redingc7679302014-10-21 13:51:53 +0200646static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100647 DRM_FORMAT_XBGR8888,
Thierry Reding7772fda2017-10-12 17:30:55 +0200648 DRM_FORMAT_ABGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100649 DRM_FORMAT_XRGB8888,
Thierry Reding7772fda2017-10-12 17:30:55 +0200650 DRM_FORMAT_ARGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100651 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100652 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100653 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100654 DRM_FORMAT_YUV420,
655 DRM_FORMAT_YUV422,
656};
657
Thierry Redingc7679302014-10-21 13:51:53 +0200658static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
659 struct tegra_dc *dc,
660 unsigned int index)
661{
662 struct tegra_plane *plane;
663 unsigned int num_formats;
664 const u32 *formats;
665 int err;
666
667 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
668 if (!plane)
669 return ERR_PTR(-ENOMEM);
670
Thierry Redingc4755fb2017-11-13 11:08:13 +0100671 /* XXX compute offset so that we can directly access windows */
672 plane->offset = 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200673 plane->index = index;
Thierry Redingc4755fb2017-11-13 11:08:13 +0100674 plane->depth = 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200675
676 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
677 formats = tegra_overlay_plane_formats;
678
679 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
Thierry Reding301e0dd2017-08-30 18:04:12 +0200680 &tegra_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700681 num_formats, NULL,
682 DRM_PLANE_TYPE_OVERLAY, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +0200683 if (err < 0) {
684 kfree(plane);
685 return ERR_PTR(err);
686 }
687
Thierry Redinga4bfa092017-08-30 17:34:10 +0200688 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100689
Thierry Redingc7679302014-10-21 13:51:53 +0200690 return &plane->base;
691}
692
Thierry Reding47307952017-08-30 17:42:54 +0200693static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
694 struct tegra_dc *dc)
Thierry Redingf34bc782012-11-04 21:47:13 +0100695{
Thierry Reding47307952017-08-30 17:42:54 +0200696 struct drm_plane *plane, *primary = NULL;
697 unsigned int i, j;
698
699 for (i = 0; i < dc->soc->num_wgrps; i++) {
700 const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
701
702 if (wgrp->dc == dc->pipe) {
703 for (j = 0; j < wgrp->num_windows; j++) {
704 unsigned int index = wgrp->windows[j];
705
706 plane = tegra_shared_plane_create(drm, dc,
707 wgrp->index,
708 index);
709 if (IS_ERR(plane))
710 return plane;
711
712 /*
713 * Choose the first shared plane owned by this
714 * head as the primary plane.
715 */
716 if (!primary) {
717 plane->type = DRM_PLANE_TYPE_PRIMARY;
718 primary = plane;
719 }
720 }
721 }
722 }
723
724 return primary;
725}
726
727static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
728 struct tegra_dc *dc)
729{
730 struct drm_plane *plane, *primary;
Thierry Redingf34bc782012-11-04 21:47:13 +0100731 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100732
Thierry Reding47307952017-08-30 17:42:54 +0200733 primary = tegra_primary_plane_create(drm, dc);
734 if (IS_ERR(primary))
735 return primary;
736
Thierry Redingf34bc782012-11-04 21:47:13 +0100737 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200738 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
Thierry Reding47307952017-08-30 17:42:54 +0200739 if (IS_ERR(plane)) {
740 /* XXX tegra_plane_destroy() */
741 drm_plane_cleanup(primary);
742 kfree(primary);
743 return plane;
744 }
Thierry Redingf34bc782012-11-04 21:47:13 +0100745 }
746
Thierry Reding47307952017-08-30 17:42:54 +0200747 return primary;
Thierry Redingf34bc782012-11-04 21:47:13 +0100748}
749
Thierry Redingf002abc2013-10-14 14:06:02 +0200750static void tegra_dc_destroy(struct drm_crtc *crtc)
751{
752 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +0200753}
754
Thierry Redingca915b12014-12-08 16:14:45 +0100755static void tegra_crtc_reset(struct drm_crtc *crtc)
756{
757 struct tegra_dc_state *state;
758
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100759 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200760 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100761
Thierry Redingca915b12014-12-08 16:14:45 +0100762 kfree(crtc->state);
763 crtc->state = NULL;
764
765 state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Reding332bbe72015-01-28 15:03:31 +0100766 if (state) {
Thierry Redingca915b12014-12-08 16:14:45 +0100767 crtc->state = &state->base;
Thierry Reding332bbe72015-01-28 15:03:31 +0100768 crtc->state->crtc = crtc;
769 }
Thierry Reding31930d42015-07-02 17:04:06 +0200770
771 drm_crtc_vblank_reset(crtc);
Thierry Redingca915b12014-12-08 16:14:45 +0100772}
773
774static struct drm_crtc_state *
775tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
776{
777 struct tegra_dc_state *state = to_dc_state(crtc->state);
778 struct tegra_dc_state *copy;
779
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100780 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
Thierry Redingca915b12014-12-08 16:14:45 +0100781 if (!copy)
782 return NULL;
783
Thierry Reding3b59b7ac2015-01-28 15:01:22 +0100784 __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
785 copy->clk = state->clk;
786 copy->pclk = state->pclk;
787 copy->div = state->div;
788 copy->planes = state->planes;
Thierry Redingca915b12014-12-08 16:14:45 +0100789
790 return &copy->base;
791}
792
793static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
794 struct drm_crtc_state *state)
795{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +0200796 __drm_atomic_helper_crtc_destroy_state(state);
Thierry Redingca915b12014-12-08 16:14:45 +0100797 kfree(state);
798}
799
Thierry Redingb95800e2017-11-08 13:40:54 +0100800#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
801
802static const struct debugfs_reg32 tegra_dc_regs[] = {
803 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
804 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
805 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
806 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
807 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
808 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
809 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
810 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
811 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
812 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
813 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
814 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
815 DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
816 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
817 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
818 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
819 DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
820 DEBUGFS_REG32(DC_CMD_INT_STATUS),
821 DEBUGFS_REG32(DC_CMD_INT_MASK),
822 DEBUGFS_REG32(DC_CMD_INT_ENABLE),
823 DEBUGFS_REG32(DC_CMD_INT_TYPE),
824 DEBUGFS_REG32(DC_CMD_INT_POLARITY),
825 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
826 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
827 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
828 DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
829 DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
830 DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
831 DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
832 DEBUGFS_REG32(DC_COM_CRC_CONTROL),
833 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
834 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
835 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
836 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
837 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
838 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
839 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
840 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
841 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
842 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
843 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
844 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
845 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
846 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
847 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
848 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
849 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
850 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
851 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
852 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
853 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
854 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
855 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
856 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
857 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
858 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
859 DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
860 DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
861 DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
862 DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
863 DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
864 DEBUGFS_REG32(DC_COM_SPI_CONTROL),
865 DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
866 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
867 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
868 DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
869 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
870 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
871 DEBUGFS_REG32(DC_COM_GPIO_CTRL),
872 DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
873 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
874 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
875 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
876 DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
877 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
878 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
879 DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
880 DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
881 DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
882 DEBUGFS_REG32(DC_DISP_BACK_PORCH),
883 DEBUGFS_REG32(DC_DISP_ACTIVE),
884 DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
885 DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
886 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
887 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
888 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
889 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
890 DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
891 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
892 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
893 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
894 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
895 DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
896 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
897 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
898 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
899 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
900 DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
901 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
902 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
903 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
904 DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
905 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
906 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
907 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
908 DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
909 DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
910 DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
911 DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
912 DEBUGFS_REG32(DC_DISP_M0_CONTROL),
913 DEBUGFS_REG32(DC_DISP_M1_CONTROL),
914 DEBUGFS_REG32(DC_DISP_DI_CONTROL),
915 DEBUGFS_REG32(DC_DISP_PP_CONTROL),
916 DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
917 DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
918 DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
919 DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
920 DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
921 DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
922 DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
923 DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
924 DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
925 DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
926 DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
927 DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
928 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
929 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
930 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
931 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
932 DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
933 DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
934 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
935 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
936 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
937 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
938 DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
939 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
940 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
941 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
942 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
943 DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
944 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
945 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
946 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
947 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
948 DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
949 DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
950 DEBUGFS_REG32(DC_DISP_SD_CONTROL),
951 DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
952 DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
953 DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
954 DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
955 DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
956 DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
957 DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
958 DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
959 DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
960 DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
961 DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
962 DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
963 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
964 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
965 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
966 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
967 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
968 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
969 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
970 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
971 DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
972 DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
973 DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
974 DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
975 DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
976 DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
977 DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
978 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
979 DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
980 DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
981 DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
982 DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
983 DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
984 DEBUGFS_REG32(DC_WIN_POSITION),
985 DEBUGFS_REG32(DC_WIN_SIZE),
986 DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
987 DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
988 DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
989 DEBUGFS_REG32(DC_WIN_DDA_INC),
990 DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
991 DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
992 DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
993 DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
994 DEBUGFS_REG32(DC_WIN_DV_CONTROL),
995 DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
996 DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
997 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
998 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
999 DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1000 DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1001 DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1002 DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1003 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1004 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1005 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1006 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1007 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1008 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1009 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1010 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1011 DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1012 DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1013 DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1014 DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1015};
1016
1017static int tegra_dc_show_regs(struct seq_file *s, void *data)
1018{
1019 struct drm_info_node *node = s->private;
1020 struct tegra_dc *dc = node->info_ent->data;
1021 unsigned int i;
1022 int err = 0;
1023
1024 drm_modeset_lock(&dc->base.mutex, NULL);
1025
1026 if (!dc->base.state->active) {
1027 err = -EBUSY;
1028 goto unlock;
1029 }
1030
1031 for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1032 unsigned int offset = tegra_dc_regs[i].offset;
1033
1034 seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1035 offset, tegra_dc_readl(dc, offset));
1036 }
1037
1038unlock:
1039 drm_modeset_unlock(&dc->base.mutex);
1040 return err;
1041}
1042
1043static int tegra_dc_show_crc(struct seq_file *s, void *data)
1044{
1045 struct drm_info_node *node = s->private;
1046 struct tegra_dc *dc = node->info_ent->data;
1047 int err = 0;
1048 u32 value;
1049
1050 drm_modeset_lock(&dc->base.mutex, NULL);
1051
1052 if (!dc->base.state->active) {
1053 err = -EBUSY;
1054 goto unlock;
1055 }
1056
1057 value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1058 tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1059 tegra_dc_commit(dc);
1060
1061 drm_crtc_wait_one_vblank(&dc->base);
1062 drm_crtc_wait_one_vblank(&dc->base);
1063
1064 value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1065 seq_printf(s, "%08x\n", value);
1066
1067 tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1068
1069unlock:
1070 drm_modeset_unlock(&dc->base.mutex);
1071 return err;
1072}
1073
1074static int tegra_dc_show_stats(struct seq_file *s, void *data)
1075{
1076 struct drm_info_node *node = s->private;
1077 struct tegra_dc *dc = node->info_ent->data;
1078
1079 seq_printf(s, "frames: %lu\n", dc->stats.frames);
1080 seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1081 seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1082 seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1083
1084 return 0;
1085}
1086
1087static struct drm_info_list debugfs_files[] = {
1088 { "regs", tegra_dc_show_regs, 0, NULL },
1089 { "crc", tegra_dc_show_crc, 0, NULL },
1090 { "stats", tegra_dc_show_stats, 0, NULL },
1091};
1092
1093static int tegra_dc_late_register(struct drm_crtc *crtc)
1094{
1095 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1096 struct drm_minor *minor = crtc->dev->primary;
1097 struct dentry *root = crtc->debugfs_entry;
1098 struct tegra_dc *dc = to_tegra_dc(crtc);
1099 int err;
1100
1101 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1102 GFP_KERNEL);
1103 if (!dc->debugfs_files)
1104 return -ENOMEM;
1105
1106 for (i = 0; i < count; i++)
1107 dc->debugfs_files[i].data = dc;
1108
1109 err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1110 if (err < 0)
1111 goto free;
1112
1113 return 0;
1114
1115free:
1116 kfree(dc->debugfs_files);
1117 dc->debugfs_files = NULL;
1118
1119 return err;
1120}
1121
1122static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1123{
1124 unsigned int count = ARRAY_SIZE(debugfs_files);
1125 struct drm_minor *minor = crtc->dev->primary;
1126 struct tegra_dc *dc = to_tegra_dc(crtc);
1127
1128 drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1129 kfree(dc->debugfs_files);
1130 dc->debugfs_files = NULL;
1131}
1132
Thierry Redingc49c81e2017-11-08 13:32:05 +01001133static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1134{
1135 struct tegra_dc *dc = to_tegra_dc(crtc);
1136
Thierry Reding47307952017-08-30 17:42:54 +02001137 /* XXX vblank syncpoints don't work with nvdisplay yet */
1138 if (dc->syncpt && !dc->soc->has_nvdisplay)
Thierry Redingc49c81e2017-11-08 13:32:05 +01001139 return host1x_syncpt_read(dc->syncpt);
1140
1141 /* fallback to software emulated VBLANK counter */
1142 return drm_crtc_vblank_count(&dc->base);
1143}
1144
1145static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1146{
1147 struct tegra_dc *dc = to_tegra_dc(crtc);
1148 unsigned long value, flags;
1149
1150 spin_lock_irqsave(&dc->lock, flags);
1151
1152 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1153 value |= VBLANK_INT;
1154 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1155
1156 spin_unlock_irqrestore(&dc->lock, flags);
1157
1158 return 0;
1159}
1160
1161static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1162{
1163 struct tegra_dc *dc = to_tegra_dc(crtc);
1164 unsigned long value, flags;
1165
1166 spin_lock_irqsave(&dc->lock, flags);
1167
1168 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1169 value &= ~VBLANK_INT;
1170 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1171
1172 spin_unlock_irqrestore(&dc->lock, flags);
1173}
1174
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001175static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding1503ca42014-11-24 17:41:23 +01001176 .page_flip = drm_atomic_helper_page_flip,
Thierry Reding74f48792014-11-24 17:08:20 +01001177 .set_config = drm_atomic_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +02001178 .destroy = tegra_dc_destroy,
Thierry Redingca915b12014-12-08 16:14:45 +01001179 .reset = tegra_crtc_reset,
1180 .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1181 .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
Thierry Redingb95800e2017-11-08 13:40:54 +01001182 .late_register = tegra_dc_late_register,
1183 .early_unregister = tegra_dc_early_unregister,
Shawn Guo10437d92017-02-07 17:16:32 +08001184 .get_vblank_counter = tegra_dc_get_vblank_counter,
1185 .enable_vblank = tegra_dc_enable_vblank,
1186 .disable_vblank = tegra_dc_disable_vblank,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001187};
1188
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001189static int tegra_dc_set_timings(struct tegra_dc *dc,
1190 struct drm_display_mode *mode)
1191{
Thierry Reding0444c0f2014-04-16 09:22:38 +02001192 unsigned int h_ref_to_sync = 1;
1193 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001194 unsigned long value;
1195
Thierry Reding47307952017-08-30 17:42:54 +02001196 if (!dc->soc->has_nvdisplay) {
1197 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001198
Thierry Reding47307952017-08-30 17:42:54 +02001199 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1200 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1201 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001202
1203 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1204 ((mode->hsync_end - mode->hsync_start) << 0);
1205 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1206
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001207 value = ((mode->vtotal - mode->vsync_end) << 16) |
1208 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +00001209 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1210
1211 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1212 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001213 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1214
1215 value = (mode->vdisplay << 16) | mode->hdisplay;
1216 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1217
1218 return 0;
1219}
1220
Thierry Reding9d910b62015-01-28 15:25:54 +01001221/**
1222 * tegra_dc_state_setup_clock - check clock settings and store them in atomic
1223 * state
1224 * @dc: display controller
1225 * @crtc_state: CRTC atomic state
1226 * @clk: parent clock for display controller
1227 * @pclk: pixel clock
1228 * @div: shift clock divider
1229 *
1230 * Returns:
1231 * 0 on success or a negative error-code on failure.
1232 */
Thierry Redingca915b12014-12-08 16:14:45 +01001233int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1234 struct drm_crtc_state *crtc_state,
1235 struct clk *clk, unsigned long pclk,
1236 unsigned int div)
1237{
1238 struct tegra_dc_state *state = to_dc_state(crtc_state);
1239
Thierry Redingd2982742015-01-22 08:48:25 +01001240 if (!clk_has_parent(dc->clk, clk))
1241 return -EINVAL;
1242
Thierry Redingca915b12014-12-08 16:14:45 +01001243 state->clk = clk;
1244 state->pclk = pclk;
1245 state->div = div;
1246
1247 return 0;
1248}
1249
Thierry Reding76d59ed2014-12-19 15:09:16 +01001250static void tegra_dc_commit_state(struct tegra_dc *dc,
1251 struct tegra_dc_state *state)
1252{
1253 u32 value;
1254 int err;
1255
1256 err = clk_set_parent(dc->clk, state->clk);
1257 if (err < 0)
1258 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1259
1260 /*
1261 * Outputs may not want to change the parent clock rate. This is only
1262 * relevant to Tegra20 where only a single display PLL is available.
1263 * Since that PLL would typically be used for HDMI, an internal LVDS
1264 * panel would need to be driven by some other clock such as PLL_P
1265 * which is shared with other peripherals. Changing the clock rate
1266 * should therefore be avoided.
1267 */
1268 if (state->pclk > 0) {
1269 err = clk_set_rate(state->clk, state->pclk);
1270 if (err < 0)
1271 dev_err(dc->dev,
1272 "failed to set clock rate to %lu Hz\n",
1273 state->pclk);
1274 }
1275
1276 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
1277 state->div);
1278 DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
1279
Thierry Reding47307952017-08-30 17:42:54 +02001280 if (!dc->soc->has_nvdisplay) {
1281 value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
1282 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1283 }
Thierry Reding39e08af2017-08-30 17:38:39 +02001284
1285 err = clk_set_rate(dc->clk, state->pclk);
1286 if (err < 0)
1287 dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1288 dc->clk, state->pclk, err);
Thierry Reding76d59ed2014-12-19 15:09:16 +01001289}
1290
Thierry Reding003fc842015-08-03 13:16:26 +02001291static void tegra_dc_stop(struct tegra_dc *dc)
1292{
1293 u32 value;
1294
1295 /* stop the display controller */
1296 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1297 value &= ~DISP_CTRL_MODE_MASK;
1298 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1299
1300 tegra_dc_commit(dc);
1301}
1302
1303static bool tegra_dc_idle(struct tegra_dc *dc)
1304{
1305 u32 value;
1306
1307 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1308
1309 return (value & DISP_CTRL_MODE_MASK) == 0;
1310}
1311
1312static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1313{
1314 timeout = jiffies + msecs_to_jiffies(timeout);
1315
1316 while (time_before(jiffies, timeout)) {
1317 if (tegra_dc_idle(dc))
1318 return 0;
1319
1320 usleep_range(1000, 2000);
1321 }
1322
1323 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1324 return -ETIMEDOUT;
1325}
1326
Laurent Pinchart64581712017-06-30 12:36:45 +03001327static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1328 struct drm_crtc_state *old_state)
Thierry Reding003fc842015-08-03 13:16:26 +02001329{
1330 struct tegra_dc *dc = to_tegra_dc(crtc);
1331 u32 value;
1332
1333 if (!tegra_dc_idle(dc)) {
1334 tegra_dc_stop(dc);
1335
1336 /*
1337 * Ignore the return value, there isn't anything useful to do
1338 * in case this fails.
1339 */
1340 tegra_dc_wait_idle(dc, 100);
1341 }
1342
1343 /*
1344 * This should really be part of the RGB encoder driver, but clearing
1345 * these bits has the side-effect of stopping the display controller.
1346 * When that happens no VBLANK interrupts will be raised. At the same
1347 * time the encoder is disabled before the display controller, so the
1348 * above code is always going to timeout waiting for the controller
1349 * to go idle.
1350 *
1351 * Given the close coupling between the RGB encoder and the display
1352 * controller doing it here is still kind of okay. None of the other
1353 * encoder drivers require these bits to be cleared.
1354 *
1355 * XXX: Perhaps given that the display controller is switched off at
1356 * this point anyway maybe clearing these bits isn't even useful for
1357 * the RGB encoder?
1358 */
1359 if (dc->rgb) {
1360 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1361 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1362 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1363 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1364 }
1365
1366 tegra_dc_stats_reset(&dc->stats);
1367 drm_crtc_vblank_off(crtc);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001368
Thierry Reding9d99ab62017-10-12 17:40:46 +02001369 spin_lock_irq(&crtc->dev->event_lock);
1370
1371 if (crtc->state->event) {
1372 drm_crtc_send_vblank_event(crtc, crtc->state->event);
1373 crtc->state->event = NULL;
1374 }
1375
1376 spin_unlock_irq(&crtc->dev->event_lock);
1377
Thierry Reding33a8eb82015-08-03 13:20:49 +02001378 pm_runtime_put_sync(dc->dev);
Thierry Reding003fc842015-08-03 13:16:26 +02001379}
1380
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001381static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
1382 struct drm_crtc_state *old_state)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001383{
Thierry Reding4aa3df72014-11-24 16:27:13 +01001384 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Thierry Reding76d59ed2014-12-19 15:09:16 +01001385 struct tegra_dc_state *state = to_dc_state(crtc->state);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001386 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001387 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001388
Thierry Reding33a8eb82015-08-03 13:20:49 +02001389 pm_runtime_get_sync(dc->dev);
1390
1391 /* initialize display controller */
1392 if (dc->syncpt) {
Thierry Reding47307952017-08-30 17:42:54 +02001393 u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
1394
1395 if (dc->soc->has_nvdisplay)
1396 enable = 1 << 31;
1397 else
1398 enable = 1 << 8;
Thierry Reding33a8eb82015-08-03 13:20:49 +02001399
1400 value = SYNCPT_CNTRL_NO_STALL;
1401 tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1402
Thierry Reding47307952017-08-30 17:42:54 +02001403 value = enable | syncpt;
Thierry Reding33a8eb82015-08-03 13:20:49 +02001404 tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
1405 }
1406
Thierry Reding47307952017-08-30 17:42:54 +02001407 if (dc->soc->has_nvdisplay) {
1408 value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
1409 DSC_OBUF_UF_INT;
1410 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001411
Thierry Reding47307952017-08-30 17:42:54 +02001412 value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
1413 DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
1414 HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
1415 REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
1416 VBLANK_INT | FRAME_END_INT;
1417 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001418
Thierry Reding47307952017-08-30 17:42:54 +02001419 value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
1420 FRAME_END_INT;
1421 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001422
Thierry Reding47307952017-08-30 17:42:54 +02001423 value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
1424 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001425
Thierry Reding47307952017-08-30 17:42:54 +02001426 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
1427 } else {
1428 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1429 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1430 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
Thierry Reding33a8eb82015-08-03 13:20:49 +02001431
Thierry Reding47307952017-08-30 17:42:54 +02001432 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1433 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1434 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1435
1436 /* initialize timer */
1437 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1438 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1439 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1440
1441 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1442 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1443 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1444
1445 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1446 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1447 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
1448
1449 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1450 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1451 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1452 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02001453
Thierry Reding7116e9a2017-11-13 11:20:48 +01001454 if (dc->soc->supports_background_color)
1455 tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
1456 else
Thierry Reding33a8eb82015-08-03 13:20:49 +02001457 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1458
1459 /* apply PLL and pixel clock changes */
Thierry Reding76d59ed2014-12-19 15:09:16 +01001460 tegra_dc_commit_state(dc, state);
1461
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001462 /* program display mode */
1463 tegra_dc_set_timings(dc, mode);
1464
Thierry Reding8620fc62013-12-12 11:03:59 +01001465 /* interlacing isn't supported yet, so disable it */
1466 if (dc->soc->supports_interlacing) {
1467 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1468 value &= ~INTERLACE_ENABLE;
1469 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1470 }
Thierry Reding666cb872014-12-08 16:32:47 +01001471
1472 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1473 value &= ~DISP_CTRL_MODE_MASK;
1474 value |= DISP_CTRL_MODE_C_DISPLAY;
1475 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1476
Thierry Reding47307952017-08-30 17:42:54 +02001477 if (!dc->soc->has_nvdisplay) {
1478 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1479 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1480 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
1481 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1482 }
1483
1484 /* enable underflow reporting and display red for missing pixels */
1485 if (dc->soc->has_nvdisplay) {
1486 value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
1487 tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
1488 }
Thierry Reding666cb872014-12-08 16:32:47 +01001489
1490 tegra_dc_commit(dc);
Thierry Reding23fb4742012-11-28 11:38:24 +01001491
Thierry Reding8ff64c12014-10-08 14:48:51 +02001492 drm_crtc_vblank_on(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001493}
1494
Thierry Reding4aa3df72014-11-24 16:27:13 +01001495static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1496 struct drm_crtc_state *state)
1497{
Thierry Redingc4755fb2017-11-13 11:08:13 +01001498 struct tegra_atomic_state *s = to_tegra_atomic_state(state->state);
1499 struct tegra_dc_state *tegra = to_dc_state(state);
1500
1501 /*
1502 * The display hub display clock needs to be fed by the display clock
1503 * with the highest frequency to ensure proper functioning of all the
1504 * displays.
1505 *
1506 * Note that this isn't used before Tegra186, but it doesn't hurt and
1507 * conditionalizing it would make the code less clean.
1508 */
1509 if (state->active) {
1510 if (!s->clk_disp || tegra->pclk > s->rate) {
1511 s->dc = to_tegra_dc(crtc);
1512 s->clk_disp = s->dc->clk;
1513 s->rate = tegra->pclk;
1514 }
1515 }
1516
Thierry Reding4aa3df72014-11-24 16:27:13 +01001517 return 0;
1518}
1519
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001520static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
1521 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001522{
Thierry Reding9d99ab62017-10-12 17:40:46 +02001523 unsigned long flags;
Thierry Reding1503ca42014-11-24 17:41:23 +01001524
1525 if (crtc->state->event) {
Thierry Reding9d99ab62017-10-12 17:40:46 +02001526 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Thierry Reding1503ca42014-11-24 17:41:23 +01001527
Thierry Reding9d99ab62017-10-12 17:40:46 +02001528 if (drm_crtc_vblank_get(crtc) != 0)
1529 drm_crtc_send_vblank_event(crtc, crtc->state->event);
1530 else
1531 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
Thierry Reding1503ca42014-11-24 17:41:23 +01001532
Thierry Reding9d99ab62017-10-12 17:40:46 +02001533 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
1534
Thierry Reding1503ca42014-11-24 17:41:23 +01001535 crtc->state->event = NULL;
1536 }
Thierry Reding4aa3df72014-11-24 16:27:13 +01001537}
1538
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001539static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
1540 struct drm_crtc_state *old_crtc_state)
Thierry Reding4aa3df72014-11-24 16:27:13 +01001541{
Thierry Reding47802b02014-11-26 12:28:39 +01001542 struct tegra_dc_state *state = to_dc_state(crtc->state);
1543 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding47307952017-08-30 17:42:54 +02001544 u32 value;
Thierry Reding47802b02014-11-26 12:28:39 +01001545
Thierry Reding47307952017-08-30 17:42:54 +02001546 value = state->planes << 8 | GENERAL_UPDATE;
1547 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
1548 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
1549
1550 value = state->planes | GENERAL_ACT_REQ;
1551 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
1552 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
Thierry Reding4aa3df72014-11-24 16:27:13 +01001553}
1554
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001555static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +01001556 .atomic_check = tegra_crtc_atomic_check,
1557 .atomic_begin = tegra_crtc_atomic_begin,
1558 .atomic_flush = tegra_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001559 .atomic_enable = tegra_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +03001560 .atomic_disable = tegra_crtc_atomic_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001561};
1562
Thierry Reding6e5ff992012-11-28 11:45:47 +01001563static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001564{
1565 struct tegra_dc *dc = data;
1566 unsigned long status;
1567
1568 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1569 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1570
1571 if (status & FRAME_END_INT) {
1572 /*
1573 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1574 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001575 dc->stats.frames++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001576 }
1577
1578 if (status & VBLANK_INT) {
1579 /*
1580 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1581 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001582 drm_crtc_handle_vblank(&dc->base);
Thierry Reding791ddb12015-07-28 21:27:05 +02001583 dc->stats.vblank++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001584 }
1585
1586 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1587 /*
1588 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1589 */
Thierry Reding791ddb12015-07-28 21:27:05 +02001590 dc->stats.underflow++;
1591 }
1592
1593 if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
1594 /*
1595 dev_dbg(dc->dev, "%s(): overflow\n", __func__);
1596 */
1597 dc->stats.overflow++;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001598 }
1599
Thierry Reding47307952017-08-30 17:42:54 +02001600 if (status & HEAD_UF_INT) {
1601 dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
1602 dc->stats.underflow++;
1603 }
1604
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001605 return IRQ_HANDLED;
1606}
1607
Thierry Reding53fa7f72013-09-24 15:35:40 +02001608static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001609{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001610 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001611 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
Thierry Reding776dc382013-10-14 14:43:22 +02001612 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001613 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001614 struct drm_plane *primary = NULL;
1615 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001616 int err;
1617
Thierry Reding617dd7c2017-08-30 12:48:31 +02001618 dc->syncpt = host1x_syncpt_request(client, flags);
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001619 if (!dc->syncpt)
1620 dev_warn(dc->dev, "failed to allocate syncpoint\n");
1621
Thierry Redingdf06b752014-06-26 21:41:53 +02001622 if (tegra->domain) {
1623 err = iommu_attach_device(tegra->domain, dc->dev);
1624 if (err < 0) {
1625 dev_err(dc->dev, "failed to attach to domain: %d\n",
1626 err);
1627 return err;
1628 }
1629
1630 dc->domain = tegra->domain;
1631 }
1632
Thierry Reding47307952017-08-30 17:42:54 +02001633 if (dc->soc->wgrps)
1634 primary = tegra_dc_add_shared_planes(drm, dc);
1635 else
1636 primary = tegra_dc_add_planes(drm, dc);
1637
Thierry Redingc7679302014-10-21 13:51:53 +02001638 if (IS_ERR(primary)) {
1639 err = PTR_ERR(primary);
1640 goto cleanup;
1641 }
1642
1643 if (dc->soc->supports_cursor) {
1644 cursor = tegra_dc_cursor_plane_create(drm, dc);
1645 if (IS_ERR(cursor)) {
1646 err = PTR_ERR(cursor);
1647 goto cleanup;
1648 }
1649 }
1650
1651 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001652 &tegra_crtc_funcs, NULL);
Thierry Redingc7679302014-10-21 13:51:53 +02001653 if (err < 0)
1654 goto cleanup;
1655
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001656 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1657
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001658 /*
1659 * Keep track of the minimum pitch alignment across all display
1660 * controllers.
1661 */
1662 if (dc->soc->pitch_align > tegra->pitch_align)
1663 tegra->pitch_align = dc->soc->pitch_align;
1664
Thierry Reding9910f5c2014-05-22 09:57:15 +02001665 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001666 if (err < 0 && err != -ENODEV) {
1667 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001668 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001669 }
1670
Thierry Reding6e5ff992012-11-28 11:45:47 +01001671 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001672 dev_name(dc->dev), dc);
1673 if (err < 0) {
1674 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1675 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001676 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001677 }
1678
1679 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001680
1681cleanup:
Thierry Reding47307952017-08-30 17:42:54 +02001682 if (!IS_ERR_OR_NULL(cursor))
Thierry Redingc7679302014-10-21 13:51:53 +02001683 drm_plane_cleanup(cursor);
1684
Thierry Reding47307952017-08-30 17:42:54 +02001685 if (!IS_ERR(primary))
Thierry Redingc7679302014-10-21 13:51:53 +02001686 drm_plane_cleanup(primary);
1687
1688 if (tegra->domain) {
1689 iommu_detach_device(tegra->domain, dc->dev);
1690 dc->domain = NULL;
1691 }
1692
1693 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001694}
1695
Thierry Reding53fa7f72013-09-24 15:35:40 +02001696static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001697{
Thierry Reding776dc382013-10-14 14:43:22 +02001698 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001699 int err;
1700
1701 devm_free_irq(dc->dev, dc->irq, dc);
1702
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001703 err = tegra_dc_rgb_exit(dc);
1704 if (err) {
1705 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1706 return err;
1707 }
1708
Thierry Redingdf06b752014-06-26 21:41:53 +02001709 if (dc->domain) {
1710 iommu_detach_device(dc->domain, dc->dev);
1711 dc->domain = NULL;
1712 }
1713
Thierry Reding2bcdcbf2015-08-24 14:47:10 +02001714 host1x_syncpt_free(dc->syncpt);
1715
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001716 return 0;
1717}
1718
1719static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001720 .init = tegra_dc_init,
1721 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001722};
1723
Thierry Reding8620fc62013-12-12 11:03:59 +01001724static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001725 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001726 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001727 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001728 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001729 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001730 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001731 .broken_reset = true,
Thierry Reding47307952017-08-30 17:42:54 +02001732 .has_nvdisplay = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001733};
1734
1735static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001736 .supports_background_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001737 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001738 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001739 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001740 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001741 .has_powergate = false,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001742 .broken_reset = false,
Thierry Reding47307952017-08-30 17:42:54 +02001743 .has_nvdisplay = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001744};
1745
1746static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001747 .supports_background_color = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001748 .supports_interlacing = false,
1749 .supports_cursor = false,
1750 .supports_block_linear = false,
1751 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001752 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001753 .broken_reset = false,
Thierry Reding47307952017-08-30 17:42:54 +02001754 .has_nvdisplay = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001755};
1756
1757static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001758 .supports_background_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001759 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001760 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001761 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001762 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001763 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001764 .broken_reset = false,
Thierry Reding47307952017-08-30 17:42:54 +02001765 .has_nvdisplay = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001766};
1767
Thierry Reding5b4f5162015-03-27 10:31:58 +01001768static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
Thierry Reding7116e9a2017-11-13 11:20:48 +01001769 .supports_background_color = true,
Thierry Reding5b4f5162015-03-27 10:31:58 +01001770 .supports_interlacing = true,
1771 .supports_cursor = true,
1772 .supports_block_linear = true,
1773 .pitch_align = 64,
1774 .has_powergate = true,
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03001775 .broken_reset = false,
Thierry Reding47307952017-08-30 17:42:54 +02001776 .has_nvdisplay = false,
1777};
1778
1779static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
1780 {
1781 .index = 0,
1782 .dc = 0,
1783 .windows = (const unsigned int[]) { 0 },
1784 .num_windows = 1,
1785 }, {
1786 .index = 1,
1787 .dc = 1,
1788 .windows = (const unsigned int[]) { 1 },
1789 .num_windows = 1,
1790 }, {
1791 .index = 2,
1792 .dc = 1,
1793 .windows = (const unsigned int[]) { 2 },
1794 .num_windows = 1,
1795 }, {
1796 .index = 3,
1797 .dc = 2,
1798 .windows = (const unsigned int[]) { 3 },
1799 .num_windows = 1,
1800 }, {
1801 .index = 4,
1802 .dc = 2,
1803 .windows = (const unsigned int[]) { 4 },
1804 .num_windows = 1,
1805 }, {
1806 .index = 5,
1807 .dc = 2,
1808 .windows = (const unsigned int[]) { 5 },
1809 .num_windows = 1,
1810 },
1811};
1812
1813static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
1814 .supports_background_color = true,
1815 .supports_interlacing = true,
1816 .supports_cursor = true,
1817 .supports_block_linear = true,
1818 .pitch_align = 64,
1819 .has_powergate = false,
1820 .broken_reset = false,
1821 .has_nvdisplay = true,
1822 .wgrps = tegra186_dc_wgrps,
1823 .num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
Thierry Reding5b4f5162015-03-27 10:31:58 +01001824};
1825
Thierry Reding8620fc62013-12-12 11:03:59 +01001826static const struct of_device_id tegra_dc_of_match[] = {
1827 {
Thierry Reding47307952017-08-30 17:42:54 +02001828 .compatible = "nvidia,tegra186-dc",
1829 .data = &tegra186_dc_soc_info,
1830 }, {
Thierry Reding5b4f5162015-03-27 10:31:58 +01001831 .compatible = "nvidia,tegra210-dc",
1832 .data = &tegra210_dc_soc_info,
1833 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001834 .compatible = "nvidia,tegra124-dc",
1835 .data = &tegra124_dc_soc_info,
1836 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001837 .compatible = "nvidia,tegra114-dc",
1838 .data = &tegra114_dc_soc_info,
1839 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001840 .compatible = "nvidia,tegra30-dc",
1841 .data = &tegra30_dc_soc_info,
1842 }, {
1843 .compatible = "nvidia,tegra20-dc",
1844 .data = &tegra20_dc_soc_info,
1845 }, {
1846 /* sentinel */
1847 }
1848};
Stephen Warrenef707282014-06-18 16:21:55 -06001849MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001850
Thierry Reding13411dd2014-01-09 17:08:36 +01001851static int tegra_dc_parse_dt(struct tegra_dc *dc)
1852{
1853 struct device_node *np;
1854 u32 value = 0;
1855 int err;
1856
1857 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1858 if (err < 0) {
1859 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1860
1861 /*
1862 * If the nvidia,head property isn't present, try to find the
1863 * correct head number by looking up the position of this
1864 * display controller's node within the device tree. Assuming
1865 * that the nodes are ordered properly in the DTS file and
1866 * that the translation into a flattened device tree blob
1867 * preserves that ordering this will actually yield the right
1868 * head number.
1869 *
1870 * If those assumptions don't hold, this will still work for
1871 * cases where only a single display controller is used.
1872 */
1873 for_each_matching_node(np, tegra_dc_of_match) {
Julia Lawallcf6b1742015-10-24 16:42:31 +02001874 if (np == dc->dev->of_node) {
1875 of_node_put(np);
Thierry Reding13411dd2014-01-09 17:08:36 +01001876 break;
Julia Lawallcf6b1742015-10-24 16:42:31 +02001877 }
Thierry Reding13411dd2014-01-09 17:08:36 +01001878
1879 value++;
1880 }
1881 }
1882
1883 dc->pipe = value;
1884
1885 return 0;
1886}
1887
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001888static int tegra_dc_probe(struct platform_device *pdev)
1889{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001890 struct resource *regs;
1891 struct tegra_dc *dc;
1892 int err;
1893
1894 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1895 if (!dc)
1896 return -ENOMEM;
1897
Thierry Redingb9ff7ae2017-08-21 16:35:17 +02001898 dc->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding8620fc62013-12-12 11:03:59 +01001899
Thierry Reding6e5ff992012-11-28 11:45:47 +01001900 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001901 INIT_LIST_HEAD(&dc->list);
1902 dc->dev = &pdev->dev;
1903
Thierry Reding13411dd2014-01-09 17:08:36 +01001904 err = tegra_dc_parse_dt(dc);
1905 if (err < 0)
1906 return err;
1907
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001908 dc->clk = devm_clk_get(&pdev->dev, NULL);
1909 if (IS_ERR(dc->clk)) {
1910 dev_err(&pdev->dev, "failed to get clock\n");
1911 return PTR_ERR(dc->clk);
1912 }
1913
Stephen Warrenca480802013-11-06 16:20:54 -07001914 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1915 if (IS_ERR(dc->rst)) {
1916 dev_err(&pdev->dev, "failed to get reset\n");
1917 return PTR_ERR(dc->rst);
1918 }
1919
Thierry Redinga2f2f742017-08-30 17:41:00 +02001920 /* assert reset and disable clock */
1921 if (!dc->soc->broken_reset) {
1922 err = clk_prepare_enable(dc->clk);
1923 if (err < 0)
1924 return err;
1925
1926 usleep_range(2000, 4000);
1927
1928 err = reset_control_assert(dc->rst);
1929 if (err < 0)
1930 return err;
1931
1932 usleep_range(2000, 4000);
1933
1934 clk_disable_unprepare(dc->clk);
1935 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02001936
Thierry Reding9c012702014-07-07 15:32:53 +02001937 if (dc->soc->has_powergate) {
1938 if (dc->pipe == 0)
1939 dc->powergate = TEGRA_POWERGATE_DIS;
1940 else
1941 dc->powergate = TEGRA_POWERGATE_DISB;
1942
Thierry Reding33a8eb82015-08-03 13:20:49 +02001943 tegra_powergate_power_off(dc->powergate);
Thierry Reding9c012702014-07-07 15:32:53 +02001944 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001945
1946 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001947 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1948 if (IS_ERR(dc->regs))
1949 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001950
1951 dc->irq = platform_get_irq(pdev, 0);
1952 if (dc->irq < 0) {
1953 dev_err(&pdev->dev, "failed to get IRQ\n");
1954 return -ENXIO;
1955 }
1956
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001957 err = tegra_dc_rgb_probe(dc);
1958 if (err < 0 && err != -ENODEV) {
1959 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1960 return err;
1961 }
1962
Thierry Reding33a8eb82015-08-03 13:20:49 +02001963 platform_set_drvdata(pdev, dc);
1964 pm_runtime_enable(&pdev->dev);
1965
1966 INIT_LIST_HEAD(&dc->client.list);
1967 dc->client.ops = &dc_client_ops;
1968 dc->client.dev = &pdev->dev;
1969
Thierry Reding776dc382013-10-14 14:43:22 +02001970 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001971 if (err < 0) {
1972 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1973 err);
1974 return err;
1975 }
1976
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001977 return 0;
1978}
1979
1980static int tegra_dc_remove(struct platform_device *pdev)
1981{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001982 struct tegra_dc *dc = platform_get_drvdata(pdev);
1983 int err;
1984
Thierry Reding776dc382013-10-14 14:43:22 +02001985 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001986 if (err < 0) {
1987 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1988 err);
1989 return err;
1990 }
1991
Thierry Reding59d29c02013-10-14 14:26:42 +02001992 err = tegra_dc_rgb_remove(dc);
1993 if (err < 0) {
1994 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1995 return err;
1996 }
1997
Thierry Reding33a8eb82015-08-03 13:20:49 +02001998 pm_runtime_disable(&pdev->dev);
1999
2000 return 0;
2001}
2002
2003#ifdef CONFIG_PM
2004static int tegra_dc_suspend(struct device *dev)
2005{
2006 struct tegra_dc *dc = dev_get_drvdata(dev);
2007 int err;
2008
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002009 if (!dc->soc->broken_reset) {
2010 err = reset_control_assert(dc->rst);
2011 if (err < 0) {
2012 dev_err(dev, "failed to assert reset: %d\n", err);
2013 return err;
2014 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002015 }
Thierry Reding9c012702014-07-07 15:32:53 +02002016
2017 if (dc->soc->has_powergate)
2018 tegra_powergate_power_off(dc->powergate);
2019
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002020 clk_disable_unprepare(dc->clk);
2021
2022 return 0;
2023}
2024
Thierry Reding33a8eb82015-08-03 13:20:49 +02002025static int tegra_dc_resume(struct device *dev)
2026{
2027 struct tegra_dc *dc = dev_get_drvdata(dev);
2028 int err;
2029
2030 if (dc->soc->has_powergate) {
2031 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2032 dc->rst);
2033 if (err < 0) {
2034 dev_err(dev, "failed to power partition: %d\n", err);
2035 return err;
2036 }
2037 } else {
2038 err = clk_prepare_enable(dc->clk);
2039 if (err < 0) {
2040 dev_err(dev, "failed to enable clock: %d\n", err);
2041 return err;
2042 }
2043
Dmitry Osipenko6ac15712017-06-15 02:18:29 +03002044 if (!dc->soc->broken_reset) {
2045 err = reset_control_deassert(dc->rst);
2046 if (err < 0) {
2047 dev_err(dev,
2048 "failed to deassert reset: %d\n", err);
2049 return err;
2050 }
Thierry Reding33a8eb82015-08-03 13:20:49 +02002051 }
2052 }
2053
2054 return 0;
2055}
2056#endif
2057
2058static const struct dev_pm_ops tegra_dc_pm_ops = {
2059 SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
2060};
2061
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002062struct platform_driver tegra_dc_driver = {
2063 .driver = {
2064 .name = "tegra-dc",
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002065 .of_match_table = tegra_dc_of_match,
Thierry Reding33a8eb82015-08-03 13:20:49 +02002066 .pm = &tegra_dc_pm_ops,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00002067 },
2068 .probe = tegra_dc_probe,
2069 .remove = tegra_dc_remove,
2070};