drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / rcar-du / rcar_du_crtc.c
blob15dc9caa128baeb7ed8c1f3ca133cb160cb739c9
1 /*
2 * rcar_du_crtc.c -- R-Car Display Unit CRTCs
4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/mutex.h>
16 #include <linux/sys_soc.h>
18 #include <drm/drmP.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_crtc_helper.h>
23 #include <drm/drm_fb_cma_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_plane_helper.h>
27 #include "rcar_du_crtc.h"
28 #include "rcar_du_drv.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"
34 static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
36 struct rcar_du_device *rcdu = rcrtc->group->dev;
38 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
41 static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
43 struct rcar_du_device *rcdu = rcrtc->group->dev;
45 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
48 static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
50 struct rcar_du_device *rcdu = rcrtc->group->dev;
52 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
53 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
56 static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
58 struct rcar_du_device *rcdu = rcrtc->group->dev;
60 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
61 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
64 static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
65 u32 clr, u32 set)
67 struct rcar_du_device *rcdu = rcrtc->group->dev;
68 u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
70 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
73 static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
75 int ret;
77 ret = clk_prepare_enable(rcrtc->clock);
78 if (ret < 0)
79 return ret;
81 ret = clk_prepare_enable(rcrtc->extclock);
82 if (ret < 0)
83 goto error_clock;
85 ret = rcar_du_group_get(rcrtc->group);
86 if (ret < 0)
87 goto error_group;
89 return 0;
91 error_group:
92 clk_disable_unprepare(rcrtc->extclock);
93 error_clock:
94 clk_disable_unprepare(rcrtc->clock);
95 return ret;
98 static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
100 rcar_du_group_put(rcrtc->group);
102 clk_disable_unprepare(rcrtc->extclock);
103 clk_disable_unprepare(rcrtc->clock);
106 /* -----------------------------------------------------------------------------
107 * Hardware Setup
110 struct dpll_info {
111 unsigned int output;
112 unsigned int fdpll;
113 unsigned int n;
114 unsigned int m;
117 static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
118 struct dpll_info *dpll,
119 unsigned long input,
120 unsigned long target)
122 unsigned long best_diff = (unsigned long)-1;
123 unsigned long diff;
124 unsigned int fdpll;
125 unsigned int m;
126 unsigned int n;
129 * fin fvco fout fclkout
130 * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
131 * +-> | | |
132 * | |
133 * +---------------- [1/N] <------------+
135 * fclkout = fvco / P / FDPLL -- (1)
137 * fin/M = fvco/P/N
139 * fvco = fin * P * N / M -- (2)
141 * (1) + (2) indicates
143 * fclkout = fin * N / M / FDPLL
145 * NOTES
146 * N : (n + 1)
147 * M : (m + 1)
148 * FDPLL : (fdpll + 1)
149 * P : 2
150 * 2kHz < fvco < 4096MHz
152 * To minimize the jitter,
153 * N : as large as possible
154 * M : as small as possible
156 for (m = 0; m < 4; m++) {
157 for (n = 119; n > 38; n--) {
159 * This code only runs on 64-bit architectures, the
160 * unsigned long type can thus be used for 64-bit
161 * computation. It will still compile without any
162 * warning on 32-bit architectures.
164 * To optimize calculations, use fout instead of fvco
165 * to verify the VCO frequency constraint.
167 unsigned long fout = input * (n + 1) / (m + 1);
169 if (fout < 1000 || fout > 2048 * 1000 * 1000U)
170 continue;
172 for (fdpll = 1; fdpll < 32; fdpll++) {
173 unsigned long output;
175 output = fout / (fdpll + 1);
176 if (output >= 400 * 1000 * 1000)
177 continue;
179 diff = abs((long)output - (long)target);
180 if (best_diff > diff) {
181 best_diff = diff;
182 dpll->n = n;
183 dpll->m = m;
184 dpll->fdpll = fdpll;
185 dpll->output = output;
188 if (diff == 0)
189 goto done;
194 done:
195 dev_dbg(rcrtc->group->dev->dev,
196 "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
197 dpll->output, dpll->fdpll, dpll->n, dpll->m,
198 best_diff);
201 static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
202 { .soc_id = "r8a7795", .revision = "ES1.*" },
203 { /* sentinel */ }
206 static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
208 const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
209 struct rcar_du_device *rcdu = rcrtc->group->dev;
210 unsigned long mode_clock = mode->clock * 1000;
211 unsigned long clk;
212 u32 value;
213 u32 escr;
214 u32 div;
217 * Compute the clock divisor and select the internal or external dot
218 * clock based on the requested frequency.
220 clk = clk_get_rate(rcrtc->clock);
221 div = DIV_ROUND_CLOSEST(clk, mode_clock);
222 div = clamp(div, 1U, 64U) - 1;
223 escr = div | ESCR_DCLKSEL_CLKS;
225 if (rcrtc->extclock) {
226 struct dpll_info dpll = { 0 };
227 unsigned long extclk;
228 unsigned long extrate;
229 unsigned long rate;
230 u32 extdiv;
232 extclk = clk_get_rate(rcrtc->extclock);
233 if (rcdu->info->dpll_ch & (1 << rcrtc->index)) {
234 unsigned long target = mode_clock;
237 * The H3 ES1.x exhibits dot clock duty cycle stability
238 * issues. We can work around them by configuring the
239 * DPLL to twice the desired frequency, coupled with a
240 * /2 post-divider. This isn't needed on other SoCs and
241 * breaks HDMI output on M3-W for a currently unknown
242 * reason, so restrict the workaround to H3 ES1.x.
244 if (soc_device_match(rcar_du_r8a7795_es1))
245 target *= 2;
247 rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
248 extclk = dpll.output;
251 extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
252 extdiv = clamp(extdiv, 1U, 64U) - 1;
254 rate = clk / (div + 1);
255 extrate = extclk / (extdiv + 1);
257 if (abs((long)extrate - (long)mode_clock) <
258 abs((long)rate - (long)mode_clock)) {
260 if (rcdu->info->dpll_ch & (1 << rcrtc->index)) {
261 u32 dpllcr = DPLLCR_CODE | DPLLCR_CLKE
262 | DPLLCR_FDPLL(dpll.fdpll)
263 | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
264 | DPLLCR_STBY;
266 if (rcrtc->index == 1)
267 dpllcr |= DPLLCR_PLCS1
268 | DPLLCR_INCS_DOTCLKIN1;
269 else
270 dpllcr |= DPLLCR_PLCS0
271 | DPLLCR_INCS_DOTCLKIN0;
273 rcar_du_group_write(rcrtc->group, DPLLCR,
274 dpllcr);
277 escr = ESCR_DCLKSEL_DCLKIN | extdiv;
280 dev_dbg(rcrtc->group->dev->dev,
281 "mode clock %lu extrate %lu rate %lu ESCR 0x%08x\n",
282 mode_clock, extrate, rate, escr);
285 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
286 escr);
287 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
289 /* Signal polarities */
290 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
291 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
292 | DSMR_DIPM_DISP | DSMR_CSPM;
293 rcar_du_crtc_write(rcrtc, DSMR, value);
295 /* Display timings */
296 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
297 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
298 mode->hdisplay - 19);
299 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
300 mode->hsync_start - 1);
301 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
303 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
304 mode->crtc_vsync_end - 2);
305 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
306 mode->crtc_vsync_end +
307 mode->crtc_vdisplay - 2);
308 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
309 mode->crtc_vsync_end +
310 mode->crtc_vsync_start - 1);
311 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
313 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
314 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
317 void rcar_du_crtc_route_output(struct drm_crtc *crtc,
318 enum rcar_du_output output)
320 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
321 struct rcar_du_device *rcdu = rcrtc->group->dev;
324 * Store the route from the CRTC output to the DU output. The DU will be
325 * configured when starting the CRTC.
327 rcrtc->outputs |= BIT(output);
330 * Store RGB routing to DPAD0, the hardware will be configured when
331 * starting the CRTC.
333 if (output == RCAR_DU_OUTPUT_DPAD0)
334 rcdu->dpad0_source = rcrtc->index;
337 static unsigned int plane_zpos(struct rcar_du_plane *plane)
339 return plane->plane.state->normalized_zpos;
342 static const struct rcar_du_format_info *
343 plane_format(struct rcar_du_plane *plane)
345 return to_rcar_plane_state(plane->plane.state)->format;
348 static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
350 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
351 struct rcar_du_device *rcdu = rcrtc->group->dev;
352 unsigned int num_planes = 0;
353 unsigned int dptsr_planes;
354 unsigned int hwplanes = 0;
355 unsigned int prio = 0;
356 unsigned int i;
357 u32 dspr = 0;
359 for (i = 0; i < rcrtc->group->num_planes; ++i) {
360 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
361 unsigned int j;
363 if (plane->plane.state->crtc != &rcrtc->crtc ||
364 !plane->plane.state->visible)
365 continue;
367 /* Insert the plane in the sorted planes array. */
368 for (j = num_planes++; j > 0; --j) {
369 if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
370 break;
371 planes[j] = planes[j-1];
374 planes[j] = plane;
375 prio += plane_format(plane)->planes * 4;
378 for (i = 0; i < num_planes; ++i) {
379 struct rcar_du_plane *plane = planes[i];
380 struct drm_plane_state *state = plane->plane.state;
381 unsigned int index = to_rcar_plane_state(state)->hwindex;
383 prio -= 4;
384 dspr |= (index + 1) << prio;
385 hwplanes |= 1 << index;
387 if (plane_format(plane)->planes == 2) {
388 index = (index + 1) % 8;
390 prio -= 4;
391 dspr |= (index + 1) << prio;
392 hwplanes |= 1 << index;
396 /* If VSP+DU integration is enabled the plane assignment is fixed. */
397 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
398 if (rcdu->info->gen < 3) {
399 dspr = (rcrtc->index % 2) + 1;
400 hwplanes = 1 << (rcrtc->index % 2);
401 } else {
402 dspr = (rcrtc->index % 2) ? 3 : 1;
403 hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
408 * Update the planes to display timing and dot clock generator
409 * associations.
411 * Updating the DPTSR register requires restarting the CRTC group,
412 * resulting in visible flicker. To mitigate the issue only update the
413 * association if needed by enabled planes. Planes being disabled will
414 * keep their current association.
416 mutex_lock(&rcrtc->group->lock);
418 dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
419 : rcrtc->group->dptsr_planes & ~hwplanes;
421 if (dptsr_planes != rcrtc->group->dptsr_planes) {
422 rcar_du_group_write(rcrtc->group, DPTSR,
423 (dptsr_planes << 16) | dptsr_planes);
424 rcrtc->group->dptsr_planes = dptsr_planes;
426 if (rcrtc->group->used_crtcs)
427 rcar_du_group_restart(rcrtc->group);
430 /* Restart the group if plane sources have changed. */
431 if (rcrtc->group->need_restart)
432 rcar_du_group_restart(rcrtc->group);
434 mutex_unlock(&rcrtc->group->lock);
436 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
437 dspr);
440 /* -----------------------------------------------------------------------------
441 * Page Flip
444 void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
446 struct drm_pending_vblank_event *event;
447 struct drm_device *dev = rcrtc->crtc.dev;
448 unsigned long flags;
450 spin_lock_irqsave(&dev->event_lock, flags);
451 event = rcrtc->event;
452 rcrtc->event = NULL;
453 spin_unlock_irqrestore(&dev->event_lock, flags);
455 if (event == NULL)
456 return;
458 spin_lock_irqsave(&dev->event_lock, flags);
459 drm_crtc_send_vblank_event(&rcrtc->crtc, event);
460 wake_up(&rcrtc->flip_wait);
461 spin_unlock_irqrestore(&dev->event_lock, flags);
463 drm_crtc_vblank_put(&rcrtc->crtc);
466 static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
468 struct drm_device *dev = rcrtc->crtc.dev;
469 unsigned long flags;
470 bool pending;
472 spin_lock_irqsave(&dev->event_lock, flags);
473 pending = rcrtc->event != NULL;
474 spin_unlock_irqrestore(&dev->event_lock, flags);
476 return pending;
479 static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
481 struct rcar_du_device *rcdu = rcrtc->group->dev;
483 if (wait_event_timeout(rcrtc->flip_wait,
484 !rcar_du_crtc_page_flip_pending(rcrtc),
485 msecs_to_jiffies(50)))
486 return;
488 dev_warn(rcdu->dev, "page flip timeout\n");
490 rcar_du_crtc_finish_page_flip(rcrtc);
493 /* -----------------------------------------------------------------------------
494 * Start/Stop and Suspend/Resume
497 static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
499 /* Set display off and background to black */
500 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
501 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
503 /* Configure display timings and output routing */
504 rcar_du_crtc_set_display_timing(rcrtc);
505 rcar_du_group_set_routing(rcrtc->group);
507 /* Start with all planes disabled. */
508 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
510 /* Enable the VSP compositor. */
511 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
512 rcar_du_vsp_enable(rcrtc);
514 /* Turn vertical blanking interrupt reporting on. */
515 drm_crtc_vblank_on(&rcrtc->crtc);
518 static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
520 bool interlaced;
523 * Select master sync mode. This enables display operation in master
524 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
525 * actively driven).
527 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
528 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
529 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
530 DSYSR_TVM_MASTER);
532 rcar_du_group_start_stop(rcrtc->group, true);
535 static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
537 struct rcar_du_device *rcdu = rcrtc->group->dev;
538 struct drm_crtc *crtc = &rcrtc->crtc;
539 u32 status;
541 /* Make sure vblank interrupts are enabled. */
542 drm_crtc_vblank_get(crtc);
545 * Disable planes and calculate how many vertical blanking interrupts we
546 * have to wait for. If a vertical blanking interrupt has been triggered
547 * but not processed yet, we don't know whether it occurred before or
548 * after the planes got disabled. We thus have to wait for two vblank
549 * interrupts in that case.
551 spin_lock_irq(&rcrtc->vblank_lock);
552 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
553 status = rcar_du_crtc_read(rcrtc, DSSR);
554 rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
555 spin_unlock_irq(&rcrtc->vblank_lock);
557 if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
558 msecs_to_jiffies(100)))
559 dev_warn(rcdu->dev, "vertical blanking timeout\n");
561 drm_crtc_vblank_put(crtc);
564 static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
566 struct drm_crtc *crtc = &rcrtc->crtc;
569 * Disable all planes and wait for the change to take effect. This is
570 * required as the plane enable registers are updated on vblank, and no
571 * vblank will occur once the CRTC is stopped. Disabling planes when
572 * starting the CRTC thus wouldn't be enough as it would start scanning
573 * out immediately from old frame buffers until the next vblank.
575 * This increases the CRTC stop delay, especially when multiple CRTCs
576 * are stopped in one operation as we now wait for one vblank per CRTC.
577 * Whether this can be improved needs to be researched.
579 rcar_du_crtc_disable_planes(rcrtc);
582 * Disable vertical blanking interrupt reporting. We first need to wait
583 * for page flip completion before stopping the CRTC as userspace
584 * expects page flips to eventually complete.
586 rcar_du_crtc_wait_page_flip(rcrtc);
587 drm_crtc_vblank_off(crtc);
589 /* Disable the VSP compositor. */
590 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
591 rcar_du_vsp_disable(rcrtc);
594 * Select switch sync mode. This stops display operation and configures
595 * the HSYNC and VSYNC signals as inputs.
597 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
599 rcar_du_group_start_stop(rcrtc->group, false);
602 /* -----------------------------------------------------------------------------
603 * CRTC Functions
606 static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
607 struct drm_crtc_state *old_state)
609 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
612 * If the CRTC has already been setup by the .atomic_begin() handler we
613 * can skip the setup stage.
615 if (!rcrtc->initialized) {
616 rcar_du_crtc_get(rcrtc);
617 rcar_du_crtc_setup(rcrtc);
618 rcrtc->initialized = true;
621 rcar_du_crtc_start(rcrtc);
624 static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
625 struct drm_crtc_state *old_state)
627 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
629 rcar_du_crtc_stop(rcrtc);
630 rcar_du_crtc_put(rcrtc);
632 spin_lock_irq(&crtc->dev->event_lock);
633 if (crtc->state->event) {
634 drm_crtc_send_vblank_event(crtc, crtc->state->event);
635 crtc->state->event = NULL;
637 spin_unlock_irq(&crtc->dev->event_lock);
639 rcrtc->initialized = false;
640 rcrtc->outputs = 0;
643 static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
644 struct drm_crtc_state *old_crtc_state)
646 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
648 WARN_ON(!crtc->state->enable);
651 * If a mode set is in progress we can be called with the CRTC disabled.
652 * We then need to first setup the CRTC in order to configure planes.
653 * The .atomic_enable() handler will notice and skip the CRTC setup.
655 if (!rcrtc->initialized) {
656 rcar_du_crtc_get(rcrtc);
657 rcar_du_crtc_setup(rcrtc);
658 rcrtc->initialized = true;
661 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
662 rcar_du_vsp_atomic_begin(rcrtc);
665 static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
666 struct drm_crtc_state *old_crtc_state)
668 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
669 struct drm_device *dev = rcrtc->crtc.dev;
670 unsigned long flags;
672 rcar_du_crtc_update_planes(rcrtc);
674 if (crtc->state->event) {
675 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
677 spin_lock_irqsave(&dev->event_lock, flags);
678 rcrtc->event = crtc->state->event;
679 crtc->state->event = NULL;
680 spin_unlock_irqrestore(&dev->event_lock, flags);
683 if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
684 rcar_du_vsp_atomic_flush(rcrtc);
687 static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
688 .atomic_begin = rcar_du_crtc_atomic_begin,
689 .atomic_flush = rcar_du_crtc_atomic_flush,
690 .atomic_enable = rcar_du_crtc_atomic_enable,
691 .atomic_disable = rcar_du_crtc_atomic_disable,
694 static struct drm_crtc_state *
695 rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
697 struct rcar_du_crtc_state *state;
698 struct rcar_du_crtc_state *copy;
700 if (WARN_ON(!crtc->state))
701 return NULL;
703 state = to_rcar_crtc_state(crtc->state);
704 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
705 if (copy == NULL)
706 return NULL;
708 __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->state);
710 return &copy->state;
713 static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
714 struct drm_crtc_state *state)
716 __drm_atomic_helper_crtc_destroy_state(state);
717 kfree(to_rcar_crtc_state(state));
720 static void rcar_du_crtc_reset(struct drm_crtc *crtc)
722 struct rcar_du_crtc_state *state;
724 if (crtc->state) {
725 rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
726 crtc->state = NULL;
729 state = kzalloc(sizeof(*state), GFP_KERNEL);
730 if (state == NULL)
731 return;
733 state->crc.source = VSP1_DU_CRC_NONE;
734 state->crc.index = 0;
736 crtc->state = &state->state;
737 crtc->state->crtc = crtc;
740 static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
742 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
744 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
745 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
746 rcrtc->vblank_enable = true;
748 return 0;
751 static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
753 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
755 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
756 rcrtc->vblank_enable = false;
759 static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
760 const char *source_name,
761 size_t *values_cnt)
763 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
764 struct drm_modeset_acquire_ctx ctx;
765 struct drm_crtc_state *crtc_state;
766 struct drm_atomic_state *state;
767 enum vsp1_du_crc_source source;
768 unsigned int index = 0;
769 unsigned int i;
770 int ret;
773 * Parse the source name. Supported values are "plane%u" to compute the
774 * CRC on an input plane (%u is the plane ID), and "auto" to compute the
775 * CRC on the composer (VSP) output.
777 if (!source_name) {
778 source = VSP1_DU_CRC_NONE;
779 } else if (!strcmp(source_name, "auto")) {
780 source = VSP1_DU_CRC_OUTPUT;
781 } else if (strstarts(source_name, "plane")) {
782 source = VSP1_DU_CRC_PLANE;
784 ret = kstrtouint(source_name + strlen("plane"), 10, &index);
785 if (ret < 0)
786 return ret;
788 for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
789 if (index == rcrtc->vsp->planes[i].plane.base.id) {
790 index = i;
791 break;
795 if (i >= rcrtc->vsp->num_planes)
796 return -EINVAL;
797 } else {
798 return -EINVAL;
801 *values_cnt = 1;
803 /* Perform an atomic commit to set the CRC source. */
804 drm_modeset_acquire_init(&ctx, 0);
806 state = drm_atomic_state_alloc(crtc->dev);
807 if (!state) {
808 ret = -ENOMEM;
809 goto unlock;
812 state->acquire_ctx = &ctx;
814 retry:
815 crtc_state = drm_atomic_get_crtc_state(state, crtc);
816 if (!IS_ERR(crtc_state)) {
817 struct rcar_du_crtc_state *rcrtc_state;
819 rcrtc_state = to_rcar_crtc_state(crtc_state);
820 rcrtc_state->crc.source = source;
821 rcrtc_state->crc.index = index;
823 ret = drm_atomic_commit(state);
824 } else {
825 ret = PTR_ERR(crtc_state);
828 if (ret == -EDEADLK) {
829 drm_atomic_state_clear(state);
830 drm_modeset_backoff(&ctx);
831 goto retry;
834 drm_atomic_state_put(state);
836 unlock:
837 drm_modeset_drop_locks(&ctx);
838 drm_modeset_acquire_fini(&ctx);
840 return 0;
843 static const struct drm_crtc_funcs crtc_funcs_gen2 = {
844 .reset = rcar_du_crtc_reset,
845 .destroy = drm_crtc_cleanup,
846 .set_config = drm_atomic_helper_set_config,
847 .page_flip = drm_atomic_helper_page_flip,
848 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
849 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
850 .enable_vblank = rcar_du_crtc_enable_vblank,
851 .disable_vblank = rcar_du_crtc_disable_vblank,
854 static const struct drm_crtc_funcs crtc_funcs_gen3 = {
855 .reset = rcar_du_crtc_reset,
856 .destroy = drm_crtc_cleanup,
857 .set_config = drm_atomic_helper_set_config,
858 .page_flip = drm_atomic_helper_page_flip,
859 .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
860 .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
861 .enable_vblank = rcar_du_crtc_enable_vblank,
862 .disable_vblank = rcar_du_crtc_disable_vblank,
863 .set_crc_source = rcar_du_crtc_set_crc_source,
866 /* -----------------------------------------------------------------------------
867 * Interrupt Handling
870 static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
872 struct rcar_du_crtc *rcrtc = arg;
873 struct rcar_du_device *rcdu = rcrtc->group->dev;
874 irqreturn_t ret = IRQ_NONE;
875 u32 status;
877 spin_lock(&rcrtc->vblank_lock);
879 status = rcar_du_crtc_read(rcrtc, DSSR);
880 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
882 if (status & DSSR_VBK) {
884 * Wake up the vblank wait if the counter reaches 0. This must
885 * be protected by the vblank_lock to avoid races in
886 * rcar_du_crtc_disable_planes().
888 if (rcrtc->vblank_count) {
889 if (--rcrtc->vblank_count == 0)
890 wake_up(&rcrtc->vblank_wait);
894 spin_unlock(&rcrtc->vblank_lock);
896 if (status & DSSR_VBK) {
897 if (rcdu->info->gen < 3) {
898 drm_crtc_handle_vblank(&rcrtc->crtc);
899 rcar_du_crtc_finish_page_flip(rcrtc);
902 ret = IRQ_HANDLED;
905 return ret;
908 /* -----------------------------------------------------------------------------
909 * Initialization
912 int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
913 unsigned int hwindex)
915 static const unsigned int mmio_offsets[] = {
916 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
919 struct rcar_du_device *rcdu = rgrp->dev;
920 struct platform_device *pdev = to_platform_device(rcdu->dev);
921 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
922 struct drm_crtc *crtc = &rcrtc->crtc;
923 struct drm_plane *primary;
924 unsigned int irqflags;
925 struct clk *clk;
926 char clk_name[9];
927 char *name;
928 int irq;
929 int ret;
931 /* Get the CRTC clock and the optional external clock. */
932 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
933 sprintf(clk_name, "du.%u", hwindex);
934 name = clk_name;
935 } else {
936 name = NULL;
939 rcrtc->clock = devm_clk_get(rcdu->dev, name);
940 if (IS_ERR(rcrtc->clock)) {
941 dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
942 return PTR_ERR(rcrtc->clock);
945 sprintf(clk_name, "dclkin.%u", hwindex);
946 clk = devm_clk_get(rcdu->dev, clk_name);
947 if (!IS_ERR(clk)) {
948 rcrtc->extclock = clk;
949 } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
950 dev_info(rcdu->dev, "can't get external clock %u\n", hwindex);
951 return -EPROBE_DEFER;
954 init_waitqueue_head(&rcrtc->flip_wait);
955 init_waitqueue_head(&rcrtc->vblank_wait);
956 spin_lock_init(&rcrtc->vblank_lock);
958 rcrtc->group = rgrp;
959 rcrtc->mmio_offset = mmio_offsets[hwindex];
960 rcrtc->index = hwindex;
962 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
963 primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
964 else
965 primary = &rgrp->planes[swindex % 2].plane;
967 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
968 rcdu->info->gen <= 2 ?
969 &crtc_funcs_gen2 : &crtc_funcs_gen3,
970 NULL);
971 if (ret < 0)
972 return ret;
974 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
976 /* Start with vertical blanking interrupt reporting disabled. */
977 drm_crtc_vblank_off(crtc);
979 /* Register the interrupt handler. */
980 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
981 /* The IRQ's are associated with the CRTC (sw)index. */
982 irq = platform_get_irq(pdev, swindex);
983 irqflags = 0;
984 } else {
985 irq = platform_get_irq(pdev, 0);
986 irqflags = IRQF_SHARED;
989 if (irq < 0) {
990 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
991 return irq;
994 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
995 dev_name(rcdu->dev), rcrtc);
996 if (ret < 0) {
997 dev_err(rcdu->dev,
998 "failed to register IRQ for CRTC %u\n", swindex);
999 return ret;
1002 return 0;