blob: a87edfac111f853056e718f646a6624559fe929a [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 Pinchart90374b52013-06-17 13:48:27 +020024#include "rcar_du_lvdsenc.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020025#include "rcar_du_regs.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020026
27/* -----------------------------------------------------------------------------
28 * Format helpers
29 */
30
31static 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
96const 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 Pinchart4bf8e192013-06-19 13:54:11 +0200109 * Frame buffer
110 */
111
Laurent Pinchart59e32642013-07-04 20:05:51 +0200112int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
113 struct drm_mode_create_dumb *args)
114{
Laurent Pinchart9e2d2de2013-06-14 20:52:52 +0200115 struct rcar_du_device *rcdu = dev->dev_private;
Laurent Pinchart59e32642013-07-04 20:05:51 +0200116 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
117 unsigned int align;
118
Laurent Pinchart9e2d2de2013-06-14 20:52:52 +0200119 /* 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 Pincharte8355e02013-11-13 13:33:45 +0100122 if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
Laurent Pinchart9e2d2de2013-06-14 20:52:52 +0200123 align = 128;
124 else
125 align = 16 * args->bpp / 8;
126
Laurent Pinchart59e32642013-07-04 20:05:51 +0200127 args->pitch = roundup(max(args->pitch, min_pitch), align);
128
129 return drm_gem_cma_dumb_create(file, dev, args);
130}
131
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200132static struct drm_framebuffer *
133rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
134 struct drm_mode_fb_cmd2 *mode_cmd)
135{
Laurent Pinchart9e2d2de2013-06-14 20:52:52 +0200136 struct rcar_du_device *rcdu = dev->dev_private;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200137 const struct rcar_du_format_info *format;
Laurent Pinchart59e32642013-07-04 20:05:51 +0200138 unsigned int align;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200139
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 Pincharte8355e02013-11-13 13:33:45 +0100147 if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
Laurent Pinchart9e2d2de2013-06-14 20:52:52 +0200148 align = 128;
149 else
150 align = 16 * format->bpp / 8;
Laurent Pinchart59e32642013-07-04 20:05:51 +0200151
152 if (mode_cmd->pitches[0] & (align - 1) ||
153 mode_cmd->pitches[0] >= 8192) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200154 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 Pinchart3864c6f2013-03-14 22:45:22 +0100170static 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 Pinchart4bf8e192013-06-19 13:54:11 +0200177static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
178 .fb_create = rcar_du_fb_create,
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100179 .output_poll_changed = rcar_du_output_poll_changed,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200180};
181
182int rcar_du_modeset_init(struct rcar_du_device *rcdu)
183{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200184 static const unsigned int mmio_offsets[] = {
185 DU0_REG_OFFSET, DU2_REG_OFFSET
186 };
187
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200188 struct drm_device *dev = rcdu->ddev;
189 struct drm_encoder *encoder;
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100190 struct drm_fbdev_cma *fbdev;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200191 unsigned int num_groups;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200192 unsigned int i;
193 int ret;
194
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100195 drm_mode_config_init(dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200196
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100197 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 Pinchart4bf8e192013-06-19 13:54:11 +0200202
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200203 rcdu->num_crtcs = rcdu->info->num_crtcs;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200204
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200205 /* Initialize the groups. */
206 num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200207
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200208 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 Pinchart3463ff62013-07-04 20:05:50 +0200216 if (ret < 0)
217 return ret;
218 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200219
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200220 /* Create the CRTCs. */
221 for (i = 0; i < rcdu->num_crtcs; ++i) {
222 struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200223
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200224 ret = rcar_du_crtc_create(rgrp, i);
225 if (ret < 0)
226 return ret;
227 }
228
229 /* Initialize the encoders. */
Laurent Pinchart90374b52013-06-17 13:48:27 +0200230 ret = rcar_du_lvdsenc_init(rcdu);
231 if (ret < 0)
232 return ret;
233
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200234 for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
235 const struct rcar_du_encoder_data *pdata =
236 &rcdu->pdata->encoders[i];
Laurent Pinchartef67a902013-06-17 03:13:11 +0200237 const struct rcar_du_output_routing *route =
238 &rcdu->info->routes[pdata->output];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200239
Laurent Pinchart91947312013-06-16 16:25:35 +0200240 if (pdata->type == RCAR_DU_ENCODER_UNUSED)
Laurent Pinchart6978f122013-06-15 15:02:12 +0200241 continue;
242
Laurent Pinchartef67a902013-06-17 03:13:11 +0200243 if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
244 route->possible_crtcs == 0) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200245 dev_warn(rcdu->dev,
246 "encoder %u references unexisting output %u, skipping\n",
247 i, pdata->output);
248 continue;
249 }
250
Laurent Pinchartd9961b22014-04-01 13:03:52 +0200251 ret = rcar_du_encoder_init(rcdu, pdata->type, pdata->output,
252 pdata);
253 if (ret < 0)
254 return ret;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200255 }
256
Laurent Pinchartef67a902013-06-17 03:13:11 +0200257 /* 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 Pinchart4bf8e192013-06-19 13:54:11 +0200260 */
261 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
262 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200263 const struct rcar_du_output_routing *route =
264 &rcdu->info->routes[renc->output];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200265
Laurent Pinchartef67a902013-06-17 03:13:11 +0200266 encoder->possible_crtcs = route->possible_crtcs;
267 encoder->possible_clones = (1 << rcdu->pdata->num_encoders) - 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200268 }
269
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200270 /* 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 Pinchart4bf8e192013-06-19 13:54:11 +0200276
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100277 drm_kms_helper_poll_init(dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200278
Laurent Pinchart3864c6f2013-03-14 22:45:22 +0100279 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 Pinchart4bf8e192013-06-19 13:54:11 +0200291
292 return 0;
293}