blob: a60f4e49b55f0d0c25ec9b4570b927435b3dfd99 [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
Laurent Pincharta42133a2015-01-17 19:09:26 +020020#include <linux/completion.h>
21
Rob Clarkcd5351f2011-11-12 12:09:40 -060022#include "omap_drv.h"
23
Andy Grossb9ed9f02012-10-16 00:17:40 -050024#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010025#include <drm/drm_plane_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060026#include "drm_crtc.h"
27#include "drm_crtc_helper.h"
28
29#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
30
31struct omap_crtc {
32 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060033
Rob Clarkbb5c2d92012-01-16 12:51:16 -060034 const char *name;
Rob Clarkf5f94542012-12-04 13:59:12 -060035 int pipe;
36 enum omap_channel channel;
37 struct omap_overlay_manager_info info;
Tomi Valkeinenc7aef122014-04-03 16:30:03 +030038 struct drm_encoder *current_encoder;
Rob Clarkf5f94542012-12-04 13:59:12 -060039
40 /*
41 * Temporary: eventually this will go away, but it is needed
42 * for now to keep the output's happy. (They only need
43 * mgr->id.) Eventually this will be replaced w/ something
44 * more common-panel-framework-y
45 */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +030046 struct omap_overlay_manager *mgr;
Rob Clarkf5f94542012-12-04 13:59:12 -060047
48 struct omap_video_timings timings;
49 bool enabled;
Rob Clarkf5f94542012-12-04 13:59:12 -060050
Laurent Pincharta42133a2015-01-17 19:09:26 +020051 struct omap_drm_irq vblank_irq;
Rob Clarkf5f94542012-12-04 13:59:12 -060052 struct omap_drm_irq error_irq;
53
Laurent Pincharta42133a2015-01-17 19:09:26 +020054 /* list of framebuffers to unpin */
55 struct list_head pending_unpins;
Rob Clarkcd5351f2011-11-12 12:09:40 -060056
Laurent Pinchart42fb61c2015-01-26 02:58:51 +020057 /*
58 * The flip_pending flag indicates if a page flip has been queued and
59 * hasn't completed yet. The flip event, if any, is stored in
60 * flip_event.
61 *
62 * The flip_work work queue handles page flip requests without caring
63 * about what context the GEM async callback is called from. Possibly we
64 * should just make omap_gem always call the cb from the worker so we
65 * don't have to care about this.
66 */
67 bool flip_pending;
68 struct drm_pending_vblank_event *flip_event;
69 struct work_struct flip_work;
Rob Clarkf5f94542012-12-04 13:59:12 -060070
Laurent Pincharta42133a2015-01-17 19:09:26 +020071 struct completion completion;
72
Tomi Valkeinena36af732015-02-26 15:20:24 +020073 bool ignore_digit_sync_lost;
Rob Clarkcd5351f2011-11-12 12:09:40 -060074};
75
Laurent Pincharta42133a2015-01-17 19:09:26 +020076struct omap_framebuffer_unpin {
77 struct list_head list;
78 struct drm_framebuffer *fb;
79};
80
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020081/* -----------------------------------------------------------------------------
82 * Helper Functions
83 */
84
Archit Taneja0d8f3712013-03-26 19:15:19 +053085uint32_t pipe2vbl(struct drm_crtc *crtc)
86{
87 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
88
89 return dispc_mgr_get_vsync_irq(omap_crtc->channel);
90}
91
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020092const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
93{
94 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
95 return &omap_crtc->timings;
96}
97
98enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
99{
100 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
101 return omap_crtc->channel;
102}
103
104/* -----------------------------------------------------------------------------
105 * DSS Manager Functions
106 */
107
Rob Clarkf5f94542012-12-04 13:59:12 -0600108/*
109 * Manager-ops, callbacks from output when they need to configure
110 * the upstream part of the video pipe.
111 *
112 * Most of these we can ignore until we add support for command-mode
113 * panels.. for video-mode the crtc-helpers already do an adequate
114 * job of sequencing the setup of the video pipe in the proper order
115 */
116
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300117/* ovl-mgr-id -> crtc */
118static struct omap_crtc *omap_crtcs[8];
119
Rob Clarkf5f94542012-12-04 13:59:12 -0600120/* we can probably ignore these until we support command-mode panels: */
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300121static int omap_crtc_connect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300122 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300123{
124 if (mgr->output)
125 return -EINVAL;
126
127 if ((mgr->supported_outputs & dst->id) == 0)
128 return -EINVAL;
129
130 dst->manager = mgr;
131 mgr->output = dst;
132
133 return 0;
134}
135
136static void omap_crtc_disconnect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300137 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300138{
139 mgr->output->manager = NULL;
140 mgr->output = NULL;
141}
142
Rob Clarkf5f94542012-12-04 13:59:12 -0600143static void omap_crtc_start_update(struct omap_overlay_manager *mgr)
144{
145}
146
Laurent Pincharta42133a2015-01-17 19:09:26 +0200147/* Called only from omap_crtc_setup and suspend/resume handlers. */
Laurent Pinchart8472b572015-01-15 00:45:17 +0200148static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
149{
150 struct drm_device *dev = crtc->dev;
151 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
152 enum omap_channel channel = omap_crtc->channel;
153 struct omap_irq_wait *wait;
154 u32 framedone_irq, vsync_irq;
155 int ret;
156
157 if (dispc_mgr_is_enabled(channel) == enable)
158 return;
159
Tomi Valkeinenef422282015-02-26 15:20:25 +0200160 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
161 /*
162 * Digit output produces some sync lost interrupts during the
163 * first frame when enabling, so we need to ignore those.
164 */
165 omap_crtc->ignore_digit_sync_lost = true;
166 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200167
168 framedone_irq = dispc_mgr_get_framedone_irq(channel);
169 vsync_irq = dispc_mgr_get_vsync_irq(channel);
170
171 if (enable) {
172 wait = omap_irq_wait_init(dev, vsync_irq, 1);
173 } else {
174 /*
175 * When we disable the digit output, we need to wait for
176 * FRAMEDONE to know that DISPC has finished with the output.
177 *
178 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
179 * that case we need to use vsync interrupt, and wait for both
180 * even and odd frames.
181 */
182
183 if (framedone_irq)
184 wait = omap_irq_wait_init(dev, framedone_irq, 1);
185 else
186 wait = omap_irq_wait_init(dev, vsync_irq, 2);
187 }
188
189 dispc_mgr_enable(channel, enable);
190
191 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
192 if (ret) {
193 dev_err(dev->dev, "%s: timeout waiting for %s\n",
194 omap_crtc->name, enable ? "enable" : "disable");
195 }
196
Tomi Valkeinenef422282015-02-26 15:20:25 +0200197 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
198 omap_crtc->ignore_digit_sync_lost = false;
199 /* make sure the irq handler sees the value above */
200 mb();
201 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200202}
203
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300204
Rob Clarkf5f94542012-12-04 13:59:12 -0600205static int omap_crtc_enable(struct omap_overlay_manager *mgr)
206{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300207 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
208
209 dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
210 dispc_mgr_set_timings(omap_crtc->channel,
211 &omap_crtc->timings);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200212 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300213
Rob Clarkf5f94542012-12-04 13:59:12 -0600214 return 0;
215}
216
217static void omap_crtc_disable(struct omap_overlay_manager *mgr)
218{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300219 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
220
Laurent Pinchart8472b572015-01-15 00:45:17 +0200221 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600222}
223
224static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
225 const struct omap_video_timings *timings)
226{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300227 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600228 DBG("%s", omap_crtc->name);
229 omap_crtc->timings = *timings;
Rob Clarkf5f94542012-12-04 13:59:12 -0600230}
231
232static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
233 const struct dss_lcd_mgr_config *config)
234{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300235 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600236 DBG("%s", omap_crtc->name);
237 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
238}
239
240static int omap_crtc_register_framedone_handler(
241 struct omap_overlay_manager *mgr,
242 void (*handler)(void *), void *data)
243{
244 return 0;
245}
246
247static void omap_crtc_unregister_framedone_handler(
248 struct omap_overlay_manager *mgr,
249 void (*handler)(void *), void *data)
250{
251}
252
253static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200254 .connect = omap_crtc_connect,
255 .disconnect = omap_crtc_disconnect,
256 .start_update = omap_crtc_start_update,
257 .enable = omap_crtc_enable,
258 .disable = omap_crtc_disable,
259 .set_timings = omap_crtc_set_timings,
260 .set_lcd_config = omap_crtc_set_lcd_config,
261 .register_framedone_handler = omap_crtc_register_framedone_handler,
262 .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
Rob Clarkf5f94542012-12-04 13:59:12 -0600263};
264
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200265/* -----------------------------------------------------------------------------
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200266 * Setup, Flush and Page Flip
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200267 */
268
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200269void omap_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
270{
271 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
272 struct drm_device *dev = crtc->dev;
273 unsigned long flags;
274
275 spin_lock_irqsave(&dev->event_lock, flags);
276
277 /* Only complete events queued for our file handle. */
278 if (omap_crtc->flip_event &&
279 file == omap_crtc->flip_event->base.file_priv) {
280 drm_send_vblank_event(dev, omap_crtc->pipe,
281 omap_crtc->flip_event);
282 omap_crtc->flip_event = NULL;
283 }
284
285 spin_unlock_irqrestore(&dev->event_lock, flags);
286}
287
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200288static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
289{
290 struct omap_crtc *omap_crtc =
291 container_of(irq, struct omap_crtc, error_irq);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200292
293 if (omap_crtc->ignore_digit_sync_lost) {
294 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
295 if (!irqstatus)
296 return;
297 }
298
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200299 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200300}
301
Laurent Pincharta42133a2015-01-17 19:09:26 +0200302static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200303{
304 struct omap_crtc *omap_crtc =
Laurent Pincharta42133a2015-01-17 19:09:26 +0200305 container_of(irq, struct omap_crtc, vblank_irq);
306 struct drm_device *dev = omap_crtc->base.dev;
307 unsigned long flags;
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200308
Laurent Pincharta42133a2015-01-17 19:09:26 +0200309 if (dispc_mgr_go_busy(omap_crtc->channel))
310 return;
311
312 DBG("%s: apply done", omap_crtc->name);
313 __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
314
315 spin_lock_irqsave(&dev->event_lock, flags);
316
317 /* wakeup userspace */
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200318 if (omap_crtc->flip_event)
319 drm_send_vblank_event(dev, omap_crtc->pipe,
320 omap_crtc->flip_event);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200321
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200322 omap_crtc->flip_event = NULL;
323 omap_crtc->flip_pending = false;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200324
325 spin_unlock_irqrestore(&dev->event_lock, flags);
326
327 complete(&omap_crtc->completion);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200328}
329
Laurent Pincharta42133a2015-01-17 19:09:26 +0200330int omap_crtc_flush(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200331{
332 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200333 struct omap_framebuffer_unpin *fb, *next;
334
335 DBG("%s: GO", omap_crtc->name);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200336
337 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Laurent Pincharta42133a2015-01-17 19:09:26 +0200338 WARN_ON(omap_crtc->vblank_irq.registered);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200339
Laurent Pincharta42133a2015-01-17 19:09:26 +0200340 dispc_runtime_get();
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200341
Laurent Pincharta42133a2015-01-17 19:09:26 +0200342 if (dispc_mgr_is_enabled(omap_crtc->channel)) {
343 dispc_mgr_go(omap_crtc->channel);
344 omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200345
Laurent Pincharta42133a2015-01-17 19:09:26 +0200346 WARN_ON(!wait_for_completion_timeout(&omap_crtc->completion,
347 msecs_to_jiffies(100)));
348 reinit_completion(&omap_crtc->completion);
349 }
350
351 dispc_runtime_put();
352
353 /* Unpin and unreference pending framebuffers. */
354 list_for_each_entry_safe(fb, next, &omap_crtc->pending_unpins, list) {
355 omap_framebuffer_unpin(fb->fb);
356 drm_framebuffer_unreference(fb->fb);
357 list_del(&fb->list);
358 kfree(fb);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200359 }
360
361 return 0;
362}
363
Laurent Pincharta42133a2015-01-17 19:09:26 +0200364int omap_crtc_queue_unpin(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200365{
Laurent Pincharta42133a2015-01-17 19:09:26 +0200366 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
367 struct omap_framebuffer_unpin *unpin;
368
369 unpin = kzalloc(sizeof(*unpin), GFP_KERNEL);
370 if (!unpin)
371 return -ENOMEM;
372
373 unpin->fb = fb;
374 list_add_tail(&unpin->list, &omap_crtc->pending_unpins);
375
376 return 0;
377}
378
379static void omap_crtc_setup(struct drm_crtc *crtc)
380{
381 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200382 struct omap_drm_private *priv = crtc->dev->dev_private;
383 struct drm_encoder *encoder = NULL;
384 unsigned int i;
385
386 DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
387
Laurent Pincharta42133a2015-01-17 19:09:26 +0200388 dispc_runtime_get();
389
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200390 for (i = 0; i < priv->num_encoders; i++) {
391 if (priv->encoders[i]->crtc == crtc) {
392 encoder = priv->encoders[i];
393 break;
394 }
395 }
396
397 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
398 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
399
400 omap_crtc->current_encoder = encoder;
401
402 if (!omap_crtc->enabled) {
403 if (encoder)
404 omap_encoder_set_enabled(encoder, false);
405 } else {
406 if (encoder) {
407 omap_encoder_set_enabled(encoder, false);
408 omap_encoder_update(encoder, omap_crtc->mgr,
409 &omap_crtc->timings);
410 omap_encoder_set_enabled(encoder, true);
411 }
412 }
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200413
Laurent Pincharta42133a2015-01-17 19:09:26 +0200414 dispc_runtime_put();
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200415}
416
417/* -----------------------------------------------------------------------------
418 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600419 */
420
Rob Clarkcd5351f2011-11-12 12:09:40 -0600421static void omap_crtc_destroy(struct drm_crtc *crtc)
422{
423 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600424
425 DBG("%s", omap_crtc->name);
426
Laurent Pincharta42133a2015-01-17 19:09:26 +0200427 WARN_ON(omap_crtc->vblank_irq.registered);
Rob Clarkf5f94542012-12-04 13:59:12 -0600428 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
429
Rob Clarkcd5351f2011-11-12 12:09:40 -0600430 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600431
Rob Clarkcd5351f2011-11-12 12:09:40 -0600432 kfree(omap_crtc);
433}
434
435static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
436{
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600437 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600438 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600439 bool enabled = (mode == DRM_MODE_DPMS_ON);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600440 int i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600441
Rob Clarkf5f94542012-12-04 13:59:12 -0600442 DBG("%s: %d", omap_crtc->name, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600443
Laurent Pincharta42133a2015-01-17 19:09:26 +0200444 if (enabled == omap_crtc->enabled)
445 return;
Rob Clarkf5f94542012-12-04 13:59:12 -0600446
Laurent Pincharta42133a2015-01-17 19:09:26 +0200447 /* Enable/disable all planes associated with the CRTC. */
448 for (i = 0; i < priv->num_planes; i++) {
449 struct drm_plane *plane = priv->planes[i];
450
451 if (plane->crtc == crtc)
452 WARN_ON(omap_plane_set_enable(plane, enabled));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600453 }
Laurent Pincharta42133a2015-01-17 19:09:26 +0200454
455 omap_crtc->enabled = enabled;
456
457 omap_crtc_setup(crtc);
458 omap_crtc_flush(crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600459}
460
461static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200462 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600463 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600464{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600465 return true;
466}
467
468static int omap_crtc_mode_set(struct drm_crtc *crtc,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600469 struct drm_display_mode *mode,
470 struct drm_display_mode *adjusted_mode,
471 int x, int y,
472 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600473{
474 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
475
Rob Clarkf5f94542012-12-04 13:59:12 -0600476 mode = adjusted_mode;
477
478 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
479 omap_crtc->name, mode->base.id, mode->name,
480 mode->vrefresh, mode->clock,
481 mode->hdisplay, mode->hsync_start,
482 mode->hsync_end, mode->htotal,
483 mode->vdisplay, mode->vsync_start,
484 mode->vsync_end, mode->vtotal,
485 mode->type, mode->flags);
486
487 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
Rob Clarkf5f94542012-12-04 13:59:12 -0600488
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200489 /*
490 * The primary plane CRTC can be reset if the plane is disabled directly
491 * through the universal plane API. Set it again here.
492 */
493 crtc->primary->crtc = crtc;
494
495 return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200496 0, 0, mode->hdisplay, mode->vdisplay,
Laurent Pincharta42133a2015-01-17 19:09:26 +0200497 x, y, mode->hdisplay, mode->vdisplay);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600498}
499
500static void omap_crtc_prepare(struct drm_crtc *crtc)
501{
502 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600503 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600504 omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
505}
506
507static void omap_crtc_commit(struct drm_crtc *crtc)
508{
509 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600510 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600511 omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
512}
513
514static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600515 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600516{
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200517 struct drm_plane *plane = crtc->primary;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600518 struct drm_display_mode *mode = &crtc->mode;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200519 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600520
Laurent Pincharta42133a2015-01-17 19:09:26 +0200521 ret = omap_plane_mode_set(plane, crtc, crtc->primary->fb,
522 0, 0, mode->hdisplay, mode->vdisplay,
523 x, y, mode->hdisplay, mode->vdisplay);
524 if (ret < 0)
525 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600526
Laurent Pincharta42133a2015-01-17 19:09:26 +0200527 return omap_crtc_flush(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600528}
529
530static void page_flip_worker(struct work_struct *work)
531{
532 struct omap_crtc *omap_crtc =
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200533 container_of(work, struct omap_crtc, flip_work);
Rob Clarkf5f94542012-12-04 13:59:12 -0600534 struct drm_crtc *crtc = &omap_crtc->base;
Rob Clarkf5f94542012-12-04 13:59:12 -0600535 struct drm_display_mode *mode = &crtc->mode;
536 struct drm_gem_object *bo;
537
Rob Clark51fd3712013-11-19 12:10:12 -0500538 drm_modeset_lock(&crtc->mutex, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200539 omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
Laurent Pincharta350da82015-01-17 22:31:42 +0200540 0, 0, mode->hdisplay, mode->vdisplay,
Laurent Pincharta42133a2015-01-17 19:09:26 +0200541 crtc->x, crtc->y, mode->hdisplay, mode->vdisplay);
542 omap_crtc_flush(crtc);
Rob Clark51fd3712013-11-19 12:10:12 -0500543 drm_modeset_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600544
Matt Roperf4510a22014-04-01 15:22:40 -0700545 bo = omap_framebuffer_bo(crtc->primary->fb, 0);
Rob Clarkf5f94542012-12-04 13:59:12 -0600546 drm_gem_object_unreference_unlocked(bo);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200547 drm_framebuffer_unreference(crtc->primary->fb);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600548}
549
Rob Clark72d0c332012-03-11 21:11:21 -0500550static void page_flip_cb(void *arg)
551{
552 struct drm_crtc *crtc = arg;
553 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600554 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clark72d0c332012-03-11 21:11:21 -0500555
Rob Clarkf5f94542012-12-04 13:59:12 -0600556 /* avoid assumptions about what ctxt we are called from: */
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200557 queue_work(priv->wq, &omap_crtc->flip_work);
Rob Clark72d0c332012-03-11 21:11:21 -0500558}
559
Laurent Pinchart077db4d2015-01-18 16:36:19 +0200560static int omap_crtc_page_flip(struct drm_crtc *crtc,
561 struct drm_framebuffer *fb,
562 struct drm_pending_vblank_event *event,
563 uint32_t page_flip_flags)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600564{
565 struct drm_device *dev = crtc->dev;
566 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700567 struct drm_plane *primary = crtc->primary;
Rob Clark119c0812012-09-04 17:46:22 -0500568 struct drm_gem_object *bo;
Archit Taneja38e55972014-04-11 12:53:35 +0530569 unsigned long flags;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600570
Matt Roperf4510a22014-04-01 15:22:40 -0700571 DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
Rob Clarkf5f94542012-12-04 13:59:12 -0600572 fb->base.id, event);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600573
Archit Taneja38e55972014-04-11 12:53:35 +0530574 spin_lock_irqsave(&dev->event_lock, flags);
575
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200576 if (omap_crtc->flip_pending) {
Archit Taneja38e55972014-04-11 12:53:35 +0530577 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600578 dev_err(dev->dev, "already a pending flip\n");
Tomi Valkeinen549a7542014-09-03 19:25:50 +0000579 return -EBUSY;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600580 }
581
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200582 omap_crtc->flip_event = event;
583 omap_crtc->flip_pending = true;
584 primary->fb = fb;
585 drm_framebuffer_reference(fb);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600586
Archit Taneja38e55972014-04-11 12:53:35 +0530587 spin_unlock_irqrestore(&dev->event_lock, flags);
588
Rob Clark119c0812012-09-04 17:46:22 -0500589 /*
590 * Hold a reference temporarily until the crtc is updated
591 * and takes the reference to the bo. This avoids it
592 * getting freed from under us:
593 */
594 bo = omap_framebuffer_bo(fb, 0);
595 drm_gem_object_reference(bo);
596
597 omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600598
599 return 0;
600}
601
Rob Clark3c810c62012-08-15 15:18:01 -0500602static int omap_crtc_set_property(struct drm_crtc *crtc,
603 struct drm_property *property, uint64_t val)
604{
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200605 if (property == crtc->dev->mode_config.rotation_property) {
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500606 crtc->invert_dimensions =
607 !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
608 }
609
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200610 return omap_plane_set_property(crtc->primary, property, val);
Rob Clark3c810c62012-08-15 15:18:01 -0500611}
612
Rob Clarkcd5351f2011-11-12 12:09:40 -0600613static const struct drm_crtc_funcs omap_crtc_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600614 .set_config = drm_crtc_helper_set_config,
615 .destroy = omap_crtc_destroy,
Laurent Pinchart077db4d2015-01-18 16:36:19 +0200616 .page_flip = omap_crtc_page_flip,
Rob Clark3c810c62012-08-15 15:18:01 -0500617 .set_property = omap_crtc_set_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600618};
619
620static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
621 .dpms = omap_crtc_dpms,
622 .mode_fixup = omap_crtc_mode_fixup,
623 .mode_set = omap_crtc_mode_set,
624 .prepare = omap_crtc_prepare,
625 .commit = omap_crtc_commit,
626 .mode_set_base = omap_crtc_mode_set_base,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600627};
628
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200629/* -----------------------------------------------------------------------------
630 * Init and Cleanup
631 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300632
Rob Clarkf5f94542012-12-04 13:59:12 -0600633static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200634 [OMAP_DSS_CHANNEL_LCD] = "lcd",
635 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
636 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
637 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600638};
639
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300640void omap_crtc_pre_init(void)
641{
642 dss_install_mgr_ops(&mgr_ops);
643}
644
Archit Taneja3a01ab22014-01-02 14:49:51 +0530645void omap_crtc_pre_uninit(void)
646{
647 dss_uninstall_mgr_ops();
648}
649
Rob Clarkcd5351f2011-11-12 12:09:40 -0600650/* initialize crtc */
651struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600652 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600653{
654 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600655 struct omap_crtc *omap_crtc;
656 struct omap_overlay_manager_info *info;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200657 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600658
Rob Clarkf5f94542012-12-04 13:59:12 -0600659 DBG("%s", channel_names[channel]);
660
661 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800662 if (!omap_crtc)
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200663 return NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600664
Rob Clarkcd5351f2011-11-12 12:09:40 -0600665 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600666
Laurent Pinchart42fb61c2015-01-26 02:58:51 +0200667 INIT_WORK(&omap_crtc->flip_work, page_flip_worker);
Rob Clarkf5f94542012-12-04 13:59:12 -0600668
Laurent Pincharta42133a2015-01-17 19:09:26 +0200669 INIT_LIST_HEAD(&omap_crtc->pending_unpins);
Rob Clarkf5f94542012-12-04 13:59:12 -0600670
Laurent Pincharta42133a2015-01-17 19:09:26 +0200671 init_completion(&omap_crtc->completion);
Rob Clarkf5f94542012-12-04 13:59:12 -0600672
Archit Taneja0d8f3712013-03-26 19:15:19 +0530673 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530674 omap_crtc->name = channel_names[channel];
675 omap_crtc->pipe = id;
676
Laurent Pincharta42133a2015-01-17 19:09:26 +0200677 omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
678 omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
Rob Clarkf5f94542012-12-04 13:59:12 -0600679
680 omap_crtc->error_irq.irqmask =
681 dispc_mgr_get_sync_lost_irq(channel);
682 omap_crtc->error_irq.irq = omap_crtc_error_irq;
683 omap_irq_register(dev, &omap_crtc->error_irq);
684
Rob Clarkf5f94542012-12-04 13:59:12 -0600685 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300686 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600687
688 /* TODO: fix hard-coded setup.. add properties! */
689 info = &omap_crtc->info;
690 info->default_color = 0x00000000;
691 info->trans_key = 0x00000000;
692 info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
693 info->trans_enabled = false;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600694
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200695 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
696 &omap_crtc_funcs);
697 if (ret < 0) {
698 kfree(omap_crtc);
699 return NULL;
700 }
701
Rob Clarkcd5351f2011-11-12 12:09:40 -0600702 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
703
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200704 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500705
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300706 omap_crtcs[channel] = omap_crtc;
707
Rob Clarkcd5351f2011-11-12 12:09:40 -0600708 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600709}