blob: cf0dca13264fcdb5071c445cfdc3c239b9af36e6 [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
Laurent Pinchart0c1c8772014-12-09 00:21:12 +0200142 /* Store RGB routing to DPAD0, the hardware will be configured when
143 * starting the CRTC.
144 */
145 if (output == RCAR_DU_OUTPUT_DPAD0)
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200146 rcdu->dpad0_source = rcrtc->index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200147}
148
149void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
150{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200151 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
152 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
153 unsigned int num_planes = 0;
154 unsigned int prio = 0;
155 unsigned int i;
156 u32 dptsr = 0;
157 u32 dspr = 0;
158
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200159 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
160 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200161 unsigned int j;
162
163 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
164 continue;
165
166 /* Insert the plane in the sorted planes array. */
167 for (j = num_planes++; j > 0; --j) {
168 if (planes[j-1]->zpos <= plane->zpos)
169 break;
170 planes[j] = planes[j-1];
171 }
172
173 planes[j] = plane;
174 prio += plane->format->planes * 4;
175 }
176
177 for (i = 0; i < num_planes; ++i) {
178 struct rcar_du_plane *plane = planes[i];
179 unsigned int index = plane->hwindex;
180
181 prio -= 4;
182 dspr |= (index + 1) << prio;
183 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
184
185 if (plane->format->planes == 2) {
186 index = (index + 1) % 8;
187
188 prio -= 4;
189 dspr |= (index + 1) << prio;
190 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
191 }
192 }
193
194 /* Select display timing and dot clock generator 2 for planes associated
195 * with superposition controller 2.
196 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200197 if (rcrtc->index % 2) {
198 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200199
200 /* The DPTSR register is updated when the display controller is
201 * stopped. We thus need to restart the DU. Once again, sorry
202 * for the flicker. One way to mitigate the issue would be to
203 * pre-associate planes with CRTCs (either with a fixed 4/4
204 * split, or through a module parameter). Flicker would then
205 * occur only if we need to break the pre-association.
206 */
207 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200208 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200209 if (rcrtc->group->used_crtcs)
210 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200211 }
212 }
213
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200214 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
215 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200216}
217
218static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
219{
220 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200221 unsigned int i;
222
223 if (rcrtc->started)
224 return;
225
226 if (WARN_ON(rcrtc->plane->format == NULL))
227 return;
228
229 /* Set display off and background to black */
230 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
231 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
232
233 /* Configure display timings and output routing */
234 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200235 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200236
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200237 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200238 rcrtc->plane->enabled = true;
239 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200240 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200241
242 /* Setup planes. */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200243 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
244 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200245
246 if (plane->crtc != crtc || !plane->enabled)
247 continue;
248
249 rcar_du_plane_setup(plane);
250 }
251
252 /* Select master sync mode. This enables display operation in master
253 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
254 * actively driven).
255 */
256 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
257
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200258 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200259
260 rcrtc->started = true;
261}
262
263static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
264{
265 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200266
267 if (!rcrtc->started)
268 return;
269
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200270 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200271 rcrtc->plane->enabled = false;
272 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200273 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200274
275 /* Select switch sync mode. This stops display operation and configures
276 * the HSYNC and VSYNC signals as inputs.
277 */
278 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
279
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200280 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200281
282 rcrtc->started = false;
283}
284
285void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
286{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200287 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200288 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200289}
290
291void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
292{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200293 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
294 return;
295
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200296 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200297 rcar_du_crtc_start(rcrtc);
298}
299
300static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
301{
302 struct drm_crtc *crtc = &rcrtc->crtc;
303
Matt Roperf4510a22014-04-01 15:22:40 -0700304 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200305 rcar_du_plane_update_base(rcrtc->plane);
306}
307
308static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
309{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200310 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
311
312 if (rcrtc->dpms == mode)
313 return;
314
315 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200316 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200317 rcar_du_crtc_start(rcrtc);
318 } else {
319 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200320 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200321 }
322
323 rcrtc->dpms = mode;
324}
325
326static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
327 const struct drm_display_mode *mode,
328 struct drm_display_mode *adjusted_mode)
329{
330 /* TODO Fixup modes */
331 return true;
332}
333
334static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
335{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200336 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
337
338 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200339 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200340 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200341 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200342
343 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
344 * result.
345 */
346 rcar_du_crtc_stop(rcrtc);
347 rcar_du_plane_release(rcrtc->plane);
348
349 rcrtc->dpms = DRM_MODE_DPMS_OFF;
350}
351
352static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
353 struct drm_display_mode *mode,
354 struct drm_display_mode *adjusted_mode,
355 int x, int y,
356 struct drm_framebuffer *old_fb)
357{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200358 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200359 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200360 const struct rcar_du_format_info *format;
361 int ret;
362
Matt Roperf4510a22014-04-01 15:22:40 -0700363 format = rcar_du_format_info(crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200364 if (format == NULL) {
365 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
Matt Roperf4510a22014-04-01 15:22:40 -0700366 crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200367 ret = -EINVAL;
368 goto error;
369 }
370
371 ret = rcar_du_plane_reserve(rcrtc->plane, format);
372 if (ret < 0)
373 goto error;
374
375 rcrtc->plane->format = format;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200376
377 rcrtc->plane->src_x = x;
378 rcrtc->plane->src_y = y;
379 rcrtc->plane->width = mode->hdisplay;
380 rcrtc->plane->height = mode->vdisplay;
381
Matt Roperf4510a22014-04-01 15:22:40 -0700382 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200383
384 rcrtc->outputs = 0;
385
386 return 0;
387
388error:
389 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200390 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200391 * here.
392 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200393 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200394 return ret;
395}
396
397static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
398{
399 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
400
401 /* We're done, restart the CRTC and set the DPMS mode to on. The
402 * reference to the DU acquired at prepare() time will thus be released
403 * by the DPMS handler (possibly called by the disable() handler).
404 */
405 rcar_du_crtc_start(rcrtc);
406 rcrtc->dpms = DRM_MODE_DPMS_ON;
407}
408
409static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
410 struct drm_framebuffer *old_fb)
411{
412 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
413
414 rcrtc->plane->src_x = x;
415 rcrtc->plane->src_y = y;
416
Laurent Pinchartf5abcc42013-11-13 14:38:03 +0100417 rcar_du_crtc_update_base(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200418
419 return 0;
420}
421
422static void rcar_du_crtc_disable(struct drm_crtc *crtc)
423{
424 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
425
426 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
427 rcar_du_plane_release(rcrtc->plane);
428}
429
430static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
431 .dpms = rcar_du_crtc_dpms,
432 .mode_fixup = rcar_du_crtc_mode_fixup,
433 .prepare = rcar_du_crtc_mode_prepare,
434 .commit = rcar_du_crtc_mode_commit,
435 .mode_set = rcar_du_crtc_mode_set,
436 .mode_set_base = rcar_du_crtc_mode_set_base,
437 .disable = rcar_du_crtc_disable,
438};
439
440void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
441 struct drm_file *file)
442{
443 struct drm_pending_vblank_event *event;
444 struct drm_device *dev = rcrtc->crtc.dev;
445 unsigned long flags;
446
447 /* Destroy the pending vertical blanking event associated with the
448 * pending page flip, if any, and disable vertical blanking interrupts.
449 */
450 spin_lock_irqsave(&dev->event_lock, flags);
451 event = rcrtc->event;
452 if (event && event->base.file_priv == file) {
453 rcrtc->event = NULL;
454 event->base.destroy(&event->base);
455 drm_vblank_put(dev, rcrtc->index);
456 }
457 spin_unlock_irqrestore(&dev->event_lock, flags);
458}
459
460static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
461{
462 struct drm_pending_vblank_event *event;
463 struct drm_device *dev = rcrtc->crtc.dev;
464 unsigned long flags;
465
466 spin_lock_irqsave(&dev->event_lock, flags);
467 event = rcrtc->event;
468 rcrtc->event = NULL;
469 spin_unlock_irqrestore(&dev->event_lock, flags);
470
471 if (event == NULL)
472 return;
473
474 spin_lock_irqsave(&dev->event_lock, flags);
475 drm_send_vblank_event(dev, rcrtc->index, event);
476 spin_unlock_irqrestore(&dev->event_lock, flags);
477
478 drm_vblank_put(dev, rcrtc->index);
479}
480
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200481static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
482{
483 struct rcar_du_crtc *rcrtc = arg;
484 irqreturn_t ret = IRQ_NONE;
485 u32 status;
486
487 status = rcar_du_crtc_read(rcrtc, DSSR);
488 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
489
490 if (status & DSSR_VBK) {
491 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
492 rcar_du_crtc_finish_page_flip(rcrtc);
493 ret = IRQ_HANDLED;
494 }
495
496 return ret;
497}
498
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200499static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
500 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700501 struct drm_pending_vblank_event *event,
502 uint32_t page_flip_flags)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200503{
504 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
505 struct drm_device *dev = rcrtc->crtc.dev;
506 unsigned long flags;
507
508 spin_lock_irqsave(&dev->event_lock, flags);
509 if (rcrtc->event != NULL) {
510 spin_unlock_irqrestore(&dev->event_lock, flags);
511 return -EBUSY;
512 }
513 spin_unlock_irqrestore(&dev->event_lock, flags);
514
Matt Roperf4510a22014-04-01 15:22:40 -0700515 crtc->primary->fb = fb;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200516 rcar_du_crtc_update_base(rcrtc);
517
518 if (event) {
519 event->pipe = rcrtc->index;
520 drm_vblank_get(dev, rcrtc->index);
521 spin_lock_irqsave(&dev->event_lock, flags);
522 rcrtc->event = event;
523 spin_unlock_irqrestore(&dev->event_lock, flags);
524 }
525
526 return 0;
527}
528
529static const struct drm_crtc_funcs crtc_funcs = {
530 .destroy = drm_crtc_cleanup,
531 .set_config = drm_crtc_helper_set_config,
532 .page_flip = rcar_du_crtc_page_flip,
533};
534
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200535int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200536{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200537 static const unsigned int mmio_offsets[] = {
538 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
539 };
540
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200541 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200542 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200543 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
544 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200545 unsigned int irqflags;
546 char clk_name[5];
547 char *name;
548 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200549 int ret;
550
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200551 /* Get the CRTC clock. */
552 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
553 sprintf(clk_name, "du.%u", index);
554 name = clk_name;
555 } else {
556 name = NULL;
557 }
558
559 rcrtc->clock = devm_clk_get(rcdu->dev, name);
560 if (IS_ERR(rcrtc->clock)) {
561 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
562 return PTR_ERR(rcrtc->clock);
563 }
564
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200565 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200566 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200567 rcrtc->index = index;
568 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200569 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200570
571 rcrtc->plane->crtc = crtc;
572
573 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
574 if (ret < 0)
575 return ret;
576
577 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
578
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200579 /* Register the interrupt handler. */
580 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
581 irq = platform_get_irq(pdev, index);
582 irqflags = 0;
583 } else {
584 irq = platform_get_irq(pdev, 0);
585 irqflags = IRQF_SHARED;
586 }
587
588 if (irq < 0) {
589 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
Julia Lawall6512f5f2014-11-23 14:11:17 +0100590 return irq;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200591 }
592
593 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
594 dev_name(rcdu->dev), rcrtc);
595 if (ret < 0) {
596 dev_err(rcdu->dev,
597 "failed to register IRQ for CRTC %u\n", index);
598 return ret;
599 }
600
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200601 return 0;
602}
603
604void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
605{
606 if (enable) {
607 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
608 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
609 } else {
610 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
611 }
612}