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 static struct drm_framebuffer *
142 rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
143 struct drm_mode_fb_cmd2 *mode_cmd)
145 const struct rcar_du_format_info *format;
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);
154 if (mode_cmd->pitches[0] & 15 || mode_cmd->pitches[0] >= 8192) {
155 dev_dbg(dev->dev, "invalid pitch value %u\n",
156 mode_cmd->pitches[0]);
157 return ERR_PTR(-EINVAL);
160 if (format->planes == 2) {
161 if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
163 "luma and chroma pitches do not match\n");
164 return ERR_PTR(-EINVAL);
168 return drm_fb_cma_create(dev, file_priv, mode_cmd);
171 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
172 .fb_create = rcar_du_fb_create,
175 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
177 struct drm_device *dev = rcdu->ddev;
178 struct drm_encoder *encoder;
182 drm_mode_config_init(rcdu->ddev);
184 rcdu->ddev->mode_config.min_width = 0;
185 rcdu->ddev->mode_config.min_height = 0;
186 rcdu->ddev->mode_config.max_width = 4095;
187 rcdu->ddev->mode_config.max_height = 2047;
188 rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
190 ret = rcar_du_plane_init(rcdu);
194 for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i) {
195 ret = rcar_du_crtc_create(rcdu, i);
200 rcdu->used_crtcs = 0;
203 for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
204 const struct rcar_du_encoder_data *pdata =
205 &rcdu->pdata->encoders[i];
207 if (pdata->output >= ARRAY_SIZE(rcdu->crtcs)) {
209 "encoder %u references unexisting output %u, skipping\n",
214 switch (pdata->encoder) {
215 case RCAR_DU_ENCODER_VGA:
216 rcar_du_vga_init(rcdu, &pdata->u.vga, pdata->output);
219 case RCAR_DU_ENCODER_LVDS:
220 rcar_du_lvds_init(rcdu, &pdata->u.lvds, pdata->output);
228 /* Set the possible CRTCs and possible clones. All encoders can be
229 * driven by the CRTC associated with the output they're connected to,
230 * as well as by CRTC 0.
232 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
233 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
235 encoder->possible_crtcs = (1 << 0) | (1 << renc->output);
236 encoder->possible_clones = 1 << 0;
239 ret = rcar_du_plane_register(rcdu);
243 drm_kms_helper_poll_init(rcdu->ddev);
245 drm_helper_disable_unused_functions(rcdu->ddev);