blob: 03dd7018dde87bc0d18f6dcbd58a91f7c54819c7 [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{
33 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
34
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{
40 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
41
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{
47 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
48
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{
55 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
56
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{
64 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
65 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{
72 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
73 int ret;
74
75 ret = clk_prepare_enable(rcrtc->clock);
76 if (ret < 0)
77 return ret;
78
79 ret = rcar_du_get(rcdu);
80 if (ret < 0)
81 clk_disable_unprepare(rcrtc->clock);
82
83 return ret;
84}
85
86static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
87{
88 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
89
90 rcar_du_put(rcdu);
91 clk_disable_unprepare(rcrtc->clock);
92}
93
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020094static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
95{
96 struct drm_crtc *crtc = &rcrtc->crtc;
97 struct rcar_du_device *rcdu = crtc->dev->dev_private;
98 const struct drm_display_mode *mode = &crtc->mode;
99 unsigned long clk;
100 u32 value;
101 u32 div;
102
103 /* Dot clock */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200104 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200105 div = DIV_ROUND_CLOSEST(clk, mode->clock * 1000);
106 div = clamp(div, 1U, 64U) - 1;
107
108 rcar_du_write(rcdu, rcrtc->index ? ESCR2 : ESCR,
109 ESCR_DCLKSEL_CLKS | div);
110 rcar_du_write(rcdu, rcrtc->index ? OTAR2 : OTAR, 0);
111
112 /* Signal polarities */
113 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
114 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
115 | DSMR_DIPM_DE;
116 rcar_du_crtc_write(rcrtc, DSMR, value);
117
118 /* Display timings */
119 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
120 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
121 mode->hdisplay - 19);
122 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
123 mode->hsync_start - 1);
124 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
125
126 rcar_du_crtc_write(rcrtc, VDSR, mode->vtotal - mode->vsync_end - 2);
127 rcar_du_crtc_write(rcrtc, VDER, mode->vtotal - mode->vsync_end +
128 mode->vdisplay - 2);
129 rcar_du_crtc_write(rcrtc, VSPR, mode->vtotal - mode->vsync_end +
130 mode->vsync_start - 1);
131 rcar_du_crtc_write(rcrtc, VCR, mode->vtotal - 1);
132
133 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
134 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
135}
136
137static void rcar_du_crtc_set_routing(struct rcar_du_crtc *rcrtc)
138{
139 struct rcar_du_device *rcdu = rcrtc->crtc.dev->dev_private;
140 u32 dorcr = rcar_du_read(rcdu, DORCR);
141
142 dorcr &= ~(DORCR_PG2T | DORCR_DK2S | DORCR_PG2D_MASK);
143
144 /* Set the DU1 pins sources. Select CRTC 0 if explicitly requested and
145 * CRTC 1 in all other cases to avoid cloning CRTC 0 to DU0 and DU1 by
146 * default.
147 */
148 if (rcrtc->outputs & (1 << 1) && rcrtc->index == 0)
149 dorcr |= DORCR_PG2D_DS1;
150 else
151 dorcr |= DORCR_PG2T | DORCR_DK2S | DORCR_PG2D_DS2;
152
153 rcar_du_write(rcdu, DORCR, dorcr);
154}
155
156static void __rcar_du_start_stop(struct rcar_du_device *rcdu, bool start)
157{
158 rcar_du_write(rcdu, DSYSR,
159 (rcar_du_read(rcdu, DSYSR) & ~(DSYSR_DRES | DSYSR_DEN)) |
160 (start ? DSYSR_DEN : DSYSR_DRES));
161}
162
163static void rcar_du_start_stop(struct rcar_du_device *rcdu, bool start)
164{
165 /* Many of the configuration bits are only updated when the display
166 * reset (DRES) bit in DSYSR is set to 1, disabling *both* CRTCs. Some
167 * of those bits could be pre-configured, but others (especially the
168 * bits related to plane assignment to display timing controllers) need
169 * to be modified at runtime.
170 *
171 * Restart the display controller if a start is requested. Sorry for the
172 * flicker. It should be possible to move most of the "DRES-update" bits
173 * setup to driver initialization time and minimize the number of cases
174 * when the display controller will have to be restarted.
175 */
176 if (start) {
177 if (rcdu->used_crtcs++ != 0)
178 __rcar_du_start_stop(rcdu, false);
179 __rcar_du_start_stop(rcdu, true);
180 } else {
181 if (--rcdu->used_crtcs == 0)
182 __rcar_du_start_stop(rcdu, false);
183 }
184}
185
186void rcar_du_crtc_route_output(struct drm_crtc *crtc, unsigned int output)
187{
188 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
189
190 /* Store the route from the CRTC output to the DU output. The DU will be
191 * configured when starting the CRTC.
192 */
193 rcrtc->outputs |= 1 << output;
194}
195
196void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
197{
198 struct rcar_du_device *rcdu = crtc->dev->dev_private;
199 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
200 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
201 unsigned int num_planes = 0;
202 unsigned int prio = 0;
203 unsigned int i;
204 u32 dptsr = 0;
205 u32 dspr = 0;
206
207 for (i = 0; i < ARRAY_SIZE(rcdu->planes.planes); ++i) {
208 struct rcar_du_plane *plane = &rcdu->planes.planes[i];
209 unsigned int j;
210
211 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
212 continue;
213
214 /* Insert the plane in the sorted planes array. */
215 for (j = num_planes++; j > 0; --j) {
216 if (planes[j-1]->zpos <= plane->zpos)
217 break;
218 planes[j] = planes[j-1];
219 }
220
221 planes[j] = plane;
222 prio += plane->format->planes * 4;
223 }
224
225 for (i = 0; i < num_planes; ++i) {
226 struct rcar_du_plane *plane = planes[i];
227 unsigned int index = plane->hwindex;
228
229 prio -= 4;
230 dspr |= (index + 1) << prio;
231 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
232
233 if (plane->format->planes == 2) {
234 index = (index + 1) % 8;
235
236 prio -= 4;
237 dspr |= (index + 1) << prio;
238 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
239 }
240 }
241
242 /* Select display timing and dot clock generator 2 for planes associated
243 * with superposition controller 2.
244 */
245 if (rcrtc->index) {
246 u32 value = rcar_du_read(rcdu, DPTSR);
247
248 /* The DPTSR register is updated when the display controller is
249 * stopped. We thus need to restart the DU. Once again, sorry
250 * for the flicker. One way to mitigate the issue would be to
251 * pre-associate planes with CRTCs (either with a fixed 4/4
252 * split, or through a module parameter). Flicker would then
253 * occur only if we need to break the pre-association.
254 */
255 if (value != dptsr) {
256 rcar_du_write(rcdu, DPTSR, dptsr);
257 if (rcdu->used_crtcs) {
258 __rcar_du_start_stop(rcdu, false);
259 __rcar_du_start_stop(rcdu, true);
260 }
261 }
262 }
263
264 rcar_du_write(rcdu, rcrtc->index ? DS2PR : DS1PR, dspr);
265}
266
267static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
268{
269 struct drm_crtc *crtc = &rcrtc->crtc;
270 struct rcar_du_device *rcdu = crtc->dev->dev_private;
271 unsigned int i;
272
273 if (rcrtc->started)
274 return;
275
276 if (WARN_ON(rcrtc->plane->format == NULL))
277 return;
278
279 /* Set display off and background to black */
280 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
281 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
282
283 /* Configure display timings and output routing */
284 rcar_du_crtc_set_display_timing(rcrtc);
285 rcar_du_crtc_set_routing(rcrtc);
286
287 mutex_lock(&rcdu->planes.lock);
288 rcrtc->plane->enabled = true;
289 rcar_du_crtc_update_planes(crtc);
290 mutex_unlock(&rcdu->planes.lock);
291
292 /* Setup planes. */
293 for (i = 0; i < ARRAY_SIZE(rcdu->planes.planes); ++i) {
294 struct rcar_du_plane *plane = &rcdu->planes.planes[i];
295
296 if (plane->crtc != crtc || !plane->enabled)
297 continue;
298
299 rcar_du_plane_setup(plane);
300 }
301
302 /* Select master sync mode. This enables display operation in master
303 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
304 * actively driven).
305 */
306 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
307
308 rcar_du_start_stop(rcdu, true);
309
310 rcrtc->started = true;
311}
312
313static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
314{
315 struct drm_crtc *crtc = &rcrtc->crtc;
316 struct rcar_du_device *rcdu = crtc->dev->dev_private;
317
318 if (!rcrtc->started)
319 return;
320
321 mutex_lock(&rcdu->planes.lock);
322 rcrtc->plane->enabled = false;
323 rcar_du_crtc_update_planes(crtc);
324 mutex_unlock(&rcdu->planes.lock);
325
326 /* Select switch sync mode. This stops display operation and configures
327 * the HSYNC and VSYNC signals as inputs.
328 */
329 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
330
331 rcar_du_start_stop(rcdu, false);
332
333 rcrtc->started = false;
334}
335
336void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
337{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200338 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200339 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200340}
341
342void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
343{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200344 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
345 return;
346
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200347 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200348 rcar_du_crtc_start(rcrtc);
349}
350
351static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
352{
353 struct drm_crtc *crtc = &rcrtc->crtc;
354
355 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
356 rcar_du_plane_update_base(rcrtc->plane);
357}
358
359static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
360{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200361 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
362
363 if (rcrtc->dpms == mode)
364 return;
365
366 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200367 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200368 rcar_du_crtc_start(rcrtc);
369 } else {
370 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200371 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200372 }
373
374 rcrtc->dpms = mode;
375}
376
377static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
378 const struct drm_display_mode *mode,
379 struct drm_display_mode *adjusted_mode)
380{
381 /* TODO Fixup modes */
382 return true;
383}
384
385static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
386{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200387 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
388
389 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200390 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200391 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200392 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200393
394 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
395 * result.
396 */
397 rcar_du_crtc_stop(rcrtc);
398 rcar_du_plane_release(rcrtc->plane);
399
400 rcrtc->dpms = DRM_MODE_DPMS_OFF;
401}
402
403static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
404 struct drm_display_mode *mode,
405 struct drm_display_mode *adjusted_mode,
406 int x, int y,
407 struct drm_framebuffer *old_fb)
408{
409 struct rcar_du_device *rcdu = crtc->dev->dev_private;
410 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
411 const struct rcar_du_format_info *format;
412 int ret;
413
414 format = rcar_du_format_info(crtc->fb->pixel_format);
415 if (format == NULL) {
416 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
417 crtc->fb->pixel_format);
418 ret = -EINVAL;
419 goto error;
420 }
421
422 ret = rcar_du_plane_reserve(rcrtc->plane, format);
423 if (ret < 0)
424 goto error;
425
426 rcrtc->plane->format = format;
427 rcrtc->plane->pitch = crtc->fb->pitches[0];
428
429 rcrtc->plane->src_x = x;
430 rcrtc->plane->src_y = y;
431 rcrtc->plane->width = mode->hdisplay;
432 rcrtc->plane->height = mode->vdisplay;
433
434 rcar_du_plane_compute_base(rcrtc->plane, crtc->fb);
435
436 rcrtc->outputs = 0;
437
438 return 0;
439
440error:
441 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200442 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200443 * here.
444 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200445 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200446 return ret;
447}
448
449static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
450{
451 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
452
453 /* We're done, restart the CRTC and set the DPMS mode to on. The
454 * reference to the DU acquired at prepare() time will thus be released
455 * by the DPMS handler (possibly called by the disable() handler).
456 */
457 rcar_du_crtc_start(rcrtc);
458 rcrtc->dpms = DRM_MODE_DPMS_ON;
459}
460
461static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
462 struct drm_framebuffer *old_fb)
463{
464 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
465
466 rcrtc->plane->src_x = x;
467 rcrtc->plane->src_y = y;
468
469 rcar_du_crtc_update_base(to_rcar_crtc(crtc));
470
471 return 0;
472}
473
474static void rcar_du_crtc_disable(struct drm_crtc *crtc)
475{
476 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
477
478 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
479 rcar_du_plane_release(rcrtc->plane);
480}
481
482static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
483 .dpms = rcar_du_crtc_dpms,
484 .mode_fixup = rcar_du_crtc_mode_fixup,
485 .prepare = rcar_du_crtc_mode_prepare,
486 .commit = rcar_du_crtc_mode_commit,
487 .mode_set = rcar_du_crtc_mode_set,
488 .mode_set_base = rcar_du_crtc_mode_set_base,
489 .disable = rcar_du_crtc_disable,
490};
491
492void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
493 struct drm_file *file)
494{
495 struct drm_pending_vblank_event *event;
496 struct drm_device *dev = rcrtc->crtc.dev;
497 unsigned long flags;
498
499 /* Destroy the pending vertical blanking event associated with the
500 * pending page flip, if any, and disable vertical blanking interrupts.
501 */
502 spin_lock_irqsave(&dev->event_lock, flags);
503 event = rcrtc->event;
504 if (event && event->base.file_priv == file) {
505 rcrtc->event = NULL;
506 event->base.destroy(&event->base);
507 drm_vblank_put(dev, rcrtc->index);
508 }
509 spin_unlock_irqrestore(&dev->event_lock, flags);
510}
511
512static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
513{
514 struct drm_pending_vblank_event *event;
515 struct drm_device *dev = rcrtc->crtc.dev;
516 unsigned long flags;
517
518 spin_lock_irqsave(&dev->event_lock, flags);
519 event = rcrtc->event;
520 rcrtc->event = NULL;
521 spin_unlock_irqrestore(&dev->event_lock, flags);
522
523 if (event == NULL)
524 return;
525
526 spin_lock_irqsave(&dev->event_lock, flags);
527 drm_send_vblank_event(dev, rcrtc->index, event);
528 spin_unlock_irqrestore(&dev->event_lock, flags);
529
530 drm_vblank_put(dev, rcrtc->index);
531}
532
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200533static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
534{
535 struct rcar_du_crtc *rcrtc = arg;
536 irqreturn_t ret = IRQ_NONE;
537 u32 status;
538
539 status = rcar_du_crtc_read(rcrtc, DSSR);
540 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
541
542 if (status & DSSR_VBK) {
543 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
544 rcar_du_crtc_finish_page_flip(rcrtc);
545 ret = IRQ_HANDLED;
546 }
547
548 return ret;
549}
550
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200551static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
552 struct drm_framebuffer *fb,
553 struct drm_pending_vblank_event *event)
554{
555 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
556 struct drm_device *dev = rcrtc->crtc.dev;
557 unsigned long flags;
558
559 spin_lock_irqsave(&dev->event_lock, flags);
560 if (rcrtc->event != NULL) {
561 spin_unlock_irqrestore(&dev->event_lock, flags);
562 return -EBUSY;
563 }
564 spin_unlock_irqrestore(&dev->event_lock, flags);
565
566 crtc->fb = fb;
567 rcar_du_crtc_update_base(rcrtc);
568
569 if (event) {
570 event->pipe = rcrtc->index;
571 drm_vblank_get(dev, rcrtc->index);
572 spin_lock_irqsave(&dev->event_lock, flags);
573 rcrtc->event = event;
574 spin_unlock_irqrestore(&dev->event_lock, flags);
575 }
576
577 return 0;
578}
579
580static const struct drm_crtc_funcs crtc_funcs = {
581 .destroy = drm_crtc_cleanup,
582 .set_config = drm_crtc_helper_set_config,
583 .page_flip = rcar_du_crtc_page_flip,
584};
585
586int rcar_du_crtc_create(struct rcar_du_device *rcdu, unsigned int index)
587{
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200588 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200589 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
590 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200591 unsigned int irqflags;
592 char clk_name[5];
593 char *name;
594 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200595 int ret;
596
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200597 /* Get the CRTC clock. */
598 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
599 sprintf(clk_name, "du.%u", index);
600 name = clk_name;
601 } else {
602 name = NULL;
603 }
604
605 rcrtc->clock = devm_clk_get(rcdu->dev, name);
606 if (IS_ERR(rcrtc->clock)) {
607 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
608 return PTR_ERR(rcrtc->clock);
609 }
610
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200611 rcrtc->mmio_offset = index ? DISP2_REG_OFFSET : 0;
612 rcrtc->index = index;
613 rcrtc->dpms = DRM_MODE_DPMS_OFF;
614 rcrtc->plane = &rcdu->planes.planes[index];
615
616 rcrtc->plane->crtc = crtc;
617
618 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
619 if (ret < 0)
620 return ret;
621
622 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
623
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200624 /* Register the interrupt handler. */
625 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
626 irq = platform_get_irq(pdev, index);
627 irqflags = 0;
628 } else {
629 irq = platform_get_irq(pdev, 0);
630 irqflags = IRQF_SHARED;
631 }
632
633 if (irq < 0) {
634 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
635 return ret;
636 }
637
638 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
639 dev_name(rcdu->dev), rcrtc);
640 if (ret < 0) {
641 dev_err(rcdu->dev,
642 "failed to register IRQ for CRTC %u\n", index);
643 return ret;
644 }
645
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200646 return 0;
647}
648
649void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
650{
651 if (enable) {
652 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
653 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
654 } else {
655 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
656 }
657}