blob: 5cd10cf579856abd54189a416ae31445bc8f9d60 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_crtc.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "omap_drv.h"
21
Andy Grossb9ed9f02012-10-16 00:17:40 -050022#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010023#include <drm/drm_plane_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060024#include "drm_crtc.h"
25#include "drm_crtc_helper.h"
26
27#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
28
29struct omap_crtc {
30 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060031
Rob Clarkbb5c2d92012-01-16 12:51:16 -060032 const char *name;
Rob Clarkf5f94542012-12-04 13:59:12 -060033 int pipe;
34 enum omap_channel channel;
35 struct omap_overlay_manager_info info;
Tomi Valkeinenc7aef122014-04-03 16:30:03 +030036 struct drm_encoder *current_encoder;
Rob Clarkf5f94542012-12-04 13:59:12 -060037
38 /*
39 * Temporary: eventually this will go away, but it is needed
40 * for now to keep the output's happy. (They only need
41 * mgr->id.) Eventually this will be replaced w/ something
42 * more common-panel-framework-y
43 */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +030044 struct omap_overlay_manager *mgr;
Rob Clarkf5f94542012-12-04 13:59:12 -060045
46 struct omap_video_timings timings;
47 bool enabled;
Rob Clarkf5f94542012-12-04 13:59:12 -060048
49 struct omap_drm_apply apply;
50
51 struct omap_drm_irq apply_irq;
52 struct omap_drm_irq error_irq;
53
54 /* list of in-progress apply's: */
55 struct list_head pending_applies;
56
57 /* list of queued apply's: */
58 struct list_head queued_applies;
59
60 /* for handling queued and in-progress applies: */
61 struct work_struct apply_work;
Rob Clarkcd5351f2011-11-12 12:09:40 -060062
Rob Clarkbb5c2d92012-01-16 12:51:16 -060063 /* if there is a pending flip, these will be non-null: */
Rob Clarkcd5351f2011-11-12 12:09:40 -060064 struct drm_pending_vblank_event *event;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060065 struct drm_framebuffer *old_fb;
Rob Clarkf5f94542012-12-04 13:59:12 -060066
67 /* for handling page flips without caring about what
68 * the callback is called from. Possibly we should just
69 * make omap_gem always call the cb from the worker so
70 * we don't have to care about this..
71 *
72 * XXX maybe fold into apply_work??
73 */
74 struct work_struct page_flip_work;
Rob Clarkcd5351f2011-11-12 12:09:40 -060075};
76
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020077/* -----------------------------------------------------------------------------
78 * Helper Functions
79 */
80
Archit Taneja0d8f3712013-03-26 19:15:19 +053081uint32_t pipe2vbl(struct drm_crtc *crtc)
82{
83 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
84
85 return dispc_mgr_get_vsync_irq(omap_crtc->channel);
86}
87
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020088const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
89{
90 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
91 return &omap_crtc->timings;
92}
93
94enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
95{
96 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
97 return omap_crtc->channel;
98}
99
100/* -----------------------------------------------------------------------------
101 * DSS Manager Functions
102 */
103
Rob Clarkf5f94542012-12-04 13:59:12 -0600104/*
105 * Manager-ops, callbacks from output when they need to configure
106 * the upstream part of the video pipe.
107 *
108 * Most of these we can ignore until we add support for command-mode
109 * panels.. for video-mode the crtc-helpers already do an adequate
110 * job of sequencing the setup of the video pipe in the proper order
111 */
112
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300113/* ovl-mgr-id -> crtc */
114static struct omap_crtc *omap_crtcs[8];
115
Rob Clarkf5f94542012-12-04 13:59:12 -0600116/* we can probably ignore these until we support command-mode panels: */
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300117static int omap_crtc_connect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300118 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300119{
120 if (mgr->output)
121 return -EINVAL;
122
123 if ((mgr->supported_outputs & dst->id) == 0)
124 return -EINVAL;
125
126 dst->manager = mgr;
127 mgr->output = dst;
128
129 return 0;
130}
131
132static void omap_crtc_disconnect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300133 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300134{
135 mgr->output->manager = NULL;
136 mgr->output = NULL;
137}
138
Rob Clarkf5f94542012-12-04 13:59:12 -0600139static void omap_crtc_start_update(struct omap_overlay_manager *mgr)
140{
141}
142
Laurent Pinchart8472b572015-01-15 00:45:17 +0200143/* Called only from CRTC pre_apply and suspend/resume handlers. */
144static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
145{
146 struct drm_device *dev = crtc->dev;
147 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
148 enum omap_channel channel = omap_crtc->channel;
149 struct omap_irq_wait *wait;
150 u32 framedone_irq, vsync_irq;
151 int ret;
152
153 if (dispc_mgr_is_enabled(channel) == enable)
154 return;
155
156 /*
157 * Digit output produces some sync lost interrupts during the first
158 * frame when enabling, so we need to ignore those.
159 */
160 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
161
162 framedone_irq = dispc_mgr_get_framedone_irq(channel);
163 vsync_irq = dispc_mgr_get_vsync_irq(channel);
164
165 if (enable) {
166 wait = omap_irq_wait_init(dev, vsync_irq, 1);
167 } else {
168 /*
169 * When we disable the digit output, we need to wait for
170 * FRAMEDONE to know that DISPC has finished with the output.
171 *
172 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
173 * that case we need to use vsync interrupt, and wait for both
174 * even and odd frames.
175 */
176
177 if (framedone_irq)
178 wait = omap_irq_wait_init(dev, framedone_irq, 1);
179 else
180 wait = omap_irq_wait_init(dev, vsync_irq, 2);
181 }
182
183 dispc_mgr_enable(channel, enable);
184
185 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
186 if (ret) {
187 dev_err(dev->dev, "%s: timeout waiting for %s\n",
188 omap_crtc->name, enable ? "enable" : "disable");
189 }
190
191 omap_irq_register(crtc->dev, &omap_crtc->error_irq);
192}
193
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300194
Rob Clarkf5f94542012-12-04 13:59:12 -0600195static int omap_crtc_enable(struct omap_overlay_manager *mgr)
196{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300197 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
198
199 dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
200 dispc_mgr_set_timings(omap_crtc->channel,
201 &omap_crtc->timings);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200202 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300203
Rob Clarkf5f94542012-12-04 13:59:12 -0600204 return 0;
205}
206
207static void omap_crtc_disable(struct omap_overlay_manager *mgr)
208{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300209 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
210
Laurent Pinchart8472b572015-01-15 00:45:17 +0200211 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600212}
213
214static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
215 const struct omap_video_timings *timings)
216{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300217 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600218 DBG("%s", omap_crtc->name);
219 omap_crtc->timings = *timings;
Rob Clarkf5f94542012-12-04 13:59:12 -0600220}
221
222static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
223 const struct dss_lcd_mgr_config *config)
224{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300225 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600226 DBG("%s", omap_crtc->name);
227 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
228}
229
230static int omap_crtc_register_framedone_handler(
231 struct omap_overlay_manager *mgr,
232 void (*handler)(void *), void *data)
233{
234 return 0;
235}
236
237static void omap_crtc_unregister_framedone_handler(
238 struct omap_overlay_manager *mgr,
239 void (*handler)(void *), void *data)
240{
241}
242
243static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200244 .connect = omap_crtc_connect,
245 .disconnect = omap_crtc_disconnect,
246 .start_update = omap_crtc_start_update,
247 .enable = omap_crtc_enable,
248 .disable = omap_crtc_disable,
249 .set_timings = omap_crtc_set_timings,
250 .set_lcd_config = omap_crtc_set_lcd_config,
251 .register_framedone_handler = omap_crtc_register_framedone_handler,
252 .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
Rob Clarkf5f94542012-12-04 13:59:12 -0600253};
254
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200255/* -----------------------------------------------------------------------------
256 * Apply Logic
257 */
258
259static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
260{
261 struct omap_crtc *omap_crtc =
262 container_of(irq, struct omap_crtc, error_irq);
263 struct drm_crtc *crtc = &omap_crtc->base;
264 DRM_ERROR("%s: errors: %08x\n", omap_crtc->name, irqstatus);
265 /* avoid getting in a flood, unregister the irq until next vblank */
266 __omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
267}
268
269static void omap_crtc_apply_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
270{
271 struct omap_crtc *omap_crtc =
272 container_of(irq, struct omap_crtc, apply_irq);
273 struct drm_crtc *crtc = &omap_crtc->base;
274
275 if (!omap_crtc->error_irq.registered)
276 __omap_irq_register(crtc->dev, &omap_crtc->error_irq);
277
278 if (!dispc_mgr_go_busy(omap_crtc->channel)) {
279 struct omap_drm_private *priv =
280 crtc->dev->dev_private;
281 DBG("%s: apply done", omap_crtc->name);
282 __omap_irq_unregister(crtc->dev, &omap_crtc->apply_irq);
283 queue_work(priv->wq, &omap_crtc->apply_work);
284 }
285}
286
287static void apply_worker(struct work_struct *work)
288{
289 struct omap_crtc *omap_crtc =
290 container_of(work, struct omap_crtc, apply_work);
291 struct drm_crtc *crtc = &omap_crtc->base;
292 struct drm_device *dev = crtc->dev;
293 struct omap_drm_apply *apply, *n;
294 bool need_apply;
295
296 /*
297 * Synchronize everything on mode_config.mutex, to keep
298 * the callbacks and list modification all serialized
299 * with respect to modesetting ioctls from userspace.
300 */
301 drm_modeset_lock(&crtc->mutex, NULL);
302 dispc_runtime_get();
303
304 /*
305 * If we are still pending a previous update, wait.. when the
306 * pending update completes, we get kicked again.
307 */
308 if (omap_crtc->apply_irq.registered)
309 goto out;
310
311 /* finish up previous apply's: */
312 list_for_each_entry_safe(apply, n,
313 &omap_crtc->pending_applies, pending_node) {
314 apply->post_apply(apply);
315 list_del(&apply->pending_node);
316 }
317
318 need_apply = !list_empty(&omap_crtc->queued_applies);
319
320 /* then handle the next round of of queued apply's: */
321 list_for_each_entry_safe(apply, n,
322 &omap_crtc->queued_applies, queued_node) {
323 apply->pre_apply(apply);
324 list_del(&apply->queued_node);
325 apply->queued = false;
326 list_add_tail(&apply->pending_node,
327 &omap_crtc->pending_applies);
328 }
329
330 if (need_apply) {
331 enum omap_channel channel = omap_crtc->channel;
332
333 DBG("%s: GO", omap_crtc->name);
334
335 if (dispc_mgr_is_enabled(channel)) {
336 dispc_mgr_go(channel);
337 omap_irq_register(dev, &omap_crtc->apply_irq);
338 } else {
339 struct omap_drm_private *priv = dev->dev_private;
340 queue_work(priv->wq, &omap_crtc->apply_work);
341 }
342 }
343
344out:
345 dispc_runtime_put();
346 drm_modeset_unlock(&crtc->mutex);
347}
348
349int omap_crtc_apply(struct drm_crtc *crtc,
350 struct omap_drm_apply *apply)
351{
352 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
353
354 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
355
356 /* no need to queue it again if it is already queued: */
357 if (apply->queued)
358 return 0;
359
360 apply->queued = true;
361 list_add_tail(&apply->queued_node, &omap_crtc->queued_applies);
362
363 /*
364 * If there are no currently pending updates, then go ahead and
365 * kick the worker immediately, otherwise it will run again when
366 * the current update finishes.
367 */
368 if (list_empty(&omap_crtc->pending_applies)) {
369 struct omap_drm_private *priv = crtc->dev->dev_private;
370 queue_work(priv->wq, &omap_crtc->apply_work);
371 }
372
373 return 0;
374}
375
376static void omap_crtc_pre_apply(struct omap_drm_apply *apply)
377{
378 struct omap_crtc *omap_crtc =
379 container_of(apply, struct omap_crtc, apply);
380 struct drm_crtc *crtc = &omap_crtc->base;
381 struct omap_drm_private *priv = crtc->dev->dev_private;
382 struct drm_encoder *encoder = NULL;
383 unsigned int i;
384
385 DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
386
387 for (i = 0; i < priv->num_encoders; i++) {
388 if (priv->encoders[i]->crtc == crtc) {
389 encoder = priv->encoders[i];
390 break;
391 }
392 }
393
394 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
395 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
396
397 omap_crtc->current_encoder = encoder;
398
399 if (!omap_crtc->enabled) {
400 if (encoder)
401 omap_encoder_set_enabled(encoder, false);
402 } else {
403 if (encoder) {
404 omap_encoder_set_enabled(encoder, false);
405 omap_encoder_update(encoder, omap_crtc->mgr,
406 &omap_crtc->timings);
407 omap_encoder_set_enabled(encoder, true);
408 }
409 }
410}
411
412static void omap_crtc_post_apply(struct omap_drm_apply *apply)
413{
414 /* nothing needed for post-apply */
415}
416
417void omap_crtc_flush(struct drm_crtc *crtc)
418{
419 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
420 int loops = 0;
421
422 while (!list_empty(&omap_crtc->pending_applies) ||
423 !list_empty(&omap_crtc->queued_applies) ||
424 omap_crtc->event || omap_crtc->old_fb) {
425
426 if (++loops > 10) {
427 dev_err(crtc->dev->dev,
428 "omap_crtc_flush() timeout\n");
429 break;
430 }
431
432 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
433 }
434}
435
436/* -----------------------------------------------------------------------------
437 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600438 */
439
Rob Clarkcd5351f2011-11-12 12:09:40 -0600440static void omap_crtc_destroy(struct drm_crtc *crtc)
441{
442 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600443
444 DBG("%s", omap_crtc->name);
445
446 WARN_ON(omap_crtc->apply_irq.registered);
447 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
448
Rob Clarkcd5351f2011-11-12 12:09:40 -0600449 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600450
Rob Clarkcd5351f2011-11-12 12:09:40 -0600451 kfree(omap_crtc);
452}
453
454static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
455{
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600456 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600457 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600458 bool enabled = (mode == DRM_MODE_DPMS_ON);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600459 int i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600460
Rob Clarkf5f94542012-12-04 13:59:12 -0600461 DBG("%s: %d", omap_crtc->name, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600462
Rob Clarkf5f94542012-12-04 13:59:12 -0600463 if (enabled != omap_crtc->enabled) {
464 omap_crtc->enabled = enabled;
Rob Clarkf5f94542012-12-04 13:59:12 -0600465 omap_crtc_apply(crtc, &omap_crtc->apply);
466
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200467 /* Enable/disable all planes associated with the CRTC. */
Rob Clarkf5f94542012-12-04 13:59:12 -0600468 for (i = 0; i < priv->num_planes; i++) {
469 struct drm_plane *plane = priv->planes[i];
470 if (plane->crtc == crtc)
Laurent Pinchart2debab92015-01-12 22:38:16 +0200471 WARN_ON(omap_plane_set_enable(plane, enabled));
Rob Clarkf5f94542012-12-04 13:59:12 -0600472 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600473 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600474}
475
476static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200477 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600478 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600479{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600480 return true;
481}
482
483static int omap_crtc_mode_set(struct drm_crtc *crtc,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600484 struct drm_display_mode *mode,
485 struct drm_display_mode *adjusted_mode,
486 int x, int y,
487 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600488{
489 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
490
Rob Clarkf5f94542012-12-04 13:59:12 -0600491 mode = adjusted_mode;
492
493 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
494 omap_crtc->name, mode->base.id, mode->name,
495 mode->vrefresh, mode->clock,
496 mode->hdisplay, mode->hsync_start,
497 mode->hsync_end, mode->htotal,
498 mode->vdisplay, mode->vsync_start,
499 mode->vsync_end, mode->vtotal,
500 mode->type, mode->flags);
501
502 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
Rob Clarkf5f94542012-12-04 13:59:12 -0600503
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200504 /*
505 * The primary plane CRTC can be reset if the plane is disabled directly
506 * through the universal plane API. Set it again here.
507 */
508 crtc->primary->crtc = crtc;
509
510 return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200511 0, 0, mode->hdisplay, mode->vdisplay,
512 x, y, mode->hdisplay, mode->vdisplay,
513 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600514}
515
516static void omap_crtc_prepare(struct drm_crtc *crtc)
517{
518 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600519 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600520 omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
521}
522
523static void omap_crtc_commit(struct drm_crtc *crtc)
524{
525 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600526 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600527 omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
528}
529
530static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600531 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600532{
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200533 struct drm_plane *plane = crtc->primary;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600534 struct drm_display_mode *mode = &crtc->mode;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600535
Matt Roperf4510a22014-04-01 15:22:40 -0700536 return omap_plane_mode_set(plane, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200537 0, 0, mode->hdisplay, mode->vdisplay,
538 x, y, mode->hdisplay, mode->vdisplay,
539 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600540}
541
Rob Clark72d0c332012-03-11 21:11:21 -0500542static void vblank_cb(void *arg)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600543{
544 struct drm_crtc *crtc = arg;
545 struct drm_device *dev = crtc->dev;
546 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600547 unsigned long flags;
548
Rob Clarkf5f94542012-12-04 13:59:12 -0600549 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600550
551 /* wakeup userspace */
Rob Clarkf5f94542012-12-04 13:59:12 -0600552 if (omap_crtc->event)
553 drm_send_vblank_event(dev, omap_crtc->pipe, omap_crtc->event);
Rob Clark7411f9c2012-03-11 21:11:22 -0500554
Rob Clarkf5f94542012-12-04 13:59:12 -0600555 omap_crtc->event = NULL;
556 omap_crtc->old_fb = NULL;
557
558 spin_unlock_irqrestore(&dev->event_lock, flags);
559}
560
561static void page_flip_worker(struct work_struct *work)
562{
563 struct omap_crtc *omap_crtc =
564 container_of(work, struct omap_crtc, page_flip_work);
565 struct drm_crtc *crtc = &omap_crtc->base;
Rob Clarkf5f94542012-12-04 13:59:12 -0600566 struct drm_display_mode *mode = &crtc->mode;
567 struct drm_gem_object *bo;
568
Rob Clark51fd3712013-11-19 12:10:12 -0500569 drm_modeset_lock(&crtc->mutex, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200570 omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200571 0, 0, mode->hdisplay, mode->vdisplay,
572 crtc->x, crtc->y, mode->hdisplay, mode->vdisplay,
573 vblank_cb, crtc);
Rob Clark51fd3712013-11-19 12:10:12 -0500574 drm_modeset_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600575
Matt Roperf4510a22014-04-01 15:22:40 -0700576 bo = omap_framebuffer_bo(crtc->primary->fb, 0);
Rob Clarkf5f94542012-12-04 13:59:12 -0600577 drm_gem_object_unreference_unlocked(bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600578}
579
Rob Clark72d0c332012-03-11 21:11:21 -0500580static void page_flip_cb(void *arg)
581{
582 struct drm_crtc *crtc = arg;
583 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600584 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clark72d0c332012-03-11 21:11:21 -0500585
Rob Clarkf5f94542012-12-04 13:59:12 -0600586 /* avoid assumptions about what ctxt we are called from: */
587 queue_work(priv->wq, &omap_crtc->page_flip_work);
Rob Clark72d0c332012-03-11 21:11:21 -0500588}
589
Rob Clarkcd5351f2011-11-12 12:09:40 -0600590static int omap_crtc_page_flip_locked(struct drm_crtc *crtc,
591 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700592 struct drm_pending_vblank_event *event,
593 uint32_t page_flip_flags)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600594{
595 struct drm_device *dev = crtc->dev;
596 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700597 struct drm_plane *primary = crtc->primary;
Rob Clark119c0812012-09-04 17:46:22 -0500598 struct drm_gem_object *bo;
Archit Taneja38e55972014-04-11 12:53:35 +0530599 unsigned long flags;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600600
Matt Roperf4510a22014-04-01 15:22:40 -0700601 DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
Rob Clarkf5f94542012-12-04 13:59:12 -0600602 fb->base.id, event);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600603
Archit Taneja38e55972014-04-11 12:53:35 +0530604 spin_lock_irqsave(&dev->event_lock, flags);
605
Rob Clarkf5f94542012-12-04 13:59:12 -0600606 if (omap_crtc->old_fb) {
Archit Taneja38e55972014-04-11 12:53:35 +0530607 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600608 dev_err(dev->dev, "already a pending flip\n");
609 return -EINVAL;
610 }
611
Rob Clarkcd5351f2011-11-12 12:09:40 -0600612 omap_crtc->event = event;
Archit Tanejabc905ace2014-04-11 12:53:34 +0530613 omap_crtc->old_fb = primary->fb = fb;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600614
Archit Taneja38e55972014-04-11 12:53:35 +0530615 spin_unlock_irqrestore(&dev->event_lock, flags);
616
Rob Clark119c0812012-09-04 17:46:22 -0500617 /*
618 * Hold a reference temporarily until the crtc is updated
619 * and takes the reference to the bo. This avoids it
620 * getting freed from under us:
621 */
622 bo = omap_framebuffer_bo(fb, 0);
623 drm_gem_object_reference(bo);
624
625 omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600626
627 return 0;
628}
629
Rob Clark3c810c62012-08-15 15:18:01 -0500630static int omap_crtc_set_property(struct drm_crtc *crtc,
631 struct drm_property *property, uint64_t val)
632{
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500633 struct omap_drm_private *priv = crtc->dev->dev_private;
634
635 if (property == priv->rotation_prop) {
636 crtc->invert_dimensions =
637 !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
638 }
639
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200640 return omap_plane_set_property(crtc->primary, property, val);
Rob Clark3c810c62012-08-15 15:18:01 -0500641}
642
Rob Clarkcd5351f2011-11-12 12:09:40 -0600643static const struct drm_crtc_funcs omap_crtc_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600644 .set_config = drm_crtc_helper_set_config,
645 .destroy = omap_crtc_destroy,
646 .page_flip = omap_crtc_page_flip_locked,
Rob Clark3c810c62012-08-15 15:18:01 -0500647 .set_property = omap_crtc_set_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600648};
649
650static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
651 .dpms = omap_crtc_dpms,
652 .mode_fixup = omap_crtc_mode_fixup,
653 .mode_set = omap_crtc_mode_set,
654 .prepare = omap_crtc_prepare,
655 .commit = omap_crtc_commit,
656 .mode_set_base = omap_crtc_mode_set_base,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600657};
658
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200659/* -----------------------------------------------------------------------------
660 * Init and Cleanup
661 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300662
Rob Clarkf5f94542012-12-04 13:59:12 -0600663static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200664 [OMAP_DSS_CHANNEL_LCD] = "lcd",
665 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
666 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
667 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600668};
669
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300670void omap_crtc_pre_init(void)
671{
672 dss_install_mgr_ops(&mgr_ops);
673}
674
Archit Taneja3a01ab22014-01-02 14:49:51 +0530675void omap_crtc_pre_uninit(void)
676{
677 dss_uninstall_mgr_ops();
678}
679
Rob Clarkcd5351f2011-11-12 12:09:40 -0600680/* initialize crtc */
681struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600682 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600683{
684 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600685 struct omap_crtc *omap_crtc;
686 struct omap_overlay_manager_info *info;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200687 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600688
Rob Clarkf5f94542012-12-04 13:59:12 -0600689 DBG("%s", channel_names[channel]);
690
691 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800692 if (!omap_crtc)
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200693 return NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600694
Rob Clarkcd5351f2011-11-12 12:09:40 -0600695 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600696
Rob Clarkf5f94542012-12-04 13:59:12 -0600697 INIT_WORK(&omap_crtc->page_flip_work, page_flip_worker);
698 INIT_WORK(&omap_crtc->apply_work, apply_worker);
699
700 INIT_LIST_HEAD(&omap_crtc->pending_applies);
701 INIT_LIST_HEAD(&omap_crtc->queued_applies);
702
703 omap_crtc->apply.pre_apply = omap_crtc_pre_apply;
704 omap_crtc->apply.post_apply = omap_crtc_post_apply;
705
Archit Taneja0d8f3712013-03-26 19:15:19 +0530706 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530707 omap_crtc->name = channel_names[channel];
708 omap_crtc->pipe = id;
709
710 omap_crtc->apply_irq.irqmask = pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600711 omap_crtc->apply_irq.irq = omap_crtc_apply_irq;
712
713 omap_crtc->error_irq.irqmask =
714 dispc_mgr_get_sync_lost_irq(channel);
715 omap_crtc->error_irq.irq = omap_crtc_error_irq;
716 omap_irq_register(dev, &omap_crtc->error_irq);
717
Rob Clarkf5f94542012-12-04 13:59:12 -0600718 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300719 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600720
721 /* TODO: fix hard-coded setup.. add properties! */
722 info = &omap_crtc->info;
723 info->default_color = 0x00000000;
724 info->trans_key = 0x00000000;
725 info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
726 info->trans_enabled = false;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600727
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200728 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
729 &omap_crtc_funcs);
730 if (ret < 0) {
731 kfree(omap_crtc);
732 return NULL;
733 }
734
Rob Clarkcd5351f2011-11-12 12:09:40 -0600735 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
736
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200737 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500738
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300739 omap_crtcs[channel] = omap_crtc;
740
Rob Clarkcd5351f2011-11-12 12:09:40 -0600741 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600742}