blob: 82d03ed9257650586b12d8aa6a4fe00f22a2d090 [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
Laurent Pinchart69a12262015-03-05 21:38:16 +020022#include <drm/drm_atomic.h>
23#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020024#include <drm/drm_crtc.h>
25#include <drm/drm_crtc_helper.h>
Andy Grossb9ed9f02012-10-16 00:17:40 -050026#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010027#include <drm/drm_plane_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020028
29#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060030
31#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
32
33struct omap_crtc {
34 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060035
Rob Clarkbb5c2d92012-01-16 12:51:16 -060036 const char *name;
Rob Clarkf5f94542012-12-04 13:59:12 -060037 enum omap_channel channel;
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;
Rob Clarkf5f94542012-12-04 13:59:12 -060049
Laurent Pincharta42133a2015-01-17 19:09:26 +020050 struct omap_drm_irq vblank_irq;
Rob Clarkf5f94542012-12-04 13:59:12 -060051 struct omap_drm_irq error_irq;
52
Laurent Pinchartfa16d262015-03-06 16:01:53 +020053 /* pending event */
54 struct drm_pending_vblank_event *event;
Laurent Pinchartc397cfd2015-01-25 22:42:30 +020055 wait_queue_head_t flip_wait;
Rob Clarkf5f94542012-12-04 13:59:12 -060056
Laurent Pincharta42133a2015-01-17 19:09:26 +020057 struct completion completion;
58
Tomi Valkeinena36af732015-02-26 15:20:24 +020059 bool ignore_digit_sync_lost;
Rob Clarkcd5351f2011-11-12 12:09:40 -060060};
61
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020062/* -----------------------------------------------------------------------------
63 * Helper Functions
64 */
65
Archit Taneja0d8f3712013-03-26 19:15:19 +053066uint32_t pipe2vbl(struct drm_crtc *crtc)
67{
68 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
69
70 return dispc_mgr_get_vsync_irq(omap_crtc->channel);
71}
72
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020073const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
74{
75 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
76 return &omap_crtc->timings;
77}
78
79enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
80{
81 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
82 return omap_crtc->channel;
83}
84
85/* -----------------------------------------------------------------------------
86 * DSS Manager Functions
87 */
88
Rob Clarkf5f94542012-12-04 13:59:12 -060089/*
90 * Manager-ops, callbacks from output when they need to configure
91 * the upstream part of the video pipe.
92 *
93 * Most of these we can ignore until we add support for command-mode
94 * panels.. for video-mode the crtc-helpers already do an adequate
95 * job of sequencing the setup of the video pipe in the proper order
96 */
97
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +030098/* ovl-mgr-id -> crtc */
99static struct omap_crtc *omap_crtcs[8];
100
Rob Clarkf5f94542012-12-04 13:59:12 -0600101/* we can probably ignore these until we support command-mode panels: */
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200102static int omap_crtc_dss_connect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300103 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300104{
105 if (mgr->output)
106 return -EINVAL;
107
108 if ((mgr->supported_outputs & dst->id) == 0)
109 return -EINVAL;
110
111 dst->manager = mgr;
112 mgr->output = dst;
113
114 return 0;
115}
116
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200117static void omap_crtc_dss_disconnect(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 mgr->output->manager = NULL;
121 mgr->output = NULL;
122}
123
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200124static void omap_crtc_dss_start_update(struct omap_overlay_manager *mgr)
Rob Clarkf5f94542012-12-04 13:59:12 -0600125{
126}
127
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300128/* Called only from omap_crtc_encoder_setup and suspend/resume handlers. */
Laurent Pinchart8472b572015-01-15 00:45:17 +0200129static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
130{
131 struct drm_device *dev = crtc->dev;
132 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
133 enum omap_channel channel = omap_crtc->channel;
134 struct omap_irq_wait *wait;
135 u32 framedone_irq, vsync_irq;
136 int ret;
137
138 if (dispc_mgr_is_enabled(channel) == enable)
139 return;
140
Tomi Valkeinenef422282015-02-26 15:20:25 +0200141 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
142 /*
143 * Digit output produces some sync lost interrupts during the
144 * first frame when enabling, so we need to ignore those.
145 */
146 omap_crtc->ignore_digit_sync_lost = true;
147 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200148
149 framedone_irq = dispc_mgr_get_framedone_irq(channel);
150 vsync_irq = dispc_mgr_get_vsync_irq(channel);
151
152 if (enable) {
153 wait = omap_irq_wait_init(dev, vsync_irq, 1);
154 } else {
155 /*
156 * When we disable the digit output, we need to wait for
157 * FRAMEDONE to know that DISPC has finished with the output.
158 *
159 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
160 * that case we need to use vsync interrupt, and wait for both
161 * even and odd frames.
162 */
163
164 if (framedone_irq)
165 wait = omap_irq_wait_init(dev, framedone_irq, 1);
166 else
167 wait = omap_irq_wait_init(dev, vsync_irq, 2);
168 }
169
170 dispc_mgr_enable(channel, enable);
171
172 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
173 if (ret) {
174 dev_err(dev->dev, "%s: timeout waiting for %s\n",
175 omap_crtc->name, enable ? "enable" : "disable");
176 }
177
Tomi Valkeinenef422282015-02-26 15:20:25 +0200178 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
179 omap_crtc->ignore_digit_sync_lost = false;
180 /* make sure the irq handler sees the value above */
181 mb();
182 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200183}
184
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300185
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200186static int omap_crtc_dss_enable(struct omap_overlay_manager *mgr)
Rob Clarkf5f94542012-12-04 13:59:12 -0600187{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300188 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Laurent Pinchartdee82602015-03-06 19:00:18 +0200189 struct omap_overlay_manager_info info;
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300190
Laurent Pinchartdee82602015-03-06 19:00:18 +0200191 memset(&info, 0, sizeof(info));
192 info.default_color = 0x00000000;
193 info.trans_key = 0x00000000;
194 info.trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
195 info.trans_enabled = false;
196
197 dispc_mgr_setup(omap_crtc->channel, &info);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300198 dispc_mgr_set_timings(omap_crtc->channel,
199 &omap_crtc->timings);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200200 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300201
Rob Clarkf5f94542012-12-04 13:59:12 -0600202 return 0;
203}
204
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200205static void omap_crtc_dss_disable(struct omap_overlay_manager *mgr)
Rob Clarkf5f94542012-12-04 13:59:12 -0600206{
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300207 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
208
Laurent Pinchart8472b572015-01-15 00:45:17 +0200209 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600210}
211
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200212static void omap_crtc_dss_set_timings(struct omap_overlay_manager *mgr,
Rob Clarkf5f94542012-12-04 13:59:12 -0600213 const struct omap_video_timings *timings)
214{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300215 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600216 DBG("%s", omap_crtc->name);
217 omap_crtc->timings = *timings;
Rob Clarkf5f94542012-12-04 13:59:12 -0600218}
219
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200220static void omap_crtc_dss_set_lcd_config(struct omap_overlay_manager *mgr,
Rob Clarkf5f94542012-12-04 13:59:12 -0600221 const struct dss_lcd_mgr_config *config)
222{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300223 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600224 DBG("%s", omap_crtc->name);
225 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
226}
227
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200228static int omap_crtc_dss_register_framedone(
Rob Clarkf5f94542012-12-04 13:59:12 -0600229 struct omap_overlay_manager *mgr,
230 void (*handler)(void *), void *data)
231{
232 return 0;
233}
234
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200235static void omap_crtc_dss_unregister_framedone(
Rob Clarkf5f94542012-12-04 13:59:12 -0600236 struct omap_overlay_manager *mgr,
237 void (*handler)(void *), void *data)
238{
239}
240
241static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200242 .connect = omap_crtc_dss_connect,
243 .disconnect = omap_crtc_dss_disconnect,
244 .start_update = omap_crtc_dss_start_update,
245 .enable = omap_crtc_dss_enable,
246 .disable = omap_crtc_dss_disable,
247 .set_timings = omap_crtc_dss_set_timings,
248 .set_lcd_config = omap_crtc_dss_set_lcd_config,
249 .register_framedone_handler = omap_crtc_dss_register_framedone,
250 .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
Rob Clarkf5f94542012-12-04 13:59:12 -0600251};
252
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200253/* -----------------------------------------------------------------------------
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200254 * Setup, Flush and Page Flip
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200255 */
256
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200257void omap_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
258{
259 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200260 struct drm_pending_vblank_event *event;
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200261 struct drm_device *dev = crtc->dev;
262 unsigned long flags;
263
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200264 /* Destroy the pending vertical blanking event associated with the
265 * pending page flip, if any, and disable vertical blanking interrupts.
266 */
267
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200268 spin_lock_irqsave(&dev->event_lock, flags);
269
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200270 event = omap_crtc->event;
271 omap_crtc->event = NULL;
272
273 if (event && event->base.file_priv == file) {
274 event->base.destroy(&event->base);
275 drm_crtc_vblank_put(crtc);
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200276 }
277
278 spin_unlock_irqrestore(&dev->event_lock, flags);
279}
280
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200281static void omap_crtc_complete_page_flip(struct drm_crtc *crtc)
Laurent Pinchart15d02e92015-01-25 22:42:30 +0200282{
283 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200284 struct drm_pending_vblank_event *event;
Laurent Pinchart15d02e92015-01-25 22:42:30 +0200285 struct drm_device *dev = crtc->dev;
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200286 unsigned long flags;
Laurent Pinchart15d02e92015-01-25 22:42:30 +0200287
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200288 spin_lock_irqsave(&dev->event_lock, flags);
289
290 event = omap_crtc->event;
291 omap_crtc->event = NULL;
292
293 if (event) {
294 drm_crtc_send_vblank_event(crtc, event);
295 wake_up(&omap_crtc->flip_wait);
296 drm_crtc_vblank_put(crtc);
Laurent Pinchart15d02e92015-01-25 22:42:30 +0200297 }
298
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200299 spin_unlock_irqrestore(&dev->event_lock, flags);
Laurent Pinchartc397cfd2015-01-25 22:42:30 +0200300}
301
302static bool omap_crtc_page_flip_pending(struct drm_crtc *crtc)
303{
304 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
305 struct drm_device *dev = crtc->dev;
306 unsigned long flags;
307 bool pending;
308
309 spin_lock_irqsave(&dev->event_lock, flags);
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200310 pending = omap_crtc->event != NULL;
Laurent Pinchartc397cfd2015-01-25 22:42:30 +0200311 spin_unlock_irqrestore(&dev->event_lock, flags);
312
313 return pending;
314}
315
316static void omap_crtc_wait_page_flip(struct drm_crtc *crtc)
317{
318 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartc397cfd2015-01-25 22:42:30 +0200319
320 if (wait_event_timeout(omap_crtc->flip_wait,
321 !omap_crtc_page_flip_pending(crtc),
322 msecs_to_jiffies(50)))
323 return;
324
325 dev_warn(crtc->dev->dev, "page flip timeout!\n");
326
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200327 omap_crtc_complete_page_flip(crtc);
Laurent Pinchart15d02e92015-01-25 22:42:30 +0200328}
329
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200330static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
331{
332 struct omap_crtc *omap_crtc =
333 container_of(irq, struct omap_crtc, error_irq);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200334
335 if (omap_crtc->ignore_digit_sync_lost) {
336 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
337 if (!irqstatus)
338 return;
339 }
340
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200341 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200342}
343
Laurent Pincharta42133a2015-01-17 19:09:26 +0200344static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200345{
346 struct omap_crtc *omap_crtc =
Laurent Pincharta42133a2015-01-17 19:09:26 +0200347 container_of(irq, struct omap_crtc, vblank_irq);
348 struct drm_device *dev = omap_crtc->base.dev;
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200349
Laurent Pincharta42133a2015-01-17 19:09:26 +0200350 if (dispc_mgr_go_busy(omap_crtc->channel))
351 return;
352
353 DBG("%s: apply done", omap_crtc->name);
354 __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
355
Laurent Pincharta42133a2015-01-17 19:09:26 +0200356 /* wakeup userspace */
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200357 omap_crtc_complete_page_flip(&omap_crtc->base);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200358
359 complete(&omap_crtc->completion);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200360}
361
Laurent Pincharta42133a2015-01-17 19:09:26 +0200362int omap_crtc_flush(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200363{
364 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200365
366 DBG("%s: GO", omap_crtc->name);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200367
368 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Laurent Pincharta42133a2015-01-17 19:09:26 +0200369 WARN_ON(omap_crtc->vblank_irq.registered);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200370
Laurent Pincharta42133a2015-01-17 19:09:26 +0200371 dispc_runtime_get();
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200372
Laurent Pincharta42133a2015-01-17 19:09:26 +0200373 if (dispc_mgr_is_enabled(omap_crtc->channel)) {
374 dispc_mgr_go(omap_crtc->channel);
375 omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200376
Laurent Pincharta42133a2015-01-17 19:09:26 +0200377 WARN_ON(!wait_for_completion_timeout(&omap_crtc->completion,
378 msecs_to_jiffies(100)));
379 reinit_completion(&omap_crtc->completion);
380 }
381
382 dispc_runtime_put();
383
Laurent Pincharta42133a2015-01-17 19:09:26 +0200384 return 0;
385}
386
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300387static void omap_crtc_encoder_setup(struct drm_crtc *crtc, bool enable)
Laurent Pincharta42133a2015-01-17 19:09:26 +0200388{
389 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200390 struct omap_drm_private *priv = crtc->dev->dev_private;
391 struct drm_encoder *encoder = NULL;
392 unsigned int i;
393
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300394 DBG("%s: enable=%d", omap_crtc->name, enable);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200395
Laurent Pincharta42133a2015-01-17 19:09:26 +0200396 dispc_runtime_get();
397
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200398 for (i = 0; i < priv->num_encoders; i++) {
399 if (priv->encoders[i]->crtc == crtc) {
400 encoder = priv->encoders[i];
401 break;
402 }
403 }
404
405 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
406 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
407
408 omap_crtc->current_encoder = encoder;
409
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300410 if (encoder) {
411 omap_encoder_set_enabled(encoder, false);
412 if (enable) {
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200413 omap_encoder_update(encoder, omap_crtc->mgr,
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300414 &omap_crtc->timings);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200415 omap_encoder_set_enabled(encoder, true);
416 }
417 }
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200418
Laurent Pincharta42133a2015-01-17 19:09:26 +0200419 dispc_runtime_put();
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200420}
421
422/* -----------------------------------------------------------------------------
423 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600424 */
425
Rob Clarkcd5351f2011-11-12 12:09:40 -0600426static void omap_crtc_destroy(struct drm_crtc *crtc)
427{
428 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600429
430 DBG("%s", omap_crtc->name);
431
Laurent Pincharta42133a2015-01-17 19:09:26 +0200432 WARN_ON(omap_crtc->vblank_irq.registered);
Rob Clarkf5f94542012-12-04 13:59:12 -0600433 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
434
Rob Clarkcd5351f2011-11-12 12:09:40 -0600435 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600436
Rob Clarkcd5351f2011-11-12 12:09:40 -0600437 kfree(omap_crtc);
438}
439
Rob Clarkcd5351f2011-11-12 12:09:40 -0600440static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200441 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600442 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600443{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600444 return true;
445}
446
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200447static void omap_crtc_enable(struct drm_crtc *crtc)
448{
449 struct omap_drm_private *priv = crtc->dev->dev_private;
450 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
451 unsigned int i;
452
453 DBG("%s", omap_crtc->name);
454
Laurent Pinchartedc72552015-03-06 19:18:56 +0200455 dispc_runtime_get();
456
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200457 /* Enable all planes associated with the CRTC. */
458 for (i = 0; i < priv->num_planes; i++) {
459 struct drm_plane *plane = priv->planes[i];
460
461 if (plane->crtc == crtc)
Laurent Pinchartedc72552015-03-06 19:18:56 +0200462 WARN_ON(omap_plane_setup(plane));
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200463 }
464
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300465 omap_crtc_encoder_setup(crtc, true);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200466 omap_crtc_flush(crtc);
467
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200468 drm_crtc_vblank_on(crtc);
Laurent Pinchartedc72552015-03-06 19:18:56 +0200469
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200470 dispc_runtime_put();
471}
472
473static void omap_crtc_disable(struct drm_crtc *crtc)
474{
475 struct omap_drm_private *priv = crtc->dev->dev_private;
476 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
477 unsigned int i;
478
479 DBG("%s", omap_crtc->name);
480
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200481 omap_crtc_wait_page_flip(crtc);
482 dispc_runtime_get();
483 drm_crtc_vblank_off(crtc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200484
485 /* Disable all planes associated with the CRTC. */
486 for (i = 0; i < priv->num_planes; i++) {
487 struct drm_plane *plane = priv->planes[i];
488
489 if (plane->crtc == crtc)
Laurent Pinchartedc72552015-03-06 19:18:56 +0200490 WARN_ON(omap_plane_setup(plane));
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200491 }
492
Laurent Pinchart6cca4812015-04-15 18:55:27 +0300493 omap_crtc_encoder_setup(crtc, false);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200494 omap_crtc_flush(crtc);
Laurent Pinchartedc72552015-03-06 19:18:56 +0200495
496 dispc_runtime_put();
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200497}
498
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200499static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600500{
501 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200502 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Rob Clarkf5f94542012-12-04 13:59:12 -0600503
504 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200505 omap_crtc->name, mode->base.id, mode->name,
506 mode->vrefresh, mode->clock,
507 mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
508 mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal,
509 mode->type, mode->flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600510
511 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600512}
513
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200514static void omap_crtc_atomic_begin(struct drm_crtc *crtc)
515{
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200516 struct drm_pending_vblank_event *event = crtc->state->event;
517 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
518 struct drm_device *dev = crtc->dev;
519 unsigned long flags;
520
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200521 dispc_runtime_get();
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200522
523 if (event) {
524 WARN_ON(omap_crtc->event);
525 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
526
527 spin_lock_irqsave(&dev->event_lock, flags);
528 omap_crtc->event = event;
529 spin_unlock_irqrestore(&dev->event_lock, flags);
530 }
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200531}
532
533static void omap_crtc_atomic_flush(struct drm_crtc *crtc)
534{
535 omap_crtc_flush(crtc);
536
537 dispc_runtime_put();
Laurent Pinchartafc34932015-03-06 18:35:16 +0200538
539 crtc->invert_dimensions = !!(crtc->primary->state->rotation &
540 (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270)));
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200541}
542
Laurent Pinchartafc34932015-03-06 18:35:16 +0200543static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
544 struct drm_crtc_state *state,
545 struct drm_property *property,
546 uint64_t val)
Rob Clark3c810c62012-08-15 15:18:01 -0500547{
Laurent Pinchartafc34932015-03-06 18:35:16 +0200548 struct drm_plane_state *plane_state;
549 struct drm_plane *plane = crtc->primary;
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500550
Laurent Pinchartafc34932015-03-06 18:35:16 +0200551 /*
552 * Delegate property set to the primary plane. Get the plane state and
553 * set the property directly.
554 */
555
556 plane_state = drm_atomic_get_plane_state(state->state, plane);
557 if (!plane_state)
558 return -EINVAL;
559
560 return drm_atomic_plane_set_property(plane, plane_state, property, val);
561}
562
563static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
564 const struct drm_crtc_state *state,
565 struct drm_property *property,
566 uint64_t *val)
567{
568 /*
569 * Delegate property get to the primary plane. The
570 * drm_atomic_plane_get_property() function isn't exported, but can be
571 * called through drm_object_property_get_value() as that will call
572 * drm_atomic_get_property() for atomic drivers.
573 */
574 return drm_object_property_get_value(&crtc->primary->base, property,
575 val);
Rob Clark3c810c62012-08-15 15:18:01 -0500576}
577
Rob Clarkcd5351f2011-11-12 12:09:40 -0600578static const struct drm_crtc_funcs omap_crtc_funcs = {
Laurent Pinchart69a12262015-03-05 21:38:16 +0200579 .reset = drm_atomic_helper_crtc_reset,
Laurent Pinchart9416c9d2015-03-05 21:54:54 +0200580 .set_config = drm_atomic_helper_set_config,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600581 .destroy = omap_crtc_destroy,
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200582 .page_flip = drm_atomic_helper_page_flip,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200583 .set_property = drm_atomic_helper_crtc_set_property,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200584 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
585 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200586 .atomic_set_property = omap_crtc_atomic_set_property,
587 .atomic_get_property = omap_crtc_atomic_get_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600588};
589
590static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600591 .mode_fixup = omap_crtc_mode_fixup,
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200592 .mode_set_nofb = omap_crtc_mode_set_nofb,
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200593 .disable = omap_crtc_disable,
594 .enable = omap_crtc_enable,
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200595 .atomic_begin = omap_crtc_atomic_begin,
596 .atomic_flush = omap_crtc_atomic_flush,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600597};
598
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200599/* -----------------------------------------------------------------------------
600 * Init and Cleanup
601 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300602
Rob Clarkf5f94542012-12-04 13:59:12 -0600603static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200604 [OMAP_DSS_CHANNEL_LCD] = "lcd",
605 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
606 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
607 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600608};
609
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300610void omap_crtc_pre_init(void)
611{
612 dss_install_mgr_ops(&mgr_ops);
613}
614
Archit Taneja3a01ab22014-01-02 14:49:51 +0530615void omap_crtc_pre_uninit(void)
616{
617 dss_uninstall_mgr_ops();
618}
619
Rob Clarkcd5351f2011-11-12 12:09:40 -0600620/* initialize crtc */
621struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600622 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600623{
624 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600625 struct omap_crtc *omap_crtc;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200626 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600627
Rob Clarkf5f94542012-12-04 13:59:12 -0600628 DBG("%s", channel_names[channel]);
629
630 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800631 if (!omap_crtc)
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200632 return NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600633
Rob Clarkcd5351f2011-11-12 12:09:40 -0600634 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600635
Laurent Pinchartc397cfd2015-01-25 22:42:30 +0200636 init_waitqueue_head(&omap_crtc->flip_wait);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200637 init_completion(&omap_crtc->completion);
Rob Clarkf5f94542012-12-04 13:59:12 -0600638
Archit Taneja0d8f3712013-03-26 19:15:19 +0530639 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530640 omap_crtc->name = channel_names[channel];
Archit Taneja0d8f3712013-03-26 19:15:19 +0530641
Laurent Pincharta42133a2015-01-17 19:09:26 +0200642 omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
643 omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
Rob Clarkf5f94542012-12-04 13:59:12 -0600644
645 omap_crtc->error_irq.irqmask =
646 dispc_mgr_get_sync_lost_irq(channel);
647 omap_crtc->error_irq.irq = omap_crtc_error_irq;
648 omap_irq_register(dev, &omap_crtc->error_irq);
649
Rob Clarkf5f94542012-12-04 13:59:12 -0600650 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300651 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600652
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200653 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
654 &omap_crtc_funcs);
655 if (ret < 0) {
656 kfree(omap_crtc);
657 return NULL;
658 }
659
Rob Clarkcd5351f2011-11-12 12:09:40 -0600660 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
661
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200662 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500663
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300664 omap_crtcs[channel] = omap_crtc;
665
Rob Clarkcd5351f2011-11-12 12:09:40 -0600666 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600667}