blob: a340224e08e6f7c3cb06861d5c7b628e5f507caa [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 Pinchart4bf8e192013-06-19 13:54:11 +0200132void rcar_du_crtc_route_output(struct drm_crtc *crtc, unsigned int output)
133{
134 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
135
136 /* Store the route from the CRTC output to the DU output. The DU will be
137 * configured when starting the CRTC.
138 */
139 rcrtc->outputs |= 1 << output;
140}
141
142void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
143{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200144 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
145 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
146 unsigned int num_planes = 0;
147 unsigned int prio = 0;
148 unsigned int i;
149 u32 dptsr = 0;
150 u32 dspr = 0;
151
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200152 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
153 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200154 unsigned int j;
155
156 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
157 continue;
158
159 /* Insert the plane in the sorted planes array. */
160 for (j = num_planes++; j > 0; --j) {
161 if (planes[j-1]->zpos <= plane->zpos)
162 break;
163 planes[j] = planes[j-1];
164 }
165
166 planes[j] = plane;
167 prio += plane->format->planes * 4;
168 }
169
170 for (i = 0; i < num_planes; ++i) {
171 struct rcar_du_plane *plane = planes[i];
172 unsigned int index = plane->hwindex;
173
174 prio -= 4;
175 dspr |= (index + 1) << prio;
176 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
177
178 if (plane->format->planes == 2) {
179 index = (index + 1) % 8;
180
181 prio -= 4;
182 dspr |= (index + 1) << prio;
183 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
184 }
185 }
186
187 /* Select display timing and dot clock generator 2 for planes associated
188 * with superposition controller 2.
189 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200190 if (rcrtc->index % 2) {
191 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200192
193 /* The DPTSR register is updated when the display controller is
194 * stopped. We thus need to restart the DU. Once again, sorry
195 * for the flicker. One way to mitigate the issue would be to
196 * pre-associate planes with CRTCs (either with a fixed 4/4
197 * split, or through a module parameter). Flicker would then
198 * occur only if we need to break the pre-association.
199 */
200 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200201 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200202 if (rcrtc->group->used_crtcs)
203 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200204 }
205 }
206
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200207 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
208 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200209}
210
211static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
212{
213 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200214 unsigned int i;
215
216 if (rcrtc->started)
217 return;
218
219 if (WARN_ON(rcrtc->plane->format == NULL))
220 return;
221
222 /* Set display off and background to black */
223 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
224 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
225
226 /* Configure display timings and output routing */
227 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200228 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200229
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200230 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200231 rcrtc->plane->enabled = true;
232 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200233 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200234
235 /* Setup planes. */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200236 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
237 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200238
239 if (plane->crtc != crtc || !plane->enabled)
240 continue;
241
242 rcar_du_plane_setup(plane);
243 }
244
245 /* Select master sync mode. This enables display operation in master
246 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
247 * actively driven).
248 */
249 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
250
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200251 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200252
253 rcrtc->started = true;
254}
255
256static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
257{
258 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200259
260 if (!rcrtc->started)
261 return;
262
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200263 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200264 rcrtc->plane->enabled = false;
265 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200266 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200267
268 /* Select switch sync mode. This stops display operation and configures
269 * the HSYNC and VSYNC signals as inputs.
270 */
271 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
272
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200273 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200274
275 rcrtc->started = false;
276}
277
278void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
279{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200280 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200281 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200282}
283
284void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
285{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200286 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
287 return;
288
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200289 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200290 rcar_du_crtc_start(rcrtc);
291}
292
293static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
294{
295 struct drm_crtc *crtc = &rcrtc->crtc;
296
297 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
298 rcar_du_plane_update_base(rcrtc->plane);
299}
300
301static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
302{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200303 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
304
305 if (rcrtc->dpms == mode)
306 return;
307
308 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200309 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200310 rcar_du_crtc_start(rcrtc);
311 } else {
312 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200313 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200314 }
315
316 rcrtc->dpms = mode;
317}
318
319static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
320 const struct drm_display_mode *mode,
321 struct drm_display_mode *adjusted_mode)
322{
323 /* TODO Fixup modes */
324 return true;
325}
326
327static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
328{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200329 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
330
331 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200332 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200333 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200334 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200335
336 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
337 * result.
338 */
339 rcar_du_crtc_stop(rcrtc);
340 rcar_du_plane_release(rcrtc->plane);
341
342 rcrtc->dpms = DRM_MODE_DPMS_OFF;
343}
344
345static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
346 struct drm_display_mode *mode,
347 struct drm_display_mode *adjusted_mode,
348 int x, int y,
349 struct drm_framebuffer *old_fb)
350{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200351 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200352 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200353 const struct rcar_du_format_info *format;
354 int ret;
355
356 format = rcar_du_format_info(crtc->fb->pixel_format);
357 if (format == NULL) {
358 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
359 crtc->fb->pixel_format);
360 ret = -EINVAL;
361 goto error;
362 }
363
364 ret = rcar_du_plane_reserve(rcrtc->plane, format);
365 if (ret < 0)
366 goto error;
367
368 rcrtc->plane->format = format;
369 rcrtc->plane->pitch = crtc->fb->pitches[0];
370
371 rcrtc->plane->src_x = x;
372 rcrtc->plane->src_y = y;
373 rcrtc->plane->width = mode->hdisplay;
374 rcrtc->plane->height = mode->vdisplay;
375
376 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
377
378 rcrtc->outputs = 0;
379
380 return 0;
381
382error:
383 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200384 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200385 * here.
386 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200387 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200388 return ret;
389}
390
391static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
392{
393 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
394
395 /* We're done, restart the CRTC and set the DPMS mode to on. The
396 * reference to the DU acquired at prepare() time will thus be released
397 * by the DPMS handler (possibly called by the disable() handler).
398 */
399 rcar_du_crtc_start(rcrtc);
400 rcrtc->dpms = DRM_MODE_DPMS_ON;
401}
402
403static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
404 struct drm_framebuffer *old_fb)
405{
406 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
407
408 rcrtc->plane->src_x = x;
409 rcrtc->plane->src_y = y;
410
411 rcar_du_crtc_update_base(to_rcar_crtc(crtc));
412
413 return 0;
414}
415
416static void rcar_du_crtc_disable(struct drm_crtc *crtc)
417{
418 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
419
420 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
421 rcar_du_plane_release(rcrtc->plane);
422}
423
424static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
425 .dpms = rcar_du_crtc_dpms,
426 .mode_fixup = rcar_du_crtc_mode_fixup,
427 .prepare = rcar_du_crtc_mode_prepare,
428 .commit = rcar_du_crtc_mode_commit,
429 .mode_set = rcar_du_crtc_mode_set,
430 .mode_set_base = rcar_du_crtc_mode_set_base,
431 .disable = rcar_du_crtc_disable,
432};
433
434void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
435 struct drm_file *file)
436{
437 struct drm_pending_vblank_event *event;
438 struct drm_device *dev = rcrtc->crtc.dev;
439 unsigned long flags;
440
441 /* Destroy the pending vertical blanking event associated with the
442 * pending page flip, if any, and disable vertical blanking interrupts.
443 */
444 spin_lock_irqsave(&dev->event_lock, flags);
445 event = rcrtc->event;
446 if (event && event->base.file_priv == file) {
447 rcrtc->event = NULL;
448 event->base.destroy(&event->base);
449 drm_vblank_put(dev, rcrtc->index);
450 }
451 spin_unlock_irqrestore(&dev->event_lock, flags);
452}
453
454static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
455{
456 struct drm_pending_vblank_event *event;
457 struct drm_device *dev = rcrtc->crtc.dev;
458 unsigned long flags;
459
460 spin_lock_irqsave(&dev->event_lock, flags);
461 event = rcrtc->event;
462 rcrtc->event = NULL;
463 spin_unlock_irqrestore(&dev->event_lock, flags);
464
465 if (event == NULL)
466 return;
467
468 spin_lock_irqsave(&dev->event_lock, flags);
469 drm_send_vblank_event(dev, rcrtc->index, event);
470 spin_unlock_irqrestore(&dev->event_lock, flags);
471
472 drm_vblank_put(dev, rcrtc->index);
473}
474
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200475static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
476{
477 struct rcar_du_crtc *rcrtc = arg;
478 irqreturn_t ret = IRQ_NONE;
479 u32 status;
480
481 status = rcar_du_crtc_read(rcrtc, DSSR);
482 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
483
484 if (status & DSSR_VBK) {
485 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
486 rcar_du_crtc_finish_page_flip(rcrtc);
487 ret = IRQ_HANDLED;
488 }
489
490 return ret;
491}
492
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200493static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
494 struct drm_framebuffer *fb,
495 struct drm_pending_vblank_event *event)
496{
497 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
498 struct drm_device *dev = rcrtc->crtc.dev;
499 unsigned long flags;
500
501 spin_lock_irqsave(&dev->event_lock, flags);
502 if (rcrtc->event != NULL) {
503 spin_unlock_irqrestore(&dev->event_lock, flags);
504 return -EBUSY;
505 }
506 spin_unlock_irqrestore(&dev->event_lock, flags);
507
508 crtc->fb = fb;
509 rcar_du_crtc_update_base(rcrtc);
510
511 if (event) {
512 event->pipe = rcrtc->index;
513 drm_vblank_get(dev, rcrtc->index);
514 spin_lock_irqsave(&dev->event_lock, flags);
515 rcrtc->event = event;
516 spin_unlock_irqrestore(&dev->event_lock, flags);
517 }
518
519 return 0;
520}
521
522static const struct drm_crtc_funcs crtc_funcs = {
523 .destroy = drm_crtc_cleanup,
524 .set_config = drm_crtc_helper_set_config,
525 .page_flip = rcar_du_crtc_page_flip,
526};
527
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200528int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200529{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200530 static const unsigned int mmio_offsets[] = {
531 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
532 };
533
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200534 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200535 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200536 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
537 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200538 unsigned int irqflags;
539 char clk_name[5];
540 char *name;
541 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200542 int ret;
543
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200544 /* Get the CRTC clock. */
545 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
546 sprintf(clk_name, "du.%u", index);
547 name = clk_name;
548 } else {
549 name = NULL;
550 }
551
552 rcrtc->clock = devm_clk_get(rcdu->dev, name);
553 if (IS_ERR(rcrtc->clock)) {
554 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
555 return PTR_ERR(rcrtc->clock);
556 }
557
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200558 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200559 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200560 rcrtc->index = index;
561 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200562 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200563
564 rcrtc->plane->crtc = crtc;
565
566 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
567 if (ret < 0)
568 return ret;
569
570 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
571
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200572 /* Register the interrupt handler. */
573 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
574 irq = platform_get_irq(pdev, index);
575 irqflags = 0;
576 } else {
577 irq = platform_get_irq(pdev, 0);
578 irqflags = IRQF_SHARED;
579 }
580
581 if (irq < 0) {
582 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
583 return ret;
584 }
585
586 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
587 dev_name(rcdu->dev), rcrtc);
588 if (ret < 0) {
589 dev_err(rcdu->dev,
590 "failed to register IRQ for CRTC %u\n", index);
591 return ret;
592 }
593
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200594 return 0;
595}
596
597void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
598{
599 if (enable) {
600 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
601 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
602 } else {
603 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
604 }
605}