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