blob: 3d862a894b1762674804ab6055e4540d5f2119b0 [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 Pinchart1b30dbd2014-12-09 00:24:49 +020077 ret = clk_prepare_enable(rcrtc->extclock);
78 if (ret < 0)
79 goto error_clock;
80
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020081 ret = rcar_du_group_get(rcrtc->group);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020082 if (ret < 0)
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020083 goto error_group;
Laurent Pinchartf66ee302013-06-14 14:15:01 +020084
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020085 return 0;
86
87error_group:
88 clk_disable_unprepare(rcrtc->extclock);
89error_clock:
90 clk_disable_unprepare(rcrtc->clock);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020091 return ret;
92}
93
94static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
95{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020096 rcar_du_group_put(rcrtc->group);
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +020097
98 clk_disable_unprepare(rcrtc->extclock);
Laurent Pinchartf66ee302013-06-14 14:15:01 +020099 clk_disable_unprepare(rcrtc->clock);
100}
101
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200102/* -----------------------------------------------------------------------------
103 * Hardware Setup
104 */
105
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200106static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
107{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200108 const struct drm_display_mode *mode = &rcrtc->crtc.mode;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200109 unsigned long mode_clock = mode->clock * 1000;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200110 unsigned long clk;
111 u32 value;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200112 u32 escr;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200113 u32 div;
114
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200115 /* Compute the clock divisor and select the internal or external dot
116 * clock based on the requested frequency.
117 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200118 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200119 div = DIV_ROUND_CLOSEST(clk, mode_clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200120 div = clamp(div, 1U, 64U) - 1;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200121 escr = div | ESCR_DCLKSEL_CLKS;
122
123 if (rcrtc->extclock) {
124 unsigned long extclk;
125 unsigned long extrate;
126 unsigned long rate;
127 u32 extdiv;
128
129 extclk = clk_get_rate(rcrtc->extclock);
130 extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
131 extdiv = clamp(extdiv, 1U, 64U) - 1;
132
133 rate = clk / (div + 1);
134 extrate = extclk / (extdiv + 1);
135
136 if (abs((long)extrate - (long)mode_clock) <
137 abs((long)rate - (long)mode_clock)) {
138 dev_dbg(rcrtc->group->dev->dev,
139 "crtc%u: using external clock\n", rcrtc->index);
140 escr = extdiv | ESCR_DCLKSEL_DCLKIN;
141 }
142 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200143
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200144 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200145 escr);
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200146 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200147
148 /* Signal polarities */
149 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
150 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
Laurent Pinchartf67e1e02014-12-09 00:40:59 +0200151 | DSMR_DIPM_DE | DSMR_CSPM;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200152 rcar_du_crtc_write(rcrtc, DSMR, value);
153
154 /* Display timings */
155 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
156 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
157 mode->hdisplay - 19);
158 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
159 mode->hsync_start - 1);
160 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
161
Laurent Pinchart906eff72014-12-09 19:11:18 +0200162 rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
163 mode->crtc_vsync_end - 2);
164 rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
165 mode->crtc_vsync_end +
166 mode->crtc_vdisplay - 2);
167 rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
168 mode->crtc_vsync_end +
169 mode->crtc_vsync_start - 1);
170 rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200171
172 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
173 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
174}
175
Laurent Pinchartef67a902013-06-17 03:13:11 +0200176void rcar_du_crtc_route_output(struct drm_crtc *crtc,
177 enum rcar_du_output output)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200178{
179 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200180 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200181
182 /* Store the route from the CRTC output to the DU output. The DU will be
183 * configured when starting the CRTC.
184 */
Laurent Pinchartef67a902013-06-17 03:13:11 +0200185 rcrtc->outputs |= BIT(output);
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200186
Laurent Pinchart0c1c8772014-12-09 00:21:12 +0200187 /* Store RGB routing to DPAD0, the hardware will be configured when
188 * starting the CRTC.
189 */
190 if (output == RCAR_DU_OUTPUT_DPAD0)
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200191 rcdu->dpad0_source = rcrtc->index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200192}
193
194void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
195{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200196 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
197 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
198 unsigned int num_planes = 0;
199 unsigned int prio = 0;
200 unsigned int i;
201 u32 dptsr = 0;
202 u32 dspr = 0;
203
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200204 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
205 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200206 unsigned int j;
207
208 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
209 continue;
210
211 /* Insert the plane in the sorted planes array. */
212 for (j = num_planes++; j > 0; --j) {
213 if (planes[j-1]->zpos <= plane->zpos)
214 break;
215 planes[j] = planes[j-1];
216 }
217
218 planes[j] = plane;
219 prio += plane->format->planes * 4;
220 }
221
222 for (i = 0; i < num_planes; ++i) {
223 struct rcar_du_plane *plane = planes[i];
224 unsigned int index = plane->hwindex;
225
226 prio -= 4;
227 dspr |= (index + 1) << prio;
228 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
229
230 if (plane->format->planes == 2) {
231 index = (index + 1) % 8;
232
233 prio -= 4;
234 dspr |= (index + 1) << prio;
235 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
236 }
237 }
238
239 /* Select display timing and dot clock generator 2 for planes associated
240 * with superposition controller 2.
241 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200242 if (rcrtc->index % 2) {
243 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200244
245 /* The DPTSR register is updated when the display controller is
246 * stopped. We thus need to restart the DU. Once again, sorry
247 * for the flicker. One way to mitigate the issue would be to
248 * pre-associate planes with CRTCs (either with a fixed 4/4
249 * split, or through a module parameter). Flicker would then
250 * occur only if we need to break the pre-association.
251 */
252 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200253 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200254 if (rcrtc->group->used_crtcs)
255 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200256 }
257 }
258
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200259 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
260 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200261}
262
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200263/* -----------------------------------------------------------------------------
264 * Page Flip
265 */
266
267void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
268 struct drm_file *file)
269{
270 struct drm_pending_vblank_event *event;
271 struct drm_device *dev = rcrtc->crtc.dev;
272 unsigned long flags;
273
274 /* Destroy the pending vertical blanking event associated with the
275 * pending page flip, if any, and disable vertical blanking interrupts.
276 */
277 spin_lock_irqsave(&dev->event_lock, flags);
278 event = rcrtc->event;
279 if (event && event->base.file_priv == file) {
280 rcrtc->event = NULL;
281 event->base.destroy(&event->base);
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200282 drm_crtc_vblank_put(&rcrtc->crtc);
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200283 }
284 spin_unlock_irqrestore(&dev->event_lock, flags);
285}
286
287static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
288{
289 struct drm_pending_vblank_event *event;
290 struct drm_device *dev = rcrtc->crtc.dev;
291 unsigned long flags;
292
293 spin_lock_irqsave(&dev->event_lock, flags);
294 event = rcrtc->event;
295 rcrtc->event = NULL;
296 spin_unlock_irqrestore(&dev->event_lock, flags);
297
298 if (event == NULL)
299 return;
300
301 spin_lock_irqsave(&dev->event_lock, flags);
302 drm_send_vblank_event(dev, rcrtc->index, event);
Laurent Pinchart36693f32015-02-18 13:21:56 +0200303 wake_up(&rcrtc->flip_wait);
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200304 spin_unlock_irqrestore(&dev->event_lock, flags);
305
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200306 drm_crtc_vblank_put(&rcrtc->crtc);
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200307}
308
Laurent Pinchart36693f32015-02-18 13:21:56 +0200309static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
310{
311 struct drm_device *dev = rcrtc->crtc.dev;
312 unsigned long flags;
313 bool pending;
314
315 spin_lock_irqsave(&dev->event_lock, flags);
316 pending = rcrtc->event != NULL;
317 spin_unlock_irqrestore(&dev->event_lock, flags);
318
319 return pending;
320}
321
322static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
323{
324 struct rcar_du_device *rcdu = rcrtc->group->dev;
325
326 if (wait_event_timeout(rcrtc->flip_wait,
327 !rcar_du_crtc_page_flip_pending(rcrtc),
328 msecs_to_jiffies(50)))
329 return;
330
331 dev_warn(rcdu->dev, "page flip timeout\n");
332
333 rcar_du_crtc_finish_page_flip(rcrtc);
334}
335
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200336/* -----------------------------------------------------------------------------
337 * Start/Stop and Suspend/Resume
338 */
339
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200340static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
341{
342 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart906eff72014-12-09 19:11:18 +0200343 bool interlaced;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200344 unsigned int i;
345
346 if (rcrtc->started)
347 return;
348
349 if (WARN_ON(rcrtc->plane->format == NULL))
350 return;
351
352 /* Set display off and background to black */
353 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
354 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
355
356 /* Configure display timings and output routing */
357 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200358 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200359
Laurent Pinchart920888a2015-02-18 12:18:05 +0200360 /* FIXME: Commit the planes state. This is required here as the CRTC can
361 * be started from the DPMS and system resume handler, which don't go
362 * through .atomic_plane_update() and .atomic_flush() to commit plane
363 * state. Similarly a mode set operation without any update to planes
364 * will not go through atomic plane configuration either. Additionally,
365 * given that the plane state atomic commit occurs between CRTC disable
366 * and enable, the hardware state could also be lost due to runtime PM,
367 * requiring a full commit here. This will be fixed later after
368 * switching to atomic updates completely.
369 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200370 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200371 rcrtc->plane->enabled = true;
372 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200373 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200374
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200375 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
376 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200377
378 if (plane->crtc != crtc || !plane->enabled)
379 continue;
380
381 rcar_du_plane_setup(plane);
382 }
383
384 /* Select master sync mode. This enables display operation in master
385 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
386 * actively driven).
387 */
Laurent Pinchart906eff72014-12-09 19:11:18 +0200388 interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
389 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
390 (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
391 DSYSR_TVM_MASTER);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200392
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200393 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200394
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200395 /* Turn vertical blanking interrupt reporting back on. */
396 drm_crtc_vblank_on(crtc);
397
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200398 rcrtc->started = true;
399}
400
401static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
402{
403 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200404
405 if (!rcrtc->started)
406 return;
407
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200408 /* Disable vertical blanking interrupt reporting. We first need to wait
409 * for page flip completion before stopping the CRTC as userspace
410 * expects page flips to eventually complete.
Laurent Pinchart36693f32015-02-18 13:21:56 +0200411 */
412 rcar_du_crtc_wait_page_flip(rcrtc);
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200413 drm_crtc_vblank_off(crtc);
Laurent Pinchart36693f32015-02-18 13:21:56 +0200414
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200415 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200416 rcrtc->plane->enabled = false;
417 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200418 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200419
420 /* Select switch sync mode. This stops display operation and configures
421 * the HSYNC and VSYNC signals as inputs.
422 */
423 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
424
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200425 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200426
427 rcrtc->started = false;
428}
429
430void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
431{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200432 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200433 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200434}
435
436void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
437{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200438 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
439 return;
440
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200441 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200442 rcar_du_crtc_start(rcrtc);
443}
444
445static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
446{
447 struct drm_crtc *crtc = &rcrtc->crtc;
448
Matt Roperf4510a22014-04-01 15:22:40 -0700449 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200450 rcar_du_plane_update_base(rcrtc->plane);
451}
452
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200453/* -----------------------------------------------------------------------------
454 * CRTC Functions
455 */
456
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200457static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
458{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200459 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
460
Laurent Pinchart3dbf11e2014-12-09 13:19:10 +0200461 if (mode != DRM_MODE_DPMS_ON)
462 mode = DRM_MODE_DPMS_OFF;
463
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200464 if (rcrtc->dpms == mode)
465 return;
466
467 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200468 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200469 rcar_du_crtc_start(rcrtc);
470 } else {
471 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200472 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200473 }
474
475 rcrtc->dpms = mode;
476}
477
478static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
479 const struct drm_display_mode *mode,
480 struct drm_display_mode *adjusted_mode)
481{
482 /* TODO Fixup modes */
483 return true;
484}
485
486static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
487{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200488 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
489
490 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200491 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200492 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200493 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200494
495 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
496 * result.
497 */
498 rcar_du_crtc_stop(rcrtc);
499 rcar_du_plane_release(rcrtc->plane);
500
501 rcrtc->dpms = DRM_MODE_DPMS_OFF;
502}
503
504static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
505 struct drm_display_mode *mode,
506 struct drm_display_mode *adjusted_mode,
507 int x, int y,
508 struct drm_framebuffer *old_fb)
509{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200510 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200511 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200512 const struct rcar_du_format_info *format;
513 int ret;
514
Matt Roperf4510a22014-04-01 15:22:40 -0700515 format = rcar_du_format_info(crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200516 if (format == NULL) {
517 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
Matt Roperf4510a22014-04-01 15:22:40 -0700518 crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200519 ret = -EINVAL;
520 goto error;
521 }
522
523 ret = rcar_du_plane_reserve(rcrtc->plane, format);
524 if (ret < 0)
525 goto error;
526
527 rcrtc->plane->format = format;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200528
529 rcrtc->plane->src_x = x;
530 rcrtc->plane->src_y = y;
531 rcrtc->plane->width = mode->hdisplay;
532 rcrtc->plane->height = mode->vdisplay;
533
Matt Roperf4510a22014-04-01 15:22:40 -0700534 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200535
536 rcrtc->outputs = 0;
537
538 return 0;
539
540error:
541 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200542 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200543 * here.
544 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200545 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200546 return ret;
547}
548
549static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
550{
551 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
552
553 /* We're done, restart the CRTC and set the DPMS mode to on. The
554 * reference to the DU acquired at prepare() time will thus be released
555 * by the DPMS handler (possibly called by the disable() handler).
556 */
557 rcar_du_crtc_start(rcrtc);
558 rcrtc->dpms = DRM_MODE_DPMS_ON;
559}
560
561static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
562 struct drm_framebuffer *old_fb)
563{
564 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
565
566 rcrtc->plane->src_x = x;
567 rcrtc->plane->src_y = y;
568
Laurent Pinchartf5abcc42013-11-13 14:38:03 +0100569 rcar_du_crtc_update_base(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200570
571 return 0;
572}
573
574static void rcar_du_crtc_disable(struct drm_crtc *crtc)
575{
576 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
577
578 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
579 rcar_du_plane_release(rcrtc->plane);
580}
581
Laurent Pinchart920888a2015-02-18 12:18:05 +0200582static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc)
583{
584 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
585
586 /* We need to access the hardware during atomic update, acquire a
587 * reference to the CRTC.
588 */
589 rcar_du_crtc_get(rcrtc);
590}
591
592static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc)
593{
594 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
595
596 /* We're done, apply the configuration and drop the reference acquired
597 * in .atomic_begin().
598 */
599 mutex_lock(&rcrtc->group->planes.lock);
600 rcar_du_crtc_update_planes(crtc);
601 mutex_unlock(&rcrtc->group->planes.lock);
602
603 rcar_du_crtc_put(rcrtc);
604}
605
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200606static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
607 .dpms = rcar_du_crtc_dpms,
608 .mode_fixup = rcar_du_crtc_mode_fixup,
609 .prepare = rcar_du_crtc_mode_prepare,
610 .commit = rcar_du_crtc_mode_commit,
611 .mode_set = rcar_du_crtc_mode_set,
612 .mode_set_base = rcar_du_crtc_mode_set_base,
613 .disable = rcar_du_crtc_disable,
Laurent Pinchart920888a2015-02-18 12:18:05 +0200614 .atomic_begin = rcar_du_crtc_atomic_begin,
615 .atomic_flush = rcar_du_crtc_atomic_flush,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200616};
617
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200618static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
619 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700620 struct drm_pending_vblank_event *event,
621 uint32_t page_flip_flags)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200622{
623 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
624 struct drm_device *dev = rcrtc->crtc.dev;
625 unsigned long flags;
626
627 spin_lock_irqsave(&dev->event_lock, flags);
628 if (rcrtc->event != NULL) {
629 spin_unlock_irqrestore(&dev->event_lock, flags);
630 return -EBUSY;
631 }
632 spin_unlock_irqrestore(&dev->event_lock, flags);
633
Matt Roperf4510a22014-04-01 15:22:40 -0700634 crtc->primary->fb = fb;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200635 rcar_du_crtc_update_base(rcrtc);
636
637 if (event) {
638 event->pipe = rcrtc->index;
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200639 drm_crtc_vblank_get(crtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200640 spin_lock_irqsave(&dev->event_lock, flags);
641 rcrtc->event = event;
642 spin_unlock_irqrestore(&dev->event_lock, flags);
643 }
644
645 return 0;
646}
647
648static const struct drm_crtc_funcs crtc_funcs = {
649 .destroy = drm_crtc_cleanup,
650 .set_config = drm_crtc_helper_set_config,
651 .page_flip = rcar_du_crtc_page_flip,
652};
653
Laurent Pinchart17f6b8a2015-02-18 13:42:40 +0200654/* -----------------------------------------------------------------------------
655 * Interrupt Handling
656 */
657
658static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
659{
660 struct rcar_du_crtc *rcrtc = arg;
661 irqreturn_t ret = IRQ_NONE;
662 u32 status;
663
664 status = rcar_du_crtc_read(rcrtc, DSSR);
665 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
666
667 if (status & DSSR_FRM) {
668 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
669 rcar_du_crtc_finish_page_flip(rcrtc);
670 ret = IRQ_HANDLED;
671 }
672
673 return ret;
674}
675
676/* -----------------------------------------------------------------------------
677 * Initialization
678 */
679
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200680int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200681{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200682 static const unsigned int mmio_offsets[] = {
683 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
684 };
685
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200686 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200687 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200688 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
689 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200690 unsigned int irqflags;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200691 struct clk *clk;
692 char clk_name[9];
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200693 char *name;
694 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200695 int ret;
696
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200697 /* Get the CRTC clock and the optional external clock. */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200698 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
699 sprintf(clk_name, "du.%u", index);
700 name = clk_name;
701 } else {
702 name = NULL;
703 }
704
705 rcrtc->clock = devm_clk_get(rcdu->dev, name);
706 if (IS_ERR(rcrtc->clock)) {
707 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
708 return PTR_ERR(rcrtc->clock);
709 }
710
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200711 sprintf(clk_name, "dclkin.%u", index);
712 clk = devm_clk_get(rcdu->dev, clk_name);
713 if (!IS_ERR(clk)) {
714 rcrtc->extclock = clk;
715 } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
716 dev_info(rcdu->dev, "can't get external clock %u\n", index);
717 return -EPROBE_DEFER;
718 }
719
Laurent Pinchart36693f32015-02-18 13:21:56 +0200720 init_waitqueue_head(&rcrtc->flip_wait);
721
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200722 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200723 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200724 rcrtc->index = index;
725 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200726 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200727
728 rcrtc->plane->crtc = crtc;
729
Laurent Pinchart917de182015-02-17 18:34:17 +0200730 ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, &rcrtc->plane->plane,
731 NULL, &crtc_funcs);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200732 if (ret < 0)
733 return ret;
734
735 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
736
Laurent Pinchart0cd90a52015-02-18 13:14:46 +0200737 /* Start with vertical blanking interrupt reporting disabled. */
738 drm_crtc_vblank_off(crtc);
739
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200740 /* Register the interrupt handler. */
741 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
742 irq = platform_get_irq(pdev, index);
743 irqflags = 0;
744 } else {
745 irq = platform_get_irq(pdev, 0);
746 irqflags = IRQF_SHARED;
747 }
748
749 if (irq < 0) {
750 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
Julia Lawall6512f5f2014-11-23 14:11:17 +0100751 return irq;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200752 }
753
754 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
755 dev_name(rcdu->dev), rcrtc);
756 if (ret < 0) {
757 dev_err(rcdu->dev,
758 "failed to register IRQ for CRTC %u\n", index);
759 return ret;
760 }
761
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200762 return 0;
763}
764
765void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
766{
767 if (enable) {
768 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
769 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
770 } else {
771 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
772 }
773}