1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/iommu.h>
11 #include <linux/interconnect.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/reset.h>
17 #include <soc/tegra/pmc.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_debugfs.h>
22 #include <drm/drm_fourcc.h>
23 #include <drm/drm_plane_helper.h>
24 #include <drm/drm_vblank.h>
32 static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
33 struct drm_crtc_state *state);
35 static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
43 /* Reads the active copy of a register. */
44 static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
48 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
49 value = tegra_dc_readl(dc, offset);
50 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
55 static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
58 if (offset >= 0x500 && offset <= 0x638) {
59 offset = 0x000 + (offset - 0x500);
60 return plane->offset + offset;
63 if (offset >= 0x700 && offset <= 0x719) {
64 offset = 0x180 + (offset - 0x700);
65 return plane->offset + offset;
68 if (offset >= 0x800 && offset <= 0x839) {
69 offset = 0x1c0 + (offset - 0x800);
70 return plane->offset + offset;
73 dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
75 return plane->offset + offset;
78 static inline u32 tegra_plane_readl(struct tegra_plane *plane,
81 return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
84 static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
87 tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
90 bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
92 struct device_node *np = dc->dev->of_node;
93 struct of_phandle_iterator it;
96 of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
97 if (it.node == dev->of_node)
104 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
105 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
106 * Latching happens mmediately if the display controller is in STOP mode or
107 * on the next frame boundary otherwise.
109 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
110 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
111 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
112 * into the ACTIVE copy, either immediately if the display controller is in
113 * STOP mode, or at the next frame boundary otherwise.
115 void tegra_dc_commit(struct tegra_dc *dc)
117 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
118 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
121 static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
124 fixed20_12 outf = dfixed_init(out);
125 fixed20_12 inf = dfixed_init(in);
146 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
147 inf.full -= dfixed_const(1);
149 dda_inc = dfixed_div(inf, outf);
150 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
155 static inline u32 compute_initial_dda(unsigned int in)
157 fixed20_12 inf = dfixed_init(in);
158 return dfixed_frac(inf);
161 static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
163 u32 background[3] = {
164 BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
165 BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
166 BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
168 u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
169 BLEND_COLOR_KEY_NONE;
170 u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
171 struct tegra_plane_state *state;
175 /* disable blending for non-overlapping case */
176 tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
177 tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
179 state = to_tegra_plane_state(plane->base.state);
183 * Since custom fix-weight blending isn't utilized and weight
184 * of top window is set to max, we can enforce dependent
185 * blending which in this case results in transparent bottom
186 * window if top window is opaque and if top window enables
187 * alpha blending, then bottom window is getting alpha value
188 * of 1 minus the sum of alpha components of the overlapping
191 background[0] |= BLEND_CONTROL_DEPENDENT;
192 background[1] |= BLEND_CONTROL_DEPENDENT;
195 * The region where three windows overlap is the intersection
196 * of the two regions where two windows overlap. It contributes
197 * to the area if all of the windows on top of it have an alpha
200 switch (state->base.normalized_zpos) {
202 if (state->blending[0].alpha &&
203 state->blending[1].alpha)
204 background[2] |= BLEND_CONTROL_DEPENDENT;
208 background[2] |= BLEND_CONTROL_DEPENDENT;
213 * Enable alpha blending if pixel format has an alpha
216 foreground |= BLEND_CONTROL_ALPHA;
219 * If any of the windows on top of this window is opaque, it
220 * will completely conceal this window within that area. If
221 * top window has an alpha component, it is blended over the
224 for (i = 0; i < 2; i++) {
225 if (state->blending[i].alpha &&
226 state->blending[i].top)
227 background[i] |= BLEND_CONTROL_DEPENDENT;
230 switch (state->base.normalized_zpos) {
232 if (state->blending[0].alpha &&
233 state->blending[1].alpha)
234 background[2] |= BLEND_CONTROL_DEPENDENT;
239 * When both middle and topmost windows have an alpha,
240 * these windows a mixed together and then the result
241 * is blended over the bottom window.
243 if (state->blending[0].alpha &&
244 state->blending[0].top)
245 background[2] |= BLEND_CONTROL_ALPHA;
247 if (state->blending[1].alpha &&
248 state->blending[1].top)
249 background[2] |= BLEND_CONTROL_ALPHA;
254 switch (state->base.normalized_zpos) {
256 tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
257 tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
258 tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
263 * If window B / C is topmost, then X / Y registers are
264 * matching the order of blending[...] state indices,
265 * otherwise a swap is required.
267 if (!state->blending[0].top && state->blending[1].top) {
268 blending[0] = foreground;
269 blending[1] = background[1];
271 blending[0] = background[0];
272 blending[1] = foreground;
275 tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
276 tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
277 tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
281 tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
282 tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
283 tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
288 static void tegra_plane_setup_blending(struct tegra_plane *plane,
289 const struct tegra_dc_window *window)
293 value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
294 BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
295 BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
296 tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
298 value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
299 BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
300 BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
301 tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
303 value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
304 tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
308 tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
309 const struct tegra_dc_window *window)
311 struct tegra_dc *dc = plane->dc;
313 if (window->src.w == window->dst.w)
316 if (plane->index == 0 && dc->soc->has_win_a_without_filters)
323 tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
324 const struct tegra_dc_window *window)
326 struct tegra_dc *dc = plane->dc;
328 if (window->src.h == window->dst.h)
331 if (plane->index == 0 && dc->soc->has_win_a_without_filters)
334 if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
340 static void tegra_dc_setup_window(struct tegra_plane *plane,
341 const struct tegra_dc_window *window)
343 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
344 struct tegra_dc *dc = plane->dc;
349 * For YUV planar modes, the number of bytes per pixel takes into
350 * account only the luma component and therefore is 1.
352 yuv = tegra_plane_format_is_yuv(window->format, &planar, NULL);
354 bpp = window->bits_per_pixel / 8;
356 bpp = planar ? 1 : 2;
358 tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
359 tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
361 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
362 tegra_plane_writel(plane, value, DC_WIN_POSITION);
364 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
365 tegra_plane_writel(plane, value, DC_WIN_SIZE);
367 h_offset = window->src.x * bpp;
368 v_offset = window->src.y;
369 h_size = window->src.w * bpp;
370 v_size = window->src.h;
372 if (window->reflect_x)
373 h_offset += (window->src.w - 1) * bpp;
375 if (window->reflect_y)
376 v_offset += window->src.h - 1;
378 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
379 tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
382 * For DDA computations the number of bytes per pixel for YUV planar
383 * modes needs to take into account all Y, U and V components.
388 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
389 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
391 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
392 tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
394 h_dda = compute_initial_dda(window->src.x);
395 v_dda = compute_initial_dda(window->src.y);
397 tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
398 tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
400 tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
401 tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
403 tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
406 tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
407 tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
408 value = window->stride[1] << 16 | window->stride[0];
409 tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
411 tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
414 tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
415 tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
417 if (dc->soc->supports_block_linear) {
418 unsigned long height = window->tiling.value;
420 switch (window->tiling.mode) {
421 case TEGRA_BO_TILING_MODE_PITCH:
422 value = DC_WINBUF_SURFACE_KIND_PITCH;
425 case TEGRA_BO_TILING_MODE_TILED:
426 value = DC_WINBUF_SURFACE_KIND_TILED;
429 case TEGRA_BO_TILING_MODE_BLOCK:
430 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
431 DC_WINBUF_SURFACE_KIND_BLOCK;
435 tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
437 switch (window->tiling.mode) {
438 case TEGRA_BO_TILING_MODE_PITCH:
439 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
440 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
443 case TEGRA_BO_TILING_MODE_TILED:
444 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
445 DC_WIN_BUFFER_ADDR_MODE_TILE;
448 case TEGRA_BO_TILING_MODE_BLOCK:
450 * No need to handle this here because ->atomic_check
451 * will already have filtered it out.
456 tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
462 /* setup default colorspace conversion coefficients */
463 tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
464 tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
465 tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
466 tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
467 tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
468 tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
469 tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
470 tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
473 } else if (window->bits_per_pixel < 24) {
474 value |= COLOR_EXPAND;
477 if (window->reflect_x)
478 value |= H_DIRECTION;
480 if (window->reflect_y)
481 value |= V_DIRECTION;
483 if (tegra_plane_use_horizontal_filtering(plane, window)) {
485 * Enable horizontal 6-tap filter and set filtering
486 * coefficients to the default values defined in TRM.
488 tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
489 tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
490 tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
491 tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
492 tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
493 tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
494 tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
495 tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
496 tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
497 tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
498 tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
499 tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
500 tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
501 tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
502 tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
503 tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
508 if (tegra_plane_use_vertical_filtering(plane, window)) {
512 * Enable vertical 2-tap filter and set filtering
513 * coefficients to the default values defined in TRM.
515 for (i = 0, k = 128; i < 16; i++, k -= 8)
516 tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
521 tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
523 if (dc->soc->has_legacy_blending)
524 tegra_plane_setup_blending_legacy(plane);
526 tegra_plane_setup_blending(plane, window);
529 static const u32 tegra20_primary_formats[] = {
536 /* non-native formats */
543 static const u64 tegra20_modifiers[] = {
544 DRM_FORMAT_MOD_LINEAR,
545 DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
546 DRM_FORMAT_MOD_INVALID
549 static const u32 tegra114_primary_formats[] = {
556 /* new on Tegra114 */
571 static const u32 tegra124_primary_formats[] = {
578 /* new on Tegra114 */
591 /* new on Tegra124 */
596 static const u64 tegra124_modifiers[] = {
597 DRM_FORMAT_MOD_LINEAR,
598 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
599 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
600 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
601 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
602 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
603 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
604 DRM_FORMAT_MOD_INVALID
607 static int tegra_plane_atomic_check(struct drm_plane *plane,
608 struct drm_atomic_state *state)
610 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
612 struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
613 unsigned int supported_rotation = DRM_MODE_ROTATE_0 |
616 unsigned int rotation = new_plane_state->rotation;
617 struct tegra_bo_tiling *tiling = &plane_state->tiling;
618 struct tegra_plane *tegra = to_tegra_plane(plane);
619 struct tegra_dc *dc = to_tegra_dc(new_plane_state->crtc);
622 plane_state->peak_memory_bandwidth = 0;
623 plane_state->avg_memory_bandwidth = 0;
625 /* no need for further checks if the plane is being disabled */
626 if (!new_plane_state->crtc) {
627 plane_state->total_peak_memory_bandwidth = 0;
631 err = tegra_plane_format(new_plane_state->fb->format->format,
632 &plane_state->format,
638 * Tegra20 and Tegra30 are special cases here because they support
639 * only variants of specific formats with an alpha component, but not
640 * the corresponding opaque formats. However, the opaque formats can
641 * be emulated by disabling alpha blending for the plane.
643 if (dc->soc->has_legacy_blending) {
644 err = tegra_plane_setup_legacy_state(tegra, plane_state);
649 err = tegra_fb_get_tiling(new_plane_state->fb, tiling);
653 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
654 !dc->soc->supports_block_linear) {
655 DRM_ERROR("hardware doesn't support block linear mode\n");
660 * Older userspace used custom BO flag in order to specify the Y
661 * reflection, while modern userspace uses the generic DRM rotation
662 * property in order to achieve the same result. The legacy BO flag
663 * duplicates the DRM rotation property when both are set.
665 if (tegra_fb_is_bottom_up(new_plane_state->fb))
666 rotation |= DRM_MODE_REFLECT_Y;
668 rotation = drm_rotation_simplify(rotation, supported_rotation);
670 if (rotation & DRM_MODE_REFLECT_X)
671 plane_state->reflect_x = true;
673 plane_state->reflect_x = false;
675 if (rotation & DRM_MODE_REFLECT_Y)
676 plane_state->reflect_y = true;
678 plane_state->reflect_y = false;
681 * Tegra doesn't support different strides for U and V planes so we
682 * error out if the user tries to display a framebuffer with such a
685 if (new_plane_state->fb->format->num_planes > 2) {
686 if (new_plane_state->fb->pitches[2] != new_plane_state->fb->pitches[1]) {
687 DRM_ERROR("unsupported UV-plane configuration\n");
692 err = tegra_plane_state_add(tegra, new_plane_state);
699 static void tegra_plane_atomic_disable(struct drm_plane *plane,
700 struct drm_atomic_state *state)
702 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
704 struct tegra_plane *p = to_tegra_plane(plane);
707 /* rien ne va plus */
708 if (!old_state || !old_state->crtc)
711 value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
712 value &= ~WIN_ENABLE;
713 tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
716 static void tegra_plane_atomic_update(struct drm_plane *plane,
717 struct drm_atomic_state *state)
719 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
721 struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
722 struct drm_framebuffer *fb = new_state->fb;
723 struct tegra_plane *p = to_tegra_plane(plane);
724 struct tegra_dc_window window;
727 /* rien ne va plus */
728 if (!new_state->crtc || !new_state->fb)
731 if (!new_state->visible)
732 return tegra_plane_atomic_disable(plane, state);
734 memset(&window, 0, sizeof(window));
735 window.src.x = new_state->src.x1 >> 16;
736 window.src.y = new_state->src.y1 >> 16;
737 window.src.w = drm_rect_width(&new_state->src) >> 16;
738 window.src.h = drm_rect_height(&new_state->src) >> 16;
739 window.dst.x = new_state->dst.x1;
740 window.dst.y = new_state->dst.y1;
741 window.dst.w = drm_rect_width(&new_state->dst);
742 window.dst.h = drm_rect_height(&new_state->dst);
743 window.bits_per_pixel = fb->format->cpp[0] * 8;
744 window.reflect_x = tegra_plane_state->reflect_x;
745 window.reflect_y = tegra_plane_state->reflect_y;
747 /* copy from state */
748 window.zpos = new_state->normalized_zpos;
749 window.tiling = tegra_plane_state->tiling;
750 window.format = tegra_plane_state->format;
751 window.swap = tegra_plane_state->swap;
753 for (i = 0; i < fb->format->num_planes; i++) {
754 window.base[i] = tegra_plane_state->iova[i] + fb->offsets[i];
757 * Tegra uses a shared stride for UV planes. Framebuffers are
758 * already checked for this in the tegra_plane_atomic_check()
759 * function, so it's safe to ignore the V-plane pitch here.
762 window.stride[i] = fb->pitches[i];
765 tegra_dc_setup_window(p, &window);
768 static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
769 .prepare_fb = tegra_plane_prepare_fb,
770 .cleanup_fb = tegra_plane_cleanup_fb,
771 .atomic_check = tegra_plane_atomic_check,
772 .atomic_disable = tegra_plane_atomic_disable,
773 .atomic_update = tegra_plane_atomic_update,
776 static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
779 * Ideally this would use drm_crtc_mask(), but that would require the
780 * CRTC to already be in the mode_config's list of CRTCs. However, it
781 * will only be added to that list in the drm_crtc_init_with_planes()
782 * (in tegra_dc_init()), which in turn requires registration of these
783 * planes. So we have ourselves a nice little chicken and egg problem
786 * We work around this by manually creating the mask from the number
787 * of CRTCs that have been registered, and should therefore always be
788 * the same as drm_crtc_index() after registration.
790 return 1 << drm->mode_config.num_crtc;
793 static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
796 unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
797 enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
798 struct tegra_plane *plane;
799 unsigned int num_formats;
800 const u64 *modifiers;
804 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
806 return ERR_PTR(-ENOMEM);
808 /* Always use window A as primary window */
809 plane->offset = 0xa00;
813 num_formats = dc->soc->num_primary_formats;
814 formats = dc->soc->primary_formats;
815 modifiers = dc->soc->modifiers;
817 err = tegra_plane_interconnect_init(plane);
823 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
824 &tegra_plane_funcs, formats,
825 num_formats, modifiers, type, NULL);
831 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
832 drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
834 err = drm_plane_create_rotation_property(&plane->base,
837 DRM_MODE_ROTATE_180 |
841 dev_err(dc->dev, "failed to create rotation property: %d\n",
847 static const u32 tegra_legacy_cursor_plane_formats[] = {
851 static const u32 tegra_cursor_plane_formats[] = {
855 static int tegra_cursor_atomic_check(struct drm_plane *plane,
856 struct drm_atomic_state *state)
858 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
860 struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
861 struct tegra_plane *tegra = to_tegra_plane(plane);
864 plane_state->peak_memory_bandwidth = 0;
865 plane_state->avg_memory_bandwidth = 0;
867 /* no need for further checks if the plane is being disabled */
868 if (!new_plane_state->crtc) {
869 plane_state->total_peak_memory_bandwidth = 0;
873 /* scaling not supported for cursor */
874 if ((new_plane_state->src_w >> 16 != new_plane_state->crtc_w) ||
875 (new_plane_state->src_h >> 16 != new_plane_state->crtc_h))
878 /* only square cursors supported */
879 if (new_plane_state->src_w != new_plane_state->src_h)
882 if (new_plane_state->crtc_w != 32 && new_plane_state->crtc_w != 64 &&
883 new_plane_state->crtc_w != 128 && new_plane_state->crtc_w != 256)
886 err = tegra_plane_state_add(tegra, new_plane_state);
893 static void tegra_cursor_atomic_update(struct drm_plane *plane,
894 struct drm_atomic_state *state)
896 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
898 struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
899 struct tegra_dc *dc = to_tegra_dc(new_state->crtc);
900 struct tegra_drm *tegra = plane->dev->dev_private;
901 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
902 u64 dma_mask = *dc->dev->dma_mask;
907 /* rien ne va plus */
908 if (!new_state->crtc || !new_state->fb)
912 * Legacy display supports hardware clipping of the cursor, but
913 * nvdisplay relies on software to clip the cursor to the screen.
915 if (!dc->soc->has_nvdisplay)
916 value |= CURSOR_CLIP_DISPLAY;
918 switch (new_state->crtc_w) {
920 value |= CURSOR_SIZE_32x32;
924 value |= CURSOR_SIZE_64x64;
928 value |= CURSOR_SIZE_128x128;
932 value |= CURSOR_SIZE_256x256;
936 WARN(1, "cursor size %ux%u not supported\n",
937 new_state->crtc_w, new_state->crtc_h);
941 value |= (tegra_plane_state->iova[0] >> 10) & 0x3fffff;
942 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
944 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
945 value = (tegra_plane_state->iova[0] >> 32) & (dma_mask >> 32);
946 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
949 /* enable cursor and set blend mode */
950 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
951 value |= CURSOR_ENABLE;
952 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
954 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
955 value &= ~CURSOR_DST_BLEND_MASK;
956 value &= ~CURSOR_SRC_BLEND_MASK;
958 if (dc->soc->has_nvdisplay)
959 value &= ~CURSOR_COMPOSITION_MODE_XOR;
961 value |= CURSOR_MODE_NORMAL;
963 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
964 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
965 value |= CURSOR_ALPHA;
966 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
968 /* nvdisplay relies on software for clipping */
969 if (dc->soc->has_nvdisplay) {
972 x = new_state->dst.x1;
973 y = new_state->dst.y1;
975 drm_rect_fp_to_int(&src, &new_state->src);
977 value = (src.y1 & tegra->vmask) << 16 | (src.x1 & tegra->hmask);
978 tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_POINT_IN_CURSOR);
980 value = (drm_rect_height(&src) & tegra->vmask) << 16 |
981 (drm_rect_width(&src) & tegra->hmask);
982 tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_SIZE_IN_CURSOR);
984 x = new_state->crtc_x;
985 y = new_state->crtc_y;
988 /* position the cursor */
989 value = ((y & tegra->vmask) << 16) | (x & tegra->hmask);
990 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
993 static void tegra_cursor_atomic_disable(struct drm_plane *plane,
994 struct drm_atomic_state *state)
996 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
1001 /* rien ne va plus */
1002 if (!old_state || !old_state->crtc)
1005 dc = to_tegra_dc(old_state->crtc);
1007 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1008 value &= ~CURSOR_ENABLE;
1009 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1012 static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
1013 .prepare_fb = tegra_plane_prepare_fb,
1014 .cleanup_fb = tegra_plane_cleanup_fb,
1015 .atomic_check = tegra_cursor_atomic_check,
1016 .atomic_update = tegra_cursor_atomic_update,
1017 .atomic_disable = tegra_cursor_atomic_disable,
1020 static const uint64_t linear_modifiers[] = {
1021 DRM_FORMAT_MOD_LINEAR,
1022 DRM_FORMAT_MOD_INVALID
1025 static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
1026 struct tegra_dc *dc)
1028 unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1029 struct tegra_plane *plane;
1030 unsigned int num_formats;
1034 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1036 return ERR_PTR(-ENOMEM);
1039 * This index is kind of fake. The cursor isn't a regular plane, but
1040 * its update and activation request bits in DC_CMD_STATE_CONTROL do
1041 * use the same programming. Setting this fake index here allows the
1042 * code in tegra_add_plane_state() to do the right thing without the
1043 * need to special-casing the cursor plane.
1048 if (!dc->soc->has_nvdisplay) {
1049 num_formats = ARRAY_SIZE(tegra_legacy_cursor_plane_formats);
1050 formats = tegra_legacy_cursor_plane_formats;
1052 err = tegra_plane_interconnect_init(plane);
1055 return ERR_PTR(err);
1058 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
1059 formats = tegra_cursor_plane_formats;
1062 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1063 &tegra_plane_funcs, formats,
1064 num_formats, linear_modifiers,
1065 DRM_PLANE_TYPE_CURSOR, NULL);
1068 return ERR_PTR(err);
1071 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
1072 drm_plane_create_zpos_immutable_property(&plane->base, 255);
1074 return &plane->base;
1077 static const u32 tegra20_overlay_formats[] = {
1078 DRM_FORMAT_ARGB4444,
1079 DRM_FORMAT_ARGB1555,
1081 DRM_FORMAT_RGBA5551,
1082 DRM_FORMAT_ABGR8888,
1083 DRM_FORMAT_ARGB8888,
1084 /* non-native formats */
1085 DRM_FORMAT_XRGB1555,
1086 DRM_FORMAT_RGBX5551,
1087 DRM_FORMAT_XBGR8888,
1088 DRM_FORMAT_XRGB8888,
1089 /* planar formats */
1096 static const u32 tegra114_overlay_formats[] = {
1097 DRM_FORMAT_ARGB4444,
1098 DRM_FORMAT_ARGB1555,
1100 DRM_FORMAT_RGBA5551,
1101 DRM_FORMAT_ABGR8888,
1102 DRM_FORMAT_ARGB8888,
1103 /* new on Tegra114 */
1104 DRM_FORMAT_ABGR4444,
1105 DRM_FORMAT_ABGR1555,
1106 DRM_FORMAT_BGRA5551,
1107 DRM_FORMAT_XRGB1555,
1108 DRM_FORMAT_RGBX5551,
1109 DRM_FORMAT_XBGR1555,
1110 DRM_FORMAT_BGRX5551,
1112 DRM_FORMAT_BGRA8888,
1113 DRM_FORMAT_RGBA8888,
1114 DRM_FORMAT_XRGB8888,
1115 DRM_FORMAT_XBGR8888,
1116 /* planar formats */
1123 static const u32 tegra124_overlay_formats[] = {
1124 DRM_FORMAT_ARGB4444,
1125 DRM_FORMAT_ARGB1555,
1127 DRM_FORMAT_RGBA5551,
1128 DRM_FORMAT_ABGR8888,
1129 DRM_FORMAT_ARGB8888,
1130 /* new on Tegra114 */
1131 DRM_FORMAT_ABGR4444,
1132 DRM_FORMAT_ABGR1555,
1133 DRM_FORMAT_BGRA5551,
1134 DRM_FORMAT_XRGB1555,
1135 DRM_FORMAT_RGBX5551,
1136 DRM_FORMAT_XBGR1555,
1137 DRM_FORMAT_BGRX5551,
1139 DRM_FORMAT_BGRA8888,
1140 DRM_FORMAT_RGBA8888,
1141 DRM_FORMAT_XRGB8888,
1142 DRM_FORMAT_XBGR8888,
1143 /* new on Tegra124 */
1144 DRM_FORMAT_RGBX8888,
1145 DRM_FORMAT_BGRX8888,
1146 /* planar formats */
1153 static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
1154 struct tegra_dc *dc,
1158 unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
1159 struct tegra_plane *plane;
1160 unsigned int num_formats;
1161 enum drm_plane_type type;
1165 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
1167 return ERR_PTR(-ENOMEM);
1169 plane->offset = 0xa00 + 0x200 * index;
1170 plane->index = index;
1173 num_formats = dc->soc->num_overlay_formats;
1174 formats = dc->soc->overlay_formats;
1176 err = tegra_plane_interconnect_init(plane);
1179 return ERR_PTR(err);
1183 type = DRM_PLANE_TYPE_OVERLAY;
1185 type = DRM_PLANE_TYPE_CURSOR;
1187 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
1188 &tegra_plane_funcs, formats,
1189 num_formats, linear_modifiers,
1193 return ERR_PTR(err);
1196 drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
1197 drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
1199 err = drm_plane_create_rotation_property(&plane->base,
1202 DRM_MODE_ROTATE_180 |
1203 DRM_MODE_REFLECT_X |
1204 DRM_MODE_REFLECT_Y);
1206 dev_err(dc->dev, "failed to create rotation property: %d\n",
1209 return &plane->base;
1212 static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
1213 struct tegra_dc *dc)
1215 struct drm_plane *plane, *primary = NULL;
1218 for (i = 0; i < dc->soc->num_wgrps; i++) {
1219 const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
1221 if (wgrp->dc == dc->pipe) {
1222 for (j = 0; j < wgrp->num_windows; j++) {
1223 unsigned int index = wgrp->windows[j];
1225 plane = tegra_shared_plane_create(drm, dc,
1232 * Choose the first shared plane owned by this
1233 * head as the primary plane.
1236 plane->type = DRM_PLANE_TYPE_PRIMARY;
1246 static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
1247 struct tegra_dc *dc)
1249 struct drm_plane *planes[2], *primary;
1250 unsigned int planes_num;
1254 primary = tegra_primary_plane_create(drm, dc);
1255 if (IS_ERR(primary))
1258 if (dc->soc->supports_cursor)
1263 for (i = 0; i < planes_num; i++) {
1264 planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
1266 if (IS_ERR(planes[i])) {
1267 err = PTR_ERR(planes[i]);
1270 tegra_plane_funcs.destroy(planes[i]);
1272 tegra_plane_funcs.destroy(primary);
1273 return ERR_PTR(err);
1280 static void tegra_dc_destroy(struct drm_crtc *crtc)
1282 drm_crtc_cleanup(crtc);
1285 static void tegra_crtc_reset(struct drm_crtc *crtc)
1287 struct tegra_dc_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
1290 tegra_crtc_atomic_destroy_state(crtc, crtc->state);
1292 __drm_atomic_helper_crtc_reset(crtc, &state->base);
1295 static struct drm_crtc_state *
1296 tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1298 struct tegra_dc_state *state = to_dc_state(crtc->state);
1299 struct tegra_dc_state *copy;
1301 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
1305 __drm_atomic_helper_crtc_duplicate_state(crtc, ©->base);
1306 copy->clk = state->clk;
1307 copy->pclk = state->pclk;
1308 copy->div = state->div;
1309 copy->planes = state->planes;
1314 static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1315 struct drm_crtc_state *state)
1317 __drm_atomic_helper_crtc_destroy_state(state);
1321 #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1323 static const struct debugfs_reg32 tegra_dc_regs[] = {
1324 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
1325 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
1326 DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
1327 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
1328 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
1329 DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
1330 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
1331 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
1332 DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
1333 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
1334 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
1335 DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
1336 DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
1337 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
1338 DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
1339 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
1340 DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
1341 DEBUGFS_REG32(DC_CMD_INT_STATUS),
1342 DEBUGFS_REG32(DC_CMD_INT_MASK),
1343 DEBUGFS_REG32(DC_CMD_INT_ENABLE),
1344 DEBUGFS_REG32(DC_CMD_INT_TYPE),
1345 DEBUGFS_REG32(DC_CMD_INT_POLARITY),
1346 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
1347 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
1348 DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
1349 DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
1350 DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
1351 DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
1352 DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
1353 DEBUGFS_REG32(DC_COM_CRC_CONTROL),
1354 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
1355 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
1356 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
1357 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
1358 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
1359 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
1360 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
1361 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
1362 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
1363 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
1364 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
1365 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
1366 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
1367 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
1368 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
1369 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
1370 DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
1371 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
1372 DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
1373 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
1374 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
1375 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
1376 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
1377 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
1378 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
1379 DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
1380 DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
1381 DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
1382 DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
1383 DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
1384 DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
1385 DEBUGFS_REG32(DC_COM_SPI_CONTROL),
1386 DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
1387 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
1388 DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
1389 DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
1390 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
1391 DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
1392 DEBUGFS_REG32(DC_COM_GPIO_CTRL),
1393 DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
1394 DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
1395 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
1396 DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
1397 DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
1398 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
1399 DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
1400 DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
1401 DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
1402 DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
1403 DEBUGFS_REG32(DC_DISP_BACK_PORCH),
1404 DEBUGFS_REG32(DC_DISP_ACTIVE),
1405 DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
1406 DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
1407 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
1408 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
1409 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
1410 DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
1411 DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
1412 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
1413 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
1414 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
1415 DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
1416 DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
1417 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
1418 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
1419 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
1420 DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
1421 DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
1422 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
1423 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
1424 DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
1425 DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
1426 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
1427 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
1428 DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
1429 DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
1430 DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
1431 DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
1432 DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
1433 DEBUGFS_REG32(DC_DISP_M0_CONTROL),
1434 DEBUGFS_REG32(DC_DISP_M1_CONTROL),
1435 DEBUGFS_REG32(DC_DISP_DI_CONTROL),
1436 DEBUGFS_REG32(DC_DISP_PP_CONTROL),
1437 DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
1438 DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
1439 DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
1440 DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
1441 DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
1442 DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
1443 DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
1444 DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
1445 DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
1446 DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
1447 DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
1448 DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
1449 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
1450 DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
1451 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
1452 DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
1453 DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
1454 DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
1455 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
1456 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
1457 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
1458 DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
1459 DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
1460 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
1461 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
1462 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
1463 DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
1464 DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
1465 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
1466 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
1467 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
1468 DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
1469 DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
1470 DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
1471 DEBUGFS_REG32(DC_DISP_SD_CONTROL),
1472 DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
1473 DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
1474 DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
1475 DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
1476 DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
1477 DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
1478 DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
1479 DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
1480 DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
1481 DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
1482 DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
1483 DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
1484 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
1485 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
1486 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
1487 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
1488 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
1489 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
1490 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
1491 DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
1492 DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
1493 DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
1494 DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
1495 DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
1496 DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
1497 DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
1498 DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
1499 DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
1500 DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
1501 DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
1502 DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
1503 DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
1504 DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
1505 DEBUGFS_REG32(DC_WIN_POSITION),
1506 DEBUGFS_REG32(DC_WIN_SIZE),
1507 DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
1508 DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
1509 DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
1510 DEBUGFS_REG32(DC_WIN_DDA_INC),
1511 DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
1512 DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
1513 DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
1514 DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
1515 DEBUGFS_REG32(DC_WIN_DV_CONTROL),
1516 DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
1517 DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
1518 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
1519 DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
1520 DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
1521 DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
1522 DEBUGFS_REG32(DC_WINBUF_START_ADDR),
1523 DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
1524 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
1525 DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
1526 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
1527 DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
1528 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
1529 DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
1530 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
1531 DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
1532 DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
1533 DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
1534 DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
1535 DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
1538 static int tegra_dc_show_regs(struct seq_file *s, void *data)
1540 struct drm_info_node *node = s->private;
1541 struct tegra_dc *dc = node->info_ent->data;
1545 drm_modeset_lock(&dc->base.mutex, NULL);
1547 if (!dc->base.state->active) {
1552 for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
1553 unsigned int offset = tegra_dc_regs[i].offset;
1555 seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
1556 offset, tegra_dc_readl(dc, offset));
1560 drm_modeset_unlock(&dc->base.mutex);
1564 static int tegra_dc_show_crc(struct seq_file *s, void *data)
1566 struct drm_info_node *node = s->private;
1567 struct tegra_dc *dc = node->info_ent->data;
1571 drm_modeset_lock(&dc->base.mutex, NULL);
1573 if (!dc->base.state->active) {
1578 value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
1579 tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
1580 tegra_dc_commit(dc);
1582 drm_crtc_wait_one_vblank(&dc->base);
1583 drm_crtc_wait_one_vblank(&dc->base);
1585 value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
1586 seq_printf(s, "%08x\n", value);
1588 tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
1591 drm_modeset_unlock(&dc->base.mutex);
1595 static int tegra_dc_show_stats(struct seq_file *s, void *data)
1597 struct drm_info_node *node = s->private;
1598 struct tegra_dc *dc = node->info_ent->data;
1600 seq_printf(s, "frames: %lu\n", dc->stats.frames);
1601 seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
1602 seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
1603 seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
1605 seq_printf(s, "frames total: %lu\n", dc->stats.frames_total);
1606 seq_printf(s, "vblank total: %lu\n", dc->stats.vblank_total);
1607 seq_printf(s, "underflow total: %lu\n", dc->stats.underflow_total);
1608 seq_printf(s, "overflow total: %lu\n", dc->stats.overflow_total);
1613 static struct drm_info_list debugfs_files[] = {
1614 { "regs", tegra_dc_show_regs, 0, NULL },
1615 { "crc", tegra_dc_show_crc, 0, NULL },
1616 { "stats", tegra_dc_show_stats, 0, NULL },
1619 static int tegra_dc_late_register(struct drm_crtc *crtc)
1621 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1622 struct drm_minor *minor = crtc->dev->primary;
1623 struct dentry *root;
1624 struct tegra_dc *dc = to_tegra_dc(crtc);
1626 #ifdef CONFIG_DEBUG_FS
1627 root = crtc->debugfs_entry;
1632 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1634 if (!dc->debugfs_files)
1637 for (i = 0; i < count; i++)
1638 dc->debugfs_files[i].data = dc;
1640 drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
1645 static void tegra_dc_early_unregister(struct drm_crtc *crtc)
1647 unsigned int count = ARRAY_SIZE(debugfs_files);
1648 struct drm_minor *minor = crtc->dev->primary;
1649 struct tegra_dc *dc = to_tegra_dc(crtc);
1651 drm_debugfs_remove_files(dc->debugfs_files, count, minor);
1652 kfree(dc->debugfs_files);
1653 dc->debugfs_files = NULL;
1656 static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
1658 struct tegra_dc *dc = to_tegra_dc(crtc);
1660 /* XXX vblank syncpoints don't work with nvdisplay yet */
1661 if (dc->syncpt && !dc->soc->has_nvdisplay)
1662 return host1x_syncpt_read(dc->syncpt);
1664 /* fallback to software emulated VBLANK counter */
1665 return (u32)drm_crtc_vblank_count(&dc->base);
1668 static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
1670 struct tegra_dc *dc = to_tegra_dc(crtc);
1673 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1674 value |= VBLANK_INT;
1675 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1680 static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
1682 struct tegra_dc *dc = to_tegra_dc(crtc);
1685 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1686 value &= ~VBLANK_INT;
1687 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
1690 static const struct drm_crtc_funcs tegra_crtc_funcs = {
1691 .page_flip = drm_atomic_helper_page_flip,
1692 .set_config = drm_atomic_helper_set_config,
1693 .destroy = tegra_dc_destroy,
1694 .reset = tegra_crtc_reset,
1695 .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
1696 .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
1697 .late_register = tegra_dc_late_register,
1698 .early_unregister = tegra_dc_early_unregister,
1699 .get_vblank_counter = tegra_dc_get_vblank_counter,
1700 .enable_vblank = tegra_dc_enable_vblank,
1701 .disable_vblank = tegra_dc_disable_vblank,
1704 static int tegra_dc_set_timings(struct tegra_dc *dc,
1705 struct drm_display_mode *mode)
1707 unsigned int h_ref_to_sync = 1;
1708 unsigned int v_ref_to_sync = 1;
1709 unsigned long value;
1711 if (!dc->soc->has_nvdisplay) {
1712 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1714 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1715 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1718 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1719 ((mode->hsync_end - mode->hsync_start) << 0);
1720 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1722 value = ((mode->vtotal - mode->vsync_end) << 16) |
1723 ((mode->htotal - mode->hsync_end) << 0);
1724 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1726 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1727 ((mode->hsync_start - mode->hdisplay) << 0);
1728 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1730 value = (mode->vdisplay << 16) | mode->hdisplay;
1731 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1737 * tegra_dc_state_setup_clock - check clock settings and store them in atomic
1739 * @dc: display controller
1740 * @crtc_state: CRTC atomic state
1741 * @clk: parent clock for display controller
1742 * @pclk: pixel clock
1743 * @div: shift clock divider
1746 * 0 on success or a negative error-code on failure.
1748 int tegra_dc_state_setup_clock(struct tegra_dc *dc,
1749 struct drm_crtc_state *crtc_state,
1750 struct clk *clk, unsigned long pclk,
1753 struct tegra_dc_state *state = to_dc_state(crtc_state);
1755 if (!clk_has_parent(dc->clk, clk))
1765 static void tegra_dc_commit_state(struct tegra_dc *dc,
1766 struct tegra_dc_state *state)
1771 err = clk_set_parent(dc->clk, state->clk);
1773 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1776 * Outputs may not want to change the parent clock rate. This is only
1777 * relevant to Tegra20 where only a single display PLL is available.
1778 * Since that PLL would typically be used for HDMI, an internal LVDS
1779 * panel would need to be driven by some other clock such as PLL_P
1780 * which is shared with other peripherals. Changing the clock rate
1781 * should therefore be avoided.
1783 if (state->pclk > 0) {
1784 err = clk_set_rate(state->clk, state->pclk);
1787 "failed to set clock rate to %lu Hz\n",
1790 err = clk_set_rate(dc->clk, state->pclk);
1792 dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
1793 dc->clk, state->pclk, err);
1796 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
1798 DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
1800 if (!dc->soc->has_nvdisplay) {
1801 value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
1802 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1806 static void tegra_dc_stop(struct tegra_dc *dc)
1810 /* stop the display controller */
1811 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1812 value &= ~DISP_CTRL_MODE_MASK;
1813 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1815 tegra_dc_commit(dc);
1818 static bool tegra_dc_idle(struct tegra_dc *dc)
1822 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1824 return (value & DISP_CTRL_MODE_MASK) == 0;
1827 static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1829 timeout = jiffies + msecs_to_jiffies(timeout);
1831 while (time_before(jiffies, timeout)) {
1832 if (tegra_dc_idle(dc))
1835 usleep_range(1000, 2000);
1838 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1843 tegra_crtc_update_memory_bandwidth(struct drm_crtc *crtc,
1844 struct drm_atomic_state *state,
1845 bool prepare_bandwidth_transition)
1847 const struct tegra_plane_state *old_tegra_state, *new_tegra_state;
1848 const struct tegra_dc_state *old_dc_state, *new_dc_state;
1849 u32 i, new_avg_bw, old_avg_bw, new_peak_bw, old_peak_bw;
1850 const struct drm_plane_state *old_plane_state;
1851 const struct drm_crtc_state *old_crtc_state;
1852 struct tegra_dc_window window, old_window;
1853 struct tegra_dc *dc = to_tegra_dc(crtc);
1854 struct tegra_plane *tegra;
1855 struct drm_plane *plane;
1857 if (dc->soc->has_nvdisplay)
1860 old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1861 old_dc_state = to_const_dc_state(old_crtc_state);
1862 new_dc_state = to_const_dc_state(crtc->state);
1864 if (!crtc->state->active) {
1865 if (!old_crtc_state->active)
1869 * When CRTC is disabled on DPMS, the state of attached planes
1870 * is kept unchanged. Hence we need to enforce removal of the
1871 * bandwidths from the ICC paths.
1873 drm_atomic_crtc_for_each_plane(plane, crtc) {
1874 tegra = to_tegra_plane(plane);
1876 icc_set_bw(tegra->icc_mem, 0, 0);
1877 icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
1883 for_each_old_plane_in_state(old_crtc_state->state, plane,
1884 old_plane_state, i) {
1885 old_tegra_state = to_const_tegra_plane_state(old_plane_state);
1886 new_tegra_state = to_const_tegra_plane_state(plane->state);
1887 tegra = to_tegra_plane(plane);
1890 * We're iterating over the global atomic state and it contains
1891 * planes from another CRTC, hence we need to filter out the
1892 * planes unrelated to this CRTC.
1894 if (tegra->dc != dc)
1897 new_avg_bw = new_tegra_state->avg_memory_bandwidth;
1898 old_avg_bw = old_tegra_state->avg_memory_bandwidth;
1900 new_peak_bw = new_tegra_state->total_peak_memory_bandwidth;
1901 old_peak_bw = old_tegra_state->total_peak_memory_bandwidth;
1904 * See the comment related to !crtc->state->active above,
1905 * which explains why bandwidths need to be updated when
1906 * CRTC is turning ON.
1908 if (new_avg_bw == old_avg_bw && new_peak_bw == old_peak_bw &&
1909 old_crtc_state->active)
1912 window.src.h = drm_rect_height(&plane->state->src) >> 16;
1913 window.dst.h = drm_rect_height(&plane->state->dst);
1915 old_window.src.h = drm_rect_height(&old_plane_state->src) >> 16;
1916 old_window.dst.h = drm_rect_height(&old_plane_state->dst);
1919 * During the preparation phase (atomic_begin), the memory
1920 * freq should go high before the DC changes are committed
1921 * if bandwidth requirement goes up, otherwise memory freq
1922 * should to stay high if BW requirement goes down. The
1923 * opposite applies to the completion phase (post_commit).
1925 if (prepare_bandwidth_transition) {
1926 new_avg_bw = max(old_avg_bw, new_avg_bw);
1927 new_peak_bw = max(old_peak_bw, new_peak_bw);
1929 if (tegra_plane_use_vertical_filtering(tegra, &old_window))
1930 window = old_window;
1933 icc_set_bw(tegra->icc_mem, new_avg_bw, new_peak_bw);
1935 if (tegra_plane_use_vertical_filtering(tegra, &window))
1936 icc_set_bw(tegra->icc_mem_vfilter, new_avg_bw, new_peak_bw);
1938 icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
1942 static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
1943 struct drm_atomic_state *state)
1945 struct tegra_dc *dc = to_tegra_dc(crtc);
1949 if (!tegra_dc_idle(dc)) {
1953 * Ignore the return value, there isn't anything useful to do
1954 * in case this fails.
1956 tegra_dc_wait_idle(dc, 100);
1960 * This should really be part of the RGB encoder driver, but clearing
1961 * these bits has the side-effect of stopping the display controller.
1962 * When that happens no VBLANK interrupts will be raised. At the same
1963 * time the encoder is disabled before the display controller, so the
1964 * above code is always going to timeout waiting for the controller
1967 * Given the close coupling between the RGB encoder and the display
1968 * controller doing it here is still kind of okay. None of the other
1969 * encoder drivers require these bits to be cleared.
1971 * XXX: Perhaps given that the display controller is switched off at
1972 * this point anyway maybe clearing these bits isn't even useful for
1976 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1977 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1978 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1979 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1982 tegra_dc_stats_reset(&dc->stats);
1983 drm_crtc_vblank_off(crtc);
1985 spin_lock_irq(&crtc->dev->event_lock);
1987 if (crtc->state->event) {
1988 drm_crtc_send_vblank_event(crtc, crtc->state->event);
1989 crtc->state->event = NULL;
1992 spin_unlock_irq(&crtc->dev->event_lock);
1994 err = host1x_client_suspend(&dc->client);
1996 dev_err(dc->dev, "failed to suspend: %d\n", err);
1999 static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
2000 struct drm_atomic_state *state)
2002 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
2003 struct tegra_dc_state *crtc_state = to_dc_state(crtc->state);
2004 struct tegra_dc *dc = to_tegra_dc(crtc);
2008 err = host1x_client_resume(&dc->client);
2010 dev_err(dc->dev, "failed to resume: %d\n", err);
2014 /* initialize display controller */
2016 u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
2018 if (dc->soc->has_nvdisplay)
2023 value = SYNCPT_CNTRL_NO_STALL;
2024 tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
2026 value = enable | syncpt;
2027 tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
2030 if (dc->soc->has_nvdisplay) {
2031 value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
2033 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
2035 value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
2036 DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
2037 HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
2038 REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
2039 VBLANK_INT | FRAME_END_INT;
2040 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
2042 value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
2044 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
2046 value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
2047 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
2049 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
2051 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
2052 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
2053 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
2055 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
2056 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
2057 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
2059 /* initialize timer */
2060 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
2061 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
2062 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
2064 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
2065 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
2066 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
2068 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
2069 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
2070 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
2072 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
2073 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
2074 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
2077 if (dc->soc->supports_background_color)
2078 tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
2080 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
2082 /* apply PLL and pixel clock changes */
2083 tegra_dc_commit_state(dc, crtc_state);
2085 /* program display mode */
2086 tegra_dc_set_timings(dc, mode);
2088 /* interlacing isn't supported yet, so disable it */
2089 if (dc->soc->supports_interlacing) {
2090 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
2091 value &= ~INTERLACE_ENABLE;
2092 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
2095 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
2096 value &= ~DISP_CTRL_MODE_MASK;
2097 value |= DISP_CTRL_MODE_C_DISPLAY;
2098 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
2100 if (!dc->soc->has_nvdisplay) {
2101 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
2102 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
2103 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
2104 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
2107 /* enable underflow reporting and display red for missing pixels */
2108 if (dc->soc->has_nvdisplay) {
2109 value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
2110 tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
2113 tegra_dc_commit(dc);
2115 drm_crtc_vblank_on(crtc);
2118 static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
2119 struct drm_atomic_state *state)
2121 unsigned long flags;
2123 tegra_crtc_update_memory_bandwidth(crtc, state, true);
2125 if (crtc->state->event) {
2126 spin_lock_irqsave(&crtc->dev->event_lock, flags);
2128 if (drm_crtc_vblank_get(crtc) != 0)
2129 drm_crtc_send_vblank_event(crtc, crtc->state->event);
2131 drm_crtc_arm_vblank_event(crtc, crtc->state->event);
2133 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2135 crtc->state->event = NULL;
2139 static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
2140 struct drm_atomic_state *state)
2142 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
2144 struct tegra_dc_state *dc_state = to_dc_state(crtc_state);
2145 struct tegra_dc *dc = to_tegra_dc(crtc);
2148 value = dc_state->planes << 8 | GENERAL_UPDATE;
2149 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
2150 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
2152 value = dc_state->planes | GENERAL_ACT_REQ;
2153 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
2154 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
2157 static bool tegra_plane_is_cursor(const struct drm_plane_state *state)
2159 const struct tegra_dc_soc_info *soc = to_tegra_dc(state->crtc)->soc;
2160 const struct drm_format_info *fmt = state->fb->format;
2161 unsigned int src_w = drm_rect_width(&state->src) >> 16;
2162 unsigned int dst_w = drm_rect_width(&state->dst);
2164 if (state->plane->type != DRM_PLANE_TYPE_CURSOR)
2167 if (soc->supports_cursor)
2170 if (src_w != dst_w || fmt->num_planes != 1 || src_w * fmt->cpp[0] > 256)
2176 static unsigned long
2177 tegra_plane_overlap_mask(struct drm_crtc_state *state,
2178 const struct drm_plane_state *plane_state)
2180 const struct drm_plane_state *other_state;
2181 const struct tegra_plane *tegra;
2182 unsigned long overlap_mask = 0;
2183 struct drm_plane *plane;
2184 struct drm_rect rect;
2186 if (!plane_state->visible || !plane_state->fb)
2190 * Data-prefetch FIFO will easily help to overcome temporal memory
2191 * pressure if other plane overlaps with the cursor plane.
2193 if (tegra_plane_is_cursor(plane_state))
2196 drm_atomic_crtc_state_for_each_plane_state(plane, other_state, state) {
2197 rect = plane_state->dst;
2199 tegra = to_tegra_plane(other_state->plane);
2201 if (!other_state->visible || !other_state->fb)
2205 * Ignore cursor plane overlaps because it's not practical to
2206 * assume that it contributes to the bandwidth in overlapping
2207 * area if window width is small.
2209 if (tegra_plane_is_cursor(other_state))
2212 if (drm_rect_intersect(&rect, &other_state->dst))
2213 overlap_mask |= BIT(tegra->index);
2216 return overlap_mask;
2219 static int tegra_crtc_calculate_memory_bandwidth(struct drm_crtc *crtc,
2220 struct drm_atomic_state *state)
2222 ulong overlap_mask[TEGRA_DC_LEGACY_PLANES_NUM] = {}, mask;
2223 u32 plane_peak_bw[TEGRA_DC_LEGACY_PLANES_NUM] = {};
2224 bool all_planes_overlap_simultaneously = true;
2225 const struct tegra_plane_state *tegra_state;
2226 const struct drm_plane_state *plane_state;
2227 struct tegra_dc *dc = to_tegra_dc(crtc);
2228 const struct drm_crtc_state *old_state;
2229 struct drm_crtc_state *new_state;
2230 struct tegra_plane *tegra;
2231 struct drm_plane *plane;
2234 * The nv-display uses shared planes. The algorithm below assumes
2235 * maximum 3 planes per-CRTC, this assumption isn't applicable to
2236 * the nv-display. Note that T124 support has additional windows,
2237 * but currently they aren't supported by the driver.
2239 if (dc->soc->has_nvdisplay)
2242 new_state = drm_atomic_get_new_crtc_state(state, crtc);
2243 old_state = drm_atomic_get_old_crtc_state(state, crtc);
2246 * For overlapping planes pixel's data is fetched for each plane at
2247 * the same time, hence bandwidths are accumulated in this case.
2248 * This needs to be taken into account for calculating total bandwidth
2249 * consumed by all planes.
2251 * Here we get the overlapping state of each plane, which is a
2252 * bitmask of plane indices telling with what planes there is an
2253 * overlap. Note that bitmask[plane] includes BIT(plane) in order
2254 * to make further code nicer and simpler.
2256 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
2257 tegra_state = to_const_tegra_plane_state(plane_state);
2258 tegra = to_tegra_plane(plane);
2260 if (WARN_ON_ONCE(tegra->index >= TEGRA_DC_LEGACY_PLANES_NUM))
2263 plane_peak_bw[tegra->index] = tegra_state->peak_memory_bandwidth;
2264 mask = tegra_plane_overlap_mask(new_state, plane_state);
2265 overlap_mask[tegra->index] = mask;
2267 if (hweight_long(mask) != 3)
2268 all_planes_overlap_simultaneously = false;
2272 * Then we calculate maximum bandwidth of each plane state.
2273 * The bandwidth includes the plane BW + BW of the "simultaneously"
2274 * overlapping planes, where "simultaneously" means areas where DC
2275 * fetches from the planes simultaneously during of scan-out process.
2277 * For example, if plane A overlaps with planes B and C, but B and C
2278 * don't overlap, then the peak bandwidth will be either in area where
2279 * A-and-B or A-and-C planes overlap.
2281 * The plane_peak_bw[] contains peak memory bandwidth values of
2282 * each plane, this information is needed by interconnect provider
2283 * in order to set up latency allowance based on the peak BW, see
2284 * tegra_crtc_update_memory_bandwidth().
2286 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
2287 u32 i, old_peak_bw, new_peak_bw, overlap_bw = 0;
2290 * Note that plane's atomic check doesn't touch the
2291 * total_peak_memory_bandwidth of enabled plane, hence the
2292 * current state contains the old bandwidth state from the
2293 * previous CRTC commit.
2295 tegra_state = to_const_tegra_plane_state(plane_state);
2296 tegra = to_tegra_plane(plane);
2298 for_each_set_bit(i, &overlap_mask[tegra->index], 3) {
2299 if (i == tegra->index)
2302 if (all_planes_overlap_simultaneously)
2303 overlap_bw += plane_peak_bw[i];
2305 overlap_bw = max(overlap_bw, plane_peak_bw[i]);
2308 new_peak_bw = plane_peak_bw[tegra->index] + overlap_bw;
2309 old_peak_bw = tegra_state->total_peak_memory_bandwidth;
2312 * If plane's peak bandwidth changed (for example plane isn't
2313 * overlapped anymore) and plane isn't in the atomic state,
2314 * then add plane to the state in order to have the bandwidth
2317 if (old_peak_bw != new_peak_bw) {
2318 struct tegra_plane_state *new_tegra_state;
2319 struct drm_plane_state *new_plane_state;
2321 new_plane_state = drm_atomic_get_plane_state(state, plane);
2322 if (IS_ERR(new_plane_state))
2323 return PTR_ERR(new_plane_state);
2325 new_tegra_state = to_tegra_plane_state(new_plane_state);
2326 new_tegra_state->total_peak_memory_bandwidth = new_peak_bw;
2333 static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
2334 struct drm_atomic_state *state)
2338 err = tegra_crtc_calculate_memory_bandwidth(crtc, state);
2345 void tegra_crtc_atomic_post_commit(struct drm_crtc *crtc,
2346 struct drm_atomic_state *state)
2349 * Display bandwidth is allowed to go down only once hardware state
2350 * is known to be armed, i.e. state was committed and VBLANK event
2353 tegra_crtc_update_memory_bandwidth(crtc, state, false);
2356 static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
2357 .atomic_check = tegra_crtc_atomic_check,
2358 .atomic_begin = tegra_crtc_atomic_begin,
2359 .atomic_flush = tegra_crtc_atomic_flush,
2360 .atomic_enable = tegra_crtc_atomic_enable,
2361 .atomic_disable = tegra_crtc_atomic_disable,
2364 static irqreturn_t tegra_dc_irq(int irq, void *data)
2366 struct tegra_dc *dc = data;
2367 unsigned long status;
2369 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
2370 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
2372 if (status & FRAME_END_INT) {
2374 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
2376 dc->stats.frames_total++;
2380 if (status & VBLANK_INT) {
2382 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
2384 drm_crtc_handle_vblank(&dc->base);
2385 dc->stats.vblank_total++;
2389 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
2391 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
2393 dc->stats.underflow_total++;
2394 dc->stats.underflow++;
2397 if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
2399 dev_dbg(dc->dev, "%s(): overflow\n", __func__);
2401 dc->stats.overflow_total++;
2402 dc->stats.overflow++;
2405 if (status & HEAD_UF_INT) {
2406 dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
2407 dc->stats.underflow_total++;
2408 dc->stats.underflow++;
2414 static bool tegra_dc_has_window_groups(struct tegra_dc *dc)
2418 if (!dc->soc->wgrps)
2421 for (i = 0; i < dc->soc->num_wgrps; i++) {
2422 const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
2424 if (wgrp->dc == dc->pipe && wgrp->num_windows > 0)
2431 static int tegra_dc_early_init(struct host1x_client *client)
2433 struct drm_device *drm = dev_get_drvdata(client->host);
2434 struct tegra_drm *tegra = drm->dev_private;
2441 static int tegra_dc_init(struct host1x_client *client)
2443 struct drm_device *drm = dev_get_drvdata(client->host);
2444 unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
2445 struct tegra_dc *dc = host1x_client_to_dc(client);
2446 struct tegra_drm *tegra = drm->dev_private;
2447 struct drm_plane *primary = NULL;
2448 struct drm_plane *cursor = NULL;
2452 * DC has been reset by now, so VBLANK syncpoint can be released
2455 host1x_syncpt_release_vblank_reservation(client, 26 + dc->pipe);
2458 * XXX do not register DCs with no window groups because we cannot
2459 * assign a primary plane to them, which in turn will cause KMS to
2462 if (!tegra_dc_has_window_groups(dc))
2466 * Set the display hub as the host1x client parent for the display
2467 * controller. This is needed for the runtime reference counting that
2468 * ensures the display hub is always powered when any of the display
2471 if (dc->soc->has_nvdisplay)
2472 client->parent = &tegra->hub->client;
2474 dc->syncpt = host1x_syncpt_request(client, flags);
2476 dev_warn(dc->dev, "failed to allocate syncpoint\n");
2478 err = host1x_client_iommu_attach(client);
2479 if (err < 0 && err != -ENODEV) {
2480 dev_err(client->dev, "failed to attach to domain: %d\n", err);
2485 primary = tegra_dc_add_shared_planes(drm, dc);
2487 primary = tegra_dc_add_planes(drm, dc);
2489 if (IS_ERR(primary)) {
2490 err = PTR_ERR(primary);
2494 if (dc->soc->supports_cursor) {
2495 cursor = tegra_dc_cursor_plane_create(drm, dc);
2496 if (IS_ERR(cursor)) {
2497 err = PTR_ERR(cursor);
2501 /* dedicate one overlay to mouse cursor */
2502 cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
2503 if (IS_ERR(cursor)) {
2504 err = PTR_ERR(cursor);
2509 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
2510 &tegra_crtc_funcs, NULL);
2514 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
2517 * Keep track of the minimum pitch alignment across all display
2520 if (dc->soc->pitch_align > tegra->pitch_align)
2521 tegra->pitch_align = dc->soc->pitch_align;
2523 /* track maximum resolution */
2524 if (dc->soc->has_nvdisplay)
2525 drm->mode_config.max_width = drm->mode_config.max_height = 16384;
2527 drm->mode_config.max_width = drm->mode_config.max_height = 4096;
2529 err = tegra_dc_rgb_init(drm, dc);
2530 if (err < 0 && err != -ENODEV) {
2531 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
2535 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
2536 dev_name(dc->dev), dc);
2538 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
2544 * Inherit the DMA parameters (such as maximum segment size) from the
2545 * parent host1x device.
2547 client->dev->dma_parms = client->host->dma_parms;
2552 if (!IS_ERR_OR_NULL(cursor))
2553 drm_plane_cleanup(cursor);
2555 if (!IS_ERR(primary))
2556 drm_plane_cleanup(primary);
2558 host1x_client_iommu_detach(client);
2559 host1x_syncpt_put(dc->syncpt);
2564 static int tegra_dc_exit(struct host1x_client *client)
2566 struct tegra_dc *dc = host1x_client_to_dc(client);
2569 if (!tegra_dc_has_window_groups(dc))
2572 /* avoid a dangling pointer just in case this disappears */
2573 client->dev->dma_parms = NULL;
2575 devm_free_irq(dc->dev, dc->irq, dc);
2577 err = tegra_dc_rgb_exit(dc);
2579 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
2583 host1x_client_iommu_detach(client);
2584 host1x_syncpt_put(dc->syncpt);
2589 static int tegra_dc_late_exit(struct host1x_client *client)
2591 struct drm_device *drm = dev_get_drvdata(client->host);
2592 struct tegra_drm *tegra = drm->dev_private;
2599 static int tegra_dc_runtime_suspend(struct host1x_client *client)
2601 struct tegra_dc *dc = host1x_client_to_dc(client);
2602 struct device *dev = client->dev;
2605 err = reset_control_assert(dc->rst);
2607 dev_err(dev, "failed to assert reset: %d\n", err);
2611 if (dc->soc->has_powergate)
2612 tegra_powergate_power_off(dc->powergate);
2614 clk_disable_unprepare(dc->clk);
2615 pm_runtime_put_sync(dev);
2620 static int tegra_dc_runtime_resume(struct host1x_client *client)
2622 struct tegra_dc *dc = host1x_client_to_dc(client);
2623 struct device *dev = client->dev;
2626 err = pm_runtime_resume_and_get(dev);
2628 dev_err(dev, "failed to get runtime PM: %d\n", err);
2632 if (dc->soc->has_powergate) {
2633 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
2636 dev_err(dev, "failed to power partition: %d\n", err);
2640 err = clk_prepare_enable(dc->clk);
2642 dev_err(dev, "failed to enable clock: %d\n", err);
2646 err = reset_control_deassert(dc->rst);
2648 dev_err(dev, "failed to deassert reset: %d\n", err);
2656 clk_disable_unprepare(dc->clk);
2658 pm_runtime_put_sync(dev);
2662 static const struct host1x_client_ops dc_client_ops = {
2663 .early_init = tegra_dc_early_init,
2664 .init = tegra_dc_init,
2665 .exit = tegra_dc_exit,
2666 .late_exit = tegra_dc_late_exit,
2667 .suspend = tegra_dc_runtime_suspend,
2668 .resume = tegra_dc_runtime_resume,
2671 static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
2672 .supports_background_color = false,
2673 .supports_interlacing = false,
2674 .supports_cursor = false,
2675 .supports_block_linear = false,
2676 .supports_sector_layout = false,
2677 .has_legacy_blending = true,
2679 .has_powergate = false,
2681 .has_nvdisplay = false,
2682 .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2683 .primary_formats = tegra20_primary_formats,
2684 .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2685 .overlay_formats = tegra20_overlay_formats,
2686 .modifiers = tegra20_modifiers,
2687 .has_win_a_without_filters = true,
2688 .has_win_b_vfilter_mem_client = true,
2689 .has_win_c_without_vert_filter = true,
2690 .plane_tiled_memory_bandwidth_x2 = false,
2693 static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
2694 .supports_background_color = false,
2695 .supports_interlacing = false,
2696 .supports_cursor = false,
2697 .supports_block_linear = false,
2698 .supports_sector_layout = false,
2699 .has_legacy_blending = true,
2701 .has_powergate = false,
2702 .coupled_pm = false,
2703 .has_nvdisplay = false,
2704 .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
2705 .primary_formats = tegra20_primary_formats,
2706 .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
2707 .overlay_formats = tegra20_overlay_formats,
2708 .modifiers = tegra20_modifiers,
2709 .has_win_a_without_filters = false,
2710 .has_win_b_vfilter_mem_client = true,
2711 .has_win_c_without_vert_filter = false,
2712 .plane_tiled_memory_bandwidth_x2 = true,
2715 static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
2716 .supports_background_color = false,
2717 .supports_interlacing = false,
2718 .supports_cursor = false,
2719 .supports_block_linear = false,
2720 .supports_sector_layout = false,
2721 .has_legacy_blending = true,
2723 .has_powergate = true,
2724 .coupled_pm = false,
2725 .has_nvdisplay = false,
2726 .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2727 .primary_formats = tegra114_primary_formats,
2728 .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2729 .overlay_formats = tegra114_overlay_formats,
2730 .modifiers = tegra20_modifiers,
2731 .has_win_a_without_filters = false,
2732 .has_win_b_vfilter_mem_client = false,
2733 .has_win_c_without_vert_filter = false,
2734 .plane_tiled_memory_bandwidth_x2 = true,
2737 static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
2738 .supports_background_color = true,
2739 .supports_interlacing = true,
2740 .supports_cursor = true,
2741 .supports_block_linear = true,
2742 .supports_sector_layout = false,
2743 .has_legacy_blending = false,
2745 .has_powergate = true,
2746 .coupled_pm = false,
2747 .has_nvdisplay = false,
2748 .num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
2749 .primary_formats = tegra124_primary_formats,
2750 .num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
2751 .overlay_formats = tegra124_overlay_formats,
2752 .modifiers = tegra124_modifiers,
2753 .has_win_a_without_filters = false,
2754 .has_win_b_vfilter_mem_client = false,
2755 .has_win_c_without_vert_filter = false,
2756 .plane_tiled_memory_bandwidth_x2 = false,
2759 static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
2760 .supports_background_color = true,
2761 .supports_interlacing = true,
2762 .supports_cursor = true,
2763 .supports_block_linear = true,
2764 .supports_sector_layout = false,
2765 .has_legacy_blending = false,
2767 .has_powergate = true,
2768 .coupled_pm = false,
2769 .has_nvdisplay = false,
2770 .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
2771 .primary_formats = tegra114_primary_formats,
2772 .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
2773 .overlay_formats = tegra114_overlay_formats,
2774 .modifiers = tegra124_modifiers,
2775 .has_win_a_without_filters = false,
2776 .has_win_b_vfilter_mem_client = false,
2777 .has_win_c_without_vert_filter = false,
2778 .plane_tiled_memory_bandwidth_x2 = false,
2781 static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
2785 .windows = (const unsigned int[]) { 0 },
2790 .windows = (const unsigned int[]) { 1 },
2795 .windows = (const unsigned int[]) { 2 },
2800 .windows = (const unsigned int[]) { 3 },
2805 .windows = (const unsigned int[]) { 4 },
2810 .windows = (const unsigned int[]) { 5 },
2815 static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
2816 .supports_background_color = true,
2817 .supports_interlacing = true,
2818 .supports_cursor = true,
2819 .supports_block_linear = true,
2820 .supports_sector_layout = false,
2821 .has_legacy_blending = false,
2823 .has_powergate = false,
2824 .coupled_pm = false,
2825 .has_nvdisplay = true,
2826 .wgrps = tegra186_dc_wgrps,
2827 .num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
2828 .plane_tiled_memory_bandwidth_x2 = false,
2831 static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
2835 .windows = (const unsigned int[]) { 0 },
2840 .windows = (const unsigned int[]) { 1 },
2845 .windows = (const unsigned int[]) { 2 },
2850 .windows = (const unsigned int[]) { 3 },
2855 .windows = (const unsigned int[]) { 4 },
2860 .windows = (const unsigned int[]) { 5 },
2865 static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
2866 .supports_background_color = true,
2867 .supports_interlacing = true,
2868 .supports_cursor = true,
2869 .supports_block_linear = true,
2870 .supports_sector_layout = true,
2871 .has_legacy_blending = false,
2873 .has_powergate = false,
2874 .coupled_pm = false,
2875 .has_nvdisplay = true,
2876 .wgrps = tegra194_dc_wgrps,
2877 .num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
2878 .plane_tiled_memory_bandwidth_x2 = false,
2881 static const struct of_device_id tegra_dc_of_match[] = {
2883 .compatible = "nvidia,tegra194-dc",
2884 .data = &tegra194_dc_soc_info,
2886 .compatible = "nvidia,tegra186-dc",
2887 .data = &tegra186_dc_soc_info,
2889 .compatible = "nvidia,tegra210-dc",
2890 .data = &tegra210_dc_soc_info,
2892 .compatible = "nvidia,tegra124-dc",
2893 .data = &tegra124_dc_soc_info,
2895 .compatible = "nvidia,tegra114-dc",
2896 .data = &tegra114_dc_soc_info,
2898 .compatible = "nvidia,tegra30-dc",
2899 .data = &tegra30_dc_soc_info,
2901 .compatible = "nvidia,tegra20-dc",
2902 .data = &tegra20_dc_soc_info,
2907 MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
2909 static int tegra_dc_parse_dt(struct tegra_dc *dc)
2911 struct device_node *np;
2915 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
2917 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
2920 * If the nvidia,head property isn't present, try to find the
2921 * correct head number by looking up the position of this
2922 * display controller's node within the device tree. Assuming
2923 * that the nodes are ordered properly in the DTS file and
2924 * that the translation into a flattened device tree blob
2925 * preserves that ordering this will actually yield the right
2928 * If those assumptions don't hold, this will still work for
2929 * cases where only a single display controller is used.
2931 for_each_matching_node(np, tegra_dc_of_match) {
2932 if (np == dc->dev->of_node) {
2946 static int tegra_dc_match_by_pipe(struct device *dev, const void *data)
2948 struct tegra_dc *dc = dev_get_drvdata(dev);
2949 unsigned int pipe = (unsigned long)(void *)data;
2951 return dc->pipe == pipe;
2954 static int tegra_dc_couple(struct tegra_dc *dc)
2957 * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
2958 * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
2959 * POWER_CONTROL registers during CRTC enabling.
2961 if (dc->soc->coupled_pm && dc->pipe == 1) {
2962 struct device *companion;
2963 struct tegra_dc *parent;
2965 companion = driver_find_device(dc->dev->driver, NULL, (const void *)0,
2966 tegra_dc_match_by_pipe);
2968 return -EPROBE_DEFER;
2970 parent = dev_get_drvdata(companion);
2971 dc->client.parent = &parent->client;
2973 dev_dbg(dc->dev, "coupled to %s\n", dev_name(companion));
2979 static int tegra_dc_probe(struct platform_device *pdev)
2981 u64 dma_mask = dma_get_mask(pdev->dev.parent);
2982 struct tegra_dc *dc;
2985 err = dma_coerce_mask_and_coherent(&pdev->dev, dma_mask);
2987 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
2991 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
2995 dc->soc = of_device_get_match_data(&pdev->dev);
2997 INIT_LIST_HEAD(&dc->list);
2998 dc->dev = &pdev->dev;
3000 err = tegra_dc_parse_dt(dc);
3004 err = tegra_dc_couple(dc);
3008 dc->clk = devm_clk_get(&pdev->dev, NULL);
3009 if (IS_ERR(dc->clk)) {
3010 dev_err(&pdev->dev, "failed to get clock\n");
3011 return PTR_ERR(dc->clk);
3014 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
3015 if (IS_ERR(dc->rst)) {
3016 dev_err(&pdev->dev, "failed to get reset\n");
3017 return PTR_ERR(dc->rst);
3020 /* assert reset and disable clock */
3021 err = clk_prepare_enable(dc->clk);
3025 usleep_range(2000, 4000);
3027 err = reset_control_assert(dc->rst);
3031 usleep_range(2000, 4000);
3033 clk_disable_unprepare(dc->clk);
3035 if (dc->soc->has_powergate) {
3037 dc->powergate = TEGRA_POWERGATE_DIS;
3039 dc->powergate = TEGRA_POWERGATE_DISB;
3041 tegra_powergate_power_off(dc->powergate);
3044 dc->regs = devm_platform_ioremap_resource(pdev, 0);
3045 if (IS_ERR(dc->regs))
3046 return PTR_ERR(dc->regs);
3048 dc->irq = platform_get_irq(pdev, 0);
3052 err = tegra_dc_rgb_probe(dc);
3053 if (err < 0 && err != -ENODEV) {
3054 const char *level = KERN_ERR;
3056 if (err == -EPROBE_DEFER)
3059 dev_printk(level, dc->dev, "failed to probe RGB output: %d\n",
3064 platform_set_drvdata(pdev, dc);
3065 pm_runtime_enable(&pdev->dev);
3067 INIT_LIST_HEAD(&dc->client.list);
3068 dc->client.ops = &dc_client_ops;
3069 dc->client.dev = &pdev->dev;
3071 err = host1x_client_register(&dc->client);
3073 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3081 pm_runtime_disable(&pdev->dev);
3082 tegra_dc_rgb_remove(dc);
3087 static int tegra_dc_remove(struct platform_device *pdev)
3089 struct tegra_dc *dc = platform_get_drvdata(pdev);
3092 err = host1x_client_unregister(&dc->client);
3094 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3099 err = tegra_dc_rgb_remove(dc);
3101 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
3105 pm_runtime_disable(&pdev->dev);
3110 struct platform_driver tegra_dc_driver = {
3113 .of_match_table = tegra_dc_of_match,
3115 .probe = tegra_dc_probe,
3116 .remove = tegra_dc_remove,