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 | * |
| 4 | * Copyright (C) 2013 Renesas Corporation |
| 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_crtc.h" |
| 21 | #include "rcar_du_drv.h" |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 22 | #include "rcar_du_encoder.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 23 | #include "rcar_du_kms.h" |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 24 | #include "rcar_du_lvdsenc.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 25 | #include "rcar_du_regs.h" |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 26 | |
| 27 | /* ----------------------------------------------------------------------------- |
| 28 | * Format helpers |
| 29 | */ |
| 30 | |
| 31 | static const struct rcar_du_format_info rcar_du_format_infos[] = { |
| 32 | { |
| 33 | .fourcc = DRM_FORMAT_RGB565, |
| 34 | .bpp = 16, |
| 35 | .planes = 1, |
| 36 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 37 | .edf = PnDDCR4_EDF_NONE, |
| 38 | }, { |
| 39 | .fourcc = DRM_FORMAT_ARGB1555, |
| 40 | .bpp = 16, |
| 41 | .planes = 1, |
| 42 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 43 | .edf = PnDDCR4_EDF_NONE, |
| 44 | }, { |
| 45 | .fourcc = DRM_FORMAT_XRGB1555, |
| 46 | .bpp = 16, |
| 47 | .planes = 1, |
| 48 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB, |
| 49 | .edf = PnDDCR4_EDF_NONE, |
| 50 | }, { |
| 51 | .fourcc = DRM_FORMAT_XRGB8888, |
| 52 | .bpp = 32, |
| 53 | .planes = 1, |
| 54 | .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP, |
| 55 | .edf = PnDDCR4_EDF_RGB888, |
| 56 | }, { |
| 57 | .fourcc = DRM_FORMAT_ARGB8888, |
| 58 | .bpp = 32, |
| 59 | .planes = 1, |
| 60 | .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP, |
| 61 | .edf = PnDDCR4_EDF_ARGB8888, |
| 62 | }, { |
| 63 | .fourcc = DRM_FORMAT_UYVY, |
| 64 | .bpp = 16, |
| 65 | .planes = 1, |
| 66 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 67 | .edf = PnDDCR4_EDF_NONE, |
| 68 | }, { |
| 69 | .fourcc = DRM_FORMAT_YUYV, |
| 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_NV12, |
| 76 | .bpp = 12, |
| 77 | .planes = 2, |
| 78 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 79 | .edf = PnDDCR4_EDF_NONE, |
| 80 | }, { |
| 81 | .fourcc = DRM_FORMAT_NV21, |
| 82 | .bpp = 12, |
| 83 | .planes = 2, |
| 84 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 85 | .edf = PnDDCR4_EDF_NONE, |
| 86 | }, { |
| 87 | /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */ |
| 88 | .fourcc = DRM_FORMAT_NV16, |
| 89 | .bpp = 16, |
| 90 | .planes = 2, |
| 91 | .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC, |
| 92 | .edf = PnDDCR4_EDF_NONE, |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc) |
| 97 | { |
| 98 | unsigned int i; |
| 99 | |
| 100 | for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) { |
| 101 | if (rcar_du_format_infos[i].fourcc == fourcc) |
| 102 | return &rcar_du_format_infos[i]; |
| 103 | } |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 109 | * Frame buffer |
| 110 | */ |
| 111 | |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 112 | int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 113 | struct drm_mode_create_dumb *args) |
| 114 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 115 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 116 | unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 117 | unsigned int align; |
| 118 | |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 119 | /* The R8A7779 DU requires a 16 pixels pitch alignment as documented, |
| 120 | * but the R8A7790 DU seems to require a 128 bytes pitch alignment. |
| 121 | */ |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 122 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 123 | align = 128; |
| 124 | else |
| 125 | align = 16 * args->bpp / 8; |
| 126 | |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 127 | args->pitch = roundup(max(args->pitch, min_pitch), align); |
| 128 | |
| 129 | return drm_gem_cma_dumb_create(file, dev, args); |
| 130 | } |
| 131 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 132 | static struct drm_framebuffer * |
| 133 | rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
| 134 | struct drm_mode_fb_cmd2 *mode_cmd) |
| 135 | { |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 136 | struct rcar_du_device *rcdu = dev->dev_private; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 137 | const struct rcar_du_format_info *format; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 138 | unsigned int align; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 139 | |
| 140 | format = rcar_du_format_info(mode_cmd->pixel_format); |
| 141 | if (format == NULL) { |
| 142 | dev_dbg(dev->dev, "unsupported pixel format %08x\n", |
| 143 | mode_cmd->pixel_format); |
| 144 | return ERR_PTR(-EINVAL); |
| 145 | } |
| 146 | |
Laurent Pinchart | e8355e0 | 2013-11-13 13:33:45 +0100 | [diff] [blame] | 147 | if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B)) |
Laurent Pinchart | 9e2d2de | 2013-06-14 20:52:52 +0200 | [diff] [blame] | 148 | align = 128; |
| 149 | else |
| 150 | align = 16 * format->bpp / 8; |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 151 | |
| 152 | if (mode_cmd->pitches[0] & (align - 1) || |
| 153 | mode_cmd->pitches[0] >= 8192) { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 154 | dev_dbg(dev->dev, "invalid pitch value %u\n", |
| 155 | mode_cmd->pitches[0]); |
| 156 | return ERR_PTR(-EINVAL); |
| 157 | } |
| 158 | |
| 159 | if (format->planes == 2) { |
| 160 | if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) { |
| 161 | dev_dbg(dev->dev, |
| 162 | "luma and chroma pitches do not match\n"); |
| 163 | return ERR_PTR(-EINVAL); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return drm_fb_cma_create(dev, file_priv, mode_cmd); |
| 168 | } |
| 169 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 170 | static void rcar_du_output_poll_changed(struct drm_device *dev) |
| 171 | { |
| 172 | struct rcar_du_device *rcdu = dev->dev_private; |
| 173 | |
| 174 | drm_fbdev_cma_hotplug_event(rcdu->fbdev); |
| 175 | } |
| 176 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 177 | static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = { |
| 178 | .fb_create = rcar_du_fb_create, |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 179 | .output_poll_changed = rcar_du_output_poll_changed, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | int rcar_du_modeset_init(struct rcar_du_device *rcdu) |
| 183 | { |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 184 | static const unsigned int mmio_offsets[] = { |
| 185 | DU0_REG_OFFSET, DU2_REG_OFFSET |
| 186 | }; |
| 187 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 188 | struct drm_device *dev = rcdu->ddev; |
| 189 | struct drm_encoder *encoder; |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 190 | struct drm_fbdev_cma *fbdev; |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 191 | unsigned int num_groups; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 192 | unsigned int i; |
| 193 | int ret; |
| 194 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 195 | drm_mode_config_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 196 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 197 | dev->mode_config.min_width = 0; |
| 198 | dev->mode_config.min_height = 0; |
| 199 | dev->mode_config.max_width = 4095; |
| 200 | dev->mode_config.max_height = 2047; |
| 201 | dev->mode_config.funcs = &rcar_du_mode_config_funcs; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 202 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 203 | rcdu->num_crtcs = rcdu->info->num_crtcs; |
Laurent Pinchart | cb2025d | 2013-06-16 21:01:02 +0200 | [diff] [blame] | 204 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 205 | /* Initialize the groups. */ |
| 206 | num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 207 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 208 | for (i = 0; i < num_groups; ++i) { |
| 209 | struct rcar_du_group *rgrp = &rcdu->groups[i]; |
| 210 | |
| 211 | rgrp->dev = rcdu; |
| 212 | rgrp->mmio_offset = mmio_offsets[i]; |
| 213 | rgrp->index = i; |
| 214 | |
| 215 | ret = rcar_du_planes_init(rgrp); |
Laurent Pinchart | 3463ff6 | 2013-07-04 20:05:50 +0200 | [diff] [blame] | 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 219 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 220 | /* Create the CRTCs. */ |
| 221 | for (i = 0; i < rcdu->num_crtcs; ++i) { |
| 222 | struct rcar_du_group *rgrp = &rcdu->groups[i / 2]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 223 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 224 | ret = rcar_du_crtc_create(rgrp, i); |
| 225 | if (ret < 0) |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /* Initialize the encoders. */ |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 230 | ret = rcar_du_lvdsenc_init(rcdu); |
| 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 234 | for (i = 0; i < rcdu->pdata->num_encoders; ++i) { |
| 235 | const struct rcar_du_encoder_data *pdata = |
| 236 | &rcdu->pdata->encoders[i]; |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 237 | const struct rcar_du_output_routing *route = |
| 238 | &rcdu->info->routes[pdata->output]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 239 | |
Laurent Pinchart | 9194731 | 2013-06-16 16:25:35 +0200 | [diff] [blame] | 240 | if (pdata->type == RCAR_DU_ENCODER_UNUSED) |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 241 | continue; |
| 242 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 243 | if (pdata->output >= RCAR_DU_OUTPUT_MAX || |
| 244 | route->possible_crtcs == 0) { |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 245 | dev_warn(rcdu->dev, |
| 246 | "encoder %u references unexisting output %u, skipping\n", |
| 247 | i, pdata->output); |
| 248 | continue; |
| 249 | } |
| 250 | |
Laurent Pinchart | d9961b2 | 2014-04-01 13:03:52 +0200 | [diff] [blame] | 251 | ret = rcar_du_encoder_init(rcdu, pdata->type, pdata->output, |
| 252 | pdata); |
| 253 | if (ret < 0) |
| 254 | return ret; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 257 | /* Set the possible CRTCs and possible clones. There's always at least |
| 258 | * one way for all encoders to clone each other, set all bits in the |
| 259 | * possible clones field. |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 260 | */ |
| 261 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 262 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 263 | const struct rcar_du_output_routing *route = |
| 264 | &rcdu->info->routes[renc->output]; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 265 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 266 | encoder->possible_crtcs = route->possible_crtcs; |
| 267 | encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 270 | /* Now that the CRTCs have been initialized register the planes. */ |
| 271 | for (i = 0; i < num_groups; ++i) { |
| 272 | ret = rcar_du_planes_register(&rcdu->groups[i]); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | } |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 276 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 277 | drm_kms_helper_poll_init(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 278 | |
Laurent Pinchart | 3864c6f | 2013-03-14 22:45:22 +0100 | [diff] [blame] | 279 | drm_helper_disable_unused_functions(dev); |
| 280 | |
| 281 | fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc, |
| 282 | dev->mode_config.num_connector); |
| 283 | if (IS_ERR(fbdev)) |
| 284 | return PTR_ERR(fbdev); |
| 285 | |
| 286 | #ifndef CONFIG_FRAMEBUFFER_CONSOLE |
| 287 | drm_fbdev_cma_restore_mode(fbdev); |
| 288 | #endif |
| 289 | |
| 290 | rcdu->fbdev = fbdev; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |