2 * rcar_du_kms.c -- R-Car Display Unit Mode Setting
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
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>
20 #include "rcar_du_crtc.h"
21 #include "rcar_du_drv.h"
22 #include "rcar_du_kms.h"
23 #include "rcar_du_lvds.h"
24 #include "rcar_du_regs.h"
25 #include "rcar_du_vga.h"
27 /* -----------------------------------------------------------------------------
31 static const struct rcar_du_format_info rcar_du_format_infos[] = {
33 .fourcc = DRM_FORMAT_RGB565,
36 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
37 .edf = PnDDCR4_EDF_NONE,
39 .fourcc = DRM_FORMAT_ARGB1555,
42 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
43 .edf = PnDDCR4_EDF_NONE,
45 .fourcc = DRM_FORMAT_XRGB1555,
48 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
49 .edf = PnDDCR4_EDF_NONE,
51 .fourcc = DRM_FORMAT_XRGB8888,
54 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
55 .edf = PnDDCR4_EDF_RGB888,
57 .fourcc = DRM_FORMAT_ARGB8888,
60 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
61 .edf = PnDDCR4_EDF_ARGB8888,
63 .fourcc = DRM_FORMAT_UYVY,
66 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
67 .edf = PnDDCR4_EDF_NONE,
69 .fourcc = DRM_FORMAT_YUYV,
72 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
73 .edf = PnDDCR4_EDF_NONE,
75 .fourcc = DRM_FORMAT_NV12,
78 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
79 .edf = PnDDCR4_EDF_NONE,
81 .fourcc = DRM_FORMAT_NV21,
84 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
85 .edf = PnDDCR4_EDF_NONE,
87 /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
88 .fourcc = DRM_FORMAT_NV16,
91 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
92 .edf = PnDDCR4_EDF_NONE,
96 const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
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];
108 /* -----------------------------------------------------------------------------
109 * Common connector and encoder functions
113 rcar_du_connector_best_encoder(struct drm_connector *connector)
115 struct rcar_du_connector *rcon = to_rcar_connector(connector);
117 return &rcon->encoder->encoder;
120 void rcar_du_encoder_mode_prepare(struct drm_encoder *encoder)
124 void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
125 struct drm_display_mode *mode,
126 struct drm_display_mode *adjusted_mode)
128 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
130 rcar_du_crtc_route_output(encoder->crtc, renc->output);
133 void rcar_du_encoder_mode_commit(struct drm_encoder *encoder)
137 /* -----------------------------------------------------------------------------
141 int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
142 struct drm_mode_create_dumb *args)
144 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
147 /* The pitch must be aligned to a 16 pixels boundary. */
148 align = 16 * args->bpp / 8;
149 args->pitch = roundup(max(args->pitch, min_pitch), align);
151 return drm_gem_cma_dumb_create(file, dev, args);
154 static struct drm_framebuffer *
155 rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
156 struct drm_mode_fb_cmd2 *mode_cmd)
158 const struct rcar_du_format_info *format;
161 format = rcar_du_format_info(mode_cmd->pixel_format);
162 if (format == NULL) {
163 dev_dbg(dev->dev, "unsupported pixel format %08x\n",
164 mode_cmd->pixel_format);
165 return ERR_PTR(-EINVAL);
168 align = 16 * format->bpp / 8;
170 if (mode_cmd->pitches[0] & (align - 1) ||
171 mode_cmd->pitches[0] >= 8192) {
172 dev_dbg(dev->dev, "invalid pitch value %u\n",
173 mode_cmd->pitches[0]);
174 return ERR_PTR(-EINVAL);
177 if (format->planes == 2) {
178 if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
180 "luma and chroma pitches do not match\n");
181 return ERR_PTR(-EINVAL);
185 return drm_fb_cma_create(dev, file_priv, mode_cmd);
188 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
189 .fb_create = rcar_du_fb_create,
192 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
194 struct drm_device *dev = rcdu->ddev;
195 struct drm_encoder *encoder;
199 drm_mode_config_init(rcdu->ddev);
201 rcdu->ddev->mode_config.min_width = 0;
202 rcdu->ddev->mode_config.min_height = 0;
203 rcdu->ddev->mode_config.max_width = 4095;
204 rcdu->ddev->mode_config.max_height = 2047;
205 rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
207 ret = rcar_du_plane_init(rcdu);
211 for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i) {
212 ret = rcar_du_crtc_create(rcdu, i);
217 rcdu->used_crtcs = 0;
220 for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
221 const struct rcar_du_encoder_data *pdata =
222 &rcdu->pdata->encoders[i];
224 if (pdata->output >= ARRAY_SIZE(rcdu->crtcs)) {
226 "encoder %u references unexisting output %u, skipping\n",
231 switch (pdata->encoder) {
232 case RCAR_DU_ENCODER_VGA:
233 rcar_du_vga_init(rcdu, &pdata->u.vga, pdata->output);
236 case RCAR_DU_ENCODER_LVDS:
237 rcar_du_lvds_init(rcdu, &pdata->u.lvds, pdata->output);
245 /* Set the possible CRTCs and possible clones. All encoders can be
246 * driven by the CRTC associated with the output they're connected to,
247 * as well as by CRTC 0.
249 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
250 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
252 encoder->possible_crtcs = (1 << 0) | (1 << renc->output);
253 encoder->possible_clones = 1 << 0;
256 ret = rcar_du_plane_register(rcdu);
260 drm_kms_helper_poll_init(rcdu->ddev);
262 drm_helper_disable_unused_functions(rcdu->ddev);