blob: b87b8ffb898b97383064e9a66e7c16b33718118c [file] [log] [blame]
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02001/*
2 * rcar_du_crtc.c -- R-Car Display Unit CRTCs
3 *
Laurent Pinchart36d50462014-02-06 18:13:52 +01004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02005 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
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.
12 */
13
14#include <linux/clk.h>
15#include <linux/mutex.h>
16
17#include <drm/drmP.h>
Laurent Pinchart3e8da872015-02-20 11:30:59 +020018#include <drm/drm_atomic.h>
19#include <drm/drm_atomic_helper.h>
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020020#include <drm/drm_crtc.h>
21#include <drm/drm_crtc_helper.h>
22#include <drm/drm_fb_cma_helper.h>
23#include <drm/drm_gem_cma_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010024#include <drm/drm_plane_helper.h>
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020025
26#include "rcar_du_crtc.h"
27#include "rcar_du_drv.h"
28#include "rcar_du_kms.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020029#include "rcar_du_plane.h"
30#include "rcar_du_regs.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020031
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020032static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
33{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020034 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020035
36 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
37}
38
39static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
40{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020041 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020042
43 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
44}
45
46static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
47{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020048 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020049
50 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
51 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
52}
53
54static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
55{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020056 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020057
58 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
59 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
60}
61
62static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
63 u32 clr, u32 set)
64{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020065 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020066 u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
67
68 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
69}
70
Laurent Pinchartf66ee302013-06-14 14:15:01 +020071static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
72{
Laurent Pinchartf66ee302013-06-14 14:15:01 +020073 int ret;
74
75 ret = clk_prepare_enable(rcrtc->clock);
76 if (ret < 0)
77 return ret;
78
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020079 ret = clk_prepare_enable(rcrtc->extclock);
80 if (ret < 0)
81 goto error_clock;
82
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020083 ret = rcar_du_group_get(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020084 if (ret < 0)
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020085 goto error_group;
Laurent Pinchartf66ee302013-06-14 14:15:01 +020086
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020087 return 0;
88
89error_group:
90 clk_disable_unprepare(rcrtc->extclock);
91error_clock:
92 clk_disable_unprepare(rcrtc->clock);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020093 return ret;
94}
95
96static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
97{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020098 rcar_du_group_put(rcrtc->group);
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020099
100 clk_disable_unprepare(rcrtc->extclock);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200101 clk_disable_unprepare(rcrtc->clock);
102}
103
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200104/* -----------------------------------------------------------------------------
105 * Hardware Setup
106 */
107
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200108static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
109{
Laurent Pinchart845f4632015-02-18 15:47:27 +0200110 const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200111 unsigned long mode_clock = mode->clock * 1000;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200112 unsigned long clk;
113 u32 value;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200114 u32 escr;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200115 u32 div;
116
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200117 /* Compute the clock divisor and select the internal or external dot
118 * clock based on the requested frequency.
119 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200120 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200121 div = DIV_ROUND_CLOSEST(clk, mode_clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200122 div = clamp(div, 1U, 64U) - 1;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200123 escr = div | ESCR_DCLKSEL_CLKS;
124
125 if (rcrtc->extclock) {
126 unsigned long extclk;
127 unsigned long extrate;
128 unsigned long rate;
129 u32 extdiv;
130
131 extclk = clk_get_rate(rcrtc->extclock);
132 extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
133 extdiv = clamp(extdiv, 1U, 64U) - 1;
134
135 rate = clk / (div + 1);
136 extrate = extclk / (extdiv + 1);
137
138 if (abs((long)extrate - (long)mode_clock) <
139 abs((long)rate - (long)mode_clock)) {
140 dev_dbg(rcrtc->group->dev->dev,
141 "crtc%u: using external clock\n", rcrtc->index);
142 escr = extdiv | ESCR_DCLKSEL_DCLKIN;
143 }
144 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200145
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200146 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200147 escr);
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200148 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200149
150 /* Signal polarities */
151 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
152 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
Laurent Pinchartf67e1e02014-12-09 00:40:59 +0200153 | DSMR_DIPM_DE | DSMR_CSPM;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200154 rcar_du_crtc_write(rcrtc, DSMR, value);
155
156 /* Display timings */
157 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
158 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
159 mode->hdisplay - 19);
160 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
161 mode->hsync_start - 1);
162 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
163
Laurent Pinchart906eff72014-12-09 19:11:18 +0200164 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
165 mode->crtc_vsync_end - 2);
166 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
167 mode->crtc_vsync_end +
168 mode->crtc_vdisplay - 2);
169 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
170 mode->crtc_vsync_end +
171 mode->crtc_vsync_start - 1);
172 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200173
174 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
175 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
176}
177
Laurent Pinchartef67a902013-06-17 03:13:11 +0200178void rcar_du_crtc_route_output(struct drm_crtc *crtc,
179 enum rcar_du_output output)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200180{
181 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200182 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200183
184 /* Store the route from the CRTC output to the DU output. The DU will be
185 * configured when starting the CRTC.
186 */
Laurent Pinchartef67a902013-06-17 03:13:11 +0200187 rcrtc->outputs |= BIT(output);
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200188
Laurent Pinchart0c1c8772014-12-09 00:21:12 +0200189 /* Store RGB routing to DPAD0, the hardware will be configured when
190 * starting the CRTC.
191 */
192 if (output == RCAR_DU_OUTPUT_DPAD0)
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200193 rcdu->dpad0_source = rcrtc->index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200194}
195
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200196static unsigned int plane_zpos(struct rcar_du_plane *plane)
197{
Laurent Pinchartec69a402015-04-29 00:48:17 +0300198 return to_rcar_plane_state(plane->plane.state)->zpos;
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200199}
200
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200201static const struct rcar_du_format_info *
202plane_format(struct rcar_du_plane *plane)
203{
Laurent Pinchartec69a402015-04-29 00:48:17 +0300204 return to_rcar_plane_state(plane->plane.state)->format;
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200205}
206
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200207static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200208{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200209 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
210 unsigned int num_planes = 0;
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300211 unsigned int dptsr_planes;
212 unsigned int hwplanes = 0;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200213 unsigned int prio = 0;
214 unsigned int i;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200215 u32 dspr = 0;
216
Laurent Pinchartd6aed572015-05-25 16:32:45 +0300217 for (i = 0; i < rcrtc->group->num_planes; ++i) {
Laurent Pinchart99caede2015-04-29 00:05:56 +0300218 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200219 unsigned int j;
220
Laurent Pinchart47094192015-02-22 19:24:59 +0200221 if (plane->plane.state->crtc != &rcrtc->crtc)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200222 continue;
223
224 /* Insert the plane in the sorted planes array. */
225 for (j = num_planes++; j > 0; --j) {
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200226 if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200227 break;
228 planes[j] = planes[j-1];
229 }
230
231 planes[j] = plane;
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200232 prio += plane_format(plane)->planes * 4;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200233 }
234
235 for (i = 0; i < num_planes; ++i) {
236 struct rcar_du_plane *plane = planes[i];
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200237 struct drm_plane_state *state = plane->plane.state;
Laurent Pinchartec69a402015-04-29 00:48:17 +0300238 unsigned int index = to_rcar_plane_state(state)->hwindex;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200239
240 prio -= 4;
241 dspr |= (index + 1) << prio;
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300242 hwplanes |= 1 << index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200243
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200244 if (plane_format(plane)->planes == 2) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200245 index = (index + 1) % 8;
246
247 prio -= 4;
248 dspr |= (index + 1) << prio;
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300249 hwplanes |= 1 << index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200250 }
251 }
252
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300253 /* Update the planes to display timing and dot clock generator
254 * associations.
255 *
256 * Updating the DPTSR register requires restarting the CRTC group,
257 * resulting in visible flicker. To mitigate the issue only update the
258 * association if needed by enabled planes. Planes being disabled will
259 * keep their current association.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200260 */
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300261 mutex_lock(&rcrtc->group->lock);
262
263 dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
264 : rcrtc->group->dptsr_planes & ~hwplanes;
265
266 if (dptsr_planes != rcrtc->group->dptsr_planes) {
267 rcar_du_group_write(rcrtc->group, DPTSR,
268 (dptsr_planes << 16) | dptsr_planes);
269 rcrtc->group->dptsr_planes = dptsr_planes;
270
271 if (rcrtc->group->used_crtcs)
272 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200273 }
274
Laurent Pinchart2a57e9b2015-04-28 18:01:45 +0300275 mutex_unlock(&rcrtc->group->lock);
276
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200277 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
278 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200279}
280
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200281/* -----------------------------------------------------------------------------
282 * Page Flip
283 */
284
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200285static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
286{
287 struct drm_pending_vblank_event *event;
288 struct drm_device *dev = rcrtc->crtc.dev;
289 unsigned long flags;
290
291 spin_lock_irqsave(&dev->event_lock, flags);
292 event = rcrtc->event;
293 rcrtc->event = NULL;
294 spin_unlock_irqrestore(&dev->event_lock, flags);
295
296 if (event == NULL)
297 return;
298
299 spin_lock_irqsave(&dev->event_lock, flags);
300 drm_send_vblank_event(dev, rcrtc->index, event);
Laurent Pinchart36693f32015-02-18 13:21:56 +0200301 wake_up(&rcrtc->flip_wait);
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200302 spin_unlock_irqrestore(&dev->event_lock, flags);
303
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200304 drm_crtc_vblank_put(&rcrtc->crtc);
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200305}
306
Laurent Pinchart36693f32015-02-18 13:21:56 +0200307static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
308{
309 struct drm_device *dev = rcrtc->crtc.dev;
310 unsigned long flags;
311 bool pending;
312
313 spin_lock_irqsave(&dev->event_lock, flags);
314 pending = rcrtc->event != NULL;
315 spin_unlock_irqrestore(&dev->event_lock, flags);
316
317 return pending;
318}
319
320static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
321{
322 struct rcar_du_device *rcdu = rcrtc->group->dev;
323
324 if (wait_event_timeout(rcrtc->flip_wait,
325 !rcar_du_crtc_page_flip_pending(rcrtc),
326 msecs_to_jiffies(50)))
327 return;
328
329 dev_warn(rcdu->dev, "page flip timeout\n");
330
331 rcar_du_crtc_finish_page_flip(rcrtc);
332}
333
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200334/* -----------------------------------------------------------------------------
335 * Start/Stop and Suspend/Resume
336 */
337
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200338static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
339{
340 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart906eff72014-12-09 19:11:18 +0200341 bool interlaced;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200342
343 if (rcrtc->started)
344 return;
345
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200346 /* Set display off and background to black */
347 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
348 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
349
350 /* Configure display timings and output routing */
351 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200352 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200353
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200354 /* Start with all planes disabled. */
355 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200356
357 /* Select master sync mode. This enables display operation in master
358 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
359 * actively driven).
360 */
Laurent Pinchart906eff72014-12-09 19:11:18 +0200361 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
362 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
363 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
364 DSYSR_TVM_MASTER);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200365
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200366 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200367
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200368 /* Turn vertical blanking interrupt reporting back on. */
369 drm_crtc_vblank_on(crtc);
370
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200371 rcrtc->started = true;
372}
373
374static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
375{
376 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200377
378 if (!rcrtc->started)
379 return;
380
Laurent Pinchart911316f2015-05-14 15:01:47 +0300381 /* Disable all planes and wait for the change to take effect. This is
382 * required as the DSnPR registers are updated on vblank, and no vblank
383 * will occur once the CRTC is stopped. Disabling planes when starting
384 * the CRTC thus wouldn't be enough as it would start scanning out
385 * immediately from old frame buffers until the next vblank.
386 *
387 * This increases the CRTC stop delay, especially when multiple CRTCs
388 * are stopped in one operation as we now wait for one vblank per CRTC.
389 * Whether this can be improved needs to be researched.
390 */
391 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
392 drm_crtc_wait_one_vblank(crtc);
393
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200394 /* Disable vertical blanking interrupt reporting. We first need to wait
395 * for page flip completion before stopping the CRTC as userspace
396 * expects page flips to eventually complete.
Laurent Pinchart36693f32015-02-18 13:21:56 +0200397 */
398 rcar_du_crtc_wait_page_flip(rcrtc);
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200399 drm_crtc_vblank_off(crtc);
Laurent Pinchart36693f32015-02-18 13:21:56 +0200400
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200401 /* Select switch sync mode. This stops display operation and configures
402 * the HSYNC and VSYNC signals as inputs.
403 */
404 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
405
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200406 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200407
408 rcrtc->started = false;
409}
410
411void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
412{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200413 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200414 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200415}
416
417void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
418{
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200419 unsigned int i;
420
Laurent Pinchart6ea22ab2016-01-25 00:28:11 +0200421 if (!rcrtc->crtc.state->active)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200422 return;
423
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200424 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200425 rcar_du_crtc_start(rcrtc);
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200426
427 /* Commit the planes state. */
Laurent Pinchartd6aed572015-05-25 16:32:45 +0300428 for (i = 0; i < rcrtc->group->num_planes; ++i) {
Laurent Pinchart99caede2015-04-29 00:05:56 +0300429 struct rcar_du_plane *plane = &rcrtc->group->planes[i];
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200430
431 if (plane->plane.state->crtc != &rcrtc->crtc)
432 continue;
433
434 rcar_du_plane_setup(plane);
435 }
436
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200437 rcar_du_crtc_update_planes(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200438}
439
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200440/* -----------------------------------------------------------------------------
441 * CRTC Functions
442 */
443
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200444static void rcar_du_crtc_enable(struct drm_crtc *crtc)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200445{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200446 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
447
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200448 rcar_du_crtc_get(rcrtc);
449 rcar_du_crtc_start(rcrtc);
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200450}
451
452static void rcar_du_crtc_disable(struct drm_crtc *crtc)
453{
454 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
455
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200456 rcar_du_crtc_stop(rcrtc);
457 rcar_du_crtc_put(rcrtc);
458
Laurent Pinchartcf1cc6f22015-02-20 15:16:55 +0200459 rcrtc->outputs = 0;
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200460}
461
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200462static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
463 const struct drm_display_mode *mode,
464 struct drm_display_mode *adjusted_mode)
465{
466 /* TODO Fixup modes */
467 return true;
468}
469
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200470static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
471 struct drm_crtc_state *old_crtc_state)
Laurent Pinchart920888a2015-02-18 12:18:05 +0200472{
Laurent Pinchartd5746642015-02-23 01:04:21 +0200473 struct drm_pending_vblank_event *event = crtc->state->event;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200474 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartd5746642015-02-23 01:04:21 +0200475 struct drm_device *dev = rcrtc->crtc.dev;
476 unsigned long flags;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200477
Laurent Pinchartd5746642015-02-23 01:04:21 +0200478 if (event) {
Laurent Pinchartd5746642015-02-23 01:04:21 +0200479 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
480
481 spin_lock_irqsave(&dev->event_lock, flags);
482 rcrtc->event = event;
483 spin_unlock_irqrestore(&dev->event_lock, flags);
484 }
Laurent Pinchart920888a2015-02-18 12:18:05 +0200485}
486
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200487static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
488 struct drm_crtc_state *old_crtc_state)
Laurent Pinchart920888a2015-02-18 12:18:05 +0200489{
490 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
491
Laurent Pinchart52055ba2015-02-23 01:39:13 +0200492 rcar_du_crtc_update_planes(rcrtc);
Laurent Pinchart920888a2015-02-18 12:18:05 +0200493}
494
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200495static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200496 .mode_fixup = rcar_du_crtc_mode_fixup,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200497 .disable = rcar_du_crtc_disable,
Laurent Pinchartbeff1552015-02-20 14:05:21 +0200498 .enable = rcar_du_crtc_enable,
Laurent Pinchart920888a2015-02-18 12:18:05 +0200499 .atomic_begin = rcar_du_crtc_atomic_begin,
500 .atomic_flush = rcar_du_crtc_atomic_flush,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200501};
502
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200503static const struct drm_crtc_funcs crtc_funcs = {
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200504 .reset = drm_atomic_helper_crtc_reset,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200505 .destroy = drm_crtc_cleanup,
Laurent Pinchartcf1cc6f22015-02-20 15:16:55 +0200506 .set_config = drm_atomic_helper_set_config,
Laurent Pinchartd5746642015-02-23 01:04:21 +0200507 .page_flip = drm_atomic_helper_page_flip,
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200508 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
509 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200510};
511
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200512/* -----------------------------------------------------------------------------
513 * Interrupt Handling
514 */
515
516static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
517{
518 struct rcar_du_crtc *rcrtc = arg;
519 irqreturn_t ret = IRQ_NONE;
520 u32 status;
521
522 status = rcar_du_crtc_read(rcrtc, DSSR);
523 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
524
525 if (status & DSSR_FRM) {
526 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
527 rcar_du_crtc_finish_page_flip(rcrtc);
528 ret = IRQ_HANDLED;
529 }
530
531 return ret;
532}
533
534/* -----------------------------------------------------------------------------
535 * Initialization
536 */
537
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200538int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200539{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200540 static const unsigned int mmio_offsets[] = {
541 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
542 };
543
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200544 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200545 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200546 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
547 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200548 unsigned int irqflags;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200549 struct clk *clk;
550 char clk_name[9];
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200551 char *name;
552 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200553 int ret;
554
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200555 /* Get the CRTC clock and the optional external clock. */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200556 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
557 sprintf(clk_name, "du.%u", index);
558 name = clk_name;
559 } else {
560 name = NULL;
561 }
562
563 rcrtc->clock = devm_clk_get(rcdu->dev, name);
564 if (IS_ERR(rcrtc->clock)) {
565 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
566 return PTR_ERR(rcrtc->clock);
567 }
568
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200569 sprintf(clk_name, "dclkin.%u", index);
570 clk = devm_clk_get(rcdu->dev, clk_name);
571 if (!IS_ERR(clk)) {
572 rcrtc->extclock = clk;
573 } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
574 dev_info(rcdu->dev, "can't get external clock %u\n", index);
575 return -EPROBE_DEFER;
576 }
577
Laurent Pinchart36693f32015-02-18 13:21:56 +0200578 init_waitqueue_head(&rcrtc->flip_wait);
579
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200580 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200581 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200582 rcrtc->index = index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200583
Laurent Pinchart53dff602015-02-23 03:20:39 +0200584 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc,
Laurent Pinchart99caede2015-04-29 00:05:56 +0300585 &rgrp->planes[index % 2].plane,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200586 NULL, &crtc_funcs, NULL);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200587 if (ret < 0)
588 return ret;
589
590 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
591
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200592 /* Start with vertical blanking interrupt reporting disabled. */
593 drm_crtc_vblank_off(crtc);
594
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200595 /* Register the interrupt handler. */
596 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
597 irq = platform_get_irq(pdev, index);
598 irqflags = 0;
599 } else {
600 irq = platform_get_irq(pdev, 0);
601 irqflags = IRQF_SHARED;
602 }
603
604 if (irq < 0) {
605 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
Julia Lawall6512f5f2014-11-23 14:11:17 +0100606 return irq;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200607 }
608
609 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
610 dev_name(rcdu->dev), rcrtc);
611 if (ret < 0) {
612 dev_err(rcdu->dev,
613 "failed to register IRQ for CRTC %u\n", index);
614 return ret;
615 }
616
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200617 return 0;
618}
619
620void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
621{
622 if (enable) {
623 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
624 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
625 } else {
626 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
627 }
628}