blob: 86766cc6360a09ee1494ab1b85862651f6cc66e6 [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 Pinchart4bf8e192013-06-19 13:54:11 +0200102static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
103{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200104 const struct drm_display_mode *mode = &rcrtc->crtc.mode;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200105 unsigned long mode_clock = mode->clock * 1000;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200106 unsigned long clk;
107 u32 value;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200108 u32 escr;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200109 u32 div;
110
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200111 /* Compute the clock divisor and select the internal or external dot
112 * clock based on the requested frequency.
113 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200114 clk = clk_get_rate(rcrtc->clock);
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200115 div = DIV_ROUND_CLOSEST(clk, mode_clock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200116 div = clamp(div, 1U, 64U) - 1;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200117 escr = div | ESCR_DCLKSEL_CLKS;
118
119 if (rcrtc->extclock) {
120 unsigned long extclk;
121 unsigned long extrate;
122 unsigned long rate;
123 u32 extdiv;
124
125 extclk = clk_get_rate(rcrtc->extclock);
126 extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
127 extdiv = clamp(extdiv, 1U, 64U) - 1;
128
129 rate = clk / (div + 1);
130 extrate = extclk / (extdiv + 1);
131
132 if (abs((long)extrate - (long)mode_clock) <
133 abs((long)rate - (long)mode_clock)) {
134 dev_dbg(rcrtc->group->dev->dev,
135 "crtc%u: using external clock\n", rcrtc->index);
136 escr = extdiv | ESCR_DCLKSEL_DCLKIN;
137 }
138 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200139
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200140 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200141 escr);
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200142 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200143
144 /* Signal polarities */
145 value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
146 | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
Laurent Pinchartf67e1e02014-12-09 00:40:59 +0200147 | DSMR_DIPM_DE | DSMR_CSPM;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200148 rcar_du_crtc_write(rcrtc, DSMR, value);
149
150 /* Display timings */
151 rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
152 rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
153 mode->hdisplay - 19);
154 rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
155 mode->hsync_start - 1);
156 rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
157
158 rcar_du_crtc_write(rcrtc, VDSR, mode->vtotal - mode->vsync_end - 2);
159 rcar_du_crtc_write(rcrtc, VDER, mode->vtotal - mode->vsync_end +
160 mode->vdisplay - 2);
161 rcar_du_crtc_write(rcrtc, VSPR, mode->vtotal - mode->vsync_end +
162 mode->vsync_start - 1);
163 rcar_du_crtc_write(rcrtc, VCR, mode->vtotal - 1);
164
165 rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
166 rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
167}
168
Laurent Pinchartef67a902013-06-17 03:13:11 +0200169void rcar_du_crtc_route_output(struct drm_crtc *crtc,
170 enum rcar_du_output output)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200171{
172 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartef67a902013-06-17 03:13:11 +0200173 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200174
175 /* Store the route from the CRTC output to the DU output. The DU will be
176 * configured when starting the CRTC.
177 */
Laurent Pinchartef67a902013-06-17 03:13:11 +0200178 rcrtc->outputs |= BIT(output);
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200179
Laurent Pinchart0c1c8772014-12-09 00:21:12 +0200180 /* Store RGB routing to DPAD0, the hardware will be configured when
181 * starting the CRTC.
182 */
183 if (output == RCAR_DU_OUTPUT_DPAD0)
Laurent Pinchart7cbc05c2013-06-17 03:20:08 +0200184 rcdu->dpad0_source = rcrtc->index;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200185}
186
187void rcar_du_crtc_update_planes(struct drm_crtc *crtc)
188{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200189 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
190 struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
191 unsigned int num_planes = 0;
192 unsigned int prio = 0;
193 unsigned int i;
194 u32 dptsr = 0;
195 u32 dspr = 0;
196
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200197 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
198 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200199 unsigned int j;
200
201 if (plane->crtc != &rcrtc->crtc || !plane->enabled)
202 continue;
203
204 /* Insert the plane in the sorted planes array. */
205 for (j = num_planes++; j > 0; --j) {
206 if (planes[j-1]->zpos <= plane->zpos)
207 break;
208 planes[j] = planes[j-1];
209 }
210
211 planes[j] = plane;
212 prio += plane->format->planes * 4;
213 }
214
215 for (i = 0; i < num_planes; ++i) {
216 struct rcar_du_plane *plane = planes[i];
217 unsigned int index = plane->hwindex;
218
219 prio -= 4;
220 dspr |= (index + 1) << prio;
221 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
222
223 if (plane->format->planes == 2) {
224 index = (index + 1) % 8;
225
226 prio -= 4;
227 dspr |= (index + 1) << prio;
228 dptsr |= DPTSR_PnDK(index) | DPTSR_PnTS(index);
229 }
230 }
231
232 /* Select display timing and dot clock generator 2 for planes associated
233 * with superposition controller 2.
234 */
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200235 if (rcrtc->index % 2) {
236 u32 value = rcar_du_group_read(rcrtc->group, DPTSR);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200237
238 /* The DPTSR register is updated when the display controller is
239 * stopped. We thus need to restart the DU. Once again, sorry
240 * for the flicker. One way to mitigate the issue would be to
241 * pre-associate planes with CRTCs (either with a fixed 4/4
242 * split, or through a module parameter). Flicker would then
243 * occur only if we need to break the pre-association.
244 */
245 if (value != dptsr) {
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200246 rcar_du_group_write(rcrtc->group, DPTSR, dptsr);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200247 if (rcrtc->group->used_crtcs)
248 rcar_du_group_restart(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200249 }
250 }
251
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200252 rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
253 dspr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200254}
255
256static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
257{
258 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200259 unsigned int i;
260
261 if (rcrtc->started)
262 return;
263
264 if (WARN_ON(rcrtc->plane->format == NULL))
265 return;
266
267 /* Set display off and background to black */
268 rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
269 rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
270
271 /* Configure display timings and output routing */
272 rcar_du_crtc_set_display_timing(rcrtc);
Laurent Pinchart2fd22db2013-06-17 00:11:05 +0200273 rcar_du_group_set_routing(rcrtc->group);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200274
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200275 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200276 rcrtc->plane->enabled = true;
277 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200278 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200279
280 /* Setup planes. */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200281 for (i = 0; i < ARRAY_SIZE(rcrtc->group->planes.planes); ++i) {
282 struct rcar_du_plane *plane = &rcrtc->group->planes.planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200283
284 if (plane->crtc != crtc || !plane->enabled)
285 continue;
286
287 rcar_du_plane_setup(plane);
288 }
289
290 /* Select master sync mode. This enables display operation in master
291 * sync mode (with the HSYNC and VSYNC signals configured as outputs and
292 * actively driven).
293 */
294 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_MASTER);
295
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200296 rcar_du_group_start_stop(rcrtc->group, true);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200297
298 rcrtc->started = true;
299}
300
301static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
302{
303 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200304
305 if (!rcrtc->started)
306 return;
307
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200308 mutex_lock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200309 rcrtc->plane->enabled = false;
310 rcar_du_crtc_update_planes(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200311 mutex_unlock(&rcrtc->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200312
313 /* Select switch sync mode. This stops display operation and configures
314 * the HSYNC and VSYNC signals as inputs.
315 */
316 rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
317
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200318 rcar_du_group_start_stop(rcrtc->group, false);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200319
320 rcrtc->started = false;
321}
322
323void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
324{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200325 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200326 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200327}
328
329void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
330{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200331 if (rcrtc->dpms != DRM_MODE_DPMS_ON)
332 return;
333
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200334 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200335 rcar_du_crtc_start(rcrtc);
336}
337
338static void rcar_du_crtc_update_base(struct rcar_du_crtc *rcrtc)
339{
340 struct drm_crtc *crtc = &rcrtc->crtc;
341
Matt Roperf4510a22014-04-01 15:22:40 -0700342 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200343 rcar_du_plane_update_base(rcrtc->plane);
344}
345
346static void rcar_du_crtc_dpms(struct drm_crtc *crtc, int mode)
347{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200348 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
349
Laurent Pinchart3dbf11e2014-12-09 13:19:10 +0200350 if (mode != DRM_MODE_DPMS_ON)
351 mode = DRM_MODE_DPMS_OFF;
352
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200353 if (rcrtc->dpms == mode)
354 return;
355
356 if (mode == DRM_MODE_DPMS_ON) {
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200357 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200358 rcar_du_crtc_start(rcrtc);
359 } else {
360 rcar_du_crtc_stop(rcrtc);
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200361 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200362 }
363
364 rcrtc->dpms = mode;
365}
366
367static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
368 const struct drm_display_mode *mode,
369 struct drm_display_mode *adjusted_mode)
370{
371 /* TODO Fixup modes */
372 return true;
373}
374
375static void rcar_du_crtc_mode_prepare(struct drm_crtc *crtc)
376{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200377 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
378
379 /* We need to access the hardware during mode set, acquire a reference
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200380 * to the CRTC.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200381 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200382 rcar_du_crtc_get(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200383
384 /* Stop the CRTC and release the plane. Force the DPMS mode to off as a
385 * result.
386 */
387 rcar_du_crtc_stop(rcrtc);
388 rcar_du_plane_release(rcrtc->plane);
389
390 rcrtc->dpms = DRM_MODE_DPMS_OFF;
391}
392
393static int rcar_du_crtc_mode_set(struct drm_crtc *crtc,
394 struct drm_display_mode *mode,
395 struct drm_display_mode *adjusted_mode,
396 int x, int y,
397 struct drm_framebuffer *old_fb)
398{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200399 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200400 struct rcar_du_device *rcdu = rcrtc->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200401 const struct rcar_du_format_info *format;
402 int ret;
403
Matt Roperf4510a22014-04-01 15:22:40 -0700404 format = rcar_du_format_info(crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200405 if (format == NULL) {
406 dev_dbg(rcdu->dev, "mode_set: unsupported format %08x\n",
Matt Roperf4510a22014-04-01 15:22:40 -0700407 crtc->primary->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200408 ret = -EINVAL;
409 goto error;
410 }
411
412 ret = rcar_du_plane_reserve(rcrtc->plane, format);
413 if (ret < 0)
414 goto error;
415
416 rcrtc->plane->format = format;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200417
418 rcrtc->plane->src_x = x;
419 rcrtc->plane->src_y = y;
420 rcrtc->plane->width = mode->hdisplay;
421 rcrtc->plane->height = mode->vdisplay;
422
Matt Roperf4510a22014-04-01 15:22:40 -0700423 rcar_du_plane_compute_base(rcrtc->plane, crtc->primary->fb);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200424
425 rcrtc->outputs = 0;
426
427 return 0;
428
429error:
430 /* There's no rollback/abort operation to clean up in case of error. We
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200431 * thus need to release the reference to the CRTC acquired in prepare()
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200432 * here.
433 */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200434 rcar_du_crtc_put(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200435 return ret;
436}
437
438static void rcar_du_crtc_mode_commit(struct drm_crtc *crtc)
439{
440 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
441
442 /* We're done, restart the CRTC and set the DPMS mode to on. The
443 * reference to the DU acquired at prepare() time will thus be released
444 * by the DPMS handler (possibly called by the disable() handler).
445 */
446 rcar_du_crtc_start(rcrtc);
447 rcrtc->dpms = DRM_MODE_DPMS_ON;
448}
449
450static int rcar_du_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
451 struct drm_framebuffer *old_fb)
452{
453 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
454
455 rcrtc->plane->src_x = x;
456 rcrtc->plane->src_y = y;
457
Laurent Pinchartf5abcc42013-11-13 14:38:03 +0100458 rcar_du_crtc_update_base(rcrtc);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200459
460 return 0;
461}
462
463static void rcar_du_crtc_disable(struct drm_crtc *crtc)
464{
465 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
466
467 rcar_du_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
468 rcar_du_plane_release(rcrtc->plane);
469}
470
471static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
472 .dpms = rcar_du_crtc_dpms,
473 .mode_fixup = rcar_du_crtc_mode_fixup,
474 .prepare = rcar_du_crtc_mode_prepare,
475 .commit = rcar_du_crtc_mode_commit,
476 .mode_set = rcar_du_crtc_mode_set,
477 .mode_set_base = rcar_du_crtc_mode_set_base,
478 .disable = rcar_du_crtc_disable,
479};
480
481void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
482 struct drm_file *file)
483{
484 struct drm_pending_vblank_event *event;
485 struct drm_device *dev = rcrtc->crtc.dev;
486 unsigned long flags;
487
488 /* Destroy the pending vertical blanking event associated with the
489 * pending page flip, if any, and disable vertical blanking interrupts.
490 */
491 spin_lock_irqsave(&dev->event_lock, flags);
492 event = rcrtc->event;
493 if (event && event->base.file_priv == file) {
494 rcrtc->event = NULL;
495 event->base.destroy(&event->base);
496 drm_vblank_put(dev, rcrtc->index);
497 }
498 spin_unlock_irqrestore(&dev->event_lock, flags);
499}
500
501static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
502{
503 struct drm_pending_vblank_event *event;
504 struct drm_device *dev = rcrtc->crtc.dev;
505 unsigned long flags;
506
507 spin_lock_irqsave(&dev->event_lock, flags);
508 event = rcrtc->event;
509 rcrtc->event = NULL;
510 spin_unlock_irqrestore(&dev->event_lock, flags);
511
512 if (event == NULL)
513 return;
514
515 spin_lock_irqsave(&dev->event_lock, flags);
516 drm_send_vblank_event(dev, rcrtc->index, event);
517 spin_unlock_irqrestore(&dev->event_lock, flags);
518
519 drm_vblank_put(dev, rcrtc->index);
520}
521
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200522static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
523{
524 struct rcar_du_crtc *rcrtc = arg;
525 irqreturn_t ret = IRQ_NONE;
526 u32 status;
527
528 status = rcar_du_crtc_read(rcrtc, DSSR);
529 rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
530
531 if (status & DSSR_VBK) {
532 drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
533 rcar_du_crtc_finish_page_flip(rcrtc);
534 ret = IRQ_HANDLED;
535 }
536
537 return ret;
538}
539
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200540static int rcar_du_crtc_page_flip(struct drm_crtc *crtc,
541 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700542 struct drm_pending_vblank_event *event,
543 uint32_t page_flip_flags)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200544{
545 struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
546 struct drm_device *dev = rcrtc->crtc.dev;
547 unsigned long flags;
548
549 spin_lock_irqsave(&dev->event_lock, flags);
550 if (rcrtc->event != NULL) {
551 spin_unlock_irqrestore(&dev->event_lock, flags);
552 return -EBUSY;
553 }
554 spin_unlock_irqrestore(&dev->event_lock, flags);
555
Matt Roperf4510a22014-04-01 15:22:40 -0700556 crtc->primary->fb = fb;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200557 rcar_du_crtc_update_base(rcrtc);
558
559 if (event) {
560 event->pipe = rcrtc->index;
561 drm_vblank_get(dev, rcrtc->index);
562 spin_lock_irqsave(&dev->event_lock, flags);
563 rcrtc->event = event;
564 spin_unlock_irqrestore(&dev->event_lock, flags);
565 }
566
567 return 0;
568}
569
570static const struct drm_crtc_funcs crtc_funcs = {
571 .destroy = drm_crtc_cleanup,
572 .set_config = drm_crtc_helper_set_config,
573 .page_flip = rcar_du_crtc_page_flip,
574};
575
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200576int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200577{
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200578 static const unsigned int mmio_offsets[] = {
579 DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
580 };
581
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200582 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200583 struct platform_device *pdev = to_platform_device(rcdu->dev);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200584 struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
585 struct drm_crtc *crtc = &rcrtc->crtc;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200586 unsigned int irqflags;
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200587 struct clk *clk;
588 char clk_name[9];
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200589 char *name;
590 int irq;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200591 int ret;
592
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200593 /* Get the CRTC clock and the optional external clock. */
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200594 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
595 sprintf(clk_name, "du.%u", index);
596 name = clk_name;
597 } else {
598 name = NULL;
599 }
600
601 rcrtc->clock = devm_clk_get(rcdu->dev, name);
602 if (IS_ERR(rcrtc->clock)) {
603 dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
604 return PTR_ERR(rcrtc->clock);
605 }
606
Laurent Pinchart1b30dbd2014-12-09 00:24:49 +0200607 sprintf(clk_name, "dclkin.%u", index);
608 clk = devm_clk_get(rcdu->dev, clk_name);
609 if (!IS_ERR(clk)) {
610 rcrtc->extclock = clk;
611 } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
612 dev_info(rcdu->dev, "can't get external clock %u\n", index);
613 return -EPROBE_DEFER;
614 }
615
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200616 rcrtc->group = rgrp;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200617 rcrtc->mmio_offset = mmio_offsets[index];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200618 rcrtc->index = index;
619 rcrtc->dpms = DRM_MODE_DPMS_OFF;
Laurent Pincharta5f0ef52013-06-17 00:29:25 +0200620 rcrtc->plane = &rgrp->planes.planes[index % 2];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200621
622 rcrtc->plane->crtc = crtc;
623
624 ret = drm_crtc_init(rcdu->ddev, crtc, &crtc_funcs);
625 if (ret < 0)
626 return ret;
627
628 drm_crtc_helper_add(crtc, &crtc_helper_funcs);
629
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200630 /* Register the interrupt handler. */
631 if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
632 irq = platform_get_irq(pdev, index);
633 irqflags = 0;
634 } else {
635 irq = platform_get_irq(pdev, 0);
636 irqflags = IRQF_SHARED;
637 }
638
639 if (irq < 0) {
640 dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
Julia Lawall6512f5f2014-11-23 14:11:17 +0100641 return irq;
Laurent Pinchartf66ee302013-06-14 14:15:01 +0200642 }
643
644 ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
645 dev_name(rcdu->dev), rcrtc);
646 if (ret < 0) {
647 dev_err(rcdu->dev,
648 "failed to register IRQ for CRTC %u\n", index);
649 return ret;
650 }
651
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200652 return 0;
653}
654
655void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
656{
657 if (enable) {
658 rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
659 rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
660 } else {
661 rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
662 }
663}