blob: 7b93880a4577c46c63b211a2459e6642316e93eb [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;
Tomi Valkeinena36af732015-02-26 15:20:24 +020075
76 bool ignore_digit_sync_lost;
Rob Clarkcd5351f2011-11-12 12:09:40 -060077};
78
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020079/* -----------------------------------------------------------------------------
80 * Helper Functions
81 */
82
Archit Taneja0d8f3712013-03-26 19:15:19 +053083uint32_t pipe2vbl(struct drm_crtc *crtc)
84{
85 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
86
87 return dispc_mgr_get_vsync_irq(omap_crtc->channel);
88}
89
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020090const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
91{
92 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
93 return &omap_crtc->timings;
94}
95
96enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
97{
98 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
99 return omap_crtc->channel;
100}
101
102/* -----------------------------------------------------------------------------
103 * DSS Manager Functions
104 */
105
Rob Clarkf5f94542012-12-04 13:59:12 -0600106/*
107 * Manager-ops, callbacks from output when they need to configure
108 * the upstream part of the video pipe.
109 *
110 * Most of these we can ignore until we add support for command-mode
111 * panels.. for video-mode the crtc-helpers already do an adequate
112 * job of sequencing the setup of the video pipe in the proper order
113 */
114
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300115/* ovl-mgr-id -> crtc */
116static struct omap_crtc *omap_crtcs[8];
117
Rob Clarkf5f94542012-12-04 13:59:12 -0600118/* we can probably ignore these until we support command-mode panels: */
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300119static int omap_crtc_connect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300120 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300121{
122 if (mgr->output)
123 return -EINVAL;
124
125 if ((mgr->supported_outputs & dst->id) == 0)
126 return -EINVAL;
127
128 dst->manager = mgr;
129 mgr->output = dst;
130
131 return 0;
132}
133
134static void omap_crtc_disconnect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300135 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300136{
137 mgr->output->manager = NULL;
138 mgr->output = NULL;
139}
140
Rob Clarkf5f94542012-12-04 13:59:12 -0600141static void omap_crtc_start_update(struct omap_overlay_manager *mgr)
142{
143}
144
Laurent Pinchart8472b572015-01-15 00:45:17 +0200145/* Called only from CRTC pre_apply and suspend/resume handlers. */
146static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
147{
148 struct drm_device *dev = crtc->dev;
149 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
150 enum omap_channel channel = omap_crtc->channel;
151 struct omap_irq_wait *wait;
152 u32 framedone_irq, vsync_irq;
153 int ret;
154
155 if (dispc_mgr_is_enabled(channel) == enable)
156 return;
157
Tomi Valkeinenef422282015-02-26 15:20:25 +0200158 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
159 /*
160 * Digit output produces some sync lost interrupts during the
161 * first frame when enabling, so we need to ignore those.
162 */
163 omap_crtc->ignore_digit_sync_lost = true;
164 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200165
166 framedone_irq = dispc_mgr_get_framedone_irq(channel);
167 vsync_irq = dispc_mgr_get_vsync_irq(channel);
168
169 if (enable) {
170 wait = omap_irq_wait_init(dev, vsync_irq, 1);
171 } else {
172 /*
173 * When we disable the digit output, we need to wait for
174 * FRAMEDONE to know that DISPC has finished with the output.
175 *
176 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
177 * that case we need to use vsync interrupt, and wait for both
178 * even and odd frames.
179 */
180
181 if (framedone_irq)
182 wait = omap_irq_wait_init(dev, framedone_irq, 1);
183 else
184 wait = omap_irq_wait_init(dev, vsync_irq, 2);
185 }
186
187 dispc_mgr_enable(channel, enable);
188
189 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
190 if (ret) {
191 dev_err(dev->dev, "%s: timeout waiting for %s\n",
192 omap_crtc->name, enable ? "enable" : "disable");
193 }
194
Tomi Valkeinenef422282015-02-26 15:20:25 +0200195 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
196 omap_crtc->ignore_digit_sync_lost = false;
197 /* make sure the irq handler sees the value above */
198 mb();
199 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200200}
201
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300202
Rob Clarkf5f94542012-12-04 13:59:12 -0600203static int omap_crtc_enable(struct omap_overlay_manager *mgr)
204{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300205 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
206
207 dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
208 dispc_mgr_set_timings(omap_crtc->channel,
209 &omap_crtc->timings);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200210 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300211
Rob Clarkf5f94542012-12-04 13:59:12 -0600212 return 0;
213}
214
215static void omap_crtc_disable(struct omap_overlay_manager *mgr)
216{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300217 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
218
Laurent Pinchart8472b572015-01-15 00:45:17 +0200219 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600220}
221
222static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
223 const struct omap_video_timings *timings)
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 omap_crtc->timings = *timings;
Rob Clarkf5f94542012-12-04 13:59:12 -0600228}
229
230static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
231 const struct dss_lcd_mgr_config *config)
232{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300233 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600234 DBG("%s", omap_crtc->name);
235 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
236}
237
238static int omap_crtc_register_framedone_handler(
239 struct omap_overlay_manager *mgr,
240 void (*handler)(void *), void *data)
241{
242 return 0;
243}
244
245static void omap_crtc_unregister_framedone_handler(
246 struct omap_overlay_manager *mgr,
247 void (*handler)(void *), void *data)
248{
249}
250
251static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200252 .connect = omap_crtc_connect,
253 .disconnect = omap_crtc_disconnect,
254 .start_update = omap_crtc_start_update,
255 .enable = omap_crtc_enable,
256 .disable = omap_crtc_disable,
257 .set_timings = omap_crtc_set_timings,
258 .set_lcd_config = omap_crtc_set_lcd_config,
259 .register_framedone_handler = omap_crtc_register_framedone_handler,
260 .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
Rob Clarkf5f94542012-12-04 13:59:12 -0600261};
262
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200263/* -----------------------------------------------------------------------------
264 * Apply Logic
265 */
266
267static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
268{
269 struct omap_crtc *omap_crtc =
270 container_of(irq, struct omap_crtc, error_irq);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200271
272 if (omap_crtc->ignore_digit_sync_lost) {
273 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
274 if (!irqstatus)
275 return;
276 }
277
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200278 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200279}
280
281static void omap_crtc_apply_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
282{
283 struct omap_crtc *omap_crtc =
284 container_of(irq, struct omap_crtc, apply_irq);
285 struct drm_crtc *crtc = &omap_crtc->base;
286
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200287 if (!dispc_mgr_go_busy(omap_crtc->channel)) {
288 struct omap_drm_private *priv =
289 crtc->dev->dev_private;
290 DBG("%s: apply done", omap_crtc->name);
291 __omap_irq_unregister(crtc->dev, &omap_crtc->apply_irq);
292 queue_work(priv->wq, &omap_crtc->apply_work);
293 }
294}
295
296static void apply_worker(struct work_struct *work)
297{
298 struct omap_crtc *omap_crtc =
299 container_of(work, struct omap_crtc, apply_work);
300 struct drm_crtc *crtc = &omap_crtc->base;
301 struct drm_device *dev = crtc->dev;
302 struct omap_drm_apply *apply, *n;
303 bool need_apply;
304
305 /*
306 * Synchronize everything on mode_config.mutex, to keep
307 * the callbacks and list modification all serialized
308 * with respect to modesetting ioctls from userspace.
309 */
310 drm_modeset_lock(&crtc->mutex, NULL);
311 dispc_runtime_get();
312
313 /*
314 * If we are still pending a previous update, wait.. when the
315 * pending update completes, we get kicked again.
316 */
317 if (omap_crtc->apply_irq.registered)
318 goto out;
319
320 /* finish up previous apply's: */
321 list_for_each_entry_safe(apply, n,
322 &omap_crtc->pending_applies, pending_node) {
323 apply->post_apply(apply);
324 list_del(&apply->pending_node);
325 }
326
327 need_apply = !list_empty(&omap_crtc->queued_applies);
328
329 /* then handle the next round of of queued apply's: */
330 list_for_each_entry_safe(apply, n,
331 &omap_crtc->queued_applies, queued_node) {
332 apply->pre_apply(apply);
333 list_del(&apply->queued_node);
334 apply->queued = false;
335 list_add_tail(&apply->pending_node,
336 &omap_crtc->pending_applies);
337 }
338
339 if (need_apply) {
340 enum omap_channel channel = omap_crtc->channel;
341
342 DBG("%s: GO", omap_crtc->name);
343
344 if (dispc_mgr_is_enabled(channel)) {
345 dispc_mgr_go(channel);
346 omap_irq_register(dev, &omap_crtc->apply_irq);
347 } else {
348 struct omap_drm_private *priv = dev->dev_private;
349 queue_work(priv->wq, &omap_crtc->apply_work);
350 }
351 }
352
353out:
354 dispc_runtime_put();
355 drm_modeset_unlock(&crtc->mutex);
356}
357
358int omap_crtc_apply(struct drm_crtc *crtc,
359 struct omap_drm_apply *apply)
360{
361 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
362
363 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
364
365 /* no need to queue it again if it is already queued: */
366 if (apply->queued)
367 return 0;
368
369 apply->queued = true;
370 list_add_tail(&apply->queued_node, &omap_crtc->queued_applies);
371
372 /*
373 * If there are no currently pending updates, then go ahead and
374 * kick the worker immediately, otherwise it will run again when
375 * the current update finishes.
376 */
377 if (list_empty(&omap_crtc->pending_applies)) {
378 struct omap_drm_private *priv = crtc->dev->dev_private;
379 queue_work(priv->wq, &omap_crtc->apply_work);
380 }
381
382 return 0;
383}
384
385static void omap_crtc_pre_apply(struct omap_drm_apply *apply)
386{
387 struct omap_crtc *omap_crtc =
388 container_of(apply, struct omap_crtc, apply);
389 struct drm_crtc *crtc = &omap_crtc->base;
390 struct omap_drm_private *priv = crtc->dev->dev_private;
391 struct drm_encoder *encoder = NULL;
392 unsigned int i;
393
394 DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
395
396 for (i = 0; i < priv->num_encoders; i++) {
397 if (priv->encoders[i]->crtc == crtc) {
398 encoder = priv->encoders[i];
399 break;
400 }
401 }
402
403 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
404 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
405
406 omap_crtc->current_encoder = encoder;
407
408 if (!omap_crtc->enabled) {
409 if (encoder)
410 omap_encoder_set_enabled(encoder, false);
411 } else {
412 if (encoder) {
413 omap_encoder_set_enabled(encoder, false);
414 omap_encoder_update(encoder, omap_crtc->mgr,
415 &omap_crtc->timings);
416 omap_encoder_set_enabled(encoder, true);
417 }
418 }
419}
420
421static void omap_crtc_post_apply(struct omap_drm_apply *apply)
422{
423 /* nothing needed for post-apply */
424}
425
426void omap_crtc_flush(struct drm_crtc *crtc)
427{
428 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
429 int loops = 0;
430
431 while (!list_empty(&omap_crtc->pending_applies) ||
432 !list_empty(&omap_crtc->queued_applies) ||
433 omap_crtc->event || omap_crtc->old_fb) {
434
435 if (++loops > 10) {
436 dev_err(crtc->dev->dev,
437 "omap_crtc_flush() timeout\n");
438 break;
439 }
440
441 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
442 }
443}
444
445/* -----------------------------------------------------------------------------
446 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600447 */
448
Rob Clarkcd5351f2011-11-12 12:09:40 -0600449static void omap_crtc_destroy(struct drm_crtc *crtc)
450{
451 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600452
453 DBG("%s", omap_crtc->name);
454
455 WARN_ON(omap_crtc->apply_irq.registered);
456 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
457
Rob Clarkcd5351f2011-11-12 12:09:40 -0600458 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600459
Rob Clarkcd5351f2011-11-12 12:09:40 -0600460 kfree(omap_crtc);
461}
462
463static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
464{
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600465 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600466 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600467 bool enabled = (mode == DRM_MODE_DPMS_ON);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600468 int i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600469
Rob Clarkf5f94542012-12-04 13:59:12 -0600470 DBG("%s: %d", omap_crtc->name, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600471
Rob Clarkf5f94542012-12-04 13:59:12 -0600472 if (enabled != omap_crtc->enabled) {
473 omap_crtc->enabled = enabled;
Rob Clarkf5f94542012-12-04 13:59:12 -0600474 omap_crtc_apply(crtc, &omap_crtc->apply);
475
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200476 /* Enable/disable all planes associated with the CRTC. */
Rob Clarkf5f94542012-12-04 13:59:12 -0600477 for (i = 0; i < priv->num_planes; i++) {
478 struct drm_plane *plane = priv->planes[i];
479 if (plane->crtc == crtc)
Laurent Pinchart2debab92015-01-12 22:38:16 +0200480 WARN_ON(omap_plane_set_enable(plane, enabled));
Rob Clarkf5f94542012-12-04 13:59:12 -0600481 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600482 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600483}
484
485static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200486 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600487 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600488{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600489 return true;
490}
491
492static int omap_crtc_mode_set(struct drm_crtc *crtc,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600493 struct drm_display_mode *mode,
494 struct drm_display_mode *adjusted_mode,
495 int x, int y,
496 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600497{
498 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
499
Rob Clarkf5f94542012-12-04 13:59:12 -0600500 mode = adjusted_mode;
501
502 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
503 omap_crtc->name, mode->base.id, mode->name,
504 mode->vrefresh, mode->clock,
505 mode->hdisplay, mode->hsync_start,
506 mode->hsync_end, mode->htotal,
507 mode->vdisplay, mode->vsync_start,
508 mode->vsync_end, mode->vtotal,
509 mode->type, mode->flags);
510
511 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
Rob Clarkf5f94542012-12-04 13:59:12 -0600512
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200513 /*
514 * The primary plane CRTC can be reset if the plane is disabled directly
515 * through the universal plane API. Set it again here.
516 */
517 crtc->primary->crtc = crtc;
518
519 return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200520 0, 0, mode->hdisplay, mode->vdisplay,
521 x, y, mode->hdisplay, mode->vdisplay,
522 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600523}
524
525static void omap_crtc_prepare(struct drm_crtc *crtc)
526{
527 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600528 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600529 omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
530}
531
532static void omap_crtc_commit(struct drm_crtc *crtc)
533{
534 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600535 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600536 omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
537}
538
539static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600540 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600541{
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200542 struct drm_plane *plane = crtc->primary;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600543 struct drm_display_mode *mode = &crtc->mode;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600544
Matt Roperf4510a22014-04-01 15:22:40 -0700545 return omap_plane_mode_set(plane, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200546 0, 0, mode->hdisplay, mode->vdisplay,
547 x, y, mode->hdisplay, mode->vdisplay,
548 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600549}
550
Rob Clark72d0c332012-03-11 21:11:21 -0500551static void vblank_cb(void *arg)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600552{
553 struct drm_crtc *crtc = arg;
554 struct drm_device *dev = crtc->dev;
555 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600556 unsigned long flags;
557
Rob Clarkf5f94542012-12-04 13:59:12 -0600558 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600559
560 /* wakeup userspace */
Rob Clarkf5f94542012-12-04 13:59:12 -0600561 if (omap_crtc->event)
562 drm_send_vblank_event(dev, omap_crtc->pipe, omap_crtc->event);
Rob Clark7411f9c2012-03-11 21:11:22 -0500563
Rob Clarkf5f94542012-12-04 13:59:12 -0600564 omap_crtc->event = NULL;
565 omap_crtc->old_fb = NULL;
566
567 spin_unlock_irqrestore(&dev->event_lock, flags);
568}
569
570static void page_flip_worker(struct work_struct *work)
571{
572 struct omap_crtc *omap_crtc =
573 container_of(work, struct omap_crtc, page_flip_work);
574 struct drm_crtc *crtc = &omap_crtc->base;
Rob Clarkf5f94542012-12-04 13:59:12 -0600575 struct drm_display_mode *mode = &crtc->mode;
576 struct drm_gem_object *bo;
577
Rob Clark51fd3712013-11-19 12:10:12 -0500578 drm_modeset_lock(&crtc->mutex, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200579 omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200580 0, 0, mode->hdisplay, mode->vdisplay,
581 crtc->x, crtc->y, mode->hdisplay, mode->vdisplay,
582 vblank_cb, crtc);
Rob Clark51fd3712013-11-19 12:10:12 -0500583 drm_modeset_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600584
Matt Roperf4510a22014-04-01 15:22:40 -0700585 bo = omap_framebuffer_bo(crtc->primary->fb, 0);
Rob Clarkf5f94542012-12-04 13:59:12 -0600586 drm_gem_object_unreference_unlocked(bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600587}
588
Rob Clark72d0c332012-03-11 21:11:21 -0500589static void page_flip_cb(void *arg)
590{
591 struct drm_crtc *crtc = arg;
592 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600593 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clark72d0c332012-03-11 21:11:21 -0500594
Rob Clarkf5f94542012-12-04 13:59:12 -0600595 /* avoid assumptions about what ctxt we are called from: */
596 queue_work(priv->wq, &omap_crtc->page_flip_work);
Rob Clark72d0c332012-03-11 21:11:21 -0500597}
598
Rob Clarkcd5351f2011-11-12 12:09:40 -0600599static int omap_crtc_page_flip_locked(struct drm_crtc *crtc,
600 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700601 struct drm_pending_vblank_event *event,
602 uint32_t page_flip_flags)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600603{
604 struct drm_device *dev = crtc->dev;
605 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700606 struct drm_plane *primary = crtc->primary;
Rob Clark119c0812012-09-04 17:46:22 -0500607 struct drm_gem_object *bo;
Archit Taneja38e55972014-04-11 12:53:35 +0530608 unsigned long flags;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600609
Matt Roperf4510a22014-04-01 15:22:40 -0700610 DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
Rob Clarkf5f94542012-12-04 13:59:12 -0600611 fb->base.id, event);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600612
Archit Taneja38e55972014-04-11 12:53:35 +0530613 spin_lock_irqsave(&dev->event_lock, flags);
614
Rob Clarkf5f94542012-12-04 13:59:12 -0600615 if (omap_crtc->old_fb) {
Archit Taneja38e55972014-04-11 12:53:35 +0530616 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600617 dev_err(dev->dev, "already a pending flip\n");
Tomi Valkeinen549a7542014-09-03 19:25:50 +0000618 return -EBUSY;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600619 }
620
Rob Clarkcd5351f2011-11-12 12:09:40 -0600621 omap_crtc->event = event;
Archit Tanejabc905ace2014-04-11 12:53:34 +0530622 omap_crtc->old_fb = primary->fb = fb;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600623
Archit Taneja38e55972014-04-11 12:53:35 +0530624 spin_unlock_irqrestore(&dev->event_lock, flags);
625
Rob Clark119c0812012-09-04 17:46:22 -0500626 /*
627 * Hold a reference temporarily until the crtc is updated
628 * and takes the reference to the bo. This avoids it
629 * getting freed from under us:
630 */
631 bo = omap_framebuffer_bo(fb, 0);
632 drm_gem_object_reference(bo);
633
634 omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600635
636 return 0;
637}
638
Rob Clark3c810c62012-08-15 15:18:01 -0500639static int omap_crtc_set_property(struct drm_crtc *crtc,
640 struct drm_property *property, uint64_t val)
641{
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500642 struct omap_drm_private *priv = crtc->dev->dev_private;
643
644 if (property == priv->rotation_prop) {
645 crtc->invert_dimensions =
646 !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
647 }
648
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200649 return omap_plane_set_property(crtc->primary, property, val);
Rob Clark3c810c62012-08-15 15:18:01 -0500650}
651
Rob Clarkcd5351f2011-11-12 12:09:40 -0600652static const struct drm_crtc_funcs omap_crtc_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600653 .set_config = drm_crtc_helper_set_config,
654 .destroy = omap_crtc_destroy,
655 .page_flip = omap_crtc_page_flip_locked,
Rob Clark3c810c62012-08-15 15:18:01 -0500656 .set_property = omap_crtc_set_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600657};
658
659static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
660 .dpms = omap_crtc_dpms,
661 .mode_fixup = omap_crtc_mode_fixup,
662 .mode_set = omap_crtc_mode_set,
663 .prepare = omap_crtc_prepare,
664 .commit = omap_crtc_commit,
665 .mode_set_base = omap_crtc_mode_set_base,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600666};
667
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200668/* -----------------------------------------------------------------------------
669 * Init and Cleanup
670 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300671
Rob Clarkf5f94542012-12-04 13:59:12 -0600672static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200673 [OMAP_DSS_CHANNEL_LCD] = "lcd",
674 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
675 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
676 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600677};
678
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300679void omap_crtc_pre_init(void)
680{
681 dss_install_mgr_ops(&mgr_ops);
682}
683
Archit Taneja3a01ab22014-01-02 14:49:51 +0530684void omap_crtc_pre_uninit(void)
685{
686 dss_uninstall_mgr_ops();
687}
688
Rob Clarkcd5351f2011-11-12 12:09:40 -0600689/* initialize crtc */
690struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600691 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600692{
693 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600694 struct omap_crtc *omap_crtc;
695 struct omap_overlay_manager_info *info;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200696 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600697
Rob Clarkf5f94542012-12-04 13:59:12 -0600698 DBG("%s", channel_names[channel]);
699
700 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800701 if (!omap_crtc)
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200702 return NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600703
Rob Clarkcd5351f2011-11-12 12:09:40 -0600704 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600705
Rob Clarkf5f94542012-12-04 13:59:12 -0600706 INIT_WORK(&omap_crtc->page_flip_work, page_flip_worker);
707 INIT_WORK(&omap_crtc->apply_work, apply_worker);
708
709 INIT_LIST_HEAD(&omap_crtc->pending_applies);
710 INIT_LIST_HEAD(&omap_crtc->queued_applies);
711
712 omap_crtc->apply.pre_apply = omap_crtc_pre_apply;
713 omap_crtc->apply.post_apply = omap_crtc_post_apply;
714
Archit Taneja0d8f3712013-03-26 19:15:19 +0530715 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530716 omap_crtc->name = channel_names[channel];
717 omap_crtc->pipe = id;
718
719 omap_crtc->apply_irq.irqmask = pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600720 omap_crtc->apply_irq.irq = omap_crtc_apply_irq;
721
722 omap_crtc->error_irq.irqmask =
723 dispc_mgr_get_sync_lost_irq(channel);
724 omap_crtc->error_irq.irq = omap_crtc_error_irq;
725 omap_irq_register(dev, &omap_crtc->error_irq);
726
Rob Clarkf5f94542012-12-04 13:59:12 -0600727 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300728 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600729
730 /* TODO: fix hard-coded setup.. add properties! */
731 info = &omap_crtc->info;
732 info->default_color = 0x00000000;
733 info->trans_key = 0x00000000;
734 info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
735 info->trans_enabled = false;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600736
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200737 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
738 &omap_crtc_funcs);
739 if (ret < 0) {
740 kfree(omap_crtc);
741 return NULL;
742 }
743
Rob Clarkcd5351f2011-11-12 12:09:40 -0600744 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
745
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200746 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500747
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300748 omap_crtcs[channel] = omap_crtc;
749
Rob Clarkcd5351f2011-11-12 12:09:40 -0600750 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600751}