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/sys_soc.h>
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_plane_helper.h>
23 #include "rcar_du_crtc.h"
24 #include "rcar_du_drv.h"
25 #include "rcar_du_kms.h"
26 #include "rcar_du_plane.h"
27 #include "rcar_du_regs.h"
28 #include "rcar_du_vsp.h"
30 static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
32 struct rcar_du_device *rcdu = rcrtc->group->dev;
34 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
37 static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
39 struct rcar_du_device *rcdu = rcrtc->group->dev;
41 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
44 static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
46 struct rcar_du_device *rcdu = rcrtc->group->dev;
48 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
49 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
52 static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
54 struct rcar_du_device *rcdu = rcrtc->group->dev;
56 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
57 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
60 void rcar_du_crtc_dsysr_clr_set(struct rcar_du_crtc *rcrtc, u32 clr, u32 set)
62 struct rcar_du_device *rcdu = rcrtc->group->dev;
64 rcrtc->dsysr = (rcrtc->dsysr & ~clr) | set;
65 rcar_du_write(rcdu, rcrtc->mmio_offset + DSYSR, rcrtc->dsysr);
68 /* -----------------------------------------------------------------------------
79 static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
80 struct dpll_info *dpll,
84 unsigned long best_diff = (unsigned long)-1;
91 * fin fvco fout fclkout
92 * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
95 * +---------------- [1/N] <------------+
97 * fclkout = fvco / P / FDPLL -- (1)
101 * fvco = fin * P * N / M -- (2)
103 * (1) + (2) indicates
105 * fclkout = fin * N / M / FDPLL
110 * FDPLL : (fdpll + 1)
112 * 2kHz < fvco < 4096MHz
114 * To minimize the jitter,
115 * N : as large as possible
116 * M : as small as possible
118 for (m = 0; m < 4; m++) {
119 for (n = 119; n > 38; n--) {
121 * This code only runs on 64-bit architectures, the
122 * unsigned long type can thus be used for 64-bit
123 * computation. It will still compile without any
124 * warning on 32-bit architectures.
126 * To optimize calculations, use fout instead of fvco
127 * to verify the VCO frequency constraint.
129 unsigned long fout = input * (n + 1) / (m + 1);
131 if (fout < 1000 || fout > 2048 * 1000 * 1000U)
134 for (fdpll = 1; fdpll < 32; fdpll++) {
135 unsigned long output;
137 output = fout / (fdpll + 1);
138 if (output >= 400 * 1000 * 1000)
141 diff = abs((long)output - (long)target);
142 if (best_diff > diff) {
147 dpll->output = output;
157 dev_dbg(rcrtc->group->dev->dev,
158 "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
159 dpll->output, dpll->fdpll, dpll->n, dpll->m,
163 struct du_clk_params {
170 static void rcar_du_escr_divider(struct clk *clk, unsigned long target,
171 u32 escr, struct du_clk_params *params)
178 * If the target rate has already been achieved perfectly we can't do
181 if (params->diff == 0)
185 * Compute the input clock rate and internal divisor values to obtain
186 * the clock rate closest to the target frequency.
188 rate = clk_round_rate(clk, target);
189 div = clamp(DIV_ROUND_CLOSEST(rate, target), 1UL, 64UL) - 1;
190 diff = abs(rate / (div + 1) - target);
193 * Store the parameters if the resulting frequency is better than any
194 * previously calculated value.
196 if (diff < params->diff) {
200 params->escr = escr | div;
204 static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
205 { .soc_id = "r8a7795", .revision = "ES1.*" },
209 static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
211 const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
212 struct rcar_du_device *rcdu = rcrtc->group->dev;
213 unsigned long mode_clock = mode->clock * 1000;
217 if (rcdu->info->dpll_mask & (1 << rcrtc->index)) {
218 unsigned long target = mode_clock;
219 struct dpll_info dpll = { 0 };
220 unsigned long extclk;
225 * DU channels that have a display PLL can't use the internal
226 * system clock, and have no internal clock divider.
230 * The H3 ES1.x exhibits dot clock duty cycle stability issues.
231 * We can work around them by configuring the DPLL to twice the
232 * desired frequency, coupled with a /2 post-divider. Restrict
233 * the workaround to H3 ES1.x as ES2.0 and all other SoCs have
234 * no post-divider when a display PLL is present (as shown by
235 * the workaround breaking HDMI output on M3-W during testing).
237 if (soc_device_match(rcar_du_r8a7795_es1)) {
242 extclk = clk_get_rate(rcrtc->extclock);
243 rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
245 dpllcr = DPLLCR_CODE | DPLLCR_CLKE
246 | DPLLCR_FDPLL(dpll.fdpll)
247 | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
250 if (rcrtc->index == 1)
251 dpllcr |= DPLLCR_PLCS1
252 | DPLLCR_INCS_DOTCLKIN1;
254 dpllcr |= DPLLCR_PLCS0
255 | DPLLCR_INCS_DOTCLKIN0;
257 rcar_du_group_write(rcrtc->group, DPLLCR, dpllcr);
259 escr = ESCR_DCLKSEL_DCLKIN | div;
260 } else if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index)) {
262 * Use the LVDS PLL output as the dot clock when outputting to
263 * the LVDS encoder on an SoC that supports this clock routing
264 * option. We use the clock directly in that case, without any
265 * additional divider.
267 escr = ESCR_DCLKSEL_DCLKIN;
269 struct du_clk_params params = { .diff = (unsigned long)-1 };
271 rcar_du_escr_divider(rcrtc->clock, mode_clock,
272 ESCR_DCLKSEL_CLKS, ¶ms);
274 rcar_du_escr_divider(rcrtc->extclock, mode_clock,
275 ESCR_DCLKSEL_DCLKIN, ¶ms);
277 dev_dbg(rcrtc->group->dev->dev, "mode clock %lu %s rate %lu\n",
278 mode_clock, params.clk == rcrtc->clock ? "cpg" : "ext",
281 clk_set_rate(params.clk, params.rate);
285 dev_dbg(rcrtc->group->dev->dev, "%s: ESCR 0x%08x\n", __func__, escr);
287 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? ESCR13 : ESCR02, escr);
288 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? OTAR13 : OTAR02, 0);
290 /* Signal polarities */
291 dsmr = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
292 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
293 | ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? DSMR_ODEV : 0)
294 | DSMR_DIPM_DISP | DSMR_CSPM;
295 rcar_du_crtc_write(rcrtc, DSMR, dsmr);
297 /* Display timings */
298 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
299 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
300 mode->hdisplay - 19);
301 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
302 mode->hsync_start - 1);
303 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
305 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
306 mode->crtc_vsync_end - 2);
307 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
308 mode->crtc_vsync_end +
309 mode->crtc_vdisplay - 2);
310 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
311 mode->crtc_vsync_end +
312 mode->crtc_vsync_start - 1);
313 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
315 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
316 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
319 void rcar_du_crtc_route_output(struct drm_crtc *crtc,
320 enum rcar_du_output output)
322 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
323 struct rcar_du_device *rcdu = rcrtc->group->dev;
326 * Store the route from the CRTC output to the DU output. The DU will be
327 * configured when starting the CRTC.
329 rcrtc->outputs |= BIT(output);
332 * Store RGB routing to DPAD0, the hardware will be configured when
335 if (output == RCAR_DU_OUTPUT_DPAD0)
336 rcdu->dpad0_source = rcrtc->index;
339 static unsigned int plane_zpos(struct rcar_du_plane *plane)
341 return plane->plane.state->normalized_zpos;
344 static const struct rcar_du_format_info *
345 plane_format(struct rcar_du_plane *plane)
347 return to_rcar_plane_state(plane->plane.state)->format;
350 static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
352 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
353 struct rcar_du_device *rcdu = rcrtc->group->dev;
354 unsigned int num_planes = 0;
355 unsigned int dptsr_planes;
356 unsigned int hwplanes = 0;
357 unsigned int prio = 0;
361 for (i = 0; i < rcrtc->group->num_planes; ++i) {
362 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
365 if (plane->plane.state->crtc != &rcrtc->crtc ||
366 !plane->plane.state->visible)
369 /* Insert the plane in the sorted planes array. */
370 for (j = num_planes++; j > 0; --j) {
371 if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
373 planes[j] = planes[j-1];
377 prio += plane_format(plane)->planes * 4;
380 for (i = 0; i < num_planes; ++i) {
381 struct rcar_du_plane *plane = planes[i];
382 struct drm_plane_state *state = plane->plane.state;
383 unsigned int index = to_rcar_plane_state(state)->hwindex;
386 dspr |= (index + 1) << prio;
387 hwplanes |= 1 << index;
389 if (plane_format(plane)->planes == 2) {
390 index = (index + 1) % 8;
393 dspr |= (index + 1) << prio;
394 hwplanes |= 1 << index;
398 /* If VSP+DU integration is enabled the plane assignment is fixed. */
399 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
400 if (rcdu->info->gen < 3) {
401 dspr = (rcrtc->index % 2) + 1;
402 hwplanes = 1 << (rcrtc->index % 2);
404 dspr = (rcrtc->index % 2) ? 3 : 1;
405 hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
410 * Update the planes to display timing and dot clock generator
413 * Updating the DPTSR register requires restarting the CRTC group,
414 * resulting in visible flicker. To mitigate the issue only update the
415 * association if needed by enabled planes. Planes being disabled will
416 * keep their current association.
418 mutex_lock(&rcrtc->group->lock);
420 dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
421 : rcrtc->group->dptsr_planes & ~hwplanes;
423 if (dptsr_planes != rcrtc->group->dptsr_planes) {
424 rcar_du_group_write(rcrtc->group, DPTSR,
425 (dptsr_planes << 16) | dptsr_planes);
426 rcrtc->group->dptsr_planes = dptsr_planes;
428 if (rcrtc->group->used_crtcs)
429 rcar_du_group_restart(rcrtc->group);
432 /* Restart the group if plane sources have changed. */
433 if (rcrtc->group->need_restart)
434 rcar_du_group_restart(rcrtc->group);
436 mutex_unlock(&rcrtc->group->lock);
438 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
442 /* -----------------------------------------------------------------------------
446 void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
448 struct drm_pending_vblank_event *event;
449 struct drm_device *dev = rcrtc->crtc.dev;
452 spin_lock_irqsave(&dev->event_lock, flags);
453 event = rcrtc->event;
455 spin_unlock_irqrestore(&dev->event_lock, flags);
460 spin_lock_irqsave(&dev->event_lock, flags);
461 drm_crtc_send_vblank_event(&rcrtc->crtc, event);
462 wake_up(&rcrtc->flip_wait);
463 spin_unlock_irqrestore(&dev->event_lock, flags);
465 drm_crtc_vblank_put(&rcrtc->crtc);
468 static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
470 struct drm_device *dev = rcrtc->crtc.dev;
474 spin_lock_irqsave(&dev->event_lock, flags);
475 pending = rcrtc->event != NULL;
476 spin_unlock_irqrestore(&dev->event_lock, flags);
481 static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
483 struct rcar_du_device *rcdu = rcrtc->group->dev;
485 if (wait_event_timeout(rcrtc->flip_wait,
486 !rcar_du_crtc_page_flip_pending(rcrtc),
487 msecs_to_jiffies(50)))
490 dev_warn(rcdu->dev, "page flip timeout\n");
492 rcar_du_crtc_finish_page_flip(rcrtc);
495 /* -----------------------------------------------------------------------------
496 * Start/Stop and Suspend/Resume
499 static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
501 /* Set display off and background to black */
502 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
503 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
505 /* Configure display timings and output routing */
506 rcar_du_crtc_set_display_timing(rcrtc);
507 rcar_du_group_set_routing(rcrtc->group);
509 /* Start with all planes disabled. */
510 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
512 /* Enable the VSP compositor. */
513 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
514 rcar_du_vsp_enable(rcrtc);
516 /* Turn vertical blanking interrupt reporting on. */
517 drm_crtc_vblank_on(&rcrtc->crtc);
520 static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
525 * Guard against double-get, as the function is called from both the
526 * .atomic_enable() and .atomic_begin() handlers.
528 if (rcrtc->initialized)
531 ret = clk_prepare_enable(rcrtc->clock);
535 ret = clk_prepare_enable(rcrtc->extclock);
539 ret = rcar_du_group_get(rcrtc->group);
543 rcar_du_crtc_setup(rcrtc);
544 rcrtc->initialized = true;
549 clk_disable_unprepare(rcrtc->extclock);
551 clk_disable_unprepare(rcrtc->clock);
555 static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
557 rcar_du_group_put(rcrtc->group);
559 clk_disable_unprepare(rcrtc->extclock);
560 clk_disable_unprepare(rcrtc->clock);
562 rcrtc->initialized = false;
565 static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
570 * Select master sync mode. This enables display operation in master
571 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
574 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
575 rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
576 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
579 rcar_du_group_start_stop(rcrtc->group, true);
582 static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
584 struct rcar_du_device *rcdu = rcrtc->group->dev;
585 struct drm_crtc *crtc = &rcrtc->crtc;
588 /* Make sure vblank interrupts are enabled. */
589 drm_crtc_vblank_get(crtc);
592 * Disable planes and calculate how many vertical blanking interrupts we
593 * have to wait for. If a vertical blanking interrupt has been triggered
594 * but not processed yet, we don't know whether it occurred before or
595 * after the planes got disabled. We thus have to wait for two vblank
596 * interrupts in that case.
598 spin_lock_irq(&rcrtc->vblank_lock);
599 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
600 status = rcar_du_crtc_read(rcrtc, DSSR);
601 rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
602 spin_unlock_irq(&rcrtc->vblank_lock);
604 if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
605 msecs_to_jiffies(100)))
606 dev_warn(rcdu->dev, "vertical blanking timeout\n");
608 drm_crtc_vblank_put(crtc);
611 static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
613 struct drm_crtc *crtc = &rcrtc->crtc;
616 * Disable all planes and wait for the change to take effect. This is
617 * required as the plane enable registers are updated on vblank, and no
618 * vblank will occur once the CRTC is stopped. Disabling planes when
619 * starting the CRTC thus wouldn't be enough as it would start scanning
620 * out immediately from old frame buffers until the next vblank.
622 * This increases the CRTC stop delay, especially when multiple CRTCs
623 * are stopped in one operation as we now wait for one vblank per CRTC.
624 * Whether this can be improved needs to be researched.
626 rcar_du_crtc_disable_planes(rcrtc);
629 * Disable vertical blanking interrupt reporting. We first need to wait
630 * for page flip completion before stopping the CRTC as userspace
631 * expects page flips to eventually complete.
633 rcar_du_crtc_wait_page_flip(rcrtc);
634 drm_crtc_vblank_off(crtc);
636 /* Disable the VSP compositor. */
637 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
638 rcar_du_vsp_disable(rcrtc);
641 * Select switch sync mode. This stops display operation and configures
642 * the HSYNC and VSYNC signals as inputs.
644 * TODO: Find another way to stop the display for DUs that don't support
647 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_TVM_SYNC))
648 rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK,
651 rcar_du_group_start_stop(rcrtc->group, false);
654 /* -----------------------------------------------------------------------------
658 static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
659 struct drm_crtc_state *old_state)
661 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
663 rcar_du_crtc_get(rcrtc);
664 rcar_du_crtc_start(rcrtc);
667 static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
668 struct drm_crtc_state *old_state)
670 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
672 rcar_du_crtc_stop(rcrtc);
673 rcar_du_crtc_put(rcrtc);
675 spin_lock_irq(&crtc->dev->event_lock);
676 if (crtc->state->event) {
677 drm_crtc_send_vblank_event(crtc, crtc->state->event);
678 crtc->state->event = NULL;
680 spin_unlock_irq(&crtc->dev->event_lock);
685 static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
686 struct drm_crtc_state *old_crtc_state)
688 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
690 WARN_ON(!crtc->state->enable);
693 * If a mode set is in progress we can be called with the CRTC disabled.
694 * We thus need to first get and setup the CRTC in order to configure
695 * planes. We must *not* put the CRTC in .atomic_flush(), as it must be
696 * kept awake until the .atomic_enable() call that will follow. The get
697 * operation in .atomic_enable() will in that case be a no-op, and the
698 * CRTC will be put later in .atomic_disable().
700 * If a mode set is not in progress the CRTC is enabled, and the
701 * following get call will be a no-op. There is thus no need to balance
702 * it in .atomic_flush() either.
704 rcar_du_crtc_get(rcrtc);
706 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
707 rcar_du_vsp_atomic_begin(rcrtc);
710 static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
711 struct drm_crtc_state *old_crtc_state)
713 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
714 struct drm_device *dev = rcrtc->crtc.dev;
717 rcar_du_crtc_update_planes(rcrtc);
719 if (crtc->state->event) {
720 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
722 spin_lock_irqsave(&dev->event_lock, flags);
723 rcrtc->event = crtc->state->event;
724 crtc->state->event = NULL;
725 spin_unlock_irqrestore(&dev->event_lock, flags);
728 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
729 rcar_du_vsp_atomic_flush(rcrtc);
732 enum drm_mode_status rcar_du_crtc_mode_valid(struct drm_crtc *crtc,
733 const struct drm_display_mode *mode)
735 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
736 struct rcar_du_device *rcdu = rcrtc->group->dev;
737 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
740 if (interlaced && !rcar_du_has(rcdu, RCAR_DU_FEATURE_INTERLACED))
741 return MODE_NO_INTERLACE;
744 * The hardware requires a minimum combined horizontal sync and back
745 * porch of 20 pixels and a minimum vertical back porch of 3 lines.
747 if (mode->htotal - mode->hsync_start < 20)
748 return MODE_HBLANK_NARROW;
750 vbp = (mode->vtotal - mode->vsync_end) / (interlaced ? 2 : 1);
752 return MODE_VBLANK_NARROW;
757 static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
758 .atomic_begin = rcar_du_crtc_atomic_begin,
759 .atomic_flush = rcar_du_crtc_atomic_flush,
760 .atomic_enable = rcar_du_crtc_atomic_enable,
761 .atomic_disable = rcar_du_crtc_atomic_disable,
762 .mode_valid = rcar_du_crtc_mode_valid,
765 static void rcar_du_crtc_crc_init(struct rcar_du_crtc *rcrtc)
767 struct rcar_du_device *rcdu = rcrtc->group->dev;
768 const char **sources;
772 /* CRC available only on Gen3 HW. */
773 if (rcdu->info->gen < 3)
776 /* Reserve 1 for "auto" source. */
777 count = rcrtc->vsp->num_planes + 1;
779 sources = kmalloc_array(count, sizeof(*sources), GFP_KERNEL);
783 sources[0] = kstrdup("auto", GFP_KERNEL);
787 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
788 struct drm_plane *plane = &rcrtc->vsp->planes[i].plane;
791 sprintf(name, "plane%u", plane->base.id);
792 sources[i + 1] = kstrdup(name, GFP_KERNEL);
797 rcrtc->sources = sources;
798 rcrtc->sources_count = count;
809 static void rcar_du_crtc_crc_cleanup(struct rcar_du_crtc *rcrtc)
816 for (i = 0; i < rcrtc->sources_count; i++)
817 kfree(rcrtc->sources[i]);
818 kfree(rcrtc->sources);
820 rcrtc->sources = NULL;
821 rcrtc->sources_count = 0;
824 static struct drm_crtc_state *
825 rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
827 struct rcar_du_crtc_state *state;
828 struct rcar_du_crtc_state *copy;
830 if (WARN_ON(!crtc->state))
833 state = to_rcar_crtc_state(crtc->state);
834 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
838 __drm_atomic_helper_crtc_duplicate_state(crtc, ©->state);
843 static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
844 struct drm_crtc_state *state)
846 __drm_atomic_helper_crtc_destroy_state(state);
847 kfree(to_rcar_crtc_state(state));
850 static void rcar_du_crtc_cleanup(struct drm_crtc *crtc)
852 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
854 rcar_du_crtc_crc_cleanup(rcrtc);
856 return drm_crtc_cleanup(crtc);
859 static void rcar_du_crtc_reset(struct drm_crtc *crtc)
861 struct rcar_du_crtc_state *state;
864 rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
868 state = kzalloc(sizeof(*state), GFP_KERNEL);
872 state->crc.source = VSP1_DU_CRC_NONE;
873 state->crc.index = 0;
875 crtc->state = &state->state;
876 crtc->state->crtc = crtc;
879 static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
881 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
883 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
884 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
885 rcrtc->vblank_enable = true;
890 static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
892 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
894 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
895 rcrtc->vblank_enable = false;
898 static int rcar_du_crtc_parse_crc_source(struct rcar_du_crtc *rcrtc,
899 const char *source_name,
900 enum vsp1_du_crc_source *source)
906 * Parse the source name. Supported values are "plane%u" to compute the
907 * CRC on an input plane (%u is the plane ID), and "auto" to compute the
908 * CRC on the composer (VSP) output.
912 *source = VSP1_DU_CRC_NONE;
914 } else if (!strcmp(source_name, "auto")) {
915 *source = VSP1_DU_CRC_OUTPUT;
917 } else if (strstarts(source_name, "plane")) {
920 *source = VSP1_DU_CRC_PLANE;
922 ret = kstrtouint(source_name + strlen("plane"), 10, &index);
926 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
927 if (index == rcrtc->vsp->planes[i].plane.base.id)
935 static int rcar_du_crtc_verify_crc_source(struct drm_crtc *crtc,
936 const char *source_name,
939 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
940 enum vsp1_du_crc_source source;
942 if (rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source) < 0) {
943 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
951 const char *const *rcar_du_crtc_get_crc_sources(struct drm_crtc *crtc,
954 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
956 *count = rcrtc->sources_count;
957 return rcrtc->sources;
960 static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
961 const char *source_name)
963 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
964 struct drm_modeset_acquire_ctx ctx;
965 struct drm_crtc_state *crtc_state;
966 struct drm_atomic_state *state;
967 enum vsp1_du_crc_source source;
971 ret = rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source);
977 /* Perform an atomic commit to set the CRC source. */
978 drm_modeset_acquire_init(&ctx, 0);
980 state = drm_atomic_state_alloc(crtc->dev);
986 state->acquire_ctx = &ctx;
989 crtc_state = drm_atomic_get_crtc_state(state, crtc);
990 if (!IS_ERR(crtc_state)) {
991 struct rcar_du_crtc_state *rcrtc_state;
993 rcrtc_state = to_rcar_crtc_state(crtc_state);
994 rcrtc_state->crc.source = source;
995 rcrtc_state->crc.index = index;
997 ret = drm_atomic_commit(state);
999 ret = PTR_ERR(crtc_state);
1002 if (ret == -EDEADLK) {
1003 drm_atomic_state_clear(state);
1004 drm_modeset_backoff(&ctx);
1008 drm_atomic_state_put(state);
1011 drm_modeset_drop_locks(&ctx);
1012 drm_modeset_acquire_fini(&ctx);
1017 static const struct drm_crtc_funcs crtc_funcs_gen2 = {
1018 .reset = rcar_du_crtc_reset,
1019 .destroy = drm_crtc_cleanup,
1020 .set_config = drm_atomic_helper_set_config,
1021 .page_flip = drm_atomic_helper_page_flip,
1022 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1023 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1024 .enable_vblank = rcar_du_crtc_enable_vblank,
1025 .disable_vblank = rcar_du_crtc_disable_vblank,
1028 static const struct drm_crtc_funcs crtc_funcs_gen3 = {
1029 .reset = rcar_du_crtc_reset,
1030 .destroy = rcar_du_crtc_cleanup,
1031 .set_config = drm_atomic_helper_set_config,
1032 .page_flip = drm_atomic_helper_page_flip,
1033 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1034 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1035 .enable_vblank = rcar_du_crtc_enable_vblank,
1036 .disable_vblank = rcar_du_crtc_disable_vblank,
1037 .set_crc_source = rcar_du_crtc_set_crc_source,
1038 .verify_crc_source = rcar_du_crtc_verify_crc_source,
1039 .get_crc_sources = rcar_du_crtc_get_crc_sources,
1042 /* -----------------------------------------------------------------------------
1043 * Interrupt Handling
1046 static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
1048 struct rcar_du_crtc *rcrtc = arg;
1049 struct rcar_du_device *rcdu = rcrtc->group->dev;
1050 irqreturn_t ret = IRQ_NONE;
1053 spin_lock(&rcrtc->vblank_lock);
1055 status = rcar_du_crtc_read(rcrtc, DSSR);
1056 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
1058 if (status & DSSR_VBK) {
1060 * Wake up the vblank wait if the counter reaches 0. This must
1061 * be protected by the vblank_lock to avoid races in
1062 * rcar_du_crtc_disable_planes().
1064 if (rcrtc->vblank_count) {
1065 if (--rcrtc->vblank_count == 0)
1066 wake_up(&rcrtc->vblank_wait);
1070 spin_unlock(&rcrtc->vblank_lock);
1072 if (status & DSSR_VBK) {
1073 if (rcdu->info->gen < 3) {
1074 drm_crtc_handle_vblank(&rcrtc->crtc);
1075 rcar_du_crtc_finish_page_flip(rcrtc);
1084 /* -----------------------------------------------------------------------------
1088 int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
1089 unsigned int hwindex)
1091 static const unsigned int mmio_offsets[] = {
1092 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
1095 struct rcar_du_device *rcdu = rgrp->dev;
1096 struct platform_device *pdev = to_platform_device(rcdu->dev);
1097 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
1098 struct drm_crtc *crtc = &rcrtc->crtc;
1099 struct drm_plane *primary;
1100 unsigned int irqflags;
1107 /* Get the CRTC clock and the optional external clock. */
1108 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1109 sprintf(clk_name, "du.%u", hwindex);
1115 rcrtc->clock = devm_clk_get(rcdu->dev, name);
1116 if (IS_ERR(rcrtc->clock)) {
1117 dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
1118 return PTR_ERR(rcrtc->clock);
1121 sprintf(clk_name, "dclkin.%u", hwindex);
1122 clk = devm_clk_get(rcdu->dev, clk_name);
1124 rcrtc->extclock = clk;
1125 } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
1126 return -EPROBE_DEFER;
1127 } else if (rcdu->info->dpll_mask & BIT(hwindex)) {
1129 * DU channels that have a display PLL can't use the internal
1130 * system clock and thus require an external clock.
1133 dev_err(rcdu->dev, "can't get dclkin.%u: %d\n", hwindex, ret);
1137 init_waitqueue_head(&rcrtc->flip_wait);
1138 init_waitqueue_head(&rcrtc->vblank_wait);
1139 spin_lock_init(&rcrtc->vblank_lock);
1141 rcrtc->group = rgrp;
1142 rcrtc->mmio_offset = mmio_offsets[hwindex];
1143 rcrtc->index = hwindex;
1144 rcrtc->dsysr = (rcrtc->index % 2 ? 0 : DSYSR_DRES) | DSYSR_TVM_TVSYNC;
1146 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
1147 primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
1149 primary = &rgrp->planes[swindex % 2].plane;
1151 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
1152 rcdu->info->gen <= 2 ?
1153 &crtc_funcs_gen2 : &crtc_funcs_gen3,
1158 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
1160 /* Start with vertical blanking interrupt reporting disabled. */
1161 drm_crtc_vblank_off(crtc);
1163 /* Register the interrupt handler. */
1164 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1165 /* The IRQ's are associated with the CRTC (sw)index. */
1166 irq = platform_get_irq(pdev, swindex);
1169 irq = platform_get_irq(pdev, 0);
1170 irqflags = IRQF_SHARED;
1174 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
1178 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
1179 dev_name(rcdu->dev), rcrtc);
1182 "failed to register IRQ for CRTC %u\n", swindex);
1186 rcar_du_crtc_crc_init(rcrtc);