blob: c5c21776131a8d919da3bff238cf8baf4621a500 [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
158 /*
159 * Digit output produces some sync lost interrupts during the first
160 * frame when enabling, so we need to ignore those.
161 */
Tomi Valkeinena36af732015-02-26 15:20:24 +0200162 omap_crtc->ignore_digit_sync_lost = true;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200163
164 framedone_irq = dispc_mgr_get_framedone_irq(channel);
165 vsync_irq = dispc_mgr_get_vsync_irq(channel);
166
167 if (enable) {
168 wait = omap_irq_wait_init(dev, vsync_irq, 1);
169 } else {
170 /*
171 * When we disable the digit output, we need to wait for
172 * FRAMEDONE to know that DISPC has finished with the output.
173 *
174 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
175 * that case we need to use vsync interrupt, and wait for both
176 * even and odd frames.
177 */
178
179 if (framedone_irq)
180 wait = omap_irq_wait_init(dev, framedone_irq, 1);
181 else
182 wait = omap_irq_wait_init(dev, vsync_irq, 2);
183 }
184
185 dispc_mgr_enable(channel, enable);
186
187 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
188 if (ret) {
189 dev_err(dev->dev, "%s: timeout waiting for %s\n",
190 omap_crtc->name, enable ? "enable" : "disable");
191 }
192
Tomi Valkeinena36af732015-02-26 15:20:24 +0200193 omap_crtc->ignore_digit_sync_lost = false;
194 /* make sure the irq handler sees the value above */
195 mb();
Laurent Pinchart8472b572015-01-15 00:45:17 +0200196}
197
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300198
Rob Clarkf5f94542012-12-04 13:59:12 -0600199static int omap_crtc_enable(struct omap_overlay_manager *mgr)
200{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300201 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
202
203 dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
204 dispc_mgr_set_timings(omap_crtc->channel,
205 &omap_crtc->timings);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200206 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300207
Rob Clarkf5f94542012-12-04 13:59:12 -0600208 return 0;
209}
210
211static void omap_crtc_disable(struct omap_overlay_manager *mgr)
212{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300213 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
214
Laurent Pinchart8472b572015-01-15 00:45:17 +0200215 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600216}
217
218static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
219 const struct omap_video_timings *timings)
220{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300221 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600222 DBG("%s", omap_crtc->name);
223 omap_crtc->timings = *timings;
Rob Clarkf5f94542012-12-04 13:59:12 -0600224}
225
226static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
227 const struct dss_lcd_mgr_config *config)
228{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300229 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600230 DBG("%s", omap_crtc->name);
231 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
232}
233
234static int omap_crtc_register_framedone_handler(
235 struct omap_overlay_manager *mgr,
236 void (*handler)(void *), void *data)
237{
238 return 0;
239}
240
241static void omap_crtc_unregister_framedone_handler(
242 struct omap_overlay_manager *mgr,
243 void (*handler)(void *), void *data)
244{
245}
246
247static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200248 .connect = omap_crtc_connect,
249 .disconnect = omap_crtc_disconnect,
250 .start_update = omap_crtc_start_update,
251 .enable = omap_crtc_enable,
252 .disable = omap_crtc_disable,
253 .set_timings = omap_crtc_set_timings,
254 .set_lcd_config = omap_crtc_set_lcd_config,
255 .register_framedone_handler = omap_crtc_register_framedone_handler,
256 .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
Rob Clarkf5f94542012-12-04 13:59:12 -0600257};
258
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200259/* -----------------------------------------------------------------------------
260 * Apply Logic
261 */
262
263static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
264{
265 struct omap_crtc *omap_crtc =
266 container_of(irq, struct omap_crtc, error_irq);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200267
268 if (omap_crtc->ignore_digit_sync_lost) {
269 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
270 if (!irqstatus)
271 return;
272 }
273
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200274 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200275}
276
277static void omap_crtc_apply_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
278{
279 struct omap_crtc *omap_crtc =
280 container_of(irq, struct omap_crtc, apply_irq);
281 struct drm_crtc *crtc = &omap_crtc->base;
282
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200283 if (!dispc_mgr_go_busy(omap_crtc->channel)) {
284 struct omap_drm_private *priv =
285 crtc->dev->dev_private;
286 DBG("%s: apply done", omap_crtc->name);
287 __omap_irq_unregister(crtc->dev, &omap_crtc->apply_irq);
288 queue_work(priv->wq, &omap_crtc->apply_work);
289 }
290}
291
292static void apply_worker(struct work_struct *work)
293{
294 struct omap_crtc *omap_crtc =
295 container_of(work, struct omap_crtc, apply_work);
296 struct drm_crtc *crtc = &omap_crtc->base;
297 struct drm_device *dev = crtc->dev;
298 struct omap_drm_apply *apply, *n;
299 bool need_apply;
300
301 /*
302 * Synchronize everything on mode_config.mutex, to keep
303 * the callbacks and list modification all serialized
304 * with respect to modesetting ioctls from userspace.
305 */
306 drm_modeset_lock(&crtc->mutex, NULL);
307 dispc_runtime_get();
308
309 /*
310 * If we are still pending a previous update, wait.. when the
311 * pending update completes, we get kicked again.
312 */
313 if (omap_crtc->apply_irq.registered)
314 goto out;
315
316 /* finish up previous apply's: */
317 list_for_each_entry_safe(apply, n,
318 &omap_crtc->pending_applies, pending_node) {
319 apply->post_apply(apply);
320 list_del(&apply->pending_node);
321 }
322
323 need_apply = !list_empty(&omap_crtc->queued_applies);
324
325 /* then handle the next round of of queued apply's: */
326 list_for_each_entry_safe(apply, n,
327 &omap_crtc->queued_applies, queued_node) {
328 apply->pre_apply(apply);
329 list_del(&apply->queued_node);
330 apply->queued = false;
331 list_add_tail(&apply->pending_node,
332 &omap_crtc->pending_applies);
333 }
334
335 if (need_apply) {
336 enum omap_channel channel = omap_crtc->channel;
337
338 DBG("%s: GO", omap_crtc->name);
339
340 if (dispc_mgr_is_enabled(channel)) {
341 dispc_mgr_go(channel);
342 omap_irq_register(dev, &omap_crtc->apply_irq);
343 } else {
344 struct omap_drm_private *priv = dev->dev_private;
345 queue_work(priv->wq, &omap_crtc->apply_work);
346 }
347 }
348
349out:
350 dispc_runtime_put();
351 drm_modeset_unlock(&crtc->mutex);
352}
353
354int omap_crtc_apply(struct drm_crtc *crtc,
355 struct omap_drm_apply *apply)
356{
357 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
358
359 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
360
361 /* no need to queue it again if it is already queued: */
362 if (apply->queued)
363 return 0;
364
365 apply->queued = true;
366 list_add_tail(&apply->queued_node, &omap_crtc->queued_applies);
367
368 /*
369 * If there are no currently pending updates, then go ahead and
370 * kick the worker immediately, otherwise it will run again when
371 * the current update finishes.
372 */
373 if (list_empty(&omap_crtc->pending_applies)) {
374 struct omap_drm_private *priv = crtc->dev->dev_private;
375 queue_work(priv->wq, &omap_crtc->apply_work);
376 }
377
378 return 0;
379}
380
381static void omap_crtc_pre_apply(struct omap_drm_apply *apply)
382{
383 struct omap_crtc *omap_crtc =
384 container_of(apply, struct omap_crtc, apply);
385 struct drm_crtc *crtc = &omap_crtc->base;
386 struct omap_drm_private *priv = crtc->dev->dev_private;
387 struct drm_encoder *encoder = NULL;
388 unsigned int i;
389
390 DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
391
392 for (i = 0; i < priv->num_encoders; i++) {
393 if (priv->encoders[i]->crtc == crtc) {
394 encoder = priv->encoders[i];
395 break;
396 }
397 }
398
399 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
400 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
401
402 omap_crtc->current_encoder = encoder;
403
404 if (!omap_crtc->enabled) {
405 if (encoder)
406 omap_encoder_set_enabled(encoder, false);
407 } else {
408 if (encoder) {
409 omap_encoder_set_enabled(encoder, false);
410 omap_encoder_update(encoder, omap_crtc->mgr,
411 &omap_crtc->timings);
412 omap_encoder_set_enabled(encoder, true);
413 }
414 }
415}
416
417static void omap_crtc_post_apply(struct omap_drm_apply *apply)
418{
419 /* nothing needed for post-apply */
420}
421
422void omap_crtc_flush(struct drm_crtc *crtc)
423{
424 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
425 int loops = 0;
426
427 while (!list_empty(&omap_crtc->pending_applies) ||
428 !list_empty(&omap_crtc->queued_applies) ||
429 omap_crtc->event || omap_crtc->old_fb) {
430
431 if (++loops > 10) {
432 dev_err(crtc->dev->dev,
433 "omap_crtc_flush() timeout\n");
434 break;
435 }
436
437 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
438 }
439}
440
441/* -----------------------------------------------------------------------------
442 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600443 */
444
Rob Clarkcd5351f2011-11-12 12:09:40 -0600445static void omap_crtc_destroy(struct drm_crtc *crtc)
446{
447 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600448
449 DBG("%s", omap_crtc->name);
450
451 WARN_ON(omap_crtc->apply_irq.registered);
452 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
453
Rob Clarkcd5351f2011-11-12 12:09:40 -0600454 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600455
Rob Clarkcd5351f2011-11-12 12:09:40 -0600456 kfree(omap_crtc);
457}
458
459static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
460{
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600461 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600462 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600463 bool enabled = (mode == DRM_MODE_DPMS_ON);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600464 int i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600465
Rob Clarkf5f94542012-12-04 13:59:12 -0600466 DBG("%s: %d", omap_crtc->name, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600467
Rob Clarkf5f94542012-12-04 13:59:12 -0600468 if (enabled != omap_crtc->enabled) {
469 omap_crtc->enabled = enabled;
Rob Clarkf5f94542012-12-04 13:59:12 -0600470 omap_crtc_apply(crtc, &omap_crtc->apply);
471
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200472 /* Enable/disable all planes associated with the CRTC. */
Rob Clarkf5f94542012-12-04 13:59:12 -0600473 for (i = 0; i < priv->num_planes; i++) {
474 struct drm_plane *plane = priv->planes[i];
475 if (plane->crtc == crtc)
Laurent Pinchart2debab92015-01-12 22:38:16 +0200476 WARN_ON(omap_plane_set_enable(plane, enabled));
Rob Clarkf5f94542012-12-04 13:59:12 -0600477 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600478 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600479}
480
481static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200482 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600483 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600484{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600485 return true;
486}
487
488static int omap_crtc_mode_set(struct drm_crtc *crtc,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600489 struct drm_display_mode *mode,
490 struct drm_display_mode *adjusted_mode,
491 int x, int y,
492 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600493{
494 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
495
Rob Clarkf5f94542012-12-04 13:59:12 -0600496 mode = adjusted_mode;
497
498 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
499 omap_crtc->name, mode->base.id, mode->name,
500 mode->vrefresh, mode->clock,
501 mode->hdisplay, mode->hsync_start,
502 mode->hsync_end, mode->htotal,
503 mode->vdisplay, mode->vsync_start,
504 mode->vsync_end, mode->vtotal,
505 mode->type, mode->flags);
506
507 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
Rob Clarkf5f94542012-12-04 13:59:12 -0600508
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200509 /*
510 * The primary plane CRTC can be reset if the plane is disabled directly
511 * through the universal plane API. Set it again here.
512 */
513 crtc->primary->crtc = crtc;
514
515 return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200516 0, 0, mode->hdisplay, mode->vdisplay,
517 x, y, mode->hdisplay, mode->vdisplay,
518 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600519}
520
521static void omap_crtc_prepare(struct drm_crtc *crtc)
522{
523 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600524 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600525 omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
526}
527
528static void omap_crtc_commit(struct drm_crtc *crtc)
529{
530 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600531 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600532 omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
533}
534
535static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600536 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600537{
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200538 struct drm_plane *plane = crtc->primary;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600539 struct drm_display_mode *mode = &crtc->mode;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600540
Matt Roperf4510a22014-04-01 15:22:40 -0700541 return omap_plane_mode_set(plane, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200542 0, 0, mode->hdisplay, mode->vdisplay,
543 x, y, mode->hdisplay, mode->vdisplay,
544 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600545}
546
Rob Clark72d0c332012-03-11 21:11:21 -0500547static void vblank_cb(void *arg)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600548{
549 struct drm_crtc *crtc = arg;
550 struct drm_device *dev = crtc->dev;
551 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600552 unsigned long flags;
553
Rob Clarkf5f94542012-12-04 13:59:12 -0600554 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600555
556 /* wakeup userspace */
Rob Clarkf5f94542012-12-04 13:59:12 -0600557 if (omap_crtc->event)
558 drm_send_vblank_event(dev, omap_crtc->pipe, omap_crtc->event);
Rob Clark7411f9c2012-03-11 21:11:22 -0500559
Rob Clarkf5f94542012-12-04 13:59:12 -0600560 omap_crtc->event = NULL;
561 omap_crtc->old_fb = NULL;
562
563 spin_unlock_irqrestore(&dev->event_lock, flags);
564}
565
566static void page_flip_worker(struct work_struct *work)
567{
568 struct omap_crtc *omap_crtc =
569 container_of(work, struct omap_crtc, page_flip_work);
570 struct drm_crtc *crtc = &omap_crtc->base;
Rob Clarkf5f94542012-12-04 13:59:12 -0600571 struct drm_display_mode *mode = &crtc->mode;
572 struct drm_gem_object *bo;
573
Rob Clark51fd3712013-11-19 12:10:12 -0500574 drm_modeset_lock(&crtc->mutex, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200575 omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200576 0, 0, mode->hdisplay, mode->vdisplay,
577 crtc->x, crtc->y, mode->hdisplay, mode->vdisplay,
578 vblank_cb, crtc);
Rob Clark51fd3712013-11-19 12:10:12 -0500579 drm_modeset_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600580
Matt Roperf4510a22014-04-01 15:22:40 -0700581 bo = omap_framebuffer_bo(crtc->primary->fb, 0);
Rob Clarkf5f94542012-12-04 13:59:12 -0600582 drm_gem_object_unreference_unlocked(bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600583}
584
Rob Clark72d0c332012-03-11 21:11:21 -0500585static void page_flip_cb(void *arg)
586{
587 struct drm_crtc *crtc = arg;
588 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600589 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clark72d0c332012-03-11 21:11:21 -0500590
Rob Clarkf5f94542012-12-04 13:59:12 -0600591 /* avoid assumptions about what ctxt we are called from: */
592 queue_work(priv->wq, &omap_crtc->page_flip_work);
Rob Clark72d0c332012-03-11 21:11:21 -0500593}
594
Rob Clarkcd5351f2011-11-12 12:09:40 -0600595static int omap_crtc_page_flip_locked(struct drm_crtc *crtc,
596 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700597 struct drm_pending_vblank_event *event,
598 uint32_t page_flip_flags)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600599{
600 struct drm_device *dev = crtc->dev;
601 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700602 struct drm_plane *primary = crtc->primary;
Rob Clark119c0812012-09-04 17:46:22 -0500603 struct drm_gem_object *bo;
Archit Taneja38e55972014-04-11 12:53:35 +0530604 unsigned long flags;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600605
Matt Roperf4510a22014-04-01 15:22:40 -0700606 DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
Rob Clarkf5f94542012-12-04 13:59:12 -0600607 fb->base.id, event);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600608
Archit Taneja38e55972014-04-11 12:53:35 +0530609 spin_lock_irqsave(&dev->event_lock, flags);
610
Rob Clarkf5f94542012-12-04 13:59:12 -0600611 if (omap_crtc->old_fb) {
Archit Taneja38e55972014-04-11 12:53:35 +0530612 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600613 dev_err(dev->dev, "already a pending flip\n");
Tomi Valkeinen549a7542014-09-03 19:25:50 +0000614 return -EBUSY;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600615 }
616
Rob Clarkcd5351f2011-11-12 12:09:40 -0600617 omap_crtc->event = event;
Archit Tanejabc905ace2014-04-11 12:53:34 +0530618 omap_crtc->old_fb = primary->fb = fb;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600619
Archit Taneja38e55972014-04-11 12:53:35 +0530620 spin_unlock_irqrestore(&dev->event_lock, flags);
621
Rob Clark119c0812012-09-04 17:46:22 -0500622 /*
623 * Hold a reference temporarily until the crtc is updated
624 * and takes the reference to the bo. This avoids it
625 * getting freed from under us:
626 */
627 bo = omap_framebuffer_bo(fb, 0);
628 drm_gem_object_reference(bo);
629
630 omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600631
632 return 0;
633}
634
Rob Clark3c810c62012-08-15 15:18:01 -0500635static int omap_crtc_set_property(struct drm_crtc *crtc,
636 struct drm_property *property, uint64_t val)
637{
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500638 struct omap_drm_private *priv = crtc->dev->dev_private;
639
640 if (property == priv->rotation_prop) {
641 crtc->invert_dimensions =
642 !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
643 }
644
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200645 return omap_plane_set_property(crtc->primary, property, val);
Rob Clark3c810c62012-08-15 15:18:01 -0500646}
647
Rob Clarkcd5351f2011-11-12 12:09:40 -0600648static const struct drm_crtc_funcs omap_crtc_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600649 .set_config = drm_crtc_helper_set_config,
650 .destroy = omap_crtc_destroy,
651 .page_flip = omap_crtc_page_flip_locked,
Rob Clark3c810c62012-08-15 15:18:01 -0500652 .set_property = omap_crtc_set_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600653};
654
655static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
656 .dpms = omap_crtc_dpms,
657 .mode_fixup = omap_crtc_mode_fixup,
658 .mode_set = omap_crtc_mode_set,
659 .prepare = omap_crtc_prepare,
660 .commit = omap_crtc_commit,
661 .mode_set_base = omap_crtc_mode_set_base,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600662};
663
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200664/* -----------------------------------------------------------------------------
665 * Init and Cleanup
666 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300667
Rob Clarkf5f94542012-12-04 13:59:12 -0600668static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200669 [OMAP_DSS_CHANNEL_LCD] = "lcd",
670 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
671 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
672 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600673};
674
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300675void omap_crtc_pre_init(void)
676{
677 dss_install_mgr_ops(&mgr_ops);
678}
679
Archit Taneja3a01ab22014-01-02 14:49:51 +0530680void omap_crtc_pre_uninit(void)
681{
682 dss_uninstall_mgr_ops();
683}
684
Rob Clarkcd5351f2011-11-12 12:09:40 -0600685/* initialize crtc */
686struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600687 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600688{
689 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600690 struct omap_crtc *omap_crtc;
691 struct omap_overlay_manager_info *info;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200692 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600693
Rob Clarkf5f94542012-12-04 13:59:12 -0600694 DBG("%s", channel_names[channel]);
695
696 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800697 if (!omap_crtc)
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200698 return NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600699
Rob Clarkcd5351f2011-11-12 12:09:40 -0600700 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600701
Rob Clarkf5f94542012-12-04 13:59:12 -0600702 INIT_WORK(&omap_crtc->page_flip_work, page_flip_worker);
703 INIT_WORK(&omap_crtc->apply_work, apply_worker);
704
705 INIT_LIST_HEAD(&omap_crtc->pending_applies);
706 INIT_LIST_HEAD(&omap_crtc->queued_applies);
707
708 omap_crtc->apply.pre_apply = omap_crtc_pre_apply;
709 omap_crtc->apply.post_apply = omap_crtc_post_apply;
710
Archit Taneja0d8f3712013-03-26 19:15:19 +0530711 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530712 omap_crtc->name = channel_names[channel];
713 omap_crtc->pipe = id;
714
715 omap_crtc->apply_irq.irqmask = pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600716 omap_crtc->apply_irq.irq = omap_crtc_apply_irq;
717
718 omap_crtc->error_irq.irqmask =
719 dispc_mgr_get_sync_lost_irq(channel);
720 omap_crtc->error_irq.irq = omap_crtc_error_irq;
721 omap_irq_register(dev, &omap_crtc->error_irq);
722
Rob Clarkf5f94542012-12-04 13:59:12 -0600723 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300724 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600725
726 /* TODO: fix hard-coded setup.. add properties! */
727 info = &omap_crtc->info;
728 info->default_color = 0x00000000;
729 info->trans_key = 0x00000000;
730 info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
731 info->trans_enabled = false;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600732
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200733 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
734 &omap_crtc_funcs);
735 if (ret < 0) {
736 kfree(omap_crtc);
737 return NULL;
738 }
739
Rob Clarkcd5351f2011-11-12 12:09:40 -0600740 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
741
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200742 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500743
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300744 omap_crtcs[channel] = omap_crtc;
745
Rob Clarkcd5351f2011-11-12 12:09:40 -0600746 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600747}