blob: 8712af79a49c9eabd87b97fb8a241d57a7c04d68 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkcd5351f2011-11-12 12:09:40 -06002/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06003 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06004 * Author: Rob Clark <rob@ti.com>
Rob Clarkcd5351f2011-11-12 12:09:40 -06005 */
6
Laurent Pinchart69a12262015-03-05 21:38:16 +02007#include <drm/drm_atomic.h>
8#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +02009#include <drm/drm_crtc.h>
Andy Grossb9ed9f02012-10-16 00:17:40 -050010#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010011#include <drm/drm_plane_helper.h>
Peter Ujfalusia7631c42017-11-30 14:12:37 +020012#include <linux/math64.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020013
14#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060015
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +020016#define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
17
18struct omap_crtc_state {
19 /* Must be first. */
20 struct drm_crtc_state base;
21 /* Shadow values for legacy userspace support. */
22 unsigned int rotation;
23 unsigned int zpos;
24};
25
Rob Clarkcd5351f2011-11-12 12:09:40 -060026#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
27
28struct omap_crtc {
29 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060030
Rob Clarkbb5c2d92012-01-16 12:51:16 -060031 const char *name;
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +020032 struct omap_drm_pipeline *pipe;
Rob Clarkf5f94542012-12-04 13:59:12 -060033 enum omap_channel channel;
Rob Clarkf5f94542012-12-04 13:59:12 -060034
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030035 struct videomode vm;
Rob Clarkf5f94542012-12-04 13:59:12 -060036
Tomi Valkeinena36af732015-02-26 15:20:24 +020037 bool ignore_digit_sync_lost;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030038
Laurent Pinchartf933a3a2016-04-18 02:54:31 +030039 bool enabled;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030040 bool pending;
41 wait_queue_head_t pending_wait;
Laurent Pinchart577d3982016-04-19 01:15:11 +030042 struct drm_pending_vblank_event *event;
Rob Clarkcd5351f2011-11-12 12:09:40 -060043};
44
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020045/* -----------------------------------------------------------------------------
46 * Helper Functions
47 */
48
Peter Ujfalusi4520ff22016-09-22 14:07:03 +030049struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020050{
51 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030052 return &omap_crtc->vm;
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020053}
54
55enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
56{
57 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
58 return omap_crtc->channel;
59}
60
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030061static bool omap_crtc_is_pending(struct drm_crtc *crtc)
62{
63 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
64 unsigned long flags;
65 bool pending;
66
67 spin_lock_irqsave(&crtc->dev->event_lock, flags);
68 pending = omap_crtc->pending;
69 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
70
71 return pending;
72}
73
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030074int omap_crtc_wait_pending(struct drm_crtc *crtc)
75{
76 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
77
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020078 /*
79 * Timeout is set to a "sufficiently" high value, which should cover
80 * a single frame refresh even on slower displays.
81 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030082 return wait_event_timeout(omap_crtc->pending_wait,
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030083 !omap_crtc_is_pending(crtc),
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020084 msecs_to_jiffies(250));
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030085}
86
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020087/* -----------------------------------------------------------------------------
88 * DSS Manager Functions
89 */
90
Rob Clarkf5f94542012-12-04 13:59:12 -060091/*
92 * Manager-ops, callbacks from output when they need to configure
93 * the upstream part of the video pipe.
94 *
95 * Most of these we can ignore until we add support for command-mode
96 * panels.. for video-mode the crtc-helpers already do an adequate
97 * job of sequencing the setup of the video pipe in the proper order
98 */
99
100/* we can probably ignore these until we support command-mode panels: */
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200101static void omap_crtc_dss_start_update(struct omap_drm_private *priv,
102 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600103{
104}
105
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300106/* Called only from the encoder enable/disable and suspend/resume handlers. */
Laurent Pinchart8472b572015-01-15 00:45:17 +0200107static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
108{
109 struct drm_device *dev = crtc->dev;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200110 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200111 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
112 enum omap_channel channel = omap_crtc->channel;
113 struct omap_irq_wait *wait;
114 u32 framedone_irq, vsync_irq;
115 int ret;
116
Laurent Pinchart03af8152016-04-18 03:09:48 +0300117 if (WARN_ON(omap_crtc->enabled == enable))
118 return;
119
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200120 if (omap_crtc->pipe->output->type == OMAP_DISPLAY_TYPE_HDMI) {
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200121 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300122 omap_crtc->enabled = enable;
Tomi Valkeinen4e4b53c2015-03-24 15:46:35 +0200123 return;
124 }
125
Tomi Valkeinenef422282015-02-26 15:20:25 +0200126 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
127 /*
128 * Digit output produces some sync lost interrupts during the
129 * first frame when enabling, so we need to ignore those.
130 */
131 omap_crtc->ignore_digit_sync_lost = true;
132 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200133
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200134 framedone_irq = priv->dispc_ops->mgr_get_framedone_irq(priv->dispc,
135 channel);
136 vsync_irq = priv->dispc_ops->mgr_get_vsync_irq(priv->dispc, channel);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200137
138 if (enable) {
139 wait = omap_irq_wait_init(dev, vsync_irq, 1);
140 } else {
141 /*
142 * When we disable the digit output, we need to wait for
143 * FRAMEDONE to know that DISPC has finished with the output.
144 *
145 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
146 * that case we need to use vsync interrupt, and wait for both
147 * even and odd frames.
148 */
149
150 if (framedone_irq)
151 wait = omap_irq_wait_init(dev, framedone_irq, 1);
152 else
153 wait = omap_irq_wait_init(dev, vsync_irq, 2);
154 }
155
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200156 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300157 omap_crtc->enabled = enable;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200158
159 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
160 if (ret) {
161 dev_err(dev->dev, "%s: timeout waiting for %s\n",
162 omap_crtc->name, enable ? "enable" : "disable");
163 }
164
Tomi Valkeinenef422282015-02-26 15:20:25 +0200165 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
166 omap_crtc->ignore_digit_sync_lost = false;
167 /* make sure the irq handler sees the value above */
168 mb();
169 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200170}
171
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300172
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200173static int omap_crtc_dss_enable(struct omap_drm_private *priv,
174 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600175{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200176 struct drm_crtc *crtc = priv->channels[channel]->crtc;
177 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300178
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200179 priv->dispc_ops->mgr_set_timings(priv->dispc, omap_crtc->channel,
180 &omap_crtc->vm);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200181 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300182
Rob Clarkf5f94542012-12-04 13:59:12 -0600183 return 0;
184}
185
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200186static void omap_crtc_dss_disable(struct omap_drm_private *priv,
187 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600188{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200189 struct drm_crtc *crtc = priv->channels[channel]->crtc;
190 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300191
Laurent Pinchart8472b572015-01-15 00:45:17 +0200192 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600193}
194
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200195static void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
196 enum omap_channel channel,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300197 const struct videomode *vm)
Rob Clarkf5f94542012-12-04 13:59:12 -0600198{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200199 struct drm_crtc *crtc = priv->channels[channel]->crtc;
200 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
201
Rob Clarkf5f94542012-12-04 13:59:12 -0600202 DBG("%s", omap_crtc->name);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300203 omap_crtc->vm = *vm;
Rob Clarkf5f94542012-12-04 13:59:12 -0600204}
205
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200206static void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
207 enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600208 const struct dss_lcd_mgr_config *config)
209{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200210 struct drm_crtc *crtc = priv->channels[channel]->crtc;
211 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200212
Rob Clarkf5f94542012-12-04 13:59:12 -0600213 DBG("%s", omap_crtc->name);
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200214 priv->dispc_ops->mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
215 config);
Rob Clarkf5f94542012-12-04 13:59:12 -0600216}
217
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200218static int omap_crtc_dss_register_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200219 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600220 void (*handler)(void *), void *data)
221{
222 return 0;
223}
224
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200225static void omap_crtc_dss_unregister_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200226 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600227 void (*handler)(void *), void *data)
228{
229}
230
231static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200232 .start_update = omap_crtc_dss_start_update,
233 .enable = omap_crtc_dss_enable,
234 .disable = omap_crtc_dss_disable,
235 .set_timings = omap_crtc_dss_set_timings,
236 .set_lcd_config = omap_crtc_dss_set_lcd_config,
237 .register_framedone_handler = omap_crtc_dss_register_framedone,
238 .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
Rob Clarkf5f94542012-12-04 13:59:12 -0600239};
240
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200241/* -----------------------------------------------------------------------------
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200242 * Setup, Flush and Page Flip
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200243 */
244
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200245void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200246{
Laurent Pincharte0519af2015-05-28 00:21:29 +0300247 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200248
249 if (omap_crtc->ignore_digit_sync_lost) {
250 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
251 if (!irqstatus)
252 return;
253 }
254
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200255 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200256}
257
Laurent Pinchart14389a32016-04-19 01:43:03 +0300258void omap_crtc_vblank_irq(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200259{
Laurent Pinchart14389a32016-04-19 01:43:03 +0300260 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200261 struct drm_device *dev = omap_crtc->base.dev;
262 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart14389a32016-04-19 01:43:03 +0300263 bool pending;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200264
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300265 spin_lock(&crtc->dev->event_lock);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300266 /*
267 * If the dispc is busy we're racing the flush operation. Try again on
268 * the next vblank interrupt.
269 */
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200270 if (priv->dispc_ops->mgr_go_busy(priv->dispc, omap_crtc->channel)) {
Laurent Pinchart14389a32016-04-19 01:43:03 +0300271 spin_unlock(&crtc->dev->event_lock);
272 return;
273 }
274
275 /* Send the vblank event if one has been requested. */
276 if (omap_crtc->event) {
277 drm_crtc_send_vblank_event(crtc, omap_crtc->event);
278 omap_crtc->event = NULL;
279 }
280
281 pending = omap_crtc->pending;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300282 omap_crtc->pending = false;
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300283 spin_unlock(&crtc->dev->event_lock);
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300284
Laurent Pinchart14389a32016-04-19 01:43:03 +0300285 if (pending)
286 drm_crtc_vblank_put(crtc);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200287
Laurent Pinchart14389a32016-04-19 01:43:03 +0300288 /* Wake up omap_atomic_complete. */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300289 wake_up(&omap_crtc->pending_wait);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300290
291 DBG("%s: apply done", omap_crtc->name);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200292}
293
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300294static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
295{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200296 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300297 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
298 struct omap_overlay_manager_info info;
299
300 memset(&info, 0, sizeof(info));
301
302 info.default_color = 0x000000;
303 info.trans_enabled = false;
304 info.partial_alpha_enabled = false;
305 info.cpr_enable = false;
306
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200307 priv->dispc_ops->mgr_setup(priv->dispc, omap_crtc->channel, &info);
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300308}
309
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200310/* -----------------------------------------------------------------------------
311 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600312 */
313
Rob Clarkcd5351f2011-11-12 12:09:40 -0600314static void omap_crtc_destroy(struct drm_crtc *crtc)
315{
316 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600317
318 DBG("%s", omap_crtc->name);
319
Rob Clarkcd5351f2011-11-12 12:09:40 -0600320 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600321
Rob Clarkcd5351f2011-11-12 12:09:40 -0600322 kfree(omap_crtc);
323}
324
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300325static void omap_crtc_arm_event(struct drm_crtc *crtc)
326{
327 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
328
329 WARN_ON(omap_crtc->pending);
330 omap_crtc->pending = true;
331
332 if (crtc->state->event) {
333 omap_crtc->event = crtc->state->event;
334 crtc->state->event = NULL;
335 }
336}
337
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300338static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
339 struct drm_crtc_state *old_state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200340{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200341 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200342 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300343 int ret;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200344
345 DBG("%s", omap_crtc->name);
346
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200347 priv->dispc_ops->runtime_get(priv->dispc);
348
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300349 spin_lock_irq(&crtc->dev->event_lock);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300350 drm_crtc_vblank_on(crtc);
351 ret = drm_crtc_vblank_get(crtc);
352 WARN_ON(ret != 0);
353
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300354 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300355 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200356}
357
Laurent Pinchart64581712017-06-30 12:36:45 +0300358static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
359 struct drm_crtc_state *old_state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200360{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200361 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200362 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200363
364 DBG("%s", omap_crtc->name);
365
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300366 spin_lock_irq(&crtc->dev->event_lock);
367 if (crtc->state->event) {
368 drm_crtc_send_vblank_event(crtc, crtc->state->event);
369 crtc->state->event = NULL;
370 }
371 spin_unlock_irq(&crtc->dev->event_lock);
372
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200373 drm_crtc_vblank_off(crtc);
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200374
375 priv->dispc_ops->runtime_put(priv->dispc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200376}
377
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200378static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
379 const struct drm_display_mode *mode)
380{
381 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchart116c7722018-09-20 00:17:42 +0300382 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
383 struct videomode vm = {0};
384 int r;
385
386 drm_display_mode_to_videomode(mode, &vm);
387 r = priv->dispc_ops->mgr_check_timings(priv->dispc, omap_crtc->channel,
388 &vm);
389 if (r)
390 return r;
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200391
392 /* Check for bandwidth limit */
393 if (priv->max_bandwidth) {
394 /*
395 * Estimation for the bandwidth need of a given mode with one
396 * full screen plane:
397 * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
398 * ^^ Refresh rate ^^
399 *
400 * The interlaced mode is taken into account by using the
401 * pixelclock in the calculation.
402 *
403 * The equation is rearranged for 64bit arithmetic.
404 */
405 uint64_t bandwidth = mode->clock * 1000;
406 unsigned int bpp = 4;
407
408 bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
409 bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
410
411 /*
412 * Reject modes which would need more bandwidth if used with one
413 * full resolution plane (most common use case).
414 */
415 if (priv->max_bandwidth < bandwidth)
416 return MODE_BAD;
417 }
418
419 return MODE_OK;
420}
421
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200422static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600423{
424 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200425 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Rob Clarkf5f94542012-12-04 13:59:12 -0600426
Shayenne Mourac39ff7e2018-12-20 10:26:10 -0200427 DBG("%s: set mode: " DRM_MODE_FMT,
428 omap_crtc->name, DRM_MODE_ARG(mode));
Rob Clarkf5f94542012-12-04 13:59:12 -0600429
Laurent Pinchart8e9c1c62018-06-07 18:32:16 +0300430 drm_display_mode_to_videomode(mode, &omap_crtc->vm);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600431}
432
Jyri Sarha492a4262016-06-07 15:09:17 +0300433static int omap_crtc_atomic_check(struct drm_crtc *crtc,
434 struct drm_crtc_state *state)
435{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200436 struct drm_plane_state *pri_state;
437
Jyri Sarha492a4262016-06-07 15:09:17 +0300438 if (state->color_mgmt_changed && state->gamma_lut) {
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200439 unsigned int length = state->gamma_lut->length /
Jyri Sarha492a4262016-06-07 15:09:17 +0300440 sizeof(struct drm_color_lut);
441
442 if (length < 2)
443 return -EINVAL;
444 }
445
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200446 pri_state = drm_atomic_get_new_plane_state(state->state, crtc->primary);
447 if (pri_state) {
448 struct omap_crtc_state *omap_crtc_state =
449 to_omap_crtc_state(state);
450
451 /* Mirror new values for zpos and rotation in omap_crtc_state */
452 omap_crtc_state->zpos = pri_state->zpos;
453 omap_crtc_state->rotation = pri_state->rotation;
454 }
455
Jyri Sarha492a4262016-06-07 15:09:17 +0300456 return 0;
457}
458
Daniel Vetterc201d002015-08-06 14:09:35 +0200459static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
Laurent Pinchart577d3982016-04-19 01:15:11 +0300460 struct drm_crtc_state *old_crtc_state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200461{
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200462}
463
Daniel Vetterc201d002015-08-06 14:09:35 +0200464static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
Laurent Pinchart577d3982016-04-19 01:15:11 +0300465 struct drm_crtc_state *old_crtc_state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200466{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200467 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300468 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300469 int ret;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300470
Jyri Sarha492a4262016-06-07 15:09:17 +0300471 if (crtc->state->color_mgmt_changed) {
472 struct drm_color_lut *lut = NULL;
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200473 unsigned int length = 0;
Jyri Sarha492a4262016-06-07 15:09:17 +0300474
475 if (crtc->state->gamma_lut) {
476 lut = (struct drm_color_lut *)
477 crtc->state->gamma_lut->data;
478 length = crtc->state->gamma_lut->length /
479 sizeof(*lut);
480 }
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200481 priv->dispc_ops->mgr_set_gamma(priv->dispc, omap_crtc->channel,
482 lut, length);
Jyri Sarha492a4262016-06-07 15:09:17 +0300483 }
484
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300485 omap_crtc_write_crtc_properties(crtc);
486
Jyri Sarhae025d382017-01-27 12:04:54 +0200487 /* Only flush the CRTC if it is currently enabled. */
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300488 if (!omap_crtc->enabled)
489 return;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300490
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300491 DBG("%s: GO", omap_crtc->name);
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300492
Laurent Pinchart14389a32016-04-19 01:43:03 +0300493 ret = drm_crtc_vblank_get(crtc);
494 WARN_ON(ret != 0);
495
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300496 spin_lock_irq(&crtc->dev->event_lock);
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200497 priv->dispc_ops->mgr_go(priv->dispc, omap_crtc->channel);
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300498 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300499 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200500}
501
Laurent Pinchartafc34932015-03-06 18:35:16 +0200502static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
503 struct drm_crtc_state *state,
504 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200505 u64 val)
Rob Clark3c810c62012-08-15 15:18:01 -0500506{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200507 struct omap_drm_private *priv = crtc->dev->dev_private;
508 struct drm_plane_state *plane_state;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200509
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200510 /*
511 * Delegate property set to the primary plane. Get the plane state and
512 * set the property directly, the shadow copy will be assigned in the
513 * omap_crtc_atomic_check callback. This way updates to plane state will
514 * always be mirrored in the crtc state correctly.
515 */
516 plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
517 if (IS_ERR(plane_state))
518 return PTR_ERR(plane_state);
Laurent Pinchartafc34932015-03-06 18:35:16 +0200519
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200520 if (property == crtc->primary->rotation_property)
521 plane_state->rotation = val;
522 else if (property == priv->zorder_prop)
523 plane_state->zpos = val;
524 else
525 return -EINVAL;
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200526
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200527 return 0;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200528}
529
530static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
531 const struct drm_crtc_state *state,
532 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200533 u64 *val)
Laurent Pinchartafc34932015-03-06 18:35:16 +0200534{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200535 struct omap_drm_private *priv = crtc->dev->dev_private;
536 struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200537
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200538 if (property == crtc->primary->rotation_property)
539 *val = omap_state->rotation;
540 else if (property == priv->zorder_prop)
541 *val = omap_state->zpos;
542 else
543 return -EINVAL;
544
545 return 0;
546}
547
548static void omap_crtc_reset(struct drm_crtc *crtc)
549{
550 if (crtc->state)
551 __drm_atomic_helper_crtc_destroy_state(crtc->state);
552
553 kfree(crtc->state);
554 crtc->state = kzalloc(sizeof(struct omap_crtc_state), GFP_KERNEL);
555
556 if (crtc->state)
557 crtc->state->crtc = crtc;
558}
559
560static struct drm_crtc_state *
561omap_crtc_duplicate_state(struct drm_crtc *crtc)
562{
563 struct omap_crtc_state *state, *current_state;
564
565 if (WARN_ON(!crtc->state))
566 return NULL;
567
568 current_state = to_omap_crtc_state(crtc->state);
569
570 state = kmalloc(sizeof(*state), GFP_KERNEL);
Dan Carpenter24196722017-08-11 23:16:06 +0300571 if (!state)
572 return NULL;
573
574 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200575
576 state->zpos = current_state->zpos;
577 state->rotation = current_state->rotation;
578
579 return &state->base;
Rob Clark3c810c62012-08-15 15:18:01 -0500580}
581
Rob Clarkcd5351f2011-11-12 12:09:40 -0600582static const struct drm_crtc_funcs omap_crtc_funcs = {
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200583 .reset = omap_crtc_reset,
Laurent Pinchart9416c9d2015-03-05 21:54:54 +0200584 .set_config = drm_atomic_helper_set_config,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600585 .destroy = omap_crtc_destroy,
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200586 .page_flip = drm_atomic_helper_page_flip,
Jyri Sarha492a4262016-06-07 15:09:17 +0300587 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200588 .atomic_duplicate_state = omap_crtc_duplicate_state,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200589 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200590 .atomic_set_property = omap_crtc_atomic_set_property,
591 .atomic_get_property = omap_crtc_atomic_get_property,
Tomi Valkeinen03961622017-02-08 13:26:00 +0200592 .enable_vblank = omap_irq_enable_vblank,
593 .disable_vblank = omap_irq_disable_vblank,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600594};
595
596static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200597 .mode_set_nofb = omap_crtc_mode_set_nofb,
Jyri Sarha492a4262016-06-07 15:09:17 +0300598 .atomic_check = omap_crtc_atomic_check,
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200599 .atomic_begin = omap_crtc_atomic_begin,
600 .atomic_flush = omap_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300601 .atomic_enable = omap_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300602 .atomic_disable = omap_crtc_atomic_disable,
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200603 .mode_valid = omap_crtc_mode_valid,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600604};
605
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200606/* -----------------------------------------------------------------------------
607 * Init and Cleanup
608 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300609
Rob Clarkf5f94542012-12-04 13:59:12 -0600610static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200611 [OMAP_DSS_CHANNEL_LCD] = "lcd",
612 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
613 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
614 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600615};
616
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200617void omap_crtc_pre_init(struct omap_drm_private *priv)
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300618{
Laurent Pinchart845417b2018-03-02 03:05:10 +0200619 dss_install_mgr_ops(priv->dss, &mgr_ops, priv);
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300620}
621
Laurent Pinchart845417b2018-03-02 03:05:10 +0200622void omap_crtc_pre_uninit(struct omap_drm_private *priv)
Archit Taneja3a01ab22014-01-02 14:49:51 +0530623{
Laurent Pinchart845417b2018-03-02 03:05:10 +0200624 dss_uninstall_mgr_ops(priv->dss);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530625}
626
Rob Clarkcd5351f2011-11-12 12:09:40 -0600627/* initialize crtc */
628struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200629 struct omap_drm_pipeline *pipe,
630 struct drm_plane *plane)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600631{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200632 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600633 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600634 struct omap_crtc *omap_crtc;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200635 enum omap_channel channel;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200636 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600637
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200638 channel = pipe->output->dispc_channel;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200639
Rob Clarkf5f94542012-12-04 13:59:12 -0600640 DBG("%s", channel_names[channel]);
641
642 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800643 if (!omap_crtc)
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200644 return ERR_PTR(-ENOMEM);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600645
Rob Clarkcd5351f2011-11-12 12:09:40 -0600646 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600647
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300648 init_waitqueue_head(&omap_crtc->pending_wait);
Rob Clarkf5f94542012-12-04 13:59:12 -0600649
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +0200650 omap_crtc->pipe = pipe;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530651 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530652 omap_crtc->name = channel_names[channel];
Archit Taneja0d8f3712013-03-26 19:15:19 +0530653
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200654 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200655 &omap_crtc_funcs, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200656 if (ret < 0) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200657 dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
Laurent Pinchart79107f22018-09-23 12:58:15 +0300658 __func__, pipe->output->name);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200659 kfree(omap_crtc);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200660 return ERR_PTR(ret);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200661 }
662
Rob Clarkcd5351f2011-11-12 12:09:40 -0600663 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
664
Jyri Sarha492a4262016-06-07 15:09:17 +0300665 /* The dispc API adapts to what ever size, but the HW supports
666 * 256 element gamma table for LCDs and 1024 element table for
667 * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
668 * tables so lets use that. Size of HW gamma table can be
669 * extracted with dispc_mgr_gamma_size(). If it returns 0
670 * gamma table is not supprted.
671 */
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200672 if (priv->dispc_ops->mgr_gamma_size(priv->dispc, channel)) {
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200673 unsigned int gamma_lut_size = 256;
Jyri Sarha492a4262016-06-07 15:09:17 +0300674
675 drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
676 drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
677 }
678
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200679 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500680
Rob Clarkcd5351f2011-11-12 12:09:40 -0600681 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600682}