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 | 2427b30 | 2015-09-07 17:34:26 +0300 | [diff] [blame] | 4 | * Copyright (C) 2013-2015 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 | 6d62ef3 | 2015-09-07 17:14:58 +0300 | [diff] [blame] | 31 | #include "rcar_du_vsp.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 32 | |
| 33 | /* ----------------------------------------------------------------------------- |
| 34 | * Format helpers |
| 35 | */ |
| 36 | |
| 37 | static const struct rcar_du_format_info rcar_du_format_infos[] = { |
| 38 | { |
| 39 | .fourcc = DRM_FORMAT_RGB565, |
| 40 | .bpp = 16, |
| 41 | .planes = 1, |
| 42 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 43 | .edf = PnDDCR4_EDF_NONE, |
| 44 | }, { |
| 45 | .fourcc = DRM_FORMAT_ARGB1555, |
| 46 | .bpp = 16, |
| 47 | .planes = 1, |
| 48 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 49 | .edf = PnDDCR4_EDF_NONE, |
| 50 | }, { |
| 51 | .fourcc = DRM_FORMAT_XRGB1555, |
| 52 | .bpp = 16, |
| 53 | .planes = 1, |
| 54 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 55 | .edf = PnDDCR4_EDF_NONE, |
| 56 | }, { |
| 57 | .fourcc = DRM_FORMAT_XRGB8888, |
| 58 | .bpp = 32, |
| 59 | .planes = 1, |
| 60 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 61 | .edf = PnDDCR4_EDF_RGB888, |
| 62 | }, { |
| 63 | .fourcc = DRM_FORMAT_ARGB8888, |
| 64 | .bpp = 32, |
| 65 | .planes = 1, |
| 66 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP, |
| 67 | .edf = PnDDCR4_EDF_ARGB8888, |
| 68 | }, { |
| 69 | .fourcc = DRM_FORMAT_UYVY, |
| 70 | .bpp = 16, |
| 71 | .planes = 1, |
| 72 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 73 | .edf = PnDDCR4_EDF_NONE, |
| 74 | }, { |
| 75 | .fourcc = DRM_FORMAT_YUYV, |
| 76 | .bpp = 16, |
| 77 | .planes = 1, |
| 78 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 79 | .edf = PnDDCR4_EDF_NONE, |
| 80 | }, { |
| 81 | .fourcc = DRM_FORMAT_NV12, |
| 82 | .bpp = 12, |
| 83 | .planes = 2, |
| 84 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 85 | .edf = PnDDCR4_EDF_NONE, |
| 86 | }, { |
| 87 | .fourcc = DRM_FORMAT_NV21, |
| 88 | .bpp = 12, |
| 89 | .planes = 2, |
| 90 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 91 | .edf = PnDDCR4_EDF_NONE, |
| 92 | }, { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 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 | }, |
Laurent Pinchart | 7863ac5 | 2015-11-12 02:03:47 +0200 | [diff] [blame] | 99 | /* The following formats are not supported on Gen2 and thus have no |
| 100 | * associated .pnmr or .edf settings. |
| 101 | */ |
| 102 | { |
| 103 | .fourcc = DRM_FORMAT_NV61, |
| 104 | .bpp = 16, |
| 105 | .planes = 2, |
| 106 | }, { |
| 107 | .fourcc = DRM_FORMAT_YUV420, |
| 108 | .bpp = 12, |
| 109 | .planes = 3, |
| 110 | }, { |
| 111 | .fourcc = DRM_FORMAT_YVU420, |
| 112 | .bpp = 12, |
| 113 | .planes = 3, |
| 114 | }, { |
| 115 | .fourcc = DRM_FORMAT_YUV422, |
| 116 | .bpp = 16, |
| 117 | .planes = 3, |
| 118 | }, { |
| 119 | .fourcc = DRM_FORMAT_YVU422, |
| 120 | .bpp = 16, |
| 121 | .planes = 3, |
| 122 | }, { |
| 123 | .fourcc = DRM_FORMAT_YUV444, |
| 124 | .bpp = 24, |
| 125 | .planes = 3, |
| 126 | }, { |
| 127 | .fourcc = DRM_FORMAT_YVU444, |
| 128 | .bpp = 24, |
| 129 | .planes = 3, |
| 130 | }, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc) |
| 134 | { |
| 135 | unsigned int i; |
| 136 | |
| 137 | for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) { |
| 138 | if (rcar_du_format_infos[i].fourcc == fourcc) |
| 139 | return &rcar_du_format_infos[i]; |
| 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 146 | * Frame buffer |
| 147 | */ |
| 148 | |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 149 | int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 150 | struct drm_mode_create_dumb *args) |
| 151 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 152 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 153 | unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 154 | unsigned int align; |
| 155 | |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 156 | /* The R8A7779 DU requires a 16 pixels pitch alignment as documented, |
| 157 | * but the R8A7790 DU seems to require a 128 bytes pitch alignment. |
| 158 | */ |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 159 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 160 | align = 128; |
| 161 | else |
| 162 | align = 16 * args->bpp / 8; |
| 163 | |
Thierry Reding | 7e295a3 | 2014-11-03 12:08:24 +0100 | [diff] [blame] | 164 | args->pitch = roundup(min_pitch, align); |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 165 | |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 166 | return drm_gem_cma_dumb_create_internal(file, dev, args); |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 169 | static struct drm_framebuffer * |
| 170 | rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 171 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 172 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 173 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 174 | const struct rcar_du_format_info *format; |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 175 | unsigned int max_pitch; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 176 | unsigned int align; |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 177 | unsigned int bpp; |
Laurent Pinchart | 7863ac5 | 2015-11-12 02:03:47 +0200 | [diff] [blame] | 178 | unsigned int i; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 179 | |
| 180 | format = rcar_du_format_info(mode_cmd->pixel_format); |
| 181 | if (format == NULL) { |
| 182 | dev_dbg(dev->dev, "unsupported pixel format %08x\n", |
| 183 | mode_cmd->pixel_format); |
| 184 | return ERR_PTR(-EINVAL); |
| 185 | } |
| 186 | |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 187 | /* |
| 188 | * The pitch and alignment constraints are expressed in pixels on the |
| 189 | * hardware side and in bytes in the DRM API. |
| 190 | */ |
Laurent Pinchart | 7863ac5 | 2015-11-12 02:03:47 +0200 | [diff] [blame] | 191 | bpp = format->planes == 1 ? format->bpp / 8 : 1; |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 192 | max_pitch = 4096 * bpp; |
| 193 | |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 194 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 195 | align = 128; |
| 196 | else |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 197 | align = 16 * bpp; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 198 | |
| 199 | if (mode_cmd->pitches[0] & (align - 1) || |
Laurent Pinchart | 8bed5cc | 2014-07-28 20:18:36 +0200 | [diff] [blame] | 200 | mode_cmd->pitches[0] >= max_pitch) { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 201 | dev_dbg(dev->dev, "invalid pitch value %u\n", |
| 202 | mode_cmd->pitches[0]); |
| 203 | return ERR_PTR(-EINVAL); |
| 204 | } |
| 205 | |
Laurent Pinchart | 7863ac5 | 2015-11-12 02:03:47 +0200 | [diff] [blame] | 206 | for (i = 1; i < format->planes; ++i) { |
| 207 | if (mode_cmd->pitches[i] != mode_cmd->pitches[0]) { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 208 | dev_dbg(dev->dev, |
| 209 | "luma and chroma pitches do not match\n"); |
| 210 | return ERR_PTR(-EINVAL); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return drm_fb_cma_create(dev, file_priv, mode_cmd); |
| 215 | } |
| 216 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 217 | static void rcar_du_output_poll_changed(struct drm_device *dev) |
| 218 | { |
| 219 | struct rcar_du_device *rcdu = dev->dev_private; |
| 220 | |
| 221 | drm_fbdev_cma_hotplug_event(rcdu->fbdev); |
| 222 | } |
| 223 | |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 224 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 225 | * Atomic Check and Update |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 226 | */ |
| 227 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 228 | static int rcar_du_atomic_check(struct drm_device *dev, |
| 229 | struct drm_atomic_state *state) |
| 230 | { |
Laurent Pinchart | 6d62ef3 | 2015-09-07 17:14:58 +0300 | [diff] [blame] | 231 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 232 | int ret; |
| 233 | |
Ville Syrjälä | 38d868e | 2016-10-10 17:50:56 +0300 | [diff] [blame] | 234 | ret = drm_atomic_helper_check_modeset(dev, state); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | |
| 238 | ret = drm_atomic_normalize_zpos(dev, state); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | ret = drm_atomic_helper_check_planes(dev, state); |
| 243 | if (ret) |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 244 | return ret; |
| 245 | |
Laurent Pinchart | 6d62ef3 | 2015-09-07 17:14:58 +0300 | [diff] [blame] | 246 | if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) |
| 247 | return 0; |
| 248 | |
Laurent Pinchart | ab334e1 | 2015-07-27 15:34:18 +0300 | [diff] [blame] | 249 | return rcar_du_atomic_check_planes(dev, state); |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 252 | struct rcar_du_commit { |
| 253 | struct work_struct work; |
| 254 | struct drm_device *dev; |
| 255 | struct drm_atomic_state *state; |
| 256 | u32 crtcs; |
| 257 | }; |
| 258 | |
| 259 | static void rcar_du_atomic_complete(struct rcar_du_commit *commit) |
| 260 | { |
| 261 | struct drm_device *dev = commit->dev; |
| 262 | struct rcar_du_device *rcdu = dev->dev_private; |
| 263 | struct drm_atomic_state *old_state = commit->state; |
| 264 | |
| 265 | /* Apply the atomic update. */ |
| 266 | drm_atomic_helper_commit_modeset_disables(dev, old_state); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 267 | drm_atomic_helper_commit_modeset_enables(dev, old_state); |
Liu Ying | 2b58e98 | 2016-08-29 17:12:03 +0800 | [diff] [blame] | 268 | drm_atomic_helper_commit_planes(dev, old_state, |
| 269 | DRM_PLANE_COMMIT_ACTIVE_ONLY); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 270 | |
| 271 | drm_atomic_helper_wait_for_vblanks(dev, old_state); |
| 272 | |
| 273 | drm_atomic_helper_cleanup_planes(dev, old_state); |
| 274 | |
| 275 | drm_atomic_state_free(old_state); |
| 276 | |
| 277 | /* Complete the commit, wake up any waiter. */ |
| 278 | spin_lock(&rcdu->commit.wait.lock); |
| 279 | rcdu->commit.pending &= ~commit->crtcs; |
| 280 | wake_up_all_locked(&rcdu->commit.wait); |
| 281 | spin_unlock(&rcdu->commit.wait.lock); |
| 282 | |
| 283 | kfree(commit); |
| 284 | } |
| 285 | |
| 286 | static void rcar_du_atomic_work(struct work_struct *work) |
| 287 | { |
| 288 | struct rcar_du_commit *commit = |
| 289 | container_of(work, struct rcar_du_commit, work); |
| 290 | |
| 291 | rcar_du_atomic_complete(commit); |
| 292 | } |
| 293 | |
| 294 | static int rcar_du_atomic_commit(struct drm_device *dev, |
Maarten Lankhorst | 3302f35 | 2016-04-26 16:11:40 +0200 | [diff] [blame] | 295 | struct drm_atomic_state *state, |
| 296 | bool nonblock) |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 297 | { |
| 298 | struct rcar_du_device *rcdu = dev->dev_private; |
| 299 | struct rcar_du_commit *commit; |
Daniel Vetter | 09ad807 | 2016-06-02 00:06:27 +0200 | [diff] [blame] | 300 | struct drm_crtc *crtc; |
| 301 | struct drm_crtc_state *crtc_state; |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 302 | unsigned int i; |
| 303 | int ret; |
| 304 | |
| 305 | ret = drm_atomic_helper_prepare_planes(dev, state); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
| 309 | /* Allocate the commit object. */ |
| 310 | commit = kzalloc(sizeof(*commit), GFP_KERNEL); |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 311 | if (commit == NULL) { |
| 312 | ret = -ENOMEM; |
| 313 | goto error; |
| 314 | } |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 315 | |
| 316 | INIT_WORK(&commit->work, rcar_du_atomic_work); |
| 317 | commit->dev = dev; |
| 318 | commit->state = state; |
| 319 | |
| 320 | /* Wait until all affected CRTCs have completed previous commits and |
| 321 | * mark them as pending. |
| 322 | */ |
Daniel Vetter | 09ad807 | 2016-06-02 00:06:27 +0200 | [diff] [blame] | 323 | for_each_crtc_in_state(state, crtc, crtc_state, i) |
| 324 | commit->crtcs |= drm_crtc_mask(crtc); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 325 | |
| 326 | spin_lock(&rcdu->commit.wait.lock); |
| 327 | ret = wait_event_interruptible_locked(rcdu->commit.wait, |
| 328 | !(rcdu->commit.pending & commit->crtcs)); |
| 329 | if (ret == 0) |
| 330 | rcdu->commit.pending |= commit->crtcs; |
| 331 | spin_unlock(&rcdu->commit.wait.lock); |
| 332 | |
| 333 | if (ret) { |
| 334 | kfree(commit); |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 335 | goto error; |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Swap the state, this is the point of no return. */ |
Daniel Vetter | 5e84c26 | 2016-06-10 00:06:32 +0200 | [diff] [blame] | 339 | drm_atomic_helper_swap_state(state, true); |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 340 | |
Maarten Lankhorst | 3302f35 | 2016-04-26 16:11:40 +0200 | [diff] [blame] | 341 | if (nonblock) |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 342 | schedule_work(&commit->work); |
| 343 | else |
| 344 | rcar_du_atomic_complete(commit); |
| 345 | |
| 346 | return 0; |
Laurent Pinchart | 39a3d57 | 2015-05-27 02:14:37 +0300 | [diff] [blame] | 347 | |
| 348 | error: |
| 349 | drm_atomic_helper_cleanup_planes(dev, state); |
| 350 | return ret; |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* ----------------------------------------------------------------------------- |
| 354 | * Initialization |
| 355 | */ |
| 356 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 357 | static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = { |
| 358 | .fb_create = rcar_du_fb_create, |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 359 | .output_poll_changed = rcar_du_output_poll_changed, |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 360 | .atomic_check = rcar_du_atomic_check, |
Laurent Pinchart | 8d3f9b2 | 2015-02-23 01:02:15 +0200 | [diff] [blame] | 361 | .atomic_commit = rcar_du_atomic_commit, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 362 | }; |
| 363 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 364 | static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu, |
| 365 | enum rcar_du_output output, |
| 366 | struct of_endpoint *ep) |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 367 | { |
| 368 | static const struct { |
| 369 | const char *compatible; |
| 370 | enum rcar_du_encoder_type type; |
| 371 | } encoders[] = { |
| 372 | { "adi,adv7123", RCAR_DU_ENCODER_VGA }, |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 373 | { "adi,adv7511w", RCAR_DU_ENCODER_HDMI }, |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 374 | { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS }, |
| 375 | }; |
| 376 | |
| 377 | enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE; |
| 378 | struct device_node *connector = NULL; |
| 379 | struct device_node *encoder = NULL; |
Philipp Zabel | f033c0b | 2014-12-01 13:32:32 +0100 | [diff] [blame] | 380 | struct device_node *ep_node = NULL; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 381 | struct device_node *entity_ep_node; |
| 382 | struct device_node *entity; |
| 383 | int ret; |
| 384 | |
| 385 | /* |
| 386 | * Locate the connected entity and infer its type from the number of |
| 387 | * endpoints. |
| 388 | */ |
| 389 | entity = of_graph_get_remote_port_parent(ep->local_node); |
| 390 | if (!entity) { |
| 391 | dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n", |
| 392 | ep->local_node->full_name); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 393 | return -ENODEV; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0); |
| 397 | |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 398 | for_each_endpoint_of_node(entity, ep_node) { |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 399 | if (ep_node == entity_ep_node) |
| 400 | continue; |
| 401 | |
| 402 | /* |
| 403 | * We've found one endpoint other than the input, this must |
| 404 | * be an encoder. Locate the connector. |
| 405 | */ |
| 406 | encoder = entity; |
| 407 | connector = of_graph_get_remote_port_parent(ep_node); |
| 408 | of_node_put(ep_node); |
| 409 | |
| 410 | if (!connector) { |
| 411 | dev_warn(rcdu->dev, |
| 412 | "no connector for encoder %s, skipping\n", |
| 413 | encoder->full_name); |
| 414 | of_node_put(entity_ep_node); |
| 415 | of_node_put(encoder); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 416 | return -ENODEV; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | of_node_put(entity_ep_node); |
| 423 | |
| 424 | if (encoder) { |
| 425 | /* |
| 426 | * If an encoder has been found, get its type based on its |
| 427 | * compatible string. |
| 428 | */ |
| 429 | unsigned int i; |
| 430 | |
| 431 | for (i = 0; i < ARRAY_SIZE(encoders); ++i) { |
| 432 | if (of_device_is_compatible(encoder, |
| 433 | encoders[i].compatible)) { |
| 434 | enc_type = encoders[i].type; |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (i == ARRAY_SIZE(encoders)) { |
| 440 | dev_warn(rcdu->dev, |
| 441 | "unknown encoder type for %s, skipping\n", |
| 442 | encoder->full_name); |
| 443 | of_node_put(encoder); |
| 444 | of_node_put(connector); |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 445 | return -EINVAL; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 446 | } |
| 447 | } else { |
| 448 | /* |
| 449 | * If no encoder has been found the entity must be the |
| 450 | * connector. |
| 451 | */ |
| 452 | connector = entity; |
| 453 | } |
| 454 | |
Laurent Pinchart | 3ea4d5e | 2014-09-17 14:41:57 +0300 | [diff] [blame] | 455 | ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 456 | of_node_put(encoder); |
| 457 | of_node_put(connector); |
| 458 | |
Laurent Pinchart | 64549cd | 2015-05-26 14:59:42 +0300 | [diff] [blame] | 459 | if (ret && ret != -EPROBE_DEFER) |
| 460 | dev_warn(rcdu->dev, |
| 461 | "failed to initialize encoder %s (%d), skipping\n", |
| 462 | encoder->full_name, ret); |
| 463 | |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 464 | return ret; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 467 | static int rcar_du_encoders_init(struct rcar_du_device *rcdu) |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 468 | { |
| 469 | struct device_node *np = rcdu->dev->of_node; |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 470 | struct device_node *ep_node; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 471 | unsigned int num_encoders = 0; |
| 472 | |
| 473 | /* |
| 474 | * Iterate over the endpoints and create one encoder for each output |
| 475 | * pipeline. |
| 476 | */ |
Philipp Zabel | 4af642d | 2014-12-22 11:46:40 +0100 | [diff] [blame] | 477 | for_each_endpoint_of_node(np, ep_node) { |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 478 | enum rcar_du_output output; |
| 479 | struct of_endpoint ep; |
| 480 | unsigned int i; |
| 481 | int ret; |
| 482 | |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 483 | ret = of_graph_parse_endpoint(ep_node, &ep); |
| 484 | if (ret < 0) { |
| 485 | of_node_put(ep_node); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | /* Find the output route corresponding to the port number. */ |
| 490 | for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) { |
| 491 | if (rcdu->info->routes[i].possible_crtcs && |
| 492 | rcdu->info->routes[i].port == ep.port) { |
| 493 | output = i; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (i == RCAR_DU_OUTPUT_MAX) { |
| 499 | dev_warn(rcdu->dev, |
| 500 | "port %u references unexisting output, skipping\n", |
| 501 | ep.port); |
| 502 | continue; |
| 503 | } |
| 504 | |
| 505 | /* Process the output pipeline. */ |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 506 | ret = rcar_du_encoders_init_one(rcdu, output, &ep); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 507 | if (ret < 0) { |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 508 | if (ret == -EPROBE_DEFER) { |
| 509 | of_node_put(ep_node); |
| 510 | return ret; |
| 511 | } |
| 512 | |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 513 | continue; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 514 | } |
| 515 | |
Laurent Pinchart | 898a2f3 | 2015-05-26 15:07:56 +0300 | [diff] [blame] | 516 | num_encoders++; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | return num_encoders; |
| 520 | } |
| 521 | |
Laurent Pinchart | 9f6aee9 | 2015-04-28 23:59:29 +0300 | [diff] [blame] | 522 | static int rcar_du_properties_init(struct rcar_du_device *rcdu) |
| 523 | { |
| 524 | rcdu->props.alpha = |
| 525 | drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255); |
| 526 | if (rcdu->props.alpha == NULL) |
| 527 | return -ENOMEM; |
| 528 | |
| 529 | /* The color key is expressed as an RGB888 triplet stored in a 32-bit |
| 530 | * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0) |
| 531 | * or enable source color keying (1). |
| 532 | */ |
| 533 | rcdu->props.colorkey = |
| 534 | drm_property_create_range(rcdu->ddev, 0, "colorkey", |
| 535 | 0, 0x01ffffff); |
| 536 | if (rcdu->props.colorkey == NULL) |
| 537 | return -ENOMEM; |
| 538 | |
Laurent Pinchart | 9f6aee9 | 2015-04-28 23:59:29 +0300 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 542 | int rcar_du_modeset_init(struct rcar_du_device *rcdu) |
| 543 | { |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 544 | static const unsigned int mmio_offsets[] = { |
| 545 | DU0_REG_OFFSET, DU2_REG_OFFSET |
| 546 | }; |
| 547 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 548 | struct drm_device *dev = rcdu->ddev; |
| 549 | struct drm_encoder *encoder; |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 550 | struct drm_fbdev_cma *fbdev; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 551 | unsigned int num_encoders; |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 552 | unsigned int num_groups; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 553 | unsigned int i; |
| 554 | int ret; |
| 555 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 556 | drm_mode_config_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 557 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 558 | dev->mode_config.min_width = 0; |
| 559 | dev->mode_config.min_height = 0; |
| 560 | dev->mode_config.max_width = 4095; |
| 561 | dev->mode_config.max_height = 2047; |
| 562 | dev->mode_config.funcs = &rcar_du_mode_config_funcs; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 563 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 564 | rcdu->num_crtcs = rcdu->info->num_crtcs; |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 565 | |
Laurent Pinchart | 9f6aee9 | 2015-04-28 23:59:29 +0300 | [diff] [blame] | 566 | ret = rcar_du_properties_init(rcdu); |
| 567 | if (ret < 0) |
| 568 | return ret; |
| 569 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 570 | /* Initialize the groups. */ |
| 571 | num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 572 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 573 | for (i = 0; i < num_groups; ++i) { |
| 574 | struct rcar_du_group *rgrp = &rcdu->groups[i]; |
| 575 | |
Laurent Pinchart | 5ee5a81 | 2015-02-25 18:27:19 +0200 | [diff] [blame] | 576 | mutex_init(&rgrp->lock); |
| 577 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 578 | rgrp->dev = rcdu; |
| 579 | rgrp->mmio_offset = mmio_offsets[i]; |
| 580 | rgrp->index = i; |
Laurent Pinchart | fe6fbe9 | 2015-04-28 17:36:33 +0300 | [diff] [blame] | 581 | rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U); |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 582 | |
Laurent Pinchart | 2610abf | 2015-04-28 22:00:28 +0300 | [diff] [blame] | 583 | /* If we have more than one CRTCs in this group pre-associate |
Laurent Pinchart | 2427b30 | 2015-09-07 17:34:26 +0300 | [diff] [blame] | 584 | * the low-order planes with CRTC 0 and the high-order planes |
| 585 | * with CRTC 1 to minimize flicker occurring when the |
| 586 | * association is changed. |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 587 | */ |
Laurent Pinchart | 2427b30 | 2015-09-07 17:34:26 +0300 | [diff] [blame] | 588 | rgrp->dptsr_planes = rgrp->num_crtcs > 1 |
| 589 | ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0) |
| 590 | : 0; |
Laurent Pinchart | c8af99b | 2015-04-29 00:51:01 +0300 | [diff] [blame] | 591 | |
Laurent Pinchart | 6d62ef3 | 2015-09-07 17:14:58 +0300 | [diff] [blame] | 592 | if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) { |
| 593 | ret = rcar_du_planes_init(rgrp); |
| 594 | if (ret < 0) |
| 595 | return ret; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* Initialize the compositors. */ |
| 600 | if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) { |
| 601 | for (i = 0; i < rcdu->num_crtcs; ++i) { |
| 602 | struct rcar_du_vsp *vsp = &rcdu->vsps[i]; |
| 603 | |
| 604 | vsp->index = i; |
| 605 | vsp->dev = rcdu; |
| 606 | rcdu->crtcs[i].vsp = vsp; |
| 607 | |
| 608 | ret = rcar_du_vsp_init(vsp); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | } |
Laurent Pinchart | 3463ff6 | 2013-07-04 20:05:50 +0200 | [diff] [blame] | 612 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 613 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 614 | /* Create the CRTCs. */ |
| 615 | for (i = 0; i < rcdu->num_crtcs; ++i) { |
| 616 | struct rcar_du_group *rgrp = &rcdu->groups[i / 2]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 617 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 618 | ret = rcar_du_crtc_create(rgrp, i); |
| 619 | if (ret < 0) |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | /* Initialize the encoders. */ |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 624 | ret = rcar_du_lvdsenc_init(rcdu); |
| 625 | if (ret < 0) |
| 626 | return ret; |
| 627 | |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 628 | ret = rcar_du_encoders_init(rcdu); |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 629 | if (ret < 0) |
| 630 | return ret; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 631 | |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 632 | if (ret == 0) { |
| 633 | dev_err(rcdu->dev, "error: no encoder could be initialized\n"); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 637 | num_encoders = ret; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 638 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 639 | /* Set the possible CRTCs and possible clones. There's always at least |
| 640 | * one way for all encoders to clone each other, set all bits in the |
| 641 | * possible clones field. |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 642 | */ |
| 643 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 644 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 645 | const struct rcar_du_output_routing *route = |
| 646 | &rcdu->info->routes[renc->output]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 647 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 648 | encoder->possible_crtcs = route->possible_crtcs; |
Laurent Pinchart | 96c0269 | 2014-01-21 15:57:26 +0100 | [diff] [blame] | 649 | encoder->possible_clones = (1 << num_encoders) - 1; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Laurent Pinchart | 3e8da87 | 2015-02-20 11:30:59 +0200 | [diff] [blame] | 652 | drm_mode_config_reset(dev); |
| 653 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 654 | drm_kms_helper_poll_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 655 | |
Laurent Pinchart | 931b733 | 2015-02-24 03:51:26 +0200 | [diff] [blame] | 656 | if (dev->mode_config.num_connector) { |
| 657 | fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc, |
| 658 | dev->mode_config.num_connector); |
| 659 | if (IS_ERR(fbdev)) |
| 660 | return PTR_ERR(fbdev); |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 661 | |
Laurent Pinchart | 931b733 | 2015-02-24 03:51:26 +0200 | [diff] [blame] | 662 | rcdu->fbdev = fbdev; |
| 663 | } else { |
| 664 | dev_info(rcdu->dev, |
| 665 | "no connector found, disabling fbdev emulation\n"); |
| 666 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } |