2 * rcar_du_kms.c -- R-Car Display Unit Mode Setting
4 * Copyright (C) 2013-2014 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>
22 #include <linux/of_graph.h>
23 #include <linux/wait.h>
25 #include "rcar_du_crtc.h"
26 #include "rcar_du_drv.h"
27 #include "rcar_du_encoder.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_lvdsenc.h"
30 #include "rcar_du_regs.h"
32 /* -----------------------------------------------------------------------------
36 static const struct rcar_du_format_info rcar_du_format_infos
[] = {
38 .fourcc
= DRM_FORMAT_RGB565
,
41 .pnmr
= PnMR_SPIM_TP
| PnMR_DDDF_16BPP
,
42 .edf
= PnDDCR4_EDF_NONE
,
44 .fourcc
= DRM_FORMAT_ARGB1555
,
47 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_ARGB
,
48 .edf
= PnDDCR4_EDF_NONE
,
50 .fourcc
= DRM_FORMAT_XRGB1555
,
53 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_ARGB
,
54 .edf
= PnDDCR4_EDF_NONE
,
56 .fourcc
= DRM_FORMAT_XRGB8888
,
59 .pnmr
= PnMR_SPIM_TP
| PnMR_DDDF_16BPP
,
60 .edf
= PnDDCR4_EDF_RGB888
,
62 .fourcc
= DRM_FORMAT_ARGB8888
,
65 .pnmr
= PnMR_SPIM_ALP
| PnMR_DDDF_16BPP
,
66 .edf
= PnDDCR4_EDF_ARGB8888
,
68 .fourcc
= DRM_FORMAT_UYVY
,
71 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
72 .edf
= PnDDCR4_EDF_NONE
,
74 .fourcc
= DRM_FORMAT_YUYV
,
77 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
78 .edf
= PnDDCR4_EDF_NONE
,
80 .fourcc
= DRM_FORMAT_NV12
,
83 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
84 .edf
= PnDDCR4_EDF_NONE
,
86 .fourcc
= DRM_FORMAT_NV21
,
89 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
90 .edf
= PnDDCR4_EDF_NONE
,
92 /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
93 .fourcc
= DRM_FORMAT_NV16
,
96 .pnmr
= PnMR_SPIM_TP_OFF
| PnMR_DDDF_YC
,
97 .edf
= PnDDCR4_EDF_NONE
,
101 const struct rcar_du_format_info
*rcar_du_format_info(u32 fourcc
)
105 for (i
= 0; i
< ARRAY_SIZE(rcar_du_format_infos
); ++i
) {
106 if (rcar_du_format_infos
[i
].fourcc
== fourcc
)
107 return &rcar_du_format_infos
[i
];
113 /* -----------------------------------------------------------------------------
117 int rcar_du_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
118 struct drm_mode_create_dumb
*args
)
120 struct rcar_du_device
*rcdu
= dev
->dev_private
;
121 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
124 /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
125 * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
127 if (rcar_du_needs(rcdu
, RCAR_DU_QUIRK_ALIGN_128B
))
130 align
= 16 * args
->bpp
/ 8;
132 args
->pitch
= roundup(min_pitch
, align
);
134 return drm_gem_cma_dumb_create_internal(file
, dev
, args
);
137 static struct drm_framebuffer
*
138 rcar_du_fb_create(struct drm_device
*dev
, struct drm_file
*file_priv
,
139 const struct drm_mode_fb_cmd2
*mode_cmd
)
141 struct rcar_du_device
*rcdu
= dev
->dev_private
;
142 const struct rcar_du_format_info
*format
;
143 unsigned int max_pitch
;
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
);
155 * The pitch and alignment constraints are expressed in pixels on the
156 * hardware side and in bytes in the DRM API.
158 bpp
= format
->planes
== 2 ? 1 : format
->bpp
/ 8;
159 max_pitch
= 4096 * bpp
;
161 if (rcar_du_needs(rcdu
, RCAR_DU_QUIRK_ALIGN_128B
))
166 if (mode_cmd
->pitches
[0] & (align
- 1) ||
167 mode_cmd
->pitches
[0] >= max_pitch
) {
168 dev_dbg(dev
->dev
, "invalid pitch value %u\n",
169 mode_cmd
->pitches
[0]);
170 return ERR_PTR(-EINVAL
);
173 if (format
->planes
== 2) {
174 if (mode_cmd
->pitches
[1] != mode_cmd
->pitches
[0]) {
176 "luma and chroma pitches do not match\n");
177 return ERR_PTR(-EINVAL
);
181 return drm_fb_cma_create(dev
, file_priv
, mode_cmd
);
184 static void rcar_du_output_poll_changed(struct drm_device
*dev
)
186 struct rcar_du_device
*rcdu
= dev
->dev_private
;
188 drm_fbdev_cma_hotplug_event(rcdu
->fbdev
);
191 /* -----------------------------------------------------------------------------
192 * Atomic Check and Update
195 static int rcar_du_atomic_check(struct drm_device
*dev
,
196 struct drm_atomic_state
*state
)
200 ret
= drm_atomic_helper_check(dev
, state
);
204 return rcar_du_atomic_check_planes(dev
, state
);
207 struct rcar_du_commit
{
208 struct work_struct work
;
209 struct drm_device
*dev
;
210 struct drm_atomic_state
*state
;
214 static void rcar_du_atomic_complete(struct rcar_du_commit
*commit
)
216 struct drm_device
*dev
= commit
->dev
;
217 struct rcar_du_device
*rcdu
= dev
->dev_private
;
218 struct drm_atomic_state
*old_state
= commit
->state
;
220 /* Apply the atomic update. */
221 drm_atomic_helper_commit_modeset_disables(dev
, old_state
);
222 drm_atomic_helper_commit_modeset_enables(dev
, old_state
);
223 drm_atomic_helper_commit_planes(dev
, old_state
, true);
225 drm_atomic_helper_wait_for_vblanks(dev
, old_state
);
227 drm_atomic_helper_cleanup_planes(dev
, old_state
);
229 drm_atomic_state_free(old_state
);
231 /* Complete the commit, wake up any waiter. */
232 spin_lock(&rcdu
->commit
.wait
.lock
);
233 rcdu
->commit
.pending
&= ~commit
->crtcs
;
234 wake_up_all_locked(&rcdu
->commit
.wait
);
235 spin_unlock(&rcdu
->commit
.wait
.lock
);
240 static void rcar_du_atomic_work(struct work_struct
*work
)
242 struct rcar_du_commit
*commit
=
243 container_of(work
, struct rcar_du_commit
, work
);
245 rcar_du_atomic_complete(commit
);
248 static int rcar_du_atomic_commit(struct drm_device
*dev
,
249 struct drm_atomic_state
*state
, bool async
)
251 struct rcar_du_device
*rcdu
= dev
->dev_private
;
252 struct rcar_du_commit
*commit
;
256 ret
= drm_atomic_helper_prepare_planes(dev
, state
);
260 /* Allocate the commit object. */
261 commit
= kzalloc(sizeof(*commit
), GFP_KERNEL
);
262 if (commit
== NULL
) {
267 INIT_WORK(&commit
->work
, rcar_du_atomic_work
);
269 commit
->state
= state
;
271 /* Wait until all affected CRTCs have completed previous commits and
272 * mark them as pending.
274 for (i
= 0; i
< dev
->mode_config
.num_crtc
; ++i
) {
276 commit
->crtcs
|= 1 << drm_crtc_index(state
->crtcs
[i
]);
279 spin_lock(&rcdu
->commit
.wait
.lock
);
280 ret
= wait_event_interruptible_locked(rcdu
->commit
.wait
,
281 !(rcdu
->commit
.pending
& commit
->crtcs
));
283 rcdu
->commit
.pending
|= commit
->crtcs
;
284 spin_unlock(&rcdu
->commit
.wait
.lock
);
291 /* Swap the state, this is the point of no return. */
292 drm_atomic_helper_swap_state(dev
, state
);
295 schedule_work(&commit
->work
);
297 rcar_du_atomic_complete(commit
);
302 drm_atomic_helper_cleanup_planes(dev
, state
);
306 /* -----------------------------------------------------------------------------
310 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs
= {
311 .fb_create
= rcar_du_fb_create
,
312 .output_poll_changed
= rcar_du_output_poll_changed
,
313 .atomic_check
= rcar_du_atomic_check
,
314 .atomic_commit
= rcar_du_atomic_commit
,
317 static int rcar_du_encoders_init_one(struct rcar_du_device
*rcdu
,
318 enum rcar_du_output output
,
319 struct of_endpoint
*ep
)
321 static const struct {
322 const char *compatible
;
323 enum rcar_du_encoder_type type
;
325 { "adi,adv7123", RCAR_DU_ENCODER_VGA
},
326 { "adi,adv7511w", RCAR_DU_ENCODER_HDMI
},
327 { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS
},
330 enum rcar_du_encoder_type enc_type
= RCAR_DU_ENCODER_NONE
;
331 struct device_node
*connector
= NULL
;
332 struct device_node
*encoder
= NULL
;
333 struct device_node
*ep_node
= NULL
;
334 struct device_node
*entity_ep_node
;
335 struct device_node
*entity
;
339 * Locate the connected entity and infer its type from the number of
342 entity
= of_graph_get_remote_port_parent(ep
->local_node
);
344 dev_dbg(rcdu
->dev
, "unconnected endpoint %s, skipping\n",
345 ep
->local_node
->full_name
);
349 entity_ep_node
= of_parse_phandle(ep
->local_node
, "remote-endpoint", 0);
351 for_each_endpoint_of_node(entity
, ep_node
) {
352 if (ep_node
== entity_ep_node
)
356 * We've found one endpoint other than the input, this must
357 * be an encoder. Locate the connector.
360 connector
= of_graph_get_remote_port_parent(ep_node
);
361 of_node_put(ep_node
);
365 "no connector for encoder %s, skipping\n",
367 of_node_put(entity_ep_node
);
368 of_node_put(encoder
);
375 of_node_put(entity_ep_node
);
379 * If an encoder has been found, get its type based on its
384 for (i
= 0; i
< ARRAY_SIZE(encoders
); ++i
) {
385 if (of_device_is_compatible(encoder
,
386 encoders
[i
].compatible
)) {
387 enc_type
= encoders
[i
].type
;
392 if (i
== ARRAY_SIZE(encoders
)) {
394 "unknown encoder type for %s, skipping\n",
396 of_node_put(encoder
);
397 of_node_put(connector
);
402 * If no encoder has been found the entity must be the
408 ret
= rcar_du_encoder_init(rcdu
, enc_type
, output
, encoder
, connector
);
409 of_node_put(encoder
);
410 of_node_put(connector
);
412 if (ret
&& ret
!= -EPROBE_DEFER
)
414 "failed to initialize encoder %s (%d), skipping\n",
415 encoder
->full_name
, ret
);
420 static int rcar_du_encoders_init(struct rcar_du_device
*rcdu
)
422 struct device_node
*np
= rcdu
->dev
->of_node
;
423 struct device_node
*ep_node
;
424 unsigned int num_encoders
= 0;
427 * Iterate over the endpoints and create one encoder for each output
430 for_each_endpoint_of_node(np
, ep_node
) {
431 enum rcar_du_output output
;
432 struct of_endpoint ep
;
436 ret
= of_graph_parse_endpoint(ep_node
, &ep
);
438 of_node_put(ep_node
);
442 /* Find the output route corresponding to the port number. */
443 for (i
= 0; i
< RCAR_DU_OUTPUT_MAX
; ++i
) {
444 if (rcdu
->info
->routes
[i
].possible_crtcs
&&
445 rcdu
->info
->routes
[i
].port
== ep
.port
) {
451 if (i
== RCAR_DU_OUTPUT_MAX
) {
453 "port %u references unexisting output, skipping\n",
458 /* Process the output pipeline. */
459 ret
= rcar_du_encoders_init_one(rcdu
, output
, &ep
);
461 if (ret
== -EPROBE_DEFER
) {
462 of_node_put(ep_node
);
475 static int rcar_du_properties_init(struct rcar_du_device
*rcdu
)
478 drm_property_create_range(rcdu
->ddev
, 0, "alpha", 0, 255);
479 if (rcdu
->props
.alpha
== NULL
)
482 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
483 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
484 * or enable source color keying (1).
486 rcdu
->props
.colorkey
=
487 drm_property_create_range(rcdu
->ddev
, 0, "colorkey",
489 if (rcdu
->props
.colorkey
== NULL
)
493 drm_property_create_range(rcdu
->ddev
, 0, "zpos", 1, 7);
494 if (rcdu
->props
.zpos
== NULL
)
500 int rcar_du_modeset_init(struct rcar_du_device
*rcdu
)
502 static const unsigned int mmio_offsets
[] = {
503 DU0_REG_OFFSET
, DU2_REG_OFFSET
506 struct drm_device
*dev
= rcdu
->ddev
;
507 struct drm_encoder
*encoder
;
508 struct drm_fbdev_cma
*fbdev
;
509 unsigned int num_encoders
;
510 unsigned int num_groups
;
514 drm_mode_config_init(dev
);
516 dev
->mode_config
.min_width
= 0;
517 dev
->mode_config
.min_height
= 0;
518 dev
->mode_config
.max_width
= 4095;
519 dev
->mode_config
.max_height
= 2047;
520 dev
->mode_config
.funcs
= &rcar_du_mode_config_funcs
;
522 rcdu
->num_crtcs
= rcdu
->info
->num_crtcs
;
524 ret
= rcar_du_properties_init(rcdu
);
528 /* Initialize the groups. */
529 num_groups
= DIV_ROUND_UP(rcdu
->num_crtcs
, 2);
531 for (i
= 0; i
< num_groups
; ++i
) {
532 struct rcar_du_group
*rgrp
= &rcdu
->groups
[i
];
534 mutex_init(&rgrp
->lock
);
537 rgrp
->mmio_offset
= mmio_offsets
[i
];
539 rgrp
->num_crtcs
= min(rcdu
->num_crtcs
- 2 * i
, 2U);
541 /* If we have more than one CRTCs in this group pre-associate
542 * planes 0-3 with CRTC 0 and planes 4-7 with CRTC 1 to minimize
543 * flicker occurring when the association is changed.
545 rgrp
->dptsr_planes
= rgrp
->num_crtcs
> 1 ? 0xf0 : 0;
547 ret
= rcar_du_planes_init(rgrp
);
552 /* Create the CRTCs. */
553 for (i
= 0; i
< rcdu
->num_crtcs
; ++i
) {
554 struct rcar_du_group
*rgrp
= &rcdu
->groups
[i
/ 2];
556 ret
= rcar_du_crtc_create(rgrp
, i
);
561 /* Initialize the encoders. */
562 ret
= rcar_du_lvdsenc_init(rcdu
);
566 ret
= rcar_du_encoders_init(rcdu
);
571 dev_err(rcdu
->dev
, "error: no encoder could be initialized\n");
577 /* Set the possible CRTCs and possible clones. There's always at least
578 * one way for all encoders to clone each other, set all bits in the
579 * possible clones field.
581 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
582 struct rcar_du_encoder
*renc
= to_rcar_encoder(encoder
);
583 const struct rcar_du_output_routing
*route
=
584 &rcdu
->info
->routes
[renc
->output
];
586 encoder
->possible_crtcs
= route
->possible_crtcs
;
587 encoder
->possible_clones
= (1 << num_encoders
) - 1;
590 drm_mode_config_reset(dev
);
592 drm_kms_helper_poll_init(dev
);
594 if (dev
->mode_config
.num_connector
) {
595 fbdev
= drm_fbdev_cma_init(dev
, 32, dev
->mode_config
.num_crtc
,
596 dev
->mode_config
.num_connector
);
598 return PTR_ERR(fbdev
);
603 "no connector found, disabling fbdev emulation\n");