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 static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
63 struct rcar_du_device *rcdu = rcrtc->group->dev;
64 u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
66 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
69 /* -----------------------------------------------------------------------------
80 static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
81 struct dpll_info *dpll,
85 unsigned long best_diff = (unsigned long)-1;
92 * fin fvco fout fclkout
93 * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
96 * +---------------- [1/N] <------------+
98 * fclkout = fvco / P / FDPLL -- (1)
102 * fvco = fin * P * N / M -- (2)
104 * (1) + (2) indicates
106 * fclkout = fin * N / M / FDPLL
111 * FDPLL : (fdpll + 1)
113 * 2kHz < fvco < 4096MHz
115 * To minimize the jitter,
116 * N : as large as possible
117 * M : as small as possible
119 for (m = 0; m < 4; m++) {
120 for (n = 119; n > 38; n--) {
122 * This code only runs on 64-bit architectures, the
123 * unsigned long type can thus be used for 64-bit
124 * computation. It will still compile without any
125 * warning on 32-bit architectures.
127 * To optimize calculations, use fout instead of fvco
128 * to verify the VCO frequency constraint.
130 unsigned long fout = input * (n + 1) / (m + 1);
132 if (fout < 1000 || fout > 2048 * 1000 * 1000U)
135 for (fdpll = 1; fdpll < 32; fdpll++) {
136 unsigned long output;
138 output = fout / (fdpll + 1);
139 if (output >= 400 * 1000 * 1000)
142 diff = abs((long)output - (long)target);
143 if (best_diff > diff) {
148 dpll->output = output;
158 dev_dbg(rcrtc->group->dev->dev,
159 "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
160 dpll->output, dpll->fdpll, dpll->n, dpll->m,
164 struct du_clk_params {
171 static void rcar_du_escr_divider(struct clk *clk, unsigned long target,
172 u32 escr, struct du_clk_params *params)
179 * If the target rate has already been achieved perfectly we can't do
182 if (params->diff == 0)
186 * Compute the input clock rate and internal divisor values to obtain
187 * the clock rate closest to the target frequency.
189 rate = clk_round_rate(clk, target);
190 div = clamp(DIV_ROUND_CLOSEST(rate, target), 1UL, 64UL) - 1;
191 diff = abs(rate / (div + 1) - target);
194 * Store the parameters if the resulting frequency is better than any
195 * previously calculated value.
197 if (diff < params->diff) {
201 params->escr = escr | div;
205 static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
206 { .soc_id = "r8a7795", .revision = "ES1.*" },
210 static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
212 const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
213 struct rcar_du_device *rcdu = rcrtc->group->dev;
214 unsigned long mode_clock = mode->clock * 1000;
218 if (rcdu->info->dpll_mask & (1 << rcrtc->index)) {
219 unsigned long target = mode_clock;
220 struct dpll_info dpll = { 0 };
221 unsigned long extclk;
226 * DU channels that have a display PLL can't use the internal
227 * system clock, and have no internal clock divider.
230 if (WARN_ON(!rcrtc->extclock))
234 * The H3 ES1.x exhibits dot clock duty cycle stability issues.
235 * We can work around them by configuring the DPLL to twice the
236 * desired frequency, coupled with a /2 post-divider. Restrict
237 * the workaround to H3 ES1.x as ES2.0 and all other SoCs have
238 * no post-divider when a display PLL is present (as shown by
239 * the workaround breaking HDMI output on M3-W during testing).
241 if (soc_device_match(rcar_du_r8a7795_es1)) {
246 extclk = clk_get_rate(rcrtc->extclock);
247 rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
249 dpllcr = DPLLCR_CODE | DPLLCR_CLKE
250 | DPLLCR_FDPLL(dpll.fdpll)
251 | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
254 if (rcrtc->index == 1)
255 dpllcr |= DPLLCR_PLCS1
256 | DPLLCR_INCS_DOTCLKIN1;
258 dpllcr |= DPLLCR_PLCS0
259 | DPLLCR_INCS_DOTCLKIN0;
261 rcar_du_group_write(rcrtc->group, DPLLCR, dpllcr);
263 escr = ESCR_DCLKSEL_DCLKIN | div;
264 } else if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index)) {
266 * Use the LVDS PLL output as the dot clock when outputting to
267 * the LVDS encoder on an SoC that supports this clock routing
268 * option. We use the clock directly in that case, without any
269 * additional divider.
271 escr = ESCR_DCLKSEL_DCLKIN;
273 struct du_clk_params params = { .diff = (unsigned long)-1 };
275 rcar_du_escr_divider(rcrtc->clock, mode_clock,
276 ESCR_DCLKSEL_CLKS, ¶ms);
278 rcar_du_escr_divider(rcrtc->extclock, mode_clock,
279 ESCR_DCLKSEL_DCLKIN, ¶ms);
281 dev_dbg(rcrtc->group->dev->dev, "mode clock %lu %s rate %lu\n",
282 mode_clock, params.clk == rcrtc->clock ? "cpg" : "ext",
285 clk_set_rate(params.clk, params.rate);
289 dev_dbg(rcrtc->group->dev->dev, "%s: ESCR 0x%08x\n", __func__, escr);
291 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? ESCR13 : ESCR02, escr);
292 rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? OTAR13 : OTAR02, 0);
294 /* Signal polarities */
295 dsmr = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
296 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
297 | ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? DSMR_ODEV : 0)
298 | DSMR_DIPM_DISP | DSMR_CSPM;
299 rcar_du_crtc_write(rcrtc, DSMR, dsmr);
301 /* Display timings */
302 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
303 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
304 mode->hdisplay - 19);
305 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
306 mode->hsync_start - 1);
307 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
309 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
310 mode->crtc_vsync_end - 2);
311 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
312 mode->crtc_vsync_end +
313 mode->crtc_vdisplay - 2);
314 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
315 mode->crtc_vsync_end +
316 mode->crtc_vsync_start - 1);
317 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
319 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
320 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
323 void rcar_du_crtc_route_output(struct drm_crtc *crtc,
324 enum rcar_du_output output)
326 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
327 struct rcar_du_device *rcdu = rcrtc->group->dev;
330 * Store the route from the CRTC output to the DU output. The DU will be
331 * configured when starting the CRTC.
333 rcrtc->outputs |= BIT(output);
336 * Store RGB routing to DPAD0, the hardware will be configured when
339 if (output == RCAR_DU_OUTPUT_DPAD0)
340 rcdu->dpad0_source = rcrtc->index;
343 static unsigned int plane_zpos(struct rcar_du_plane *plane)
345 return plane->plane.state->normalized_zpos;
348 static const struct rcar_du_format_info *
349 plane_format(struct rcar_du_plane *plane)
351 return to_rcar_plane_state(plane->plane.state)->format;
354 static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
356 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
357 struct rcar_du_device *rcdu = rcrtc->group->dev;
358 unsigned int num_planes = 0;
359 unsigned int dptsr_planes;
360 unsigned int hwplanes = 0;
361 unsigned int prio = 0;
365 for (i = 0; i < rcrtc->group->num_planes; ++i) {
366 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
369 if (plane->plane.state->crtc != &rcrtc->crtc ||
370 !plane->plane.state->visible)
373 /* Insert the plane in the sorted planes array. */
374 for (j = num_planes++; j > 0; --j) {
375 if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
377 planes[j] = planes[j-1];
381 prio += plane_format(plane)->planes * 4;
384 for (i = 0; i < num_planes; ++i) {
385 struct rcar_du_plane *plane = planes[i];
386 struct drm_plane_state *state = plane->plane.state;
387 unsigned int index = to_rcar_plane_state(state)->hwindex;
390 dspr |= (index + 1) << prio;
391 hwplanes |= 1 << index;
393 if (plane_format(plane)->planes == 2) {
394 index = (index + 1) % 8;
397 dspr |= (index + 1) << prio;
398 hwplanes |= 1 << index;
402 /* If VSP+DU integration is enabled the plane assignment is fixed. */
403 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
404 if (rcdu->info->gen < 3) {
405 dspr = (rcrtc->index % 2) + 1;
406 hwplanes = 1 << (rcrtc->index % 2);
408 dspr = (rcrtc->index % 2) ? 3 : 1;
409 hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
414 * Update the planes to display timing and dot clock generator
417 * Updating the DPTSR register requires restarting the CRTC group,
418 * resulting in visible flicker. To mitigate the issue only update the
419 * association if needed by enabled planes. Planes being disabled will
420 * keep their current association.
422 mutex_lock(&rcrtc->group->lock);
424 dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
425 : rcrtc->group->dptsr_planes & ~hwplanes;
427 if (dptsr_planes != rcrtc->group->dptsr_planes) {
428 rcar_du_group_write(rcrtc->group, DPTSR,
429 (dptsr_planes << 16) | dptsr_planes);
430 rcrtc->group->dptsr_planes = dptsr_planes;
432 if (rcrtc->group->used_crtcs)
433 rcar_du_group_restart(rcrtc->group);
436 /* Restart the group if plane sources have changed. */
437 if (rcrtc->group->need_restart)
438 rcar_du_group_restart(rcrtc->group);
440 mutex_unlock(&rcrtc->group->lock);
442 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
446 /* -----------------------------------------------------------------------------
450 void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
452 struct drm_pending_vblank_event *event;
453 struct drm_device *dev = rcrtc->crtc.dev;
456 spin_lock_irqsave(&dev->event_lock, flags);
457 event = rcrtc->event;
459 spin_unlock_irqrestore(&dev->event_lock, flags);
464 spin_lock_irqsave(&dev->event_lock, flags);
465 drm_crtc_send_vblank_event(&rcrtc->crtc, event);
466 wake_up(&rcrtc->flip_wait);
467 spin_unlock_irqrestore(&dev->event_lock, flags);
469 drm_crtc_vblank_put(&rcrtc->crtc);
472 static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
474 struct drm_device *dev = rcrtc->crtc.dev;
478 spin_lock_irqsave(&dev->event_lock, flags);
479 pending = rcrtc->event != NULL;
480 spin_unlock_irqrestore(&dev->event_lock, flags);
485 static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
487 struct rcar_du_device *rcdu = rcrtc->group->dev;
489 if (wait_event_timeout(rcrtc->flip_wait,
490 !rcar_du_crtc_page_flip_pending(rcrtc),
491 msecs_to_jiffies(50)))
494 dev_warn(rcdu->dev, "page flip timeout\n");
496 rcar_du_crtc_finish_page_flip(rcrtc);
499 /* -----------------------------------------------------------------------------
500 * Start/Stop and Suspend/Resume
503 static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
505 /* Set display off and background to black */
506 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
507 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
509 /* Configure display timings and output routing */
510 rcar_du_crtc_set_display_timing(rcrtc);
511 rcar_du_group_set_routing(rcrtc->group);
513 /* Start with all planes disabled. */
514 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
516 /* Enable the VSP compositor. */
517 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
518 rcar_du_vsp_enable(rcrtc);
520 /* Turn vertical blanking interrupt reporting on. */
521 drm_crtc_vblank_on(&rcrtc->crtc);
524 static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
529 * Guard against double-get, as the function is called from both the
530 * .atomic_enable() and .atomic_begin() handlers.
532 if (rcrtc->initialized)
535 ret = clk_prepare_enable(rcrtc->clock);
539 ret = clk_prepare_enable(rcrtc->extclock);
543 ret = rcar_du_group_get(rcrtc->group);
547 rcar_du_crtc_setup(rcrtc);
548 rcrtc->initialized = true;
553 clk_disable_unprepare(rcrtc->extclock);
555 clk_disable_unprepare(rcrtc->clock);
559 static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
561 rcar_du_group_put(rcrtc->group);
563 clk_disable_unprepare(rcrtc->extclock);
564 clk_disable_unprepare(rcrtc->clock);
566 rcrtc->initialized = false;
569 static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
574 * Select master sync mode. This enables display operation in master
575 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
578 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
579 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
580 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
583 rcar_du_group_start_stop(rcrtc->group, true);
586 static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
588 struct rcar_du_device *rcdu = rcrtc->group->dev;
589 struct drm_crtc *crtc = &rcrtc->crtc;
592 /* Make sure vblank interrupts are enabled. */
593 drm_crtc_vblank_get(crtc);
596 * Disable planes and calculate how many vertical blanking interrupts we
597 * have to wait for. If a vertical blanking interrupt has been triggered
598 * but not processed yet, we don't know whether it occurred before or
599 * after the planes got disabled. We thus have to wait for two vblank
600 * interrupts in that case.
602 spin_lock_irq(&rcrtc->vblank_lock);
603 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
604 status = rcar_du_crtc_read(rcrtc, DSSR);
605 rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
606 spin_unlock_irq(&rcrtc->vblank_lock);
608 if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
609 msecs_to_jiffies(100)))
610 dev_warn(rcdu->dev, "vertical blanking timeout\n");
612 drm_crtc_vblank_put(crtc);
615 static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
617 struct drm_crtc *crtc = &rcrtc->crtc;
620 * Disable all planes and wait for the change to take effect. This is
621 * required as the plane enable registers are updated on vblank, and no
622 * vblank will occur once the CRTC is stopped. Disabling planes when
623 * starting the CRTC thus wouldn't be enough as it would start scanning
624 * out immediately from old frame buffers until the next vblank.
626 * This increases the CRTC stop delay, especially when multiple CRTCs
627 * are stopped in one operation as we now wait for one vblank per CRTC.
628 * Whether this can be improved needs to be researched.
630 rcar_du_crtc_disable_planes(rcrtc);
633 * Disable vertical blanking interrupt reporting. We first need to wait
634 * for page flip completion before stopping the CRTC as userspace
635 * expects page flips to eventually complete.
637 rcar_du_crtc_wait_page_flip(rcrtc);
638 drm_crtc_vblank_off(crtc);
640 /* Disable the VSP compositor. */
641 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
642 rcar_du_vsp_disable(rcrtc);
645 * Select switch sync mode. This stops display operation and configures
646 * the HSYNC and VSYNC signals as inputs.
648 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
650 rcar_du_group_start_stop(rcrtc->group, false);
653 /* -----------------------------------------------------------------------------
657 static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
658 struct drm_crtc_state *old_state)
660 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
662 rcar_du_crtc_get(rcrtc);
663 rcar_du_crtc_start(rcrtc);
666 static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
667 struct drm_crtc_state *old_state)
669 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
671 rcar_du_crtc_stop(rcrtc);
672 rcar_du_crtc_put(rcrtc);
674 spin_lock_irq(&crtc->dev->event_lock);
675 if (crtc->state->event) {
676 drm_crtc_send_vblank_event(crtc, crtc->state->event);
677 crtc->state->event = NULL;
679 spin_unlock_irq(&crtc->dev->event_lock);
684 static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
685 struct drm_crtc_state *old_crtc_state)
687 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
689 WARN_ON(!crtc->state->enable);
692 * If a mode set is in progress we can be called with the CRTC disabled.
693 * We thus need to first get and setup the CRTC in order to configure
694 * planes. We must *not* put the CRTC in .atomic_flush(), as it must be
695 * kept awake until the .atomic_enable() call that will follow. The get
696 * operation in .atomic_enable() will in that case be a no-op, and the
697 * CRTC will be put later in .atomic_disable().
699 * If a mode set is not in progress the CRTC is enabled, and the
700 * following get call will be a no-op. There is thus no need to belance
701 * it in .atomic_flush() either.
703 rcar_du_crtc_get(rcrtc);
705 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
706 rcar_du_vsp_atomic_begin(rcrtc);
709 static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
710 struct drm_crtc_state *old_crtc_state)
712 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
713 struct drm_device *dev = rcrtc->crtc.dev;
716 rcar_du_crtc_update_planes(rcrtc);
718 if (crtc->state->event) {
719 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
721 spin_lock_irqsave(&dev->event_lock, flags);
722 rcrtc->event = crtc->state->event;
723 crtc->state->event = NULL;
724 spin_unlock_irqrestore(&dev->event_lock, flags);
727 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
728 rcar_du_vsp_atomic_flush(rcrtc);
731 enum drm_mode_status rcar_du_crtc_mode_valid(struct drm_crtc *crtc,
732 const struct drm_display_mode *mode)
734 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
735 struct rcar_du_device *rcdu = rcrtc->group->dev;
736 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
738 if (interlaced && !rcar_du_has(rcdu, RCAR_DU_FEATURE_INTERLACED))
739 return MODE_NO_INTERLACE;
744 static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
745 .atomic_begin = rcar_du_crtc_atomic_begin,
746 .atomic_flush = rcar_du_crtc_atomic_flush,
747 .atomic_enable = rcar_du_crtc_atomic_enable,
748 .atomic_disable = rcar_du_crtc_atomic_disable,
749 .mode_valid = rcar_du_crtc_mode_valid,
752 static void rcar_du_crtc_crc_init(struct rcar_du_crtc *rcrtc)
754 struct rcar_du_device *rcdu = rcrtc->group->dev;
755 const char **sources;
759 /* CRC available only on Gen3 HW. */
760 if (rcdu->info->gen < 3)
763 /* Reserve 1 for "auto" source. */
764 count = rcrtc->vsp->num_planes + 1;
766 sources = kmalloc_array(count, sizeof(*sources), GFP_KERNEL);
770 sources[0] = kstrdup("auto", GFP_KERNEL);
774 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
775 struct drm_plane *plane = &rcrtc->vsp->planes[i].plane;
778 sprintf(name, "plane%u", plane->base.id);
779 sources[i + 1] = kstrdup(name, GFP_KERNEL);
784 rcrtc->sources = sources;
785 rcrtc->sources_count = count;
796 static void rcar_du_crtc_crc_cleanup(struct rcar_du_crtc *rcrtc)
803 for (i = 0; i < rcrtc->sources_count; i++)
804 kfree(rcrtc->sources[i]);
805 kfree(rcrtc->sources);
807 rcrtc->sources = NULL;
808 rcrtc->sources_count = 0;
811 static struct drm_crtc_state *
812 rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
814 struct rcar_du_crtc_state *state;
815 struct rcar_du_crtc_state *copy;
817 if (WARN_ON(!crtc->state))
820 state = to_rcar_crtc_state(crtc->state);
821 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
825 __drm_atomic_helper_crtc_duplicate_state(crtc, ©->state);
830 static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
831 struct drm_crtc_state *state)
833 __drm_atomic_helper_crtc_destroy_state(state);
834 kfree(to_rcar_crtc_state(state));
837 static void rcar_du_crtc_cleanup(struct drm_crtc *crtc)
839 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
841 rcar_du_crtc_crc_cleanup(rcrtc);
843 return drm_crtc_cleanup(crtc);
846 static void rcar_du_crtc_reset(struct drm_crtc *crtc)
848 struct rcar_du_crtc_state *state;
851 rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
855 state = kzalloc(sizeof(*state), GFP_KERNEL);
859 state->crc.source = VSP1_DU_CRC_NONE;
860 state->crc.index = 0;
862 crtc->state = &state->state;
863 crtc->state->crtc = crtc;
866 static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
868 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
870 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
871 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
872 rcrtc->vblank_enable = true;
877 static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
879 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
881 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
882 rcrtc->vblank_enable = false;
885 static int rcar_du_crtc_parse_crc_source(struct rcar_du_crtc *rcrtc,
886 const char *source_name,
887 enum vsp1_du_crc_source *source)
893 * Parse the source name. Supported values are "plane%u" to compute the
894 * CRC on an input plane (%u is the plane ID), and "auto" to compute the
895 * CRC on the composer (VSP) output.
899 *source = VSP1_DU_CRC_NONE;
901 } else if (!strcmp(source_name, "auto")) {
902 *source = VSP1_DU_CRC_OUTPUT;
904 } else if (strstarts(source_name, "plane")) {
907 *source = VSP1_DU_CRC_PLANE;
909 ret = kstrtouint(source_name + strlen("plane"), 10, &index);
913 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
914 if (index == rcrtc->vsp->planes[i].plane.base.id)
922 static int rcar_du_crtc_verify_crc_source(struct drm_crtc *crtc,
923 const char *source_name,
926 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
927 enum vsp1_du_crc_source source;
929 if (rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source) < 0) {
930 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
938 const char *const *rcar_du_crtc_get_crc_sources(struct drm_crtc *crtc,
941 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
943 *count = rcrtc->sources_count;
944 return rcrtc->sources;
947 static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
948 const char *source_name)
950 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
951 struct drm_modeset_acquire_ctx ctx;
952 struct drm_crtc_state *crtc_state;
953 struct drm_atomic_state *state;
954 enum vsp1_du_crc_source source;
958 ret = rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source);
964 /* Perform an atomic commit to set the CRC source. */
965 drm_modeset_acquire_init(&ctx, 0);
967 state = drm_atomic_state_alloc(crtc->dev);
973 state->acquire_ctx = &ctx;
976 crtc_state = drm_atomic_get_crtc_state(state, crtc);
977 if (!IS_ERR(crtc_state)) {
978 struct rcar_du_crtc_state *rcrtc_state;
980 rcrtc_state = to_rcar_crtc_state(crtc_state);
981 rcrtc_state->crc.source = source;
982 rcrtc_state->crc.index = index;
984 ret = drm_atomic_commit(state);
986 ret = PTR_ERR(crtc_state);
989 if (ret == -EDEADLK) {
990 drm_atomic_state_clear(state);
991 drm_modeset_backoff(&ctx);
995 drm_atomic_state_put(state);
998 drm_modeset_drop_locks(&ctx);
999 drm_modeset_acquire_fini(&ctx);
1004 static const struct drm_crtc_funcs crtc_funcs_gen2 = {
1005 .reset = rcar_du_crtc_reset,
1006 .destroy = drm_crtc_cleanup,
1007 .set_config = drm_atomic_helper_set_config,
1008 .page_flip = drm_atomic_helper_page_flip,
1009 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1010 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1011 .enable_vblank = rcar_du_crtc_enable_vblank,
1012 .disable_vblank = rcar_du_crtc_disable_vblank,
1015 static const struct drm_crtc_funcs crtc_funcs_gen3 = {
1016 .reset = rcar_du_crtc_reset,
1017 .destroy = rcar_du_crtc_cleanup,
1018 .set_config = drm_atomic_helper_set_config,
1019 .page_flip = drm_atomic_helper_page_flip,
1020 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
1021 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
1022 .enable_vblank = rcar_du_crtc_enable_vblank,
1023 .disable_vblank = rcar_du_crtc_disable_vblank,
1024 .set_crc_source = rcar_du_crtc_set_crc_source,
1025 .verify_crc_source = rcar_du_crtc_verify_crc_source,
1026 .get_crc_sources = rcar_du_crtc_get_crc_sources,
1029 /* -----------------------------------------------------------------------------
1030 * Interrupt Handling
1033 static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
1035 struct rcar_du_crtc *rcrtc = arg;
1036 struct rcar_du_device *rcdu = rcrtc->group->dev;
1037 irqreturn_t ret = IRQ_NONE;
1040 spin_lock(&rcrtc->vblank_lock);
1042 status = rcar_du_crtc_read(rcrtc, DSSR);
1043 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
1045 if (status & DSSR_VBK) {
1047 * Wake up the vblank wait if the counter reaches 0. This must
1048 * be protected by the vblank_lock to avoid races in
1049 * rcar_du_crtc_disable_planes().
1051 if (rcrtc->vblank_count) {
1052 if (--rcrtc->vblank_count == 0)
1053 wake_up(&rcrtc->vblank_wait);
1057 spin_unlock(&rcrtc->vblank_lock);
1059 if (status & DSSR_VBK) {
1060 if (rcdu->info->gen < 3) {
1061 drm_crtc_handle_vblank(&rcrtc->crtc);
1062 rcar_du_crtc_finish_page_flip(rcrtc);
1071 /* -----------------------------------------------------------------------------
1075 int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
1076 unsigned int hwindex)
1078 static const unsigned int mmio_offsets[] = {
1079 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
1082 struct rcar_du_device *rcdu = rgrp->dev;
1083 struct platform_device *pdev = to_platform_device(rcdu->dev);
1084 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
1085 struct drm_crtc *crtc = &rcrtc->crtc;
1086 struct drm_plane *primary;
1087 unsigned int irqflags;
1094 /* Get the CRTC clock and the optional external clock. */
1095 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1096 sprintf(clk_name, "du.%u", hwindex);
1102 rcrtc->clock = devm_clk_get(rcdu->dev, name);
1103 if (IS_ERR(rcrtc->clock)) {
1104 dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
1105 return PTR_ERR(rcrtc->clock);
1108 sprintf(clk_name, "dclkin.%u", hwindex);
1109 clk = devm_clk_get(rcdu->dev, clk_name);
1111 rcrtc->extclock = clk;
1112 } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
1113 dev_info(rcdu->dev, "can't get external clock %u\n", hwindex);
1114 return -EPROBE_DEFER;
1117 init_waitqueue_head(&rcrtc->flip_wait);
1118 init_waitqueue_head(&rcrtc->vblank_wait);
1119 spin_lock_init(&rcrtc->vblank_lock);
1121 rcrtc->group = rgrp;
1122 rcrtc->mmio_offset = mmio_offsets[hwindex];
1123 rcrtc->index = hwindex;
1125 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
1126 primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
1128 primary = &rgrp->planes[swindex % 2].plane;
1130 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
1131 rcdu->info->gen <= 2 ?
1132 &crtc_funcs_gen2 : &crtc_funcs_gen3,
1137 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
1139 /* Start with vertical blanking interrupt reporting disabled. */
1140 drm_crtc_vblank_off(crtc);
1142 /* Register the interrupt handler. */
1143 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
1144 /* The IRQ's are associated with the CRTC (sw)index. */
1145 irq = platform_get_irq(pdev, swindex);
1148 irq = platform_get_irq(pdev, 0);
1149 irqflags = IRQF_SHARED;
1153 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
1157 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
1158 dev_name(rcdu->dev), rcrtc);
1161 "failed to register IRQ for CRTC %u\n", swindex);
1165 rcar_du_crtc_crc_init(rcrtc);