blob: a8eef167d51a14a3752328ee153d71a521359e58 [file] [log] [blame]
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02001/*
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 Pinchart6978f122013-06-15 15:02:12 +020022#include "rcar_du_encoder.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020023#include "rcar_du_kms.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020024#include "rcar_du_regs.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020025
26/* -----------------------------------------------------------------------------
27 * Format helpers
28 */
29
30static const struct rcar_du_format_info rcar_du_format_infos[] = {
31 {
32 .fourcc = DRM_FORMAT_RGB565,
33 .bpp = 16,
34 .planes = 1,
35 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
36 .edf = PnDDCR4_EDF_NONE,
37 }, {
38 .fourcc = DRM_FORMAT_ARGB1555,
39 .bpp = 16,
40 .planes = 1,
41 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
42 .edf = PnDDCR4_EDF_NONE,
43 }, {
44 .fourcc = DRM_FORMAT_XRGB1555,
45 .bpp = 16,
46 .planes = 1,
47 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
48 .edf = PnDDCR4_EDF_NONE,
49 }, {
50 .fourcc = DRM_FORMAT_XRGB8888,
51 .bpp = 32,
52 .planes = 1,
53 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
54 .edf = PnDDCR4_EDF_RGB888,
55 }, {
56 .fourcc = DRM_FORMAT_ARGB8888,
57 .bpp = 32,
58 .planes = 1,
59 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
60 .edf = PnDDCR4_EDF_ARGB8888,
61 }, {
62 .fourcc = DRM_FORMAT_UYVY,
63 .bpp = 16,
64 .planes = 1,
65 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
66 .edf = PnDDCR4_EDF_NONE,
67 }, {
68 .fourcc = DRM_FORMAT_YUYV,
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_NV12,
75 .bpp = 12,
76 .planes = 2,
77 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
78 .edf = PnDDCR4_EDF_NONE,
79 }, {
80 .fourcc = DRM_FORMAT_NV21,
81 .bpp = 12,
82 .planes = 2,
83 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
84 .edf = PnDDCR4_EDF_NONE,
85 }, {
86 /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
87 .fourcc = DRM_FORMAT_NV16,
88 .bpp = 16,
89 .planes = 2,
90 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
91 .edf = PnDDCR4_EDF_NONE,
92 },
93};
94
95const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
96{
97 unsigned int i;
98
99 for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
100 if (rcar_du_format_infos[i].fourcc == fourcc)
101 return &rcar_du_format_infos[i];
102 }
103
104 return NULL;
105}
106
107/* -----------------------------------------------------------------------------
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200108 * Frame buffer
109 */
110
Laurent Pinchart59e32642013-07-04 20:05:51 +0200111int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
112 struct drm_mode_create_dumb *args)
113{
114 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
115 unsigned int align;
116
117 /* The pitch must be aligned to a 16 pixels boundary. */
118 align = 16 * args->bpp / 8;
119 args->pitch = roundup(max(args->pitch, min_pitch), align);
120
121 return drm_gem_cma_dumb_create(file, dev, args);
122}
123
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200124static struct drm_framebuffer *
125rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
126 struct drm_mode_fb_cmd2 *mode_cmd)
127{
128 const struct rcar_du_format_info *format;
Laurent Pinchart59e32642013-07-04 20:05:51 +0200129 unsigned int align;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200130
131 format = rcar_du_format_info(mode_cmd->pixel_format);
132 if (format == NULL) {
133 dev_dbg(dev->dev, "unsupported pixel format %08x\n",
134 mode_cmd->pixel_format);
135 return ERR_PTR(-EINVAL);
136 }
137
Laurent Pinchart59e32642013-07-04 20:05:51 +0200138 align = 16 * format->bpp / 8;
139
140 if (mode_cmd->pitches[0] & (align - 1) ||
141 mode_cmd->pitches[0] >= 8192) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200142 dev_dbg(dev->dev, "invalid pitch value %u\n",
143 mode_cmd->pitches[0]);
144 return ERR_PTR(-EINVAL);
145 }
146
147 if (format->planes == 2) {
148 if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
149 dev_dbg(dev->dev,
150 "luma and chroma pitches do not match\n");
151 return ERR_PTR(-EINVAL);
152 }
153 }
154
155 return drm_fb_cma_create(dev, file_priv, mode_cmd);
156}
157
158static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
159 .fb_create = rcar_du_fb_create,
160};
161
162int rcar_du_modeset_init(struct rcar_du_device *rcdu)
163{
164 struct drm_device *dev = rcdu->ddev;
165 struct drm_encoder *encoder;
166 unsigned int i;
167 int ret;
168
169 drm_mode_config_init(rcdu->ddev);
170
171 rcdu->ddev->mode_config.min_width = 0;
172 rcdu->ddev->mode_config.min_height = 0;
173 rcdu->ddev->mode_config.max_width = 4095;
174 rcdu->ddev->mode_config.max_height = 2047;
175 rcdu->ddev->mode_config.funcs = &rcar_du_mode_config_funcs;
176
177 ret = rcar_du_plane_init(rcdu);
178 if (ret < 0)
179 return ret;
180
Laurent Pinchart3463ff62013-07-04 20:05:50 +0200181 for (i = 0; i < ARRAY_SIZE(rcdu->crtcs); ++i) {
182 ret = rcar_du_crtc_create(rcdu, i);
183 if (ret < 0)
184 return ret;
185 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200186
187 rcdu->used_crtcs = 0;
188 rcdu->num_crtcs = i;
189
190 for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
191 const struct rcar_du_encoder_data *pdata =
192 &rcdu->pdata->encoders[i];
193
Laurent Pinchart91947312013-06-16 16:25:35 +0200194 if (pdata->type == RCAR_DU_ENCODER_UNUSED)
Laurent Pinchart6978f122013-06-15 15:02:12 +0200195 continue;
196
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200197 if (pdata->output >= ARRAY_SIZE(rcdu->crtcs)) {
198 dev_warn(rcdu->dev,
199 "encoder %u references unexisting output %u, skipping\n",
200 i, pdata->output);
201 continue;
202 }
203
Laurent Pinchart91947312013-06-16 16:25:35 +0200204 rcar_du_encoder_init(rcdu, pdata->type, pdata->output, pdata);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200205 }
206
207 /* Set the possible CRTCs and possible clones. All encoders can be
208 * driven by the CRTC associated with the output they're connected to,
209 * as well as by CRTC 0.
210 */
211 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
212 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
213
214 encoder->possible_crtcs = (1 << 0) | (1 << renc->output);
215 encoder->possible_clones = 1 << 0;
216 }
217
218 ret = rcar_du_plane_register(rcdu);
219 if (ret < 0)
220 return ret;
221
222 drm_kms_helper_poll_init(rcdu->ddev);
223
224 drm_helper_disable_unused_functions(rcdu->ddev);
225
226 return 0;
227}