Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * rcar_du_kms.c -- R-Car Display Unit Mode Setting |
| 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> |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 15 | #include <drm/drm_atomic.h> |
Laurent Pinchart | 336d04a | 2015-02-20 13:18:56 +0200 | [diff] [blame] | 16 | #include <drm/drm_atomic_helper.h> |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 17 | #include <drm/drm_crtc.h> |
| 18 | #include <drm/drm_crtc_helper.h> |
| 19 | #include <drm/drm_fb_cma_helper.h> |
| 20 | #include <drm/drm_gem_cma_helper.h> |
| 21 | |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 22 | #include <linux/of_graph.h> |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 23 | #include <linux/wait.h> |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 24 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 25 | #include "rcar_du_crtc.h" |
| 26 | #include "rcar_du_drv.h" |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 27 | #include "rcar_du_encoder.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 28 | #include "rcar_du_kms.h" |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 29 | #include "rcar_du_lvdsenc.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 30 | #include "rcar_du_regs.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 31 | |
| 32 | /* ----------------------------------------------------------------------------- |
| 33 | * Format helpers |
| 34 | */ |
| 35 | |
| 36 | static const struct rcar_du_format_info rcar_du_format_infos[] = { |
| 37 | { |
| 38 | .fourcc = DRM_FORMAT_RGB565, |
| 39 | .bpp = 16, |
| 40 | .planes = 1, |
| 41 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 42 | .edf = PnDDCR4_EDF_NONE, |
| 43 | }, { |
| 44 | .fourcc = DRM_FORMAT_ARGB1555, |
| 45 | .bpp = 16, |
| 46 | .planes = 1, |
| 47 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 48 | .edf = PnDDCR4_EDF_NONE, |
| 49 | }, { |
| 50 | .fourcc = DRM_FORMAT_XRGB1555, |
| 51 | .bpp = 16, |
| 52 | .planes = 1, |
| 53 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 54 | .edf = PnDDCR4_EDF_NONE, |
| 55 | }, { |
| 56 | .fourcc = DRM_FORMAT_XRGB8888, |
| 57 | .bpp = 32, |
| 58 | .planes = 1, |
| 59 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 60 | .edf = PnDDCR4_EDF_RGB888, |
| 61 | }, { |
| 62 | .fourcc = DRM_FORMAT_ARGB8888, |
| 63 | .bpp = 32, |
| 64 | .planes = 1, |
| 65 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP, |
| 66 | .edf = PnDDCR4_EDF_ARGB8888, |
| 67 | }, { |
| 68 | .fourcc = DRM_FORMAT_UYVY, |
| 69 | .bpp = 16, |
| 70 | .planes = 1, |
| 71 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 72 | .edf = PnDDCR4_EDF_NONE, |
| 73 | }, { |
| 74 | .fourcc = DRM_FORMAT_YUYV, |
| 75 | .bpp = 16, |
| 76 | .planes = 1, |
| 77 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 78 | .edf = PnDDCR4_EDF_NONE, |
| 79 | }, { |
| 80 | .fourcc = DRM_FORMAT_NV12, |
| 81 | .bpp = 12, |
| 82 | .planes = 2, |
| 83 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 84 | .edf = PnDDCR4_EDF_NONE, |
| 85 | }, { |
| 86 | .fourcc = DRM_FORMAT_NV21, |
| 87 | .bpp = 12, |
| 88 | .planes = 2, |
| 89 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 90 | .edf = PnDDCR4_EDF_NONE, |
| 91 | }, { |
| 92 | /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */ |
| 93 | .fourcc = DRM_FORMAT_NV16, |
| 94 | .bpp = 16, |
| 95 | .planes = 2, |
| 96 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 97 | .edf = PnDDCR4_EDF_NONE, |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc) |
| 102 | { |
| 103 | unsigned int i; |
| 104 | |
| 105 | for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) { |
| 106 | if (rcar_du_format_infos[i].fourcc == fourcc) |
| 107 | return &rcar_du_format_infos[i]; |
| 108 | } |
| 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 114 | * Frame buffer |
| 115 | */ |
| 116 | |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 117 | int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 118 | struct drm_mode_create_dumb *args) |
| 119 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 120 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 121 | unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 122 | unsigned int align; |
| 123 | |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 124 | /* The R8A7779 DU requires a 16 pixels pitch alignment as documented, |
| 125 | * but the R8A7790 DU seems to require a 128 bytes pitch alignment. |
| 126 | */ |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 127 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 128 | align = 128; |
| 129 | else |
| 130 | align = 16 * args->bpp / 8; |
| 131 | |
Thierry Reding | 7e295a3 | 2014-11-03 12:08:24 +0100 | [diff] [blame] | 132 | args->pitch = roundup(min_pitch, align); |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 133 | |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 134 | return drm_gem_cma_dumb_create_internal(file, dev, args); |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 137 | static struct drm_framebuffer * |
| 138 | rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
| 139 | struct drm_mode_fb_cmd2 *mode_cmd) |
| 140 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 141 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 142 | const struct rcar_du_format_info *format; |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 143 | unsigned int max_pitch; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 144 | unsigned int align; |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 145 | unsigned int bpp; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 146 | |
| 147 | format = rcar_du_format_info(mode_cmd->pixel_format); |
| 148 | if (format == NULL) { |
| 149 | dev_dbg(dev->dev, "unsupported pixel format %08x\n", |
| 150 | mode_cmd->pixel_format); |
| 151 | return ERR_PTR(-EINVAL); |
| 152 | } |
| 153 | |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 154 | /* |
| 155 | * The pitch and alignment constraints are expressed in pixels on the |
| 156 | * hardware side and in bytes in the DRM API. |
| 157 | */ |
| 158 | bpp = format->planes == 2 ? 1 : format->bpp / 8; |
| 159 | max_pitch = 4096 * bpp; |
| 160 | |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 161 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 162 | align = 128; |
| 163 | else |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 164 | align = 16 * bpp; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 165 | |
| 166 | if (mode_cmd->pitches[0] & (align - 1) || |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 167 | mode_cmd->pitches[0] >= max_pitch) { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 168 | dev_dbg(dev->dev, "invalid pitch value %u\n", |
| 169 | mode_cmd->pitches[0]); |
| 170 | return ERR_PTR(-EINVAL); |
| 171 | } |
| 172 | |
| 173 | if (format->planes == 2) { |
| 174 | if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) { |
| 175 | dev_dbg(dev->dev, |
| 176 | "luma and chroma pitches do not match\n"); |
| 177 | return ERR_PTR(-EINVAL); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return drm_fb_cma_create(dev, file_priv, mode_cmd); |
| 182 | } |
| 183 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 184 | static void rcar_du_output_poll_changed(struct drm_device *dev) |
| 185 | { |
| 186 | struct rcar_du_device *rcdu = dev->dev_private; |
| 187 | |
| 188 | drm_fbdev_cma_hotplug_event(rcdu->fbdev); |
| 189 | } |
| 190 | |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 191 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 192 | * Atomic Check and Update |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 193 | */ |
| 194 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 195 | /* |
| 196 | * Atomic hardware plane allocator |
| 197 | * |
| 198 | * The hardware plane allocator is solely based on the atomic plane states |
| 199 | * without keeping any external state to avoid races between .atomic_check() |
| 200 | * and .atomic_commit(). |
| 201 | * |
| 202 | * The core idea is to avoid using a free planes bitmask that would need to be |
| 203 | * shared between check and commit handlers with a collective knowledge based on |
| 204 | * the allocated hardware plane(s) for each KMS plane. The allocator then loops |
| 205 | * over all plane states to compute the free planes bitmask, allocates hardware |
| 206 | * planes based on that bitmask, and stores the result back in the plane states. |
| 207 | * |
| 208 | * For this to work we need to access the current state of planes not touched by |
| 209 | * the atomic update. To ensure that it won't be modified, we need to lock all |
| 210 | * planes using drm_atomic_get_plane_state(). This effectively serializes atomic |
| 211 | * updates from .atomic_check() up to completion (when swapping the states if |
| 212 | * the check step has succeeded) or rollback (when freeing the states if the |
| 213 | * check step has failed). |
| 214 | * |
| 215 | * Allocation is performed in the .atomic_check() handler and applied |
| 216 | * automatically when the core swaps the old and new states. |
| 217 | */ |
| 218 | |
| 219 | static bool rcar_du_plane_needs_realloc(struct rcar_du_plane *plane, |
| 220 | struct rcar_du_plane_state *state) |
| 221 | { |
| 222 | const struct rcar_du_format_info *cur_format; |
| 223 | |
Laurent Pinchart | ec69a40 | 2015-04-29 00:48:17 +0300 | [diff] [blame] | 224 | cur_format = to_rcar_plane_state(plane->plane.state)->format; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 225 | |
| 226 | /* Lowering the number of planes doesn't strictly require reallocation |
| 227 | * as the extra hardware plane will be freed when committing, but doing |
| 228 | * so could lead to more fragmentation. |
| 229 | */ |
| 230 | return !cur_format || cur_format->planes != state->format->planes; |
| 231 | } |
| 232 | |
| 233 | static unsigned int rcar_du_plane_hwmask(struct rcar_du_plane_state *state) |
| 234 | { |
| 235 | unsigned int mask; |
| 236 | |
| 237 | if (state->hwindex == -1) |
| 238 | return 0; |
| 239 | |
| 240 | mask = 1 << state->hwindex; |
| 241 | if (state->format->planes == 2) |
| 242 | mask |= 1 << ((state->hwindex + 1) % 8); |
| 243 | |
| 244 | return mask; |
| 245 | } |
| 246 | |
| 247 | static int rcar_du_plane_hwalloc(unsigned int num_planes, unsigned int free) |
| 248 | { |
| 249 | unsigned int i; |
| 250 | |
| 251 | for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) { |
| 252 | if (!(free & (1 << i))) |
| 253 | continue; |
| 254 | |
| 255 | if (num_planes == 1 || free & (1 << ((i + 1) % 8))) |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | return i == RCAR_DU_NUM_HW_PLANES ? -EBUSY : i; |
| 260 | } |
| 261 | |
| 262 | static int rcar_du_atomic_check(struct drm_device *dev, |
| 263 | struct drm_atomic_state *state) |
| 264 | { |
| 265 | struct rcar_du_device *rcdu = dev->dev_private; |
| 266 | unsigned int group_freed_planes[RCAR_DU_MAX_GROUPS] = { 0, }; |
| 267 | unsigned int group_free_planes[RCAR_DU_MAX_GROUPS] = { 0, }; |
| 268 | bool needs_realloc = false; |
| 269 | unsigned int groups = 0; |
| 270 | unsigned int i; |
| 271 | int ret; |
| 272 | |
| 273 | ret = drm_atomic_helper_check(dev, state); |
| 274 | if (ret < 0) |
| 275 | return ret; |
| 276 | |
| 277 | /* Check if hardware planes need to be reallocated. */ |
| 278 | for (i = 0; i < dev->mode_config.num_total_plane; ++i) { |
| 279 | struct rcar_du_plane_state *plane_state; |
| 280 | struct rcar_du_plane *plane; |
| 281 | unsigned int index; |
| 282 | |
| 283 | if (!state->planes[i]) |
| 284 | continue; |
| 285 | |
| 286 | plane = to_rcar_plane(state->planes[i]); |
Laurent Pinchart | ec69a40 | 2015-04-29 00:48:17 +0300 | [diff] [blame] | 287 | plane_state = to_rcar_plane_state(state->plane_states[i]); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 288 | |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 289 | dev_dbg(rcdu->dev, "%s: checking plane (%u,%u)\n", __func__, |
| 290 | plane->group->index, plane - plane->group->planes); |
| 291 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 292 | /* If the plane is being disabled we don't need to go through |
| 293 | * the full reallocation procedure. Just mark the hardware |
| 294 | * plane(s) as freed. |
| 295 | */ |
| 296 | if (!plane_state->format) { |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 297 | dev_dbg(rcdu->dev, "%s: plane is being disabled\n", |
| 298 | __func__); |
Laurent Pinchart | 99caede | 2015-04-29 00:05:56 +0300 | [diff] [blame] | 299 | index = plane - plane->group->planes; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 300 | group_freed_planes[plane->group->index] |= 1 << index; |
| 301 | plane_state->hwindex = -1; |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | /* If the plane needs to be reallocated mark it as such, and |
| 306 | * mark the hardware plane(s) as free. |
| 307 | */ |
| 308 | if (rcar_du_plane_needs_realloc(plane, plane_state)) { |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 309 | dev_dbg(rcdu->dev, "%s: plane needs reallocation\n", |
| 310 | __func__); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 311 | groups |= 1 << plane->group->index; |
| 312 | needs_realloc = true; |
| 313 | |
Laurent Pinchart | 99caede | 2015-04-29 00:05:56 +0300 | [diff] [blame] | 314 | index = plane - plane->group->planes; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 315 | group_freed_planes[plane->group->index] |= 1 << index; |
| 316 | plane_state->hwindex = -1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (!needs_realloc) |
| 321 | return 0; |
| 322 | |
| 323 | /* Grab all plane states for the groups that need reallocation to ensure |
| 324 | * locking and avoid racy updates. This serializes the update operation, |
| 325 | * but there's not much we can do about it as that's the hardware |
| 326 | * design. |
| 327 | * |
| 328 | * Compute the used planes mask for each group at the same time to avoid |
| 329 | * looping over the planes separately later. |
| 330 | */ |
| 331 | while (groups) { |
| 332 | unsigned int index = ffs(groups) - 1; |
| 333 | struct rcar_du_group *group = &rcdu->groups[index]; |
| 334 | unsigned int used_planes = 0; |
| 335 | |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 336 | dev_dbg(rcdu->dev, "%s: finding free planes for group %u\n", |
| 337 | __func__, index); |
| 338 | |
Laurent Pinchart | d6aed57 | 2015-05-25 16:32:45 +0300 | [diff] [blame] | 339 | for (i = 0; i < group->num_planes; ++i) { |
Laurent Pinchart | 99caede | 2015-04-29 00:05:56 +0300 | [diff] [blame] | 340 | struct rcar_du_plane *plane = &group->planes[i]; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 341 | struct rcar_du_plane_state *plane_state; |
| 342 | struct drm_plane_state *s; |
| 343 | |
| 344 | s = drm_atomic_get_plane_state(state, &plane->plane); |
| 345 | if (IS_ERR(s)) |
| 346 | return PTR_ERR(s); |
| 347 | |
| 348 | /* If the plane has been freed in the above loop its |
| 349 | * hardware planes must not be added to the used planes |
| 350 | * bitmask. However, the current state doesn't reflect |
| 351 | * the free state yet, as we've modified the new state |
| 352 | * above. Use the local freed planes list to check for |
| 353 | * that condition instead. |
| 354 | */ |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 355 | if (group_freed_planes[index] & (1 << i)) { |
| 356 | dev_dbg(rcdu->dev, |
| 357 | "%s: plane (%u,%u) has been freed, skipping\n", |
| 358 | __func__, plane->group->index, |
| 359 | plane - plane->group->planes); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 360 | continue; |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 361 | } |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 362 | |
Laurent Pinchart | ec69a40 | 2015-04-29 00:48:17 +0300 | [diff] [blame] | 363 | plane_state = to_rcar_plane_state(plane->plane.state); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 364 | used_planes |= rcar_du_plane_hwmask(plane_state); |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 365 | |
| 366 | dev_dbg(rcdu->dev, |
| 367 | "%s: plane (%u,%u) uses %u hwplanes (index %d)\n", |
| 368 | __func__, plane->group->index, |
| 369 | plane - plane->group->planes, |
| 370 | plane_state->format ? |
| 371 | plane_state->format->planes : 0, |
| 372 | plane_state->hwindex); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | group_free_planes[index] = 0xff & ~used_planes; |
| 376 | groups &= ~(1 << index); |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 377 | |
| 378 | dev_dbg(rcdu->dev, "%s: group %u free planes mask 0x%02x\n", |
| 379 | __func__, index, group_free_planes[index]); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* Reallocate hardware planes for each plane that needs it. */ |
| 383 | for (i = 0; i < dev->mode_config.num_total_plane; ++i) { |
| 384 | struct rcar_du_plane_state *plane_state; |
| 385 | struct rcar_du_plane *plane; |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 386 | unsigned int crtc_planes; |
| 387 | unsigned int free; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 388 | int idx; |
| 389 | |
| 390 | if (!state->planes[i]) |
| 391 | continue; |
| 392 | |
| 393 | plane = to_rcar_plane(state->planes[i]); |
Laurent Pinchart | ec69a40 | 2015-04-29 00:48:17 +0300 | [diff] [blame] | 394 | plane_state = to_rcar_plane_state(state->plane_states[i]); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 395 | |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 396 | dev_dbg(rcdu->dev, "%s: allocating plane (%u,%u)\n", __func__, |
| 397 | plane->group->index, plane - plane->group->planes); |
| 398 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 399 | /* Skip planes that are being disabled or don't need to be |
| 400 | * reallocated. |
| 401 | */ |
| 402 | if (!plane_state->format || |
| 403 | !rcar_du_plane_needs_realloc(plane, plane_state)) |
| 404 | continue; |
| 405 | |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 406 | /* Try to allocate the plane from the free planes currently |
| 407 | * associated with the target CRTC to avoid restarting the CRTC |
| 408 | * group and thus minimize flicker. If it fails fall back to |
| 409 | * allocating from all free planes. |
| 410 | */ |
| 411 | crtc_planes = to_rcar_crtc(plane_state->state.crtc)->index % 2 |
| 412 | ? plane->group->dptsr_planes |
| 413 | : ~plane->group->dptsr_planes; |
| 414 | free = group_free_planes[plane->group->index]; |
| 415 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 416 | idx = rcar_du_plane_hwalloc(plane_state->format->planes, |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 417 | free & crtc_planes); |
| 418 | if (idx < 0) |
| 419 | idx = rcar_du_plane_hwalloc(plane_state->format->planes, |
| 420 | free); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 421 | if (idx < 0) { |
| 422 | dev_dbg(rcdu->dev, "%s: no available hardware plane\n", |
| 423 | __func__); |
| 424 | return idx; |
| 425 | } |
| 426 | |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 427 | dev_dbg(rcdu->dev, "%s: allocated %u hwplanes (index %u)\n", |
| 428 | __func__, plane_state->format->planes, idx); |
| 429 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 430 | plane_state->hwindex = idx; |
| 431 | |
| 432 | group_free_planes[plane->group->index] &= |
| 433 | ~rcar_du_plane_hwmask(plane_state); |
Laurent Pinchart | 0805861 | 2015-02-25 22:31:52 +0200 | [diff] [blame] | 434 | |
| 435 | dev_dbg(rcdu->dev, "%s: group %u free planes mask 0x%02x\n", |
| 436 | __func__, plane->group->index, |
| 437 | group_free_planes[plane->group->index]); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 443 | struct rcar_du_commit { |
| 444 | struct work_struct work; |
| 445 | struct drm_device *dev; |
| 446 | struct drm_atomic_state *state; |
| 447 | u32 crtcs; |
| 448 | }; |
| 449 | |
| 450 | static void rcar_du_atomic_complete(struct rcar_du_commit *commit) |
| 451 | { |
| 452 | struct drm_device *dev = commit->dev; |
| 453 | struct rcar_du_device *rcdu = dev->dev_private; |
| 454 | struct drm_atomic_state *old_state = commit->state; |
| 455 | |
| 456 | /* Apply the atomic update. */ |
| 457 | drm_atomic_helper_commit_modeset_disables(dev, old_state); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 458 | drm_atomic_helper_commit_modeset_enables(dev, old_state); |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 459 | drm_atomic_helper_commit_planes(dev, old_state, false); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 460 | |
| 461 | drm_atomic_helper_wait_for_vblanks(dev, old_state); |
| 462 | |
| 463 | drm_atomic_helper_cleanup_planes(dev, old_state); |
| 464 | |
| 465 | drm_atomic_state_free(old_state); |
| 466 | |
| 467 | /* Complete the commit, wake up any waiter. */ |
| 468 | spin_lock(&rcdu->commit.wait.lock); |
| 469 | rcdu->commit.pending &= ~commit->crtcs; |
| 470 | wake_up_all_locked(&rcdu->commit.wait); |
| 471 | spin_unlock(&rcdu->commit.wait.lock); |
| 472 | |
| 473 | kfree(commit); |
| 474 | } |
| 475 | |
| 476 | static void rcar_du_atomic_work(struct work_struct *work) |
| 477 | { |
| 478 | struct rcar_du_commit *commit = |
| 479 | container_of(work, struct rcar_du_commit, work); |
| 480 | |
| 481 | rcar_du_atomic_complete(commit); |
| 482 | } |
| 483 | |
| 484 | static int rcar_du_atomic_commit(struct drm_device *dev, |
| 485 | struct drm_atomic_state *state, bool async) |
| 486 | { |
| 487 | struct rcar_du_device *rcdu = dev->dev_private; |
| 488 | struct rcar_du_commit *commit; |
| 489 | unsigned int i; |
| 490 | int ret; |
| 491 | |
| 492 | ret = drm_atomic_helper_prepare_planes(dev, state); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | |
| 496 | /* Allocate the commit object. */ |
| 497 | commit = kzalloc(sizeof(*commit), GFP_KERNEL); |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 498 | if (commit == NULL) { |
| 499 | ret = -ENOMEM; |
| 500 | goto error; |
| 501 | } |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 502 | |
| 503 | INIT_WORK(&commit->work, rcar_du_atomic_work); |
| 504 | commit->dev = dev; |
| 505 | commit->state = state; |
| 506 | |
| 507 | /* Wait until all affected CRTCs have completed previous commits and |
| 508 | * mark them as pending. |
| 509 | */ |
| 510 | for (i = 0; i < dev->mode_config.num_crtc; ++i) { |
| 511 | if (state->crtcs[i]) |
| 512 | commit->crtcs |= 1 << drm_crtc_index(state->crtcs[i]); |
| 513 | } |
| 514 | |
| 515 | spin_lock(&rcdu->commit.wait.lock); |
| 516 | ret = wait_event_interruptible_locked(rcdu->commit.wait, |
| 517 | !(rcdu->commit.pending & commit->crtcs)); |
| 518 | if (ret == 0) |
| 519 | rcdu->commit.pending |= commit->crtcs; |
| 520 | spin_unlock(&rcdu->commit.wait.lock); |
| 521 | |
| 522 | if (ret) { |
| 523 | kfree(commit); |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 524 | goto error; |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Swap the state, this is the point of no return. */ |
| 528 | drm_atomic_helper_swap_state(dev, state); |
| 529 | |
| 530 | if (async) |
| 531 | schedule_work(&commit->work); |
| 532 | else |
| 533 | rcar_du_atomic_complete(commit); |
| 534 | |
| 535 | return 0; |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 536 | |
| 537 | error: |
| 538 | drm_atomic_helper_cleanup_planes(dev, state); |
| 539 | return ret; |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* ----------------------------------------------------------------------------- |
| 543 | * Initialization |
| 544 | */ |
| 545 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 546 | static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = { |
| 547 | .fb_create = rcar_du_fb_create, |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 548 | .output_poll_changed = rcar_du_output_poll_changed, |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 549 | .atomic_check = rcar_du_atomic_check, |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 550 | .atomic_commit = rcar_du_atomic_commit, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 551 | }; |
| 552 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 553 | static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu, |
| 554 | enum rcar_du_output output, |
| 555 | struct of_endpoint *ep) |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 556 | { |
| 557 | static const struct { |
| 558 | const char *compatible; |
| 559 | enum rcar_du_encoder_type type; |
| 560 | } encoders[] = { |
| 561 | { "adi,adv7123", RCAR_DU_ENCODER_VGA }, |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 562 | { "adi,adv7511w", RCAR_DU_ENCODER_HDMI }, |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 563 | { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS }, |
| 564 | }; |
| 565 | |
| 566 | enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE; |
| 567 | struct device_node *connector = NULL; |
| 568 | struct device_node *encoder = NULL; |
Philipp Zabel | f033c0b | 2014-12-01 13:32:32 +0100 | [diff] [blame] | 569 | struct device_node *ep_node = NULL; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 570 | struct device_node *entity_ep_node; |
| 571 | struct device_node *entity; |
| 572 | int ret; |
| 573 | |
| 574 | /* |
| 575 | * Locate the connected entity and infer its type from the number of |
| 576 | * endpoints. |
| 577 | */ |
| 578 | entity = of_graph_get_remote_port_parent(ep->local_node); |
| 579 | if (!entity) { |
| 580 | dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n", |
| 581 | ep->local_node->full_name); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 582 | return -ENODEV; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0); |
| 586 | |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 587 | for_each_endpoint_of_node(entity, ep_node) { |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 588 | if (ep_node == entity_ep_node) |
| 589 | continue; |
| 590 | |
| 591 | /* |
| 592 | * We've found one endpoint other than the input, this must |
| 593 | * be an encoder. Locate the connector. |
| 594 | */ |
| 595 | encoder = entity; |
| 596 | connector = of_graph_get_remote_port_parent(ep_node); |
| 597 | of_node_put(ep_node); |
| 598 | |
| 599 | if (!connector) { |
| 600 | dev_warn(rcdu->dev, |
| 601 | "no connector for encoder %s, skipping\n", |
| 602 | encoder->full_name); |
| 603 | of_node_put(entity_ep_node); |
| 604 | of_node_put(encoder); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 605 | return -ENODEV; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | of_node_put(entity_ep_node); |
| 612 | |
| 613 | if (encoder) { |
| 614 | /* |
| 615 | * If an encoder has been found, get its type based on its |
| 616 | * compatible string. |
| 617 | */ |
| 618 | unsigned int i; |
| 619 | |
| 620 | for (i = 0; i < ARRAY_SIZE(encoders); ++i) { |
| 621 | if (of_device_is_compatible(encoder, |
| 622 | encoders[i].compatible)) { |
| 623 | enc_type = encoders[i].type; |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (i == ARRAY_SIZE(encoders)) { |
| 629 | dev_warn(rcdu->dev, |
| 630 | "unknown encoder type for %s, skipping\n", |
| 631 | encoder->full_name); |
| 632 | of_node_put(encoder); |
| 633 | of_node_put(connector); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 634 | return -EINVAL; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 635 | } |
| 636 | } else { |
| 637 | /* |
| 638 | * If no encoder has been found the entity must be the |
| 639 | * connector. |
| 640 | */ |
| 641 | connector = entity; |
| 642 | } |
| 643 | |
Laurent Pinchart | 3ea4d5e | 2014-09-17 14:41:57 +0300 | [diff] [blame] | 644 | ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 645 | of_node_put(encoder); |
| 646 | of_node_put(connector); |
| 647 | |
Laurent Pinchart | 64549cd | 2015-05-26 14:59:42 +0300 | [diff] [blame] | 648 | if (ret && ret != -EPROBE_DEFER) |
| 649 | dev_warn(rcdu->dev, |
| 650 | "failed to initialize encoder %s (%d), skipping\n", |
| 651 | encoder->full_name, ret); |
| 652 | |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 653 | return ret; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 656 | static int rcar_du_encoders_init(struct rcar_du_device *rcdu) |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 657 | { |
| 658 | struct device_node *np = rcdu->dev->of_node; |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 659 | struct device_node *ep_node; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 660 | unsigned int num_encoders = 0; |
| 661 | |
| 662 | /* |
| 663 | * Iterate over the endpoints and create one encoder for each output |
| 664 | * pipeline. |
| 665 | */ |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 666 | for_each_endpoint_of_node(np, ep_node) { |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 667 | enum rcar_du_output output; |
| 668 | struct of_endpoint ep; |
| 669 | unsigned int i; |
| 670 | int ret; |
| 671 | |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 672 | ret = of_graph_parse_endpoint(ep_node, &ep); |
| 673 | if (ret < 0) { |
| 674 | of_node_put(ep_node); |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | /* Find the output route corresponding to the port number. */ |
| 679 | for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) { |
| 680 | if (rcdu->info->routes[i].possible_crtcs && |
| 681 | rcdu->info->routes[i].port == ep.port) { |
| 682 | output = i; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (i == RCAR_DU_OUTPUT_MAX) { |
| 688 | dev_warn(rcdu->dev, |
| 689 | "port %u references unexisting output, skipping\n", |
| 690 | ep.port); |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | /* Process the output pipeline. */ |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 695 | ret = rcar_du_encoders_init_one(rcdu, output, &ep); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 696 | if (ret < 0) { |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 697 | if (ret == -EPROBE_DEFER) { |
| 698 | of_node_put(ep_node); |
| 699 | return ret; |
| 700 | } |
| 701 | |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 702 | continue; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 705 | num_encoders++; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | return num_encoders; |
| 709 | } |
| 710 | |
Laurent Pinchart | 9f6aee9 | 2015-04-28 23:59:29 +0300 | [diff] [blame] | 711 | static int rcar_du_properties_init(struct rcar_du_device *rcdu) |
| 712 | { |
| 713 | rcdu->props.alpha = |
| 714 | drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255); |
| 715 | if (rcdu->props.alpha == NULL) |
| 716 | return -ENOMEM; |
| 717 | |
| 718 | /* The color key is expressed as an RGB888 triplet stored in a 32-bit |
| 719 | * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0) |
| 720 | * or enable source color keying (1). |
| 721 | */ |
| 722 | rcdu->props.colorkey = |
| 723 | drm_property_create_range(rcdu->ddev, 0, "colorkey", |
| 724 | 0, 0x01ffffff); |
| 725 | if (rcdu->props.colorkey == NULL) |
| 726 | return -ENOMEM; |
| 727 | |
| 728 | rcdu->props.zpos = |
| 729 | drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7); |
| 730 | if (rcdu->props.zpos == NULL) |
| 731 | return -ENOMEM; |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 736 | int rcar_du_modeset_init(struct rcar_du_device *rcdu) |
| 737 | { |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 738 | static const unsigned int mmio_offsets[] = { |
| 739 | DU0_REG_OFFSET, DU2_REG_OFFSET |
| 740 | }; |
| 741 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 742 | struct drm_device *dev = rcdu->ddev; |
| 743 | struct drm_encoder *encoder; |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 744 | struct drm_fbdev_cma *fbdev; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 745 | unsigned int num_encoders; |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 746 | unsigned int num_groups; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 747 | unsigned int i; |
| 748 | int ret; |
| 749 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 750 | drm_mode_config_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 751 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 752 | dev->mode_config.min_width = 0; |
| 753 | dev->mode_config.min_height = 0; |
| 754 | dev->mode_config.max_width = 4095; |
| 755 | dev->mode_config.max_height = 2047; |
| 756 | dev->mode_config.funcs = &rcar_du_mode_config_funcs; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 757 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 758 | rcdu->num_crtcs = rcdu->info->num_crtcs; |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 759 | |
Laurent Pinchart | 9f6aee9 | 2015-04-28 23:59:29 +0300 | [diff] [blame] | 760 | ret = rcar_du_properties_init(rcdu); |
| 761 | if (ret < 0) |
| 762 | return ret; |
| 763 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 764 | /* Initialize the groups. */ |
| 765 | num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 766 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 767 | for (i = 0; i < num_groups; ++i) { |
| 768 | struct rcar_du_group *rgrp = &rcdu->groups[i]; |
| 769 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 770 | mutex_init(&rgrp->lock); |
| 771 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 772 | rgrp->dev = rcdu; |
| 773 | rgrp->mmio_offset = mmio_offsets[i]; |
| 774 | rgrp->index = i; |
Laurent Pinchart | fe6fbe9 | 2015-04-28 17:36:33 +0300 | [diff] [blame] | 775 | rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U); |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 776 | |
Laurent Pinchart | 2610abf | 2015-04-28 22:00:28 +0300 | [diff] [blame] | 777 | /* If we have more than one CRTCs in this group pre-associate |
| 778 | * planes 0-3 with CRTC 0 and planes 4-7 with CRTC 1 to minimize |
| 779 | * flicker occurring when the association is changed. |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 780 | */ |
Laurent Pinchart | 2610abf | 2015-04-28 22:00:28 +0300 | [diff] [blame] | 781 | rgrp->dptsr_planes = rgrp->num_crtcs > 1 ? 0xf0 : 0; |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 782 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 783 | ret = rcar_du_planes_init(rgrp); |
Laurent Pinchart | 3463ff6 | 2013-07-04 20:05:50 +0200 | [diff] [blame] | 784 | if (ret < 0) |
| 785 | return ret; |
| 786 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 787 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 788 | /* Create the CRTCs. */ |
| 789 | for (i = 0; i < rcdu->num_crtcs; ++i) { |
| 790 | struct rcar_du_group *rgrp = &rcdu->groups[i / 2]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 791 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 792 | ret = rcar_du_crtc_create(rgrp, i); |
| 793 | if (ret < 0) |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | /* Initialize the encoders. */ |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 798 | ret = rcar_du_lvdsenc_init(rcdu); |
| 799 | if (ret < 0) |
| 800 | return ret; |
| 801 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 802 | ret = rcar_du_encoders_init(rcdu); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 803 | if (ret < 0) |
| 804 | return ret; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 805 | |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 806 | if (ret == 0) { |
| 807 | dev_err(rcdu->dev, "error: no encoder could be initialized\n"); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 811 | num_encoders = ret; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 812 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 813 | /* Set the possible CRTCs and possible clones. There's always at least |
| 814 | * one way for all encoders to clone each other, set all bits in the |
| 815 | * possible clones field. |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 816 | */ |
| 817 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 818 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 819 | const struct rcar_du_output_routing *route = |
| 820 | &rcdu->info->routes[renc->output]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 821 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 822 | encoder->possible_crtcs = route->possible_crtcs; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 823 | encoder->possible_clones = (1 << num_encoders) - 1; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Laurent Pinchart | 3e8da87 | 2015-02-20 11:30:59 +0200 | [diff] [blame] | 826 | drm_mode_config_reset(dev); |
| 827 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 828 | drm_kms_helper_poll_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 829 | |
Laurent Pinchart | 931b733 | 2015-02-24 03:51:26 +0200 | [diff] [blame] | 830 | if (dev->mode_config.num_connector) { |
| 831 | fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc, |
| 832 | dev->mode_config.num_connector); |
| 833 | if (IS_ERR(fbdev)) |
| 834 | return PTR_ERR(fbdev); |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 835 | |
Laurent Pinchart | 931b733 | 2015-02-24 03:51:26 +0200 | [diff] [blame] | 836 | rcdu->fbdev = fbdev; |
| 837 | } else { |
| 838 | dev_info(rcdu->dev, |
| 839 | "no connector found, disabling fbdev emulation\n"); |
| 840 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |