blob: 23cc910951f430a279b6b01ae88e5f03d7de3e74 [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>
18#include <drm/drm_crtc.h>
19#include <drm/drm_crtc_helper.h>
20#include <drm/drm_fb_cma_helper.h>
21#include <drm/drm_gem_cma_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010022#include <drm/drm_plane_helper.h>
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020023
24#include "rcar_du_crtc.h"
25#include "rcar_du_drv.h"
26#include "rcar_du_kms.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020027#include "rcar_du_plane.h"
28#include "rcar_du_regs.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020029
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020030static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
31{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020032 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020033
34 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
35}
36
37static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
38{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020039 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020040
41 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
42}
43
44static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
45{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020046 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020047
48 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
49 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
50}
51
52static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
53{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020054 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020055
56 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
57 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
58}
59
60static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
61 u32 clr, u32 set)
62{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020063 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020064 u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
65
66 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
67}
68
Laurent Pinchartf66ee302013-06-14 14:15:01 +020069static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
70{
Laurent Pinchartf66ee302013-06-14 14:15:01 +020071 int ret;
72
73 ret = clk_prepare_enable(rcrtc->clock);
74 if (ret < 0)
75 return ret;
76
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020077 ret = rcar_du_group_get(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020078 if (ret < 0)
79 clk_disable_unprepare(rcrtc->clock);
80
81 return ret;
82}
83
84static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
85{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020086 rcar_du_group_put(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020087 clk_disable_unprepare(rcrtc->clock);
88}
89
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020090static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
91{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020092 const struct drm_display_mode *mode = &rcrtc->crtc.mode;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020093 unsigned long clk;
94 u32 value;
95 u32 div;
96
97 /* Dot clock */
Laurent Pinchartf66ee302013-06-14 14:15:01 +020098 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020099 div = DIV_ROUND_CLOSEST(clk, mode->clock * 1000);
100 div = clamp(div, 1U, 64U) - 1;
101
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200102 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
103 ESCR_DCLKSEL_CLKS | div);
104 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200105
106 /* Signal polarities */
107 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
108 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
109 | DSMR_DIPM_DE;
110 rcar_du_crtc_write(rcrtc, DSMR, value);
111
112 /* Display timings */
113 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
114 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
115 mode->hdisplay - 19);
116 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
117 mode->hsync_start - 1);
118 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
119
120 rcar_du_crtc_write(rcrtc, VDSR, mode->vtotal - mode->vsync_end - 2);
121 rcar_du_crtc_write(rcrtc, VDER, mode->vtotal - mode->vsync_end +
122 mode->vdisplay - 2);
123 rcar_du_crtc_write(rcrtc, VSPR, mode->vtotal - mode->vsync_end +
124 mode->vsync_start - 1);
125 rcar_du_crtc_write(rcrtc, VCR, mode->vtotal - 1);
126
127 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
128 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
129}
130
Laurent Pinchartef67a902013-06-17 03:13:11 +0200131void rcar_du_crtc_route_output(struct drm_crtc *crtc,
132 enum rcar_du_output output)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200133{
134 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200135 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200136
137 /* Store the route from the CRTC output to the DU output. The DU will be
138 * configured when starting the CRTC.
139 */
Laurent Pinchartef67a902013-06-17 03:13:11 +0200140 rcrtc->outputs |= BIT(output);
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200141
142 /* Store RGB routing to DPAD0 for R8A7790. */
143 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_DEFR8) &&
144 output == RCAR_DU_OUTPUT_DPAD0)
145 rcdu->dpad0_source = rcrtc->index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200146}
147
148void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
149{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200150 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
151 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
152 unsigned int num_planes = 0;
153 unsigned int prio = 0;
154 unsigned int i;
155 u32 dptsr = 0;
156 u32 dspr = 0;
157
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200158 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
159 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200160 unsigned int j;
161
162 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
163 continue;
164
165 /* Insert the plane in the sorted planes array. */
166 for (j = num_planes++; j > 0; --j) {
167 if (planes[j-1]->zpos <= plane->zpos)
168 break;
169 planes[j] = planes[j-1];
170 }
171
172 planes[j] = plane;
173 prio += plane->format->planes * 4;
174 }
175
176 for (i = 0; i < num_planes; ++i) {
177 struct rcar_du_plane *plane = planes[i];
178 unsigned int index = plane->hwindex;
179
180 prio -= 4;
181 dspr |= (index + 1) << prio;
182 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
183
184 if (plane->format->planes == 2) {
185 index = (index + 1) % 8;
186
187 prio -= 4;
188 dspr |= (index + 1) << prio;
189 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
190 }
191 }
192
193 /* Select display timing and dot clock generator 2 for planes associated
194 * with superposition controller 2.
195 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200196 if (rcrtc->index % 2) {
197 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200198
199 /* The DPTSR register is updated when the display controller is
200 * stopped. We thus need to restart the DU. Once again, sorry
201 * for the flicker. One way to mitigate the issue would be to
202 * pre-associate planes with CRTCs (either with a fixed 4/4
203 * split, or through a module parameter). Flicker would then
204 * occur only if we need to break the pre-association.
205 */
206 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200207 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200208 if (rcrtc->group->used_crtcs)
209 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200210 }
211 }
212
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200213 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
214 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200215}
216
217static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
218{
219 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200220 unsigned int i;
221
222 if (rcrtc->started)
223 return;
224
225 if (WARN_ON(rcrtc->plane->format == NULL))
226 return;
227
228 /* Set display off and background to black */
229 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
230 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
231
232 /* Configure display timings and output routing */
233 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200234 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200235
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200236 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200237 rcrtc->plane->enabled = true;
238 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200239 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200240
241 /* Setup planes. */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200242 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
243 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200244
245 if (plane->crtc != crtc || !plane->enabled)
246 continue;
247
248 rcar_du_plane_setup(plane);
249 }
250
251 /* Select master sync mode. This enables display operation in master
252 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
253 * actively driven).
254 */
255 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
256
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200257 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200258
259 rcrtc->started = true;
260}
261
262static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
263{
264 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200265
266 if (!rcrtc->started)
267 return;
268
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200269 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200270 rcrtc->plane->enabled = false;
271 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200272 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200273
274 /* Select switch sync mode. This stops display operation and configures
275 * the HSYNC and VSYNC signals as inputs.
276 */
277 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
278
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200279 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200280
281 rcrtc->started = false;
282}
283
284void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
285{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200286 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200287 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200288}
289
290void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
291{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200292 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
293 return;
294
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200295 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200296 rcar_du_crtc_start(rcrtc);
297}
298
299static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
300{
301 struct drm_crtc *crtc = &rcrtc->crtc;
302
Matt Roperf4510a22014-04-01 15:22:40 -0700303 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200304 rcar_du_plane_update_base(rcrtc->plane);
305}
306
307static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
308{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200309 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
310
311 if (rcrtc->dpms == mode)
312 return;
313
314 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200315 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200316 rcar_du_crtc_start(rcrtc);
317 } else {
318 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200319 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200320 }
321
322 rcrtc->dpms = mode;
323}
324
325static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
326 const struct drm_display_mode *mode,
327 struct drm_display_mode *adjusted_mode)
328{
329 /* TODO Fixup modes */
330 return true;
331}
332
333static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
334{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200335 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
336
337 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200338 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200339 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200340 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200341
342 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
343 * result.
344 */
345 rcar_du_crtc_stop(rcrtc);
346 rcar_du_plane_release(rcrtc->plane);
347
348 rcrtc->dpms = DRM_MODE_DPMS_OFF;
349}
350
351static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
352 struct drm_display_mode *mode,
353 struct drm_display_mode *adjusted_mode,
354 int x, int y,
355 struct drm_framebuffer *old_fb)
356{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200357 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200358 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200359 const struct rcar_du_format_info *format;
360 int ret;
361
Matt Roperf4510a22014-04-01 15:22:40 -0700362 format = rcar_du_format_info(crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200363 if (format == NULL) {
364 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
Matt Roperf4510a22014-04-01 15:22:40 -0700365 crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200366 ret = -EINVAL;
367 goto error;
368 }
369
370 ret = rcar_du_plane_reserve(rcrtc->plane, format);
371 if (ret < 0)
372 goto error;
373
374 rcrtc->plane->format = format;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200375
376 rcrtc->plane->src_x = x;
377 rcrtc->plane->src_y = y;
378 rcrtc->plane->width = mode->hdisplay;
379 rcrtc->plane->height = mode->vdisplay;
380
Matt Roperf4510a22014-04-01 15:22:40 -0700381 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200382
383 rcrtc->outputs = 0;
384
385 return 0;
386
387error:
388 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200389 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200390 * here.
391 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200392 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200393 return ret;
394}
395
396static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
397{
398 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
399
400 /* We're done, restart the CRTC and set the DPMS mode to on. The
401 * reference to the DU acquired at prepare() time will thus be released
402 * by the DPMS handler (possibly called by the disable() handler).
403 */
404 rcar_du_crtc_start(rcrtc);
405 rcrtc->dpms = DRM_MODE_DPMS_ON;
406}
407
408static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
409 struct drm_framebuffer *old_fb)
410{
411 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
412
413 rcrtc->plane->src_x = x;
414 rcrtc->plane->src_y = y;
415
Laurent Pinchartf5abcc42013-11-13 14:38:03 +0100416 rcar_du_crtc_update_base(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200417
418 return 0;
419}
420
421static void rcar_du_crtc_disable(struct drm_crtc *crtc)
422{
423 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
424
425 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
426 rcar_du_plane_release(rcrtc->plane);
427}
428
429static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
430 .dpms = rcar_du_crtc_dpms,
431 .mode_fixup = rcar_du_crtc_mode_fixup,
432 .prepare = rcar_du_crtc_mode_prepare,
433 .commit = rcar_du_crtc_mode_commit,
434 .mode_set = rcar_du_crtc_mode_set,
435 .mode_set_base = rcar_du_crtc_mode_set_base,
436 .disable = rcar_du_crtc_disable,
437};
438
439void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
440 struct drm_file *file)
441{
442 struct drm_pending_vblank_event *event;
443 struct drm_device *dev = rcrtc->crtc.dev;
444 unsigned long flags;
445
446 /* Destroy the pending vertical blanking event associated with the
447 * pending page flip, if any, and disable vertical blanking interrupts.
448 */
449 spin_lock_irqsave(&dev->event_lock, flags);
450 event = rcrtc->event;
451 if (event && event->base.file_priv == file) {
452 rcrtc->event = NULL;
453 event->base.destroy(&event->base);
454 drm_vblank_put(dev, rcrtc->index);
455 }
456 spin_unlock_irqrestore(&dev->event_lock, flags);
457}
458
459static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
460{
461 struct drm_pending_vblank_event *event;
462 struct drm_device *dev = rcrtc->crtc.dev;
463 unsigned long flags;
464
465 spin_lock_irqsave(&dev->event_lock, flags);
466 event = rcrtc->event;
467 rcrtc->event = NULL;
468 spin_unlock_irqrestore(&dev->event_lock, flags);
469
470 if (event == NULL)
471 return;
472
473 spin_lock_irqsave(&dev->event_lock, flags);
474 drm_send_vblank_event(dev, rcrtc->index, event);
475 spin_unlock_irqrestore(&dev->event_lock, flags);
476
477 drm_vblank_put(dev, rcrtc->index);
478}
479
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200480static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
481{
482 struct rcar_du_crtc *rcrtc = arg;
483 irqreturn_t ret = IRQ_NONE;
484 u32 status;
485
486 status = rcar_du_crtc_read(rcrtc, DSSR);
487 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
488
489 if (status & DSSR_VBK) {
490 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
491 rcar_du_crtc_finish_page_flip(rcrtc);
492 ret = IRQ_HANDLED;
493 }
494
495 return ret;
496}
497
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200498static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
499 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700500 struct drm_pending_vblank_event *event,
501 uint32_t page_flip_flags)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200502{
503 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
504 struct drm_device *dev = rcrtc->crtc.dev;
505 unsigned long flags;
506
507 spin_lock_irqsave(&dev->event_lock, flags);
508 if (rcrtc->event != NULL) {
509 spin_unlock_irqrestore(&dev->event_lock, flags);
510 return -EBUSY;
511 }
512 spin_unlock_irqrestore(&dev->event_lock, flags);
513
Matt Roperf4510a22014-04-01 15:22:40 -0700514 crtc->primary->fb = fb;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200515 rcar_du_crtc_update_base(rcrtc);
516
517 if (event) {
518 event->pipe = rcrtc->index;
519 drm_vblank_get(dev, rcrtc->index);
520 spin_lock_irqsave(&dev->event_lock, flags);
521 rcrtc->event = event;
522 spin_unlock_irqrestore(&dev->event_lock, flags);
523 }
524
525 return 0;
526}
527
528static const struct drm_crtc_funcs crtc_funcs = {
529 .destroy = drm_crtc_cleanup,
530 .set_config = drm_crtc_helper_set_config,
531 .page_flip = rcar_du_crtc_page_flip,
532};
533
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200534int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200535{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200536 static const unsigned int mmio_offsets[] = {
537 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
538 };
539
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200540 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200541 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200542 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
543 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200544 unsigned int irqflags;
545 char clk_name[5];
546 char *name;
547 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200548 int ret;
549
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200550 /* Get the CRTC clock. */
551 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
552 sprintf(clk_name, "du.%u", index);
553 name = clk_name;
554 } else {
555 name = NULL;
556 }
557
558 rcrtc->clock = devm_clk_get(rcdu->dev, name);
559 if (IS_ERR(rcrtc->clock)) {
560 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
561 return PTR_ERR(rcrtc->clock);
562 }
563
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200564 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200565 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200566 rcrtc->index = index;
567 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200568 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200569
570 rcrtc->plane->crtc = crtc;
571
572 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
573 if (ret < 0)
574 return ret;
575
576 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
577
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200578 /* Register the interrupt handler. */
579 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
580 irq = platform_get_irq(pdev, index);
581 irqflags = 0;
582 } else {
583 irq = platform_get_irq(pdev, 0);
584 irqflags = IRQF_SHARED;
585 }
586
587 if (irq < 0) {
588 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
Julia Lawall6512f5f2014-11-23 14:11:17 +0100589 return irq;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200590 }
591
592 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
593 dev_name(rcdu->dev), rcrtc);
594 if (ret < 0) {
595 dev_err(rcdu->dev,
596 "failed to register IRQ for CRTC %u\n", index);
597 return ret;
598 }
599
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200600 return 0;
601}
602
603void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
604{
605 if (enable) {
606 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
607 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
608 } else {
609 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
610 }
611}