2 * rcar_du_kms.c -- R-Car Display Unit Mode Setting
4 * Copyright (C) 2013-2015 Renesas Electronics 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_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <linux/of_graph.h>
24 #include <linux/wait.h>
26 #include "rcar_du_crtc.h"
27 #include "rcar_du_drv.h"
28 #include "rcar_du_encoder.h"
29 #include "rcar_du_kms.h"
30 #include "rcar_du_regs.h"
31 #include "rcar_du_vsp.h"
33 /* -----------------------------------------------------------------------------
37 static const struct rcar_du_format_info rcar_du_format_infos[] = {
39 .fourcc = DRM_FORMAT_RGB565,
42 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
43 .edf = PnDDCR4_EDF_NONE,
45 .fourcc = DRM_FORMAT_ARGB1555,
48 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
49 .edf = PnDDCR4_EDF_NONE,
51 .fourcc = DRM_FORMAT_XRGB1555,
54 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
55 .edf = PnDDCR4_EDF_NONE,
57 .fourcc = DRM_FORMAT_XRGB8888,
60 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
61 .edf = PnDDCR4_EDF_RGB888,
63 .fourcc = DRM_FORMAT_ARGB8888,
66 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
67 .edf = PnDDCR4_EDF_ARGB8888,
69 .fourcc = DRM_FORMAT_UYVY,
72 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
73 .edf = PnDDCR4_EDF_NONE,
75 .fourcc = DRM_FORMAT_YUYV,
78 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
79 .edf = PnDDCR4_EDF_NONE,
81 .fourcc = DRM_FORMAT_NV12,
84 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
85 .edf = PnDDCR4_EDF_NONE,
87 .fourcc = DRM_FORMAT_NV21,
90 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
91 .edf = PnDDCR4_EDF_NONE,
93 .fourcc = DRM_FORMAT_NV16,
96 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
97 .edf = PnDDCR4_EDF_NONE,
100 * The following formats are not supported on Gen2 and thus have no
101 * associated .pnmr or .edf settings.
104 .fourcc = DRM_FORMAT_NV61,
108 .fourcc = DRM_FORMAT_YUV420,
112 .fourcc = DRM_FORMAT_YVU420,
116 .fourcc = DRM_FORMAT_YUV422,
120 .fourcc = DRM_FORMAT_YVU422,
124 .fourcc = DRM_FORMAT_YUV444,
128 .fourcc = DRM_FORMAT_YVU444,
134 const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
138 for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
139 if (rcar_du_format_infos[i].fourcc == fourcc)
140 return &rcar_du_format_infos[i];
146 /* -----------------------------------------------------------------------------
150 int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
151 struct drm_mode_create_dumb *args)
153 struct rcar_du_device *rcdu = dev->dev_private;
154 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
158 * The R8A7779 DU requires a 16 pixels pitch alignment as documented,
159 * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
161 if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
164 align = 16 * args->bpp / 8;
166 args->pitch = roundup(min_pitch, align);
168 return drm_gem_cma_dumb_create_internal(file, dev, args);
171 static struct drm_framebuffer *
172 rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
173 const struct drm_mode_fb_cmd2 *mode_cmd)
175 struct rcar_du_device *rcdu = dev->dev_private;
176 const struct rcar_du_format_info *format;
177 unsigned int max_pitch;
182 format = rcar_du_format_info(mode_cmd->pixel_format);
183 if (format == NULL) {
184 dev_dbg(dev->dev, "unsupported pixel format %08x\n",
185 mode_cmd->pixel_format);
186 return ERR_PTR(-EINVAL);
190 * The pitch and alignment constraints are expressed in pixels on the
191 * hardware side and in bytes in the DRM API.
193 bpp = format->planes == 1 ? format->bpp / 8 : 1;
194 max_pitch = 4096 * bpp;
196 if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
201 if (mode_cmd->pitches[0] & (align - 1) ||
202 mode_cmd->pitches[0] >= max_pitch) {
203 dev_dbg(dev->dev, "invalid pitch value %u\n",
204 mode_cmd->pitches[0]);
205 return ERR_PTR(-EINVAL);
208 for (i = 1; i < format->planes; ++i) {
209 if (mode_cmd->pitches[i] != mode_cmd->pitches[0]) {
211 "luma and chroma pitches do not match\n");
212 return ERR_PTR(-EINVAL);
216 return drm_gem_fb_create(dev, file_priv, mode_cmd);
219 static void rcar_du_output_poll_changed(struct drm_device *dev)
221 struct rcar_du_device *rcdu = dev->dev_private;
223 drm_fbdev_cma_hotplug_event(rcdu->fbdev);
226 /* -----------------------------------------------------------------------------
227 * Atomic Check and Update
230 static int rcar_du_atomic_check(struct drm_device *dev,
231 struct drm_atomic_state *state)
233 struct rcar_du_device *rcdu = dev->dev_private;
236 ret = drm_atomic_helper_check_modeset(dev, state);
240 ret = drm_atomic_normalize_zpos(dev, state);
244 ret = drm_atomic_helper_check_planes(dev, state);
248 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
251 return rcar_du_atomic_check_planes(dev, state);
254 static void rcar_du_atomic_commit_tail(struct drm_atomic_state *old_state)
256 struct drm_device *dev = old_state->dev;
258 /* Apply the atomic update. */
259 drm_atomic_helper_commit_modeset_disables(dev, old_state);
260 drm_atomic_helper_commit_planes(dev, old_state,
261 DRM_PLANE_COMMIT_ACTIVE_ONLY);
262 drm_atomic_helper_commit_modeset_enables(dev, old_state);
264 drm_atomic_helper_commit_hw_done(old_state);
265 drm_atomic_helper_wait_for_flip_done(dev, old_state);
267 drm_atomic_helper_cleanup_planes(dev, old_state);
270 /* -----------------------------------------------------------------------------
274 static const struct drm_mode_config_helper_funcs rcar_du_mode_config_helper = {
275 .atomic_commit_tail = rcar_du_atomic_commit_tail,
278 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
279 .fb_create = rcar_du_fb_create,
280 .output_poll_changed = rcar_du_output_poll_changed,
281 .atomic_check = rcar_du_atomic_check,
282 .atomic_commit = drm_atomic_helper_commit,
285 static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
286 enum rcar_du_output output,
287 struct of_endpoint *ep)
289 struct device_node *connector = NULL;
290 struct device_node *encoder = NULL;
291 struct device_node *ep_node = NULL;
292 struct device_node *entity_ep_node;
293 struct device_node *entity;
297 * Locate the connected entity and infer its type from the number of
300 entity = of_graph_get_remote_port_parent(ep->local_node);
302 dev_dbg(rcdu->dev, "unconnected endpoint %pOF, skipping\n",
307 if (!of_device_is_available(entity)) {
309 "connected entity %pOF is disabled, skipping\n",
314 entity_ep_node = of_graph_get_remote_endpoint(ep->local_node);
316 for_each_endpoint_of_node(entity, ep_node) {
317 if (ep_node == entity_ep_node)
321 * We've found one endpoint other than the input, this must
322 * be an encoder. Locate the connector.
325 connector = of_graph_get_remote_port_parent(ep_node);
326 of_node_put(ep_node);
330 "no connector for encoder %pOF, skipping\n",
332 of_node_put(entity_ep_node);
333 of_node_put(encoder);
340 of_node_put(entity_ep_node);
344 "no encoder found for endpoint %pOF, skipping\n",
349 ret = rcar_du_encoder_init(rcdu, output, encoder, connector);
350 if (ret && ret != -EPROBE_DEFER)
352 "failed to initialize encoder %pOF on output %u (%d), skipping\n",
353 encoder, output, ret);
355 of_node_put(encoder);
356 of_node_put(connector);
361 static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
363 struct device_node *np = rcdu->dev->of_node;
364 struct device_node *ep_node;
365 unsigned int num_encoders = 0;
368 * Iterate over the endpoints and create one encoder for each output
371 for_each_endpoint_of_node(np, ep_node) {
372 enum rcar_du_output output;
373 struct of_endpoint ep;
377 ret = of_graph_parse_endpoint(ep_node, &ep);
379 of_node_put(ep_node);
383 /* Find the output route corresponding to the port number. */
384 for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
385 if (rcdu->info->routes[i].possible_crtcs &&
386 rcdu->info->routes[i].port == ep.port) {
392 if (i == RCAR_DU_OUTPUT_MAX) {
394 "port %u references unexisting output, skipping\n",
399 /* Process the output pipeline. */
400 ret = rcar_du_encoders_init_one(rcdu, output, &ep);
402 if (ret == -EPROBE_DEFER) {
403 of_node_put(ep_node);
416 static int rcar_du_properties_init(struct rcar_du_device *rcdu)
419 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
420 if (rcdu->props.alpha == NULL)
424 * The color key is expressed as an RGB888 triplet stored in a 32-bit
425 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
426 * or enable source color keying (1).
428 rcdu->props.colorkey =
429 drm_property_create_range(rcdu->ddev, 0, "colorkey",
431 if (rcdu->props.colorkey == NULL)
437 static int rcar_du_vsps_init(struct rcar_du_device *rcdu)
439 const struct device_node *np = rcdu->dev->of_node;
440 struct of_phandle_args args;
442 struct device_node *np;
443 unsigned int crtcs_mask;
444 } vsps[RCAR_DU_MAX_VSPS] = { { 0, }, };
445 unsigned int vsps_count = 0;
451 * First parse the DT vsps property to populate the list of VSPs. Each
452 * entry contains a pointer to the VSP DT node and a bitmask of the
453 * connected DU CRTCs.
455 cells = of_property_count_u32_elems(np, "vsps") / rcdu->num_crtcs - 1;
459 for (i = 0; i < rcdu->num_crtcs; ++i) {
462 ret = of_parse_phandle_with_fixed_args(np, "vsps", cells, i,
468 * Add the VSP to the list or update the corresponding existing
469 * entry if the VSP has already been added.
471 for (j = 0; j < vsps_count; ++j) {
472 if (vsps[j].np == args.np)
477 of_node_put(args.np);
479 vsps[vsps_count++].np = args.np;
481 vsps[j].crtcs_mask |= BIT(i);
483 /* Store the VSP pointer and pipe index in the CRTC. */
484 rcdu->crtcs[i].vsp = &rcdu->vsps[j];
485 rcdu->crtcs[i].vsp_pipe = cells >= 1 ? args.args[0] : 0;
489 * Then initialize all the VSPs from the node pointers and CRTCs bitmask
490 * computed previously.
492 for (i = 0; i < vsps_count; ++i) {
493 struct rcar_du_vsp *vsp = &rcdu->vsps[i];
498 ret = rcar_du_vsp_init(vsp, vsps[i].np, vsps[i].crtcs_mask);
506 for (i = 0; i < ARRAY_SIZE(vsps); ++i)
507 of_node_put(vsps[i].np);
512 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
514 static const unsigned int mmio_offsets[] = {
515 DU0_REG_OFFSET, DU2_REG_OFFSET
518 struct drm_device *dev = rcdu->ddev;
519 struct drm_encoder *encoder;
520 struct drm_fbdev_cma *fbdev;
521 unsigned int num_encoders;
522 unsigned int num_groups;
526 drm_mode_config_init(dev);
528 dev->mode_config.min_width = 0;
529 dev->mode_config.min_height = 0;
530 dev->mode_config.max_width = 4095;
531 dev->mode_config.max_height = 2047;
532 dev->mode_config.funcs = &rcar_du_mode_config_funcs;
533 dev->mode_config.helper_private = &rcar_du_mode_config_helper;
535 rcdu->num_crtcs = rcdu->info->num_crtcs;
537 ret = rcar_du_properties_init(rcdu);
542 * Initialize vertical blanking interrupts handling. Start with vblank
543 * disabled for all CRTCs.
545 ret = drm_vblank_init(dev, (1 << rcdu->info->num_crtcs) - 1);
549 /* Initialize the groups. */
550 num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
552 for (i = 0; i < num_groups; ++i) {
553 struct rcar_du_group *rgrp = &rcdu->groups[i];
555 mutex_init(&rgrp->lock);
558 rgrp->mmio_offset = mmio_offsets[i];
560 rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U);
563 * If we have more than one CRTCs in this group pre-associate
564 * the low-order planes with CRTC 0 and the high-order planes
565 * with CRTC 1 to minimize flicker occurring when the
566 * association is changed.
568 rgrp->dptsr_planes = rgrp->num_crtcs > 1
569 ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
572 if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
573 ret = rcar_du_planes_init(rgrp);
579 /* Initialize the compositors. */
580 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
581 ret = rcar_du_vsps_init(rcdu);
586 /* Create the CRTCs. */
587 for (i = 0; i < rcdu->num_crtcs; ++i) {
588 struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
590 ret = rcar_du_crtc_create(rgrp, i);
595 /* Initialize the encoders. */
596 ret = rcar_du_encoders_init(rcdu);
601 dev_err(rcdu->dev, "error: no encoder could be initialized\n");
608 * Set the possible CRTCs and possible clones. There's always at least
609 * one way for all encoders to clone each other, set all bits in the
610 * possible clones field.
612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
613 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
614 const struct rcar_du_output_routing *route =
615 &rcdu->info->routes[renc->output];
617 encoder->possible_crtcs = route->possible_crtcs;
618 encoder->possible_clones = (1 << num_encoders) - 1;
621 drm_mode_config_reset(dev);
623 drm_kms_helper_poll_init(dev);
625 if (dev->mode_config.num_connector) {
626 fbdev = drm_fbdev_cma_init(dev, 32,
627 dev->mode_config.num_connector);
629 return PTR_ERR(fbdev);
634 "no connector found, disabling fbdev emulation\n");