blob: 680606ef11d8f3cee2c35f1bce5b2ca8ec354387 [file] [log] [blame]
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02001/*
2 * rcar_du_crtc.c -- R-Car Display Unit CRTCs
3 *
4 * Copyright (C) 2013 Renesas Corporation
5 *
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>
22
23#include "rcar_du_crtc.h"
24#include "rcar_du_drv.h"
25#include "rcar_du_kms.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020026#include "rcar_du_plane.h"
27#include "rcar_du_regs.h"
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020028
29#define to_rcar_crtc(c) container_of(c, struct rcar_du_crtc, crtc)
30
31static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
32{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020033 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020034
35 return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
36}
37
38static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
39{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020040 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020041
42 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
43}
44
45static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
46{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020047 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020048
49 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
50 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
51}
52
53static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
54{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020055 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020056
57 rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
58 rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
59}
60
61static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
62 u32 clr, u32 set)
63{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020064 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020065 u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
66
67 rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
68}
69
Laurent Pinchartf66ee302013-06-14 14:15:01 +020070static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
71{
Laurent Pinchartf66ee302013-06-14 14:15:01 +020072 int ret;
73
74 ret = clk_prepare_enable(rcrtc->clock);
75 if (ret < 0)
76 return ret;
77
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020078 ret = rcar_du_group_get(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020079 if (ret < 0)
80 clk_disable_unprepare(rcrtc->clock);
81
82 return ret;
83}
84
85static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
86{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020087 rcar_du_group_put(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020088 clk_disable_unprepare(rcrtc->clock);
89}
90
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020091static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
92{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020093 const struct drm_display_mode *mode = &rcrtc->crtc.mode;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020094 unsigned long clk;
95 u32 value;
96 u32 div;
97
98 /* Dot clock */
Laurent Pinchartf66ee302013-06-14 14:15:01 +020099 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200100 div = DIV_ROUND_CLOSEST(clk, mode->clock * 1000);
101 div = clamp(div, 1U, 64U) - 1;
102
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200103 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
104 ESCR_DCLKSEL_CLKS | div);
105 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200106
107 /* Signal polarities */
108 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
109 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
110 | DSMR_DIPM_DE;
111 rcar_du_crtc_write(rcrtc, DSMR, value);
112
113 /* Display timings */
114 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
115 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
116 mode->hdisplay - 19);
117 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
118 mode->hsync_start - 1);
119 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
120
121 rcar_du_crtc_write(rcrtc, VDSR, mode->vtotal - mode->vsync_end - 2);
122 rcar_du_crtc_write(rcrtc, VDER, mode->vtotal - mode->vsync_end +
123 mode->vdisplay - 2);
124 rcar_du_crtc_write(rcrtc, VSPR, mode->vtotal - mode->vsync_end +
125 mode->vsync_start - 1);
126 rcar_du_crtc_write(rcrtc, VCR, mode->vtotal - 1);
127
128 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
129 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
130}
131
Laurent Pinchartef67a902013-06-17 03:13:11 +0200132void rcar_du_crtc_route_output(struct drm_crtc *crtc,
133 enum rcar_du_output output)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200134{
135 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200136 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200137
138 /* Store the route from the CRTC output to the DU output. The DU will be
139 * configured when starting the CRTC.
140 */
Laurent Pinchartef67a902013-06-17 03:13:11 +0200141 rcrtc->outputs |= BIT(output);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200142}
143
144void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
145{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200146 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
147 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
148 unsigned int num_planes = 0;
149 unsigned int prio = 0;
150 unsigned int i;
151 u32 dptsr = 0;
152 u32 dspr = 0;
153
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200154 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
155 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200156 unsigned int j;
157
158 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
159 continue;
160
161 /* Insert the plane in the sorted planes array. */
162 for (j = num_planes++; j > 0; --j) {
163 if (planes[j-1]->zpos <= plane->zpos)
164 break;
165 planes[j] = planes[j-1];
166 }
167
168 planes[j] = plane;
169 prio += plane->format->planes * 4;
170 }
171
172 for (i = 0; i < num_planes; ++i) {
173 struct rcar_du_plane *plane = planes[i];
174 unsigned int index = plane->hwindex;
175
176 prio -= 4;
177 dspr |= (index + 1) << prio;
178 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
179
180 if (plane->format->planes == 2) {
181 index = (index + 1) % 8;
182
183 prio -= 4;
184 dspr |= (index + 1) << prio;
185 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
186 }
187 }
188
189 /* Select display timing and dot clock generator 2 for planes associated
190 * with superposition controller 2.
191 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200192 if (rcrtc->index % 2) {
193 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200194
195 /* The DPTSR register is updated when the display controller is
196 * stopped. We thus need to restart the DU. Once again, sorry
197 * for the flicker. One way to mitigate the issue would be to
198 * pre-associate planes with CRTCs (either with a fixed 4/4
199 * split, or through a module parameter). Flicker would then
200 * occur only if we need to break the pre-association.
201 */
202 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200203 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200204 if (rcrtc->group->used_crtcs)
205 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200206 }
207 }
208
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200209 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
210 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200211}
212
213static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
214{
215 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200216 unsigned int i;
217
218 if (rcrtc->started)
219 return;
220
221 if (WARN_ON(rcrtc->plane->format == NULL))
222 return;
223
224 /* Set display off and background to black */
225 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
226 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
227
228 /* Configure display timings and output routing */
229 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200230 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200231
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200232 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200233 rcrtc->plane->enabled = true;
234 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200235 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200236
237 /* Setup planes. */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200238 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
239 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200240
241 if (plane->crtc != crtc || !plane->enabled)
242 continue;
243
244 rcar_du_plane_setup(plane);
245 }
246
247 /* Select master sync mode. This enables display operation in master
248 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
249 * actively driven).
250 */
251 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
252
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200253 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200254
255 rcrtc->started = true;
256}
257
258static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
259{
260 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200261
262 if (!rcrtc->started)
263 return;
264
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200265 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200266 rcrtc->plane->enabled = false;
267 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200268 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200269
270 /* Select switch sync mode. This stops display operation and configures
271 * the HSYNC and VSYNC signals as inputs.
272 */
273 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
274
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200275 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200276
277 rcrtc->started = false;
278}
279
280void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
281{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200282 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200283 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200284}
285
286void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
287{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200288 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
289 return;
290
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200291 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200292 rcar_du_crtc_start(rcrtc);
293}
294
295static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
296{
297 struct drm_crtc *crtc = &rcrtc->crtc;
298
299 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
300 rcar_du_plane_update_base(rcrtc->plane);
301}
302
303static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
304{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200305 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
306
307 if (rcrtc->dpms == mode)
308 return;
309
310 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200311 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200312 rcar_du_crtc_start(rcrtc);
313 } else {
314 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200315 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200316 }
317
318 rcrtc->dpms = mode;
319}
320
321static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
322 const struct drm_display_mode *mode,
323 struct drm_display_mode *adjusted_mode)
324{
325 /* TODO Fixup modes */
326 return true;
327}
328
329static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
330{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200331 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
332
333 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200334 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200335 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200336 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200337
338 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
339 * result.
340 */
341 rcar_du_crtc_stop(rcrtc);
342 rcar_du_plane_release(rcrtc->plane);
343
344 rcrtc->dpms = DRM_MODE_DPMS_OFF;
345}
346
347static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
348 struct drm_display_mode *mode,
349 struct drm_display_mode *adjusted_mode,
350 int x, int y,
351 struct drm_framebuffer *old_fb)
352{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200353 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200354 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200355 const struct rcar_du_format_info *format;
356 int ret;
357
358 format = rcar_du_format_info(crtc->fb->pixel_format);
359 if (format == NULL) {
360 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
361 crtc->fb->pixel_format);
362 ret = -EINVAL;
363 goto error;
364 }
365
366 ret = rcar_du_plane_reserve(rcrtc->plane, format);
367 if (ret < 0)
368 goto error;
369
370 rcrtc->plane->format = format;
371 rcrtc->plane->pitch = crtc->fb->pitches[0];
372
373 rcrtc->plane->src_x = x;
374 rcrtc->plane->src_y = y;
375 rcrtc->plane->width = mode->hdisplay;
376 rcrtc->plane->height = mode->vdisplay;
377
378 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
379
380 rcrtc->outputs = 0;
381
382 return 0;
383
384error:
385 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200386 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200387 * here.
388 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200389 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200390 return ret;
391}
392
393static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
394{
395 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
396
397 /* We're done, restart the CRTC and set the DPMS mode to on. The
398 * reference to the DU acquired at prepare() time will thus be released
399 * by the DPMS handler (possibly called by the disable() handler).
400 */
401 rcar_du_crtc_start(rcrtc);
402 rcrtc->dpms = DRM_MODE_DPMS_ON;
403}
404
405static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
406 struct drm_framebuffer *old_fb)
407{
408 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
409
410 rcrtc->plane->src_x = x;
411 rcrtc->plane->src_y = y;
412
413 rcar_du_crtc_update_base(to_rcar_crtc(crtc));
414
415 return 0;
416}
417
418static void rcar_du_crtc_disable(struct drm_crtc *crtc)
419{
420 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
421
422 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
423 rcar_du_plane_release(rcrtc->plane);
424}
425
426static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
427 .dpms = rcar_du_crtc_dpms,
428 .mode_fixup = rcar_du_crtc_mode_fixup,
429 .prepare = rcar_du_crtc_mode_prepare,
430 .commit = rcar_du_crtc_mode_commit,
431 .mode_set = rcar_du_crtc_mode_set,
432 .mode_set_base = rcar_du_crtc_mode_set_base,
433 .disable = rcar_du_crtc_disable,
434};
435
436void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
437 struct drm_file *file)
438{
439 struct drm_pending_vblank_event *event;
440 struct drm_device *dev = rcrtc->crtc.dev;
441 unsigned long flags;
442
443 /* Destroy the pending vertical blanking event associated with the
444 * pending page flip, if any, and disable vertical blanking interrupts.
445 */
446 spin_lock_irqsave(&dev->event_lock, flags);
447 event = rcrtc->event;
448 if (event && event->base.file_priv == file) {
449 rcrtc->event = NULL;
450 event->base.destroy(&event->base);
451 drm_vblank_put(dev, rcrtc->index);
452 }
453 spin_unlock_irqrestore(&dev->event_lock, flags);
454}
455
456static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
457{
458 struct drm_pending_vblank_event *event;
459 struct drm_device *dev = rcrtc->crtc.dev;
460 unsigned long flags;
461
462 spin_lock_irqsave(&dev->event_lock, flags);
463 event = rcrtc->event;
464 rcrtc->event = NULL;
465 spin_unlock_irqrestore(&dev->event_lock, flags);
466
467 if (event == NULL)
468 return;
469
470 spin_lock_irqsave(&dev->event_lock, flags);
471 drm_send_vblank_event(dev, rcrtc->index, event);
472 spin_unlock_irqrestore(&dev->event_lock, flags);
473
474 drm_vblank_put(dev, rcrtc->index);
475}
476
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200477static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
478{
479 struct rcar_du_crtc *rcrtc = arg;
480 irqreturn_t ret = IRQ_NONE;
481 u32 status;
482
483 status = rcar_du_crtc_read(rcrtc, DSSR);
484 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
485
486 if (status & DSSR_VBK) {
487 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
488 rcar_du_crtc_finish_page_flip(rcrtc);
489 ret = IRQ_HANDLED;
490 }
491
492 return ret;
493}
494
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200495static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
496 struct drm_framebuffer *fb,
497 struct drm_pending_vblank_event *event)
498{
499 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
500 struct drm_device *dev = rcrtc->crtc.dev;
501 unsigned long flags;
502
503 spin_lock_irqsave(&dev->event_lock, flags);
504 if (rcrtc->event != NULL) {
505 spin_unlock_irqrestore(&dev->event_lock, flags);
506 return -EBUSY;
507 }
508 spin_unlock_irqrestore(&dev->event_lock, flags);
509
510 crtc->fb = fb;
511 rcar_du_crtc_update_base(rcrtc);
512
513 if (event) {
514 event->pipe = rcrtc->index;
515 drm_vblank_get(dev, rcrtc->index);
516 spin_lock_irqsave(&dev->event_lock, flags);
517 rcrtc->event = event;
518 spin_unlock_irqrestore(&dev->event_lock, flags);
519 }
520
521 return 0;
522}
523
524static const struct drm_crtc_funcs crtc_funcs = {
525 .destroy = drm_crtc_cleanup,
526 .set_config = drm_crtc_helper_set_config,
527 .page_flip = rcar_du_crtc_page_flip,
528};
529
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200530int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200531{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200532 static const unsigned int mmio_offsets[] = {
533 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
534 };
535
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200536 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200537 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200538 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
539 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200540 unsigned int irqflags;
541 char clk_name[5];
542 char *name;
543 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200544 int ret;
545
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200546 /* Get the CRTC clock. */
547 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
548 sprintf(clk_name, "du.%u", index);
549 name = clk_name;
550 } else {
551 name = NULL;
552 }
553
554 rcrtc->clock = devm_clk_get(rcdu->dev, name);
555 if (IS_ERR(rcrtc->clock)) {
556 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
557 return PTR_ERR(rcrtc->clock);
558 }
559
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200560 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200561 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200562 rcrtc->index = index;
563 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200564 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200565
566 rcrtc->plane->crtc = crtc;
567
568 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
569 if (ret < 0)
570 return ret;
571
572 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
573
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200574 /* Register the interrupt handler. */
575 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
576 irq = platform_get_irq(pdev, index);
577 irqflags = 0;
578 } else {
579 irq = platform_get_irq(pdev, 0);
580 irqflags = IRQF_SHARED;
581 }
582
583 if (irq < 0) {
584 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
585 return ret;
586 }
587
588 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
589 dev_name(rcdu->dev), rcrtc);
590 if (ret < 0) {
591 dev_err(rcdu->dev,
592 "failed to register IRQ for CRTC %u\n", index);
593 return ret;
594 }
595
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200596 return 0;
597}
598
599void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
600{
601 if (enable) {
602 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
603 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
604 } else {
605 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
606 }
607}