Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * rcar_du_plane.c -- R-Car Display Unit Planes |
| 3 | * |
Laurent Pinchart | 36d5046 | 2014-02-06 18:13:52 +0100 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 5 | * |
| 6 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <drm/drmP.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
| 17 | #include <drm/drm_fb_cma_helper.h> |
| 18 | #include <drm/drm_gem_cma_helper.h> |
| 19 | |
| 20 | #include "rcar_du_drv.h" |
| 21 | #include "rcar_du_kms.h" |
| 22 | #include "rcar_du_plane.h" |
| 23 | #include "rcar_du_regs.h" |
| 24 | |
| 25 | #define RCAR_DU_COLORKEY_NONE (0 << 24) |
| 26 | #define RCAR_DU_COLORKEY_SOURCE (1 << 24) |
| 27 | #define RCAR_DU_COLORKEY_MASK (1 << 24) |
| 28 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 29 | static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane) |
| 30 | { |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 31 | return container_of(plane, struct rcar_du_plane, plane); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 34 | static u32 rcar_du_plane_read(struct rcar_du_group *rgrp, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 35 | unsigned int index, u32 reg) |
| 36 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 37 | return rcar_du_read(rgrp->dev, |
| 38 | rgrp->mmio_offset + index * PLANE_OFF + reg); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 41 | static void rcar_du_plane_write(struct rcar_du_group *rgrp, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 42 | unsigned int index, u32 reg, u32 data) |
| 43 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 44 | rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg, |
| 45 | data); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int rcar_du_plane_reserve(struct rcar_du_plane *plane, |
| 49 | const struct rcar_du_format_info *format) |
| 50 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 51 | struct rcar_du_group *rgrp = plane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 52 | unsigned int i; |
| 53 | int ret = -EBUSY; |
| 54 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 55 | mutex_lock(&rgrp->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 56 | |
Laurent Pinchart | 3053460 | 2015-02-25 18:38:25 +0200 | [diff] [blame^] | 57 | for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 58 | if (!(rgrp->planes.free & (1 << i))) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 59 | continue; |
| 60 | |
| 61 | if (format->planes == 1 || |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 62 | rgrp->planes.free & (1 << ((i + 1) % 8))) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 63 | break; |
| 64 | } |
| 65 | |
Laurent Pinchart | 3053460 | 2015-02-25 18:38:25 +0200 | [diff] [blame^] | 66 | if (i == RCAR_DU_NUM_HW_PLANES) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 67 | goto done; |
| 68 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 69 | rgrp->planes.free &= ~(1 << i); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 70 | if (format->planes == 2) |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 71 | rgrp->planes.free &= ~(1 << ((i + 1) % 8)); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 72 | |
| 73 | plane->hwindex = i; |
| 74 | |
| 75 | ret = 0; |
| 76 | |
| 77 | done: |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 78 | mutex_unlock(&rgrp->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | void rcar_du_plane_release(struct rcar_du_plane *plane) |
| 83 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 84 | struct rcar_du_group *rgrp = plane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 85 | |
| 86 | if (plane->hwindex == -1) |
| 87 | return; |
| 88 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 89 | mutex_lock(&rgrp->planes.lock); |
| 90 | rgrp->planes.free |= 1 << plane->hwindex; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 91 | if (plane->format->planes == 2) |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 92 | rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8); |
| 93 | mutex_unlock(&rgrp->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 94 | |
| 95 | plane->hwindex = -1; |
| 96 | } |
| 97 | |
| 98 | void rcar_du_plane_update_base(struct rcar_du_plane *plane) |
| 99 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 100 | struct rcar_du_group *rgrp = plane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 101 | unsigned int index = plane->hwindex; |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 102 | bool interlaced; |
Laurent Pinchart | eb86301 | 2013-11-13 14:26:01 +0100 | [diff] [blame] | 103 | u32 mwr; |
| 104 | |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 105 | interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE; |
| 106 | |
| 107 | /* Memory pitch (expressed in pixels). Must be doubled for interlaced |
| 108 | * operation with 32bpp formats. |
| 109 | */ |
Laurent Pinchart | eb86301 | 2013-11-13 14:26:01 +0100 | [diff] [blame] | 110 | if (plane->format->planes == 2) |
| 111 | mwr = plane->pitch; |
| 112 | else |
| 113 | mwr = plane->pitch * 8 / plane->format->bpp; |
| 114 | |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 115 | if (interlaced && plane->format->bpp == 32) |
| 116 | mwr *= 2; |
| 117 | |
Laurent Pinchart | eb86301 | 2013-11-13 14:26:01 +0100 | [diff] [blame] | 118 | rcar_du_plane_write(rgrp, index, PnMWR, mwr); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 119 | |
Laurent Pinchart | 9e7db06 | 2013-06-14 20:54:16 +0200 | [diff] [blame] | 120 | /* The Y position is expressed in raster line units and must be doubled |
| 121 | * for 32bpp formats, according to the R8A7790 datasheet. No mention of |
| 122 | * doubling the Y position is found in the R8A7779 datasheet, but the |
| 123 | * rule seems to apply there as well. |
| 124 | * |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 125 | * Despite not being documented, doubling seem not to be needed when |
| 126 | * operating in interlaced mode. |
| 127 | * |
Laurent Pinchart | 9e7db06 | 2013-06-14 20:54:16 +0200 | [diff] [blame] | 128 | * Similarly, for the second plane, NV12 and NV21 formats seem to |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 129 | * require a halved Y position value, in both progressive and interlaced |
| 130 | * modes. |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 131 | */ |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 132 | rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x); |
| 133 | rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y * |
Laurent Pinchart | 906eff7 | 2014-12-09 19:11:18 +0200 | [diff] [blame] | 134 | (!interlaced && plane->format->bpp == 32 ? 2 : 1)); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 135 | rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 136 | |
| 137 | if (plane->format->planes == 2) { |
| 138 | index = (index + 1) % 8; |
| 139 | |
Laurent Pinchart | 49785e2 | 2014-12-09 22:45:11 +0200 | [diff] [blame] | 140 | rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch); |
| 141 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 142 | rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x); |
| 143 | rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y * |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 144 | (plane->format->bpp == 16 ? 2 : 1) / 2); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 145 | rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | void rcar_du_plane_compute_base(struct rcar_du_plane *plane, |
| 150 | struct drm_framebuffer *fb) |
| 151 | { |
| 152 | struct drm_gem_cma_object *gem; |
| 153 | |
Laurent Pinchart | eb86301 | 2013-11-13 14:26:01 +0100 | [diff] [blame] | 154 | plane->pitch = fb->pitches[0]; |
| 155 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 156 | gem = drm_fb_cma_get_gem_obj(fb, 0); |
| 157 | plane->dma[0] = gem->paddr + fb->offsets[0]; |
| 158 | |
| 159 | if (plane->format->planes == 2) { |
| 160 | gem = drm_fb_cma_get_gem_obj(fb, 1); |
| 161 | plane->dma[1] = gem->paddr + fb->offsets[1]; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane, |
| 166 | unsigned int index) |
| 167 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 168 | struct rcar_du_group *rgrp = plane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 169 | u32 colorkey; |
| 170 | u32 pnmr; |
| 171 | |
| 172 | /* The PnALPHAR register controls alpha-blending in 16bpp formats |
| 173 | * (ARGB1555 and XRGB1555). |
| 174 | * |
| 175 | * For ARGB, set the alpha value to 0, and enable alpha-blending when |
| 176 | * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255. |
| 177 | * |
| 178 | * For XRGB, set the alpha value to the plane-wide alpha value and |
| 179 | * enable alpha-blending regardless of the X bit value. |
| 180 | */ |
| 181 | if (plane->format->fourcc != DRM_FORMAT_XRGB1555) |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 182 | rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 183 | else |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 184 | rcar_du_plane_write(rgrp, index, PnALPHAR, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 185 | PnALPHAR_ABIT_X | plane->alpha); |
| 186 | |
| 187 | pnmr = PnMR_BM_MD | plane->format->pnmr; |
| 188 | |
| 189 | /* Disable color keying when requested. YUV formats have the |
| 190 | * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying |
| 191 | * automatically. |
| 192 | */ |
| 193 | if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE) |
| 194 | pnmr |= PnMR_SPIM_TP_OFF; |
| 195 | |
| 196 | /* For packed YUV formats we need to select the U/V order. */ |
| 197 | if (plane->format->fourcc == DRM_FORMAT_YUYV) |
| 198 | pnmr |= PnMR_YCDF_YUYV; |
| 199 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 200 | rcar_du_plane_write(rgrp, index, PnMR, pnmr); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 201 | |
| 202 | switch (plane->format->fourcc) { |
| 203 | case DRM_FORMAT_RGB565: |
| 204 | colorkey = ((plane->colorkey & 0xf80000) >> 8) |
| 205 | | ((plane->colorkey & 0x00fc00) >> 5) |
| 206 | | ((plane->colorkey & 0x0000f8) >> 3); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 207 | rcar_du_plane_write(rgrp, index, PnTC2R, colorkey); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | case DRM_FORMAT_ARGB1555: |
| 211 | case DRM_FORMAT_XRGB1555: |
| 212 | colorkey = ((plane->colorkey & 0xf80000) >> 9) |
| 213 | | ((plane->colorkey & 0x00f800) >> 6) |
| 214 | | ((plane->colorkey & 0x0000f8) >> 3); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 215 | rcar_du_plane_write(rgrp, index, PnTC2R, colorkey); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | case DRM_FORMAT_XRGB8888: |
| 219 | case DRM_FORMAT_ARGB8888: |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 220 | rcar_du_plane_write(rgrp, index, PnTC3R, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 221 | PnTC3R_CODE | (plane->colorkey & 0xffffff)); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void __rcar_du_plane_setup(struct rcar_du_plane *plane, |
| 227 | unsigned int index) |
| 228 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 229 | struct rcar_du_group *rgrp = plane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 230 | u32 ddcr2 = PnDDCR2_CODE; |
| 231 | u32 ddcr4; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 232 | |
| 233 | /* Data format |
| 234 | * |
| 235 | * The data format is selected by the DDDF field in PnMR and the EDF |
| 236 | * field in DDCR4. |
| 237 | */ |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 238 | ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 239 | ddcr4 &= ~PnDDCR4_EDF_MASK; |
| 240 | ddcr4 |= plane->format->edf | PnDDCR4_CODE; |
| 241 | |
| 242 | rcar_du_plane_setup_mode(plane, index); |
| 243 | |
| 244 | if (plane->format->planes == 2) { |
| 245 | if (plane->hwindex != index) { |
| 246 | if (plane->format->fourcc == DRM_FORMAT_NV12 || |
| 247 | plane->format->fourcc == DRM_FORMAT_NV21) |
| 248 | ddcr2 |= PnDDCR2_Y420; |
| 249 | |
| 250 | if (plane->format->fourcc == DRM_FORMAT_NV21) |
| 251 | ddcr2 |= PnDDCR2_NV21; |
| 252 | |
| 253 | ddcr2 |= PnDDCR2_DIVU; |
| 254 | } else { |
| 255 | ddcr2 |= PnDDCR2_DIVY; |
| 256 | } |
| 257 | } |
| 258 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 259 | rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2); |
| 260 | rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 261 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 262 | /* Destination position and size */ |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 263 | rcar_du_plane_write(rgrp, index, PnDSXR, plane->width); |
| 264 | rcar_du_plane_write(rgrp, index, PnDSYR, plane->height); |
| 265 | rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x); |
| 266 | rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 267 | |
| 268 | /* Wrap-around and blinking, disabled */ |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 269 | rcar_du_plane_write(rgrp, index, PnWASPR, 0); |
| 270 | rcar_du_plane_write(rgrp, index, PnWAMWR, 4095); |
| 271 | rcar_du_plane_write(rgrp, index, PnBTR, 0); |
| 272 | rcar_du_plane_write(rgrp, index, PnMLR, 0); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void rcar_du_plane_setup(struct rcar_du_plane *plane) |
| 276 | { |
| 277 | __rcar_du_plane_setup(plane, plane->hwindex); |
| 278 | if (plane->format->planes == 2) |
| 279 | __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8); |
| 280 | |
| 281 | rcar_du_plane_update_base(plane); |
| 282 | } |
| 283 | |
| 284 | static int |
| 285 | rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc, |
| 286 | struct drm_framebuffer *fb, int crtc_x, int crtc_y, |
| 287 | unsigned int crtc_w, unsigned int crtc_h, |
| 288 | uint32_t src_x, uint32_t src_y, |
| 289 | uint32_t src_w, uint32_t src_h) |
| 290 | { |
| 291 | struct rcar_du_plane *rplane = to_rcar_plane(plane); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 292 | struct rcar_du_device *rcdu = rplane->group->dev; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 293 | const struct rcar_du_format_info *format; |
| 294 | unsigned int nplanes; |
| 295 | int ret; |
| 296 | |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 297 | if (plane->type != DRM_PLANE_TYPE_OVERLAY) |
| 298 | return -EINVAL; |
| 299 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 300 | format = rcar_du_format_info(fb->pixel_format); |
| 301 | if (format == NULL) { |
| 302 | dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__, |
| 303 | fb->pixel_format); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) { |
| 308 | dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__); |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | nplanes = rplane->format ? rplane->format->planes : 0; |
| 313 | |
| 314 | /* Reallocate hardware planes if the number of required planes has |
| 315 | * changed. |
| 316 | */ |
| 317 | if (format->planes != nplanes) { |
| 318 | rcar_du_plane_release(rplane); |
| 319 | ret = rcar_du_plane_reserve(rplane, format); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | rplane->crtc = crtc; |
| 325 | rplane->format = format; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 326 | |
| 327 | rplane->src_x = src_x >> 16; |
| 328 | rplane->src_y = src_y >> 16; |
| 329 | rplane->dst_x = crtc_x; |
| 330 | rplane->dst_y = crtc_y; |
| 331 | rplane->width = crtc_w; |
| 332 | rplane->height = crtc_h; |
| 333 | |
| 334 | rcar_du_plane_compute_base(rplane, fb); |
| 335 | rcar_du_plane_setup(rplane); |
| 336 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 337 | mutex_lock(&rplane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 338 | rplane->enabled = true; |
| 339 | rcar_du_crtc_update_planes(rplane->crtc); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 340 | mutex_unlock(&rplane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int rcar_du_plane_disable(struct drm_plane *plane) |
| 346 | { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 347 | struct rcar_du_plane *rplane = to_rcar_plane(plane); |
| 348 | |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 349 | if (plane->type != DRM_PLANE_TYPE_OVERLAY) |
| 350 | return -EINVAL; |
| 351 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 352 | if (!rplane->enabled) |
| 353 | return 0; |
| 354 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 355 | mutex_lock(&rplane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 356 | rplane->enabled = false; |
| 357 | rcar_du_crtc_update_planes(rplane->crtc); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 358 | mutex_unlock(&rplane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 359 | |
| 360 | rcar_du_plane_release(rplane); |
| 361 | |
| 362 | rplane->crtc = NULL; |
| 363 | rplane->format = NULL; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* Both the .set_property and the .update_plane operations are called with the |
| 369 | * mode_config lock held. There is this no need to explicitly protect access to |
| 370 | * the alpha and colorkey fields and the mode register. |
| 371 | */ |
| 372 | static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha) |
| 373 | { |
| 374 | if (plane->alpha == alpha) |
| 375 | return; |
| 376 | |
| 377 | plane->alpha = alpha; |
| 378 | if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555) |
| 379 | return; |
| 380 | |
| 381 | rcar_du_plane_setup_mode(plane, plane->hwindex); |
| 382 | } |
| 383 | |
| 384 | static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane, |
| 385 | u32 colorkey) |
| 386 | { |
| 387 | if (plane->colorkey == colorkey) |
| 388 | return; |
| 389 | |
| 390 | plane->colorkey = colorkey; |
| 391 | if (!plane->enabled) |
| 392 | return; |
| 393 | |
| 394 | rcar_du_plane_setup_mode(plane, plane->hwindex); |
| 395 | } |
| 396 | |
| 397 | static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane, |
| 398 | unsigned int zpos) |
| 399 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 400 | mutex_lock(&plane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 401 | if (plane->zpos == zpos) |
| 402 | goto done; |
| 403 | |
| 404 | plane->zpos = zpos; |
| 405 | if (!plane->enabled) |
| 406 | goto done; |
| 407 | |
| 408 | rcar_du_crtc_update_planes(plane->crtc); |
| 409 | |
| 410 | done: |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 411 | mutex_unlock(&plane->group->planes.lock); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static int rcar_du_plane_set_property(struct drm_plane *plane, |
| 415 | struct drm_property *property, |
| 416 | uint64_t value) |
| 417 | { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 418 | struct rcar_du_plane *rplane = to_rcar_plane(plane); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 419 | struct rcar_du_group *rgrp = rplane->group; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 420 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 421 | if (property == rgrp->planes.alpha) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 422 | rcar_du_plane_set_alpha(rplane, value); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 423 | else if (property == rgrp->planes.colorkey) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 424 | rcar_du_plane_set_colorkey(rplane, value); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 425 | else if (property == rgrp->planes.zpos) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 426 | rcar_du_plane_set_zpos(rplane, value); |
| 427 | else |
| 428 | return -EINVAL; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static const struct drm_plane_funcs rcar_du_plane_funcs = { |
| 434 | .update_plane = rcar_du_plane_update, |
| 435 | .disable_plane = rcar_du_plane_disable, |
| 436 | .set_property = rcar_du_plane_set_property, |
| 437 | .destroy = drm_plane_cleanup, |
| 438 | }; |
| 439 | |
| 440 | static const uint32_t formats[] = { |
| 441 | DRM_FORMAT_RGB565, |
| 442 | DRM_FORMAT_ARGB1555, |
| 443 | DRM_FORMAT_XRGB1555, |
| 444 | DRM_FORMAT_XRGB8888, |
| 445 | DRM_FORMAT_ARGB8888, |
| 446 | DRM_FORMAT_UYVY, |
| 447 | DRM_FORMAT_YUYV, |
| 448 | DRM_FORMAT_NV12, |
| 449 | DRM_FORMAT_NV21, |
| 450 | DRM_FORMAT_NV16, |
| 451 | }; |
| 452 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 453 | int rcar_du_planes_init(struct rcar_du_group *rgrp) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 454 | { |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 455 | struct rcar_du_planes *planes = &rgrp->planes; |
| 456 | struct rcar_du_device *rcdu = rgrp->dev; |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 457 | unsigned int num_planes; |
| 458 | unsigned int num_crtcs; |
| 459 | unsigned int crtcs; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 460 | unsigned int i; |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 461 | int ret; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 462 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 463 | mutex_init(&planes->lock); |
| 464 | planes->free = 0xff; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 465 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 466 | planes->alpha = |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 467 | drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 468 | if (planes->alpha == NULL) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 469 | return -ENOMEM; |
| 470 | |
| 471 | /* The color key is expressed as an RGB888 triplet stored in a 32-bit |
| 472 | * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0) |
| 473 | * or enable source color keying (1). |
| 474 | */ |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 475 | planes->colorkey = |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 476 | drm_property_create_range(rcdu->ddev, 0, "colorkey", |
| 477 | 0, 0x01ffffff); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 478 | if (planes->colorkey == NULL) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 479 | return -ENOMEM; |
| 480 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 481 | planes->zpos = |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 482 | drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7); |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 483 | if (planes->zpos == NULL) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 484 | return -ENOMEM; |
| 485 | |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 486 | /* Create one primary plane per in this group CRTC and seven overlay |
| 487 | * planes. |
| 488 | */ |
| 489 | num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U); |
| 490 | num_planes = num_crtcs + 7; |
| 491 | |
| 492 | crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index)); |
| 493 | |
| 494 | for (i = 0; i < num_planes; ++i) { |
| 495 | enum drm_plane_type type = i < num_crtcs |
| 496 | ? DRM_PLANE_TYPE_PRIMARY |
| 497 | : DRM_PLANE_TYPE_OVERLAY; |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 498 | struct rcar_du_plane *plane = &planes->planes[i]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 499 | |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 500 | plane->group = rgrp; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 501 | plane->hwindex = -1; |
| 502 | plane->alpha = 255; |
| 503 | plane->colorkey = RCAR_DU_COLORKEY_NONE; |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 504 | plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 505 | |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 506 | ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs, |
| 507 | &rcar_du_plane_funcs, formats, |
| 508 | ARRAY_SIZE(formats), type); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 509 | if (ret < 0) |
| 510 | return ret; |
| 511 | |
Laurent Pinchart | 917de18 | 2015-02-17 18:34:17 +0200 | [diff] [blame] | 512 | if (type == DRM_PLANE_TYPE_PRIMARY) |
| 513 | continue; |
| 514 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 515 | drm_object_attach_property(&plane->plane.base, |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 516 | planes->alpha, 255); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 517 | drm_object_attach_property(&plane->plane.base, |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 518 | planes->colorkey, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 519 | RCAR_DU_COLORKEY_NONE); |
| 520 | drm_object_attach_property(&plane->plane.base, |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 521 | planes->zpos, 1); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |