1 // SPDX-License-Identifier: GPL-2.0+
3 * rcar_du_crtc.c -- R-Car Display Unit CRTCs
5 * Copyright (C) 2013-2015 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <linux/clk.h>
11 #include <linux/mutex.h>
12 #include <linux/platform_device.h>
13 #include <linux/sys_soc.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_bridge.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_fb_cma_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_plane_helper.h>
23 #include <drm/drm_vblank.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_plane.h"
31 #include "rcar_du_regs.h"
32 #include "rcar_du_vsp.h"
33 #include "rcar_lvds.h"
35 static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
37 struct rcar_du_device *rcdu = rcrtc->dev;
39 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
42 static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
44 struct rcar_du_device *rcdu = rcrtc->dev;
46 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
49 static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
51 struct rcar_du_device *rcdu = rcrtc->dev;
53 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
54 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
57 static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
59 struct rcar_du_device *rcdu = rcrtc->dev;
61 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
62 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
65 void rcar_du_crtc_dsysr_clr_set(struct rcar_du_crtc *rcrtc, u32 clr, u32 set)
67 struct rcar_du_device *rcdu = rcrtc->dev;
69 rcrtc->dsysr = (rcrtc->dsysr & ~clr) | set;
70 rcar_du_write(rcdu, rcrtc->mmio_offset + DSYSR, rcrtc->dsysr);
73 /* -----------------------------------------------------------------------------
84 static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
85 struct dpll_info *dpll,
89 unsigned long best_diff = (unsigned long)-1;
96 * fin fvco fout fclkout
97 * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
100 * +---------------- [1/N] <------------+
102 * fclkout = fvco / P / FDPLL -- (1)
106 * fvco = fin * P * N / M -- (2)
108 * (1) + (2) indicates
110 * fclkout = fin * N / M / FDPLL
115 * FDPLL : (fdpll + 1)
117 * 2kHz < fvco < 4096MHz
119 * To minimize the jitter,
120 * N : as large as possible
121 * M : as small as possible
123 for (m = 0; m < 4; m++) {
124 for (n = 119; n > 38; n--) {
126 * This code only runs on 64-bit architectures, the
127 * unsigned long type can thus be used for 64-bit
128 * computation. It will still compile without any
129 * warning on 32-bit architectures.
131 * To optimize calculations, use fout instead of fvco
132 * to verify the VCO frequency constraint.
134 unsigned long fout = input * (n + 1) / (m + 1);
136 if (fout < 1000 || fout > 2048 * 1000 * 1000U)
139 for (fdpll = 1; fdpll < 32; fdpll++) {
140 unsigned long output;
142 output = fout / (fdpll + 1);
143 if (output >= 400 * 1000 * 1000)
146 diff = abs((long)output - (long)target);
147 if (best_diff > diff) {
152 dpll->output = output;
162 dev_dbg(rcrtc->dev->dev,
163 "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
164 dpll->output, dpll->fdpll, dpll->n, dpll->m, best_diff);
167 struct du_clk_params {
174 static void rcar_du_escr_divider(struct clk *clk, unsigned long target,
175 u32 escr, struct du_clk_params *params)
182 * If the target rate has already been achieved perfectly we can't do
185 if (params->diff == 0)
189 * Compute the input clock rate and internal divisor values to obtain
190 * the clock rate closest to the target frequency.
192 rate = clk_round_rate(clk, target);
193 div = clamp(DIV_ROUND_CLOSEST(rate, target), 1UL, 64UL) - 1;
194 diff = abs(rate / (div + 1) - target);
197 * Store the parameters if the resulting frequency is better than any
198 * previously calculated value.
200 if (diff < params->diff) {
204 params->escr = escr | div;
208 static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
209 { .soc_id = "r8a7795", .revision = "ES1.*" },
213 static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
215 const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
216 struct rcar_du_device *rcdu = rcrtc->dev;
217 unsigned long mode_clock = mode->clock * 1000;
218 unsigned int hdse_offset;
222 if (rcdu->info->dpll_mask & (1 << rcrtc->index)) {
223 unsigned long target = mode_clock;
224 struct dpll_info dpll = { 0 };
225 unsigned long extclk;
230 * DU channels that have a display PLL can't use the internal
231 * system clock, and have no internal clock divider.
235 * The H3 ES1.x exhibits dot clock duty cycle stability issues.
236 * We can work around them by configuring the DPLL to twice the
237 * desired frequency, coupled with a /2 post-divider. Restrict
238 * the workaround to H3 ES1.x as ES2.0 and all other SoCs have
239 * no post-divider when a display PLL is present (as shown by
240 * the workaround breaking HDMI output on M3-W during testing).
242 if (soc_device_match(rcar_du_r8a7795_es1)) {
247 extclk = clk_get_rate(rcrtc->extclock);
248 rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
250 dpllcr = DPLLCR_CODE | DPLLCR_CLKE
251 | DPLLCR_FDPLL(dpll.fdpll)
252 | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
255 if (rcrtc->index == 1)
256 dpllcr |= DPLLCR_PLCS1
257 | DPLLCR_INCS_DOTCLKIN1;
259 dpllcr |= DPLLCR_PLCS0
260 | DPLLCR_INCS_DOTCLKIN0;
262 rcar_du_group_write(rcrtc->group, DPLLCR, dpllcr);
264 escr = ESCR_DCLKSEL_DCLKIN | div;
265 } else if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index)) {
267 * Use the LVDS PLL output as the dot clock when outputting to
268 * the LVDS encoder on an SoC that supports this clock routing
269 * option. We use the clock directly in that case, without any
270 * additional divider.
272 escr = ESCR_DCLKSEL_DCLKIN;
274 struct du_clk_params params = { .diff = (unsigned long)-1 };
276 rcar_du_escr_divider(rcrtc->clock, mode_clock,
277 ESCR_DCLKSEL_CLKS, ¶ms);
279 rcar_du_escr_divider(rcrtc->extclock, mode_clock,
280 ESCR_DCLKSEL_DCLKIN, ¶ms);
282 dev_dbg(rcrtc->dev->dev, "mode clock %lu %s rate %lu\n",
283 mode_clock, params.clk == rcrtc->clock ? "cpg" : "ext",
286 clk_set_rate(params.clk, params.rate);
290 dev_dbg(rcrtc->dev->dev, "%s: ESCR 0x%08x\n", __func__, escr);
292 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? ESCR13 : ESCR02, escr);
293 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? OTAR13 : OTAR02, 0);
295 /* Signal polarities */
296 dsmr = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
297 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
298 | ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? DSMR_ODEV : 0)
299 | DSMR_DIPM_DISP | DSMR_CSPM;
300 rcar_du_crtc_write(rcrtc, DSMR, dsmr);
303 if (rcrtc->group->cmms_mask & BIT(rcrtc->index % 2))
306 /* Display timings */
307 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start -
309 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
310 mode->hdisplay - hdse_offset);
311 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
312 mode->hsync_start - 1);
313 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
315 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
316 mode->crtc_vsync_end - 2);
317 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
318 mode->crtc_vsync_end +
319 mode->crtc_vdisplay - 2);
320 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
321 mode->crtc_vsync_end +
322 mode->crtc_vsync_start - 1);
323 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
325 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
326 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
329 static unsigned int plane_zpos(struct rcar_du_plane *plane)
331 return plane->plane.state->normalized_zpos;
334 static const struct rcar_du_format_info *
335 plane_format(struct rcar_du_plane *plane)
337 return to_rcar_plane_state(plane->plane.state)->format;
340 static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
342 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
343 struct rcar_du_device *rcdu = rcrtc->dev;
344 unsigned int num_planes = 0;
345 unsigned int dptsr_planes;
346 unsigned int hwplanes = 0;
347 unsigned int prio = 0;
351 for (i = 0; i < rcrtc->group->num_planes; ++i) {
352 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
355 if (plane->plane.state->crtc != &rcrtc->crtc ||
356 !plane->plane.state->visible)
359 /* Insert the plane in the sorted planes array. */
360 for (j = num_planes++; j > 0; --j) {
361 if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
363 planes[j] = planes[j-1];
367 prio += plane_format(plane)->planes * 4;
370 for (i = 0; i < num_planes; ++i) {
371 struct rcar_du_plane *plane = planes[i];
372 struct drm_plane_state *state = plane->plane.state;
373 unsigned int index = to_rcar_plane_state(state)->hwindex;
376 dspr |= (index + 1) << prio;
377 hwplanes |= 1 << index;
379 if (plane_format(plane)->planes == 2) {
380 index = (index + 1) % 8;
383 dspr |= (index + 1) << prio;
384 hwplanes |= 1 << index;
388 /* If VSP+DU integration is enabled the plane assignment is fixed. */
389 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
390 if (rcdu->info->gen < 3) {
391 dspr = (rcrtc->index % 2) + 1;
392 hwplanes = 1 << (rcrtc->index % 2);
394 dspr = (rcrtc->index % 2) ? 3 : 1;
395 hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
400 * Update the planes to display timing and dot clock generator
403 * Updating the DPTSR register requires restarting the CRTC group,
404 * resulting in visible flicker. To mitigate the issue only update the
405 * association if needed by enabled planes. Planes being disabled will
406 * keep their current association.
408 mutex_lock(&rcrtc->group->lock);
410 dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
411 : rcrtc->group->dptsr_planes & ~hwplanes;
413 if (dptsr_planes != rcrtc->group->dptsr_planes) {
414 rcar_du_group_write(rcrtc->group, DPTSR,
415 (dptsr_planes << 16) | dptsr_planes);
416 rcrtc->group->dptsr_planes = dptsr_planes;
418 if (rcrtc->group->used_crtcs)
419 rcar_du_group_restart(rcrtc->group);
422 /* Restart the group if plane sources have changed. */
423 if (rcrtc->group->need_restart)
424 rcar_du_group_restart(rcrtc->group);
426 mutex_unlock(&rcrtc->group->lock);
428 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
432 /* -----------------------------------------------------------------------------
436 void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
438 struct drm_pending_vblank_event *event;
439 struct drm_device *dev = rcrtc->crtc.dev;
442 spin_lock_irqsave(&dev->event_lock, flags);
443 event = rcrtc->event;
445 spin_unlock_irqrestore(&dev->event_lock, flags);
450 spin_lock_irqsave(&dev->event_lock, flags);
451 drm_crtc_send_vblank_event(&rcrtc->crtc, event);
452 wake_up(&rcrtc->flip_wait);
453 spin_unlock_irqrestore(&dev->event_lock, flags);
455 drm_crtc_vblank_put(&rcrtc->crtc);
458 static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
460 struct drm_device *dev = rcrtc->crtc.dev;
464 spin_lock_irqsave(&dev->event_lock, flags);
465 pending = rcrtc->event != NULL;
466 spin_unlock_irqrestore(&dev->event_lock, flags);
471 static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
473 struct rcar_du_device *rcdu = rcrtc->dev;
475 if (wait_event_timeout(rcrtc->flip_wait,
476 !rcar_du_crtc_page_flip_pending(rcrtc),
477 msecs_to_jiffies(50)))
480 dev_warn(rcdu->dev, "page flip timeout\n");
482 rcar_du_crtc_finish_page_flip(rcrtc);
485 /* -----------------------------------------------------------------------------
486 * Color Management Module (CMM)
489 static int rcar_du_cmm_check(struct drm_crtc *crtc,
490 struct drm_crtc_state *state)
492 struct drm_property_blob *drm_lut = state->gamma_lut;
493 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
494 struct device *dev = rcrtc->dev->dev;
499 /* We only accept fully populated LUT tables. */
500 if (drm_color_lut_size(drm_lut) != CM2_LUT_SIZE) {
501 dev_err(dev, "invalid gamma lut size: %zu bytes\n",
509 static void rcar_du_cmm_setup(struct drm_crtc *crtc)
511 struct drm_property_blob *drm_lut = crtc->state->gamma_lut;
512 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
513 struct rcar_cmm_config cmm_config = {};
519 cmm_config.lut.table = (struct drm_color_lut *)drm_lut->data;
521 rcar_cmm_setup(rcrtc->cmm, &cmm_config);
524 /* -----------------------------------------------------------------------------
525 * Start/Stop and Suspend/Resume
528 static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
530 /* Set display off and background to black */
531 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
532 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
534 /* Configure display timings and output routing */
535 rcar_du_crtc_set_display_timing(rcrtc);
536 rcar_du_group_set_routing(rcrtc->group);
538 /* Start with all planes disabled. */
539 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
541 /* Enable the VSP compositor. */
542 if (rcar_du_has(rcrtc->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
543 rcar_du_vsp_enable(rcrtc);
545 /* Turn vertical blanking interrupt reporting on. */
546 drm_crtc_vblank_on(&rcrtc->crtc);
549 static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
554 * Guard against double-get, as the function is called from both the
555 * .atomic_enable() and .atomic_begin() handlers.
557 if (rcrtc->initialized)
560 ret = clk_prepare_enable(rcrtc->clock);
564 ret = clk_prepare_enable(rcrtc->extclock);
568 ret = rcar_du_group_get(rcrtc->group);
572 rcar_du_crtc_setup(rcrtc);
573 rcrtc->initialized = true;
578 clk_disable_unprepare(rcrtc->extclock);
580 clk_disable_unprepare(rcrtc->clock);
584 static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
586 rcar_du_group_put(rcrtc->group);
588 clk_disable_unprepare(rcrtc->extclock);
589 clk_disable_unprepare(rcrtc->clock);
591 rcrtc->initialized = false;
594 static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
599 * Select master sync mode. This enables display operation in master
600 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
603 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
604 rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
605 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
608 rcar_du_group_start_stop(rcrtc->group, true);
611 static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
613 struct rcar_du_device *rcdu = rcrtc->dev;
614 struct drm_crtc *crtc = &rcrtc->crtc;
617 /* Make sure vblank interrupts are enabled. */
618 drm_crtc_vblank_get(crtc);
621 * Disable planes and calculate how many vertical blanking interrupts we
622 * have to wait for. If a vertical blanking interrupt has been triggered
623 * but not processed yet, we don't know whether it occurred before or
624 * after the planes got disabled. We thus have to wait for two vblank
625 * interrupts in that case.
627 spin_lock_irq(&rcrtc->vblank_lock);
628 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
629 status = rcar_du_crtc_read(rcrtc, DSSR);
630 rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
631 spin_unlock_irq(&rcrtc->vblank_lock);
633 if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
634 msecs_to_jiffies(100)))
635 dev_warn(rcdu->dev, "vertical blanking timeout\n");
637 drm_crtc_vblank_put(crtc);
640 static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
642 struct drm_crtc *crtc = &rcrtc->crtc;
645 * Disable all planes and wait for the change to take effect. This is
646 * required as the plane enable registers are updated on vblank, and no
647 * vblank will occur once the CRTC is stopped. Disabling planes when
648 * starting the CRTC thus wouldn't be enough as it would start scanning
649 * out immediately from old frame buffers until the next vblank.
651 * This increases the CRTC stop delay, especially when multiple CRTCs
652 * are stopped in one operation as we now wait for one vblank per CRTC.
653 * Whether this can be improved needs to be researched.
655 rcar_du_crtc_disable_planes(rcrtc);
658 * Disable vertical blanking interrupt reporting. We first need to wait
659 * for page flip completion before stopping the CRTC as userspace
660 * expects page flips to eventually complete.
662 rcar_du_crtc_wait_page_flip(rcrtc);
663 drm_crtc_vblank_off(crtc);
665 /* Disable the VSP compositor. */
666 if (rcar_du_has(rcrtc->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
667 rcar_du_vsp_disable(rcrtc);
670 rcar_cmm_disable(rcrtc->cmm);
673 * Select switch sync mode. This stops display operation and configures
674 * the HSYNC and VSYNC signals as inputs.
676 * TODO: Find another way to stop the display for DUs that don't support
679 if (rcar_du_has(rcrtc->dev, RCAR_DU_FEATURE_TVM_SYNC))
680 rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK,
683 rcar_du_group_start_stop(rcrtc->group, false);
686 /* -----------------------------------------------------------------------------
690 static int rcar_du_crtc_atomic_check(struct drm_crtc *crtc,
691 struct drm_crtc_state *state)
693 struct rcar_du_crtc_state *rstate = to_rcar_crtc_state(state);
694 struct drm_encoder *encoder;
697 ret = rcar_du_cmm_check(crtc, state);
701 /* Store the routes from the CRTC output to the DU outputs. */
704 drm_for_each_encoder_mask(encoder, crtc->dev, state->encoder_mask) {
705 struct rcar_du_encoder *renc;
707 /* Skip the writeback encoder. */
708 if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
711 renc = to_rcar_encoder(encoder);
712 rstate->outputs |= BIT(renc->output);
718 static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
719 struct drm_crtc_state *old_state)
721 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
722 struct rcar_du_crtc_state *rstate = to_rcar_crtc_state(crtc->state);
723 struct rcar_du_device *rcdu = rcrtc->dev;
726 rcar_cmm_enable(rcrtc->cmm);
727 rcar_du_crtc_get(rcrtc);
730 * On D3/E3 the dot clock is provided by the LVDS encoder attached to
731 * the DU channel. We need to enable its clock output explicitly if
732 * the LVDS output is disabled.
734 if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index) &&
735 rstate->outputs == BIT(RCAR_DU_OUTPUT_DPAD0)) {
736 struct drm_bridge *bridge = rcdu->lvds[rcrtc->index];
737 const struct drm_display_mode *mode =
738 &crtc->state->adjusted_mode;
740 rcar_lvds_clk_enable(bridge, mode->clock * 1000);
743 rcar_du_crtc_start(rcrtc);
746 * TODO: The chip manual indicates that CMM tables should be written
747 * after the DU channel has been activated. Investigate the impact
748 * of this restriction on the first displayed frame.
750 rcar_du_cmm_setup(crtc);
753 static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
754 struct drm_crtc_state *old_state)
756 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
757 struct rcar_du_crtc_state *rstate = to_rcar_crtc_state(old_state);
758 struct rcar_du_device *rcdu = rcrtc->dev;
760 rcar_du_crtc_stop(rcrtc);
761 rcar_du_crtc_put(rcrtc);
763 if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index) &&
764 rstate->outputs == BIT(RCAR_DU_OUTPUT_DPAD0)) {
765 struct drm_bridge *bridge = rcdu->lvds[rcrtc->index];
768 * Disable the LVDS clock output, see
769 * rcar_du_crtc_atomic_enable().
771 rcar_lvds_clk_disable(bridge);
774 spin_lock_irq(&crtc->dev->event_lock);
775 if (crtc->state->event) {
776 drm_crtc_send_vblank_event(crtc, crtc->state->event);
777 crtc->state->event = NULL;
779 spin_unlock_irq(&crtc->dev->event_lock);
782 static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
783 struct drm_crtc_state *old_crtc_state)
785 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
787 WARN_ON(!crtc->state->enable);
790 * If a mode set is in progress we can be called with the CRTC disabled.
791 * We thus need to first get and setup the CRTC in order to configure
792 * planes. We must *not* put the CRTC in .atomic_flush(), as it must be
793 * kept awake until the .atomic_enable() call that will follow. The get
794 * operation in .atomic_enable() will in that case be a no-op, and the
795 * CRTC will be put later in .atomic_disable().
797 * If a mode set is not in progress the CRTC is enabled, and the
798 * following get call will be a no-op. There is thus no need to balance
799 * it in .atomic_flush() either.
801 rcar_du_crtc_get(rcrtc);
803 /* If the active state changed, we let .atomic_enable handle CMM. */
804 if (crtc->state->color_mgmt_changed && !crtc->state->active_changed)
805 rcar_du_cmm_setup(crtc);
807 if (rcar_du_has(rcrtc->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
808 rcar_du_vsp_atomic_begin(rcrtc);
811 static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
812 struct drm_crtc_state *old_crtc_state)
814 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
815 struct drm_device *dev = rcrtc->crtc.dev;
818 rcar_du_crtc_update_planes(rcrtc);
820 if (crtc->state->event) {
821 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
823 spin_lock_irqsave(&dev->event_lock, flags);
824 rcrtc->event = crtc->state->event;
825 crtc->state->event = NULL;
826 spin_unlock_irqrestore(&dev->event_lock, flags);
829 if (rcar_du_has(rcrtc->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
830 rcar_du_vsp_atomic_flush(rcrtc);
833 static enum drm_mode_status
834 rcar_du_crtc_mode_valid(struct drm_crtc *crtc,
835 const struct drm_display_mode *mode)
837 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
838 struct rcar_du_device *rcdu = rcrtc->dev;
839 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
840 unsigned int min_sync_porch;
843 if (interlaced && !rcar_du_has(rcdu, RCAR_DU_FEATURE_INTERLACED))
844 return MODE_NO_INTERLACE;
847 * The hardware requires a minimum combined horizontal sync and back
848 * porch of 20 pixels (when CMM isn't used) or 45 pixels (when CMM is
849 * used), and a minimum vertical back porch of 3 lines.
852 if (rcrtc->group->cmms_mask & BIT(rcrtc->index % 2))
853 min_sync_porch += 25;
855 if (mode->htotal - mode->hsync_start < min_sync_porch)
856 return MODE_HBLANK_NARROW;
858 vbp = (mode->vtotal - mode->vsync_end) / (interlaced ? 2 : 1);
860 return MODE_VBLANK_NARROW;
865 static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
866 .atomic_check = rcar_du_crtc_atomic_check,
867 .atomic_begin = rcar_du_crtc_atomic_begin,
868 .atomic_flush = rcar_du_crtc_atomic_flush,
869 .atomic_enable = rcar_du_crtc_atomic_enable,
870 .atomic_disable = rcar_du_crtc_atomic_disable,
871 .mode_valid = rcar_du_crtc_mode_valid,
874 static void rcar_du_crtc_crc_init(struct rcar_du_crtc *rcrtc)
876 struct rcar_du_device *rcdu = rcrtc->dev;
877 const char **sources;
881 /* CRC available only on Gen3 HW. */
882 if (rcdu->info->gen < 3)
885 /* Reserve 1 for "auto" source. */
886 count = rcrtc->vsp->num_planes + 1;
888 sources = kmalloc_array(count, sizeof(*sources), GFP_KERNEL);
892 sources[0] = kstrdup("auto", GFP_KERNEL);
896 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
897 struct drm_plane *plane = &rcrtc->vsp->planes[i].plane;
900 sprintf(name, "plane%u", plane->base.id);
901 sources[i + 1] = kstrdup(name, GFP_KERNEL);
906 rcrtc->sources = sources;
907 rcrtc->sources_count = count;
918 static void rcar_du_crtc_crc_cleanup(struct rcar_du_crtc *rcrtc)
925 for (i = 0; i < rcrtc->sources_count; i++)
926 kfree(rcrtc->sources[i]);
927 kfree(rcrtc->sources);
929 rcrtc->sources = NULL;
930 rcrtc->sources_count = 0;
933 static struct drm_crtc_state *
934 rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
936 struct rcar_du_crtc_state *state;
937 struct rcar_du_crtc_state *copy;
939 if (WARN_ON(!crtc->state))
942 state = to_rcar_crtc_state(crtc->state);
943 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
947 __drm_atomic_helper_crtc_duplicate_state(crtc, ©->state);
952 static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
953 struct drm_crtc_state *state)
955 __drm_atomic_helper_crtc_destroy_state(state);
956 kfree(to_rcar_crtc_state(state));
959 static void rcar_du_crtc_cleanup(struct drm_crtc *crtc)
961 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
963 rcar_du_crtc_crc_cleanup(rcrtc);
965 return drm_crtc_cleanup(crtc);
968 static void rcar_du_crtc_reset(struct drm_crtc *crtc)
970 struct rcar_du_crtc_state *state;
973 rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
977 state = kzalloc(sizeof(*state), GFP_KERNEL);
981 state->crc.source = VSP1_DU_CRC_NONE;
982 state->crc.index = 0;
984 __drm_atomic_helper_crtc_reset(crtc, &state->state);
987 static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
989 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
991 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
992 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
993 rcrtc->vblank_enable = true;
998 static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
1000 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
1002 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
1003 rcrtc->vblank_enable = false;
1006 static int rcar_du_crtc_parse_crc_source(struct rcar_du_crtc *rcrtc,
1007 const char *source_name,
1008 enum vsp1_du_crc_source *source)
1014 * Parse the source name. Supported values are "plane%u" to compute the
1015 * CRC on an input plane (%u is the plane ID), and "auto" to compute the
1016 * CRC on the composer (VSP) output.
1020 *source = VSP1_DU_CRC_NONE;
1022 } else if (!strcmp(source_name, "auto")) {
1023 *source = VSP1_DU_CRC_OUTPUT;
1025 } else if (strstarts(source_name, "plane")) {
1028 *source = VSP1_DU_CRC_PLANE;
1030 ret = kstrtouint(source_name + strlen("plane"), 10, &index);
1034 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
1035 if (index == rcrtc->vsp->planes[i].plane.base.id)
1043 static int rcar_du_crtc_verify_crc_source(struct drm_crtc *crtc,
1044 const char *source_name,
1047 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
1048 enum vsp1_du_crc_source source;
1050 if (rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source) < 0) {
1051 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
1059 static const char *const *
1060 rcar_du_crtc_get_crc_sources(struct drm_crtc *crtc, size_t *count)
1062 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
1064 *count = rcrtc->sources_count;
1065 return rcrtc->sources;
1068 static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
1069 const char *source_name)
1071 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
1072 struct drm_modeset_acquire_ctx ctx;
1073 struct drm_crtc_state *crtc_state;
1074 struct drm_atomic_state *state;
1075 enum vsp1_du_crc_source source;
1079 ret = rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source);
1085 /* Perform an atomic commit to set the CRC source. */
1086 drm_modeset_acquire_init(&ctx, 0);
1088 state = drm_atomic_state_alloc(crtc->dev);
1094 state->acquire_ctx = &ctx;
1097 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1098 if (!IS_ERR(crtc_state)) {
1099 struct rcar_du_crtc_state *rcrtc_state;
1101 rcrtc_state = to_rcar_crtc_state(crtc_state);
1102 rcrtc_state->crc.source = source;
1103 rcrtc_state->crc.index = index;
1105 ret = drm_atomic_commit(state);
1107 ret = PTR_ERR(crtc_state);
1110 if (ret == -EDEADLK) {
1111 drm_atomic_state_clear(state);
1112 drm_modeset_backoff(&ctx);
1116 drm_atomic_state_put(state);
1119 drm_modeset_drop_locks(&ctx);
1120 drm_modeset_acquire_fini(&ctx);
1125 static const struct drm_crtc_funcs crtc_funcs_gen2 = {
1126 .reset = rcar_du_crtc_reset,
1127 .destroy = drm_crtc_cleanup,
1128 .set_config = drm_atomic_helper_set_config,
1129 .page_flip = drm_atomic_helper_page_flip,
1130 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1131 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1132 .enable_vblank = rcar_du_crtc_enable_vblank,
1133 .disable_vblank = rcar_du_crtc_disable_vblank,
1136 static const struct drm_crtc_funcs crtc_funcs_gen3 = {
1137 .reset = rcar_du_crtc_reset,
1138 .destroy = rcar_du_crtc_cleanup,
1139 .set_config = drm_atomic_helper_set_config,
1140 .page_flip = drm_atomic_helper_page_flip,
1141 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1142 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1143 .enable_vblank = rcar_du_crtc_enable_vblank,
1144 .disable_vblank = rcar_du_crtc_disable_vblank,
1145 .set_crc_source = rcar_du_crtc_set_crc_source,
1146 .verify_crc_source = rcar_du_crtc_verify_crc_source,
1147 .get_crc_sources = rcar_du_crtc_get_crc_sources,
1148 .gamma_set = drm_atomic_helper_legacy_gamma_set,
1151 /* -----------------------------------------------------------------------------
1152 * Interrupt Handling
1155 static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
1157 struct rcar_du_crtc *rcrtc = arg;
1158 struct rcar_du_device *rcdu = rcrtc->dev;
1159 irqreturn_t ret = IRQ_NONE;
1162 spin_lock(&rcrtc->vblank_lock);
1164 status = rcar_du_crtc_read(rcrtc, DSSR);
1165 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
1167 if (status & DSSR_VBK) {
1169 * Wake up the vblank wait if the counter reaches 0. This must
1170 * be protected by the vblank_lock to avoid races in
1171 * rcar_du_crtc_disable_planes().
1173 if (rcrtc->vblank_count) {
1174 if (--rcrtc->vblank_count == 0)
1175 wake_up(&rcrtc->vblank_wait);
1179 spin_unlock(&rcrtc->vblank_lock);
1181 if (status & DSSR_VBK) {
1182 if (rcdu->info->gen < 3) {
1183 drm_crtc_handle_vblank(&rcrtc->crtc);
1184 rcar_du_crtc_finish_page_flip(rcrtc);
1193 /* -----------------------------------------------------------------------------
1197 int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
1198 unsigned int hwindex)
1200 static const unsigned int mmio_offsets[] = {
1201 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
1204 struct rcar_du_device *rcdu = rgrp->dev;
1205 struct platform_device *pdev = to_platform_device(rcdu->dev);
1206 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
1207 struct drm_crtc *crtc = &rcrtc->crtc;
1208 struct drm_plane *primary;
1209 unsigned int irqflags;
1216 /* Get the CRTC clock and the optional external clock. */
1217 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1218 sprintf(clk_name, "du.%u", hwindex);
1224 rcrtc->clock = devm_clk_get(rcdu->dev, name);
1225 if (IS_ERR(rcrtc->clock)) {
1226 dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
1227 return PTR_ERR(rcrtc->clock);
1230 sprintf(clk_name, "dclkin.%u", hwindex);
1231 clk = devm_clk_get(rcdu->dev, clk_name);
1233 rcrtc->extclock = clk;
1234 } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
1235 return -EPROBE_DEFER;
1236 } else if (rcdu->info->dpll_mask & BIT(hwindex)) {
1238 * DU channels that have a display PLL can't use the internal
1239 * system clock and thus require an external clock.
1242 dev_err(rcdu->dev, "can't get dclkin.%u: %d\n", hwindex, ret);
1246 init_waitqueue_head(&rcrtc->flip_wait);
1247 init_waitqueue_head(&rcrtc->vblank_wait);
1248 spin_lock_init(&rcrtc->vblank_lock);
1251 rcrtc->group = rgrp;
1252 rcrtc->mmio_offset = mmio_offsets[hwindex];
1253 rcrtc->index = hwindex;
1254 rcrtc->dsysr = (rcrtc->index % 2 ? 0 : DSYSR_DRES) | DSYSR_TVM_TVSYNC;
1256 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
1257 primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
1259 primary = &rgrp->planes[swindex % 2].plane;
1261 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
1262 rcdu->info->gen <= 2 ?
1263 &crtc_funcs_gen2 : &crtc_funcs_gen3,
1268 /* CMM might be disabled for this CRTC. */
1269 if (rcdu->cmms[swindex]) {
1270 rcrtc->cmm = rcdu->cmms[swindex];
1271 rgrp->cmms_mask |= BIT(hwindex % 2);
1273 drm_mode_crtc_set_gamma_size(crtc, CM2_LUT_SIZE);
1274 drm_crtc_enable_color_mgmt(crtc, 0, false, CM2_LUT_SIZE);
1277 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
1279 /* Register the interrupt handler. */
1280 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1281 /* The IRQ's are associated with the CRTC (sw)index. */
1282 irq = platform_get_irq(pdev, swindex);
1285 irq = platform_get_irq(pdev, 0);
1286 irqflags = IRQF_SHARED;
1290 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
1294 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
1295 dev_name(rcdu->dev), rcrtc);
1298 "failed to register IRQ for CRTC %u\n", swindex);
1302 rcar_du_crtc_crc_init(rcrtc);