blob: 40acf4ce7c9f3a59f6e79971d25d11c192aa0a10 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 * Author: Rob Clark <rob@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Laurent Pinchart69a12262015-03-05 21:38:16 +020018#include <drm/drm_atomic.h>
19#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020020#include <drm/drm_crtc.h>
21#include <drm/drm_crtc_helper.h>
Andy Grossb9ed9f02012-10-16 00:17:40 -050022#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010023#include <drm/drm_plane_helper.h>
Peter Ujfalusia7631c42017-11-30 14:12:37 +020024#include <linux/math64.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020025
26#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +020028#define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
29
30struct omap_crtc_state {
31 /* Must be first. */
32 struct drm_crtc_state base;
33 /* Shadow values for legacy userspace support. */
34 unsigned int rotation;
35 unsigned int zpos;
36};
37
Rob Clarkcd5351f2011-11-12 12:09:40 -060038#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
39
40struct omap_crtc {
41 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060042
Rob Clarkbb5c2d92012-01-16 12:51:16 -060043 const char *name;
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +020044 struct omap_drm_pipeline *pipe;
Rob Clarkf5f94542012-12-04 13:59:12 -060045 enum omap_channel channel;
Rob Clarkf5f94542012-12-04 13:59:12 -060046
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030047 struct videomode vm;
Rob Clarkf5f94542012-12-04 13:59:12 -060048
Tomi Valkeinena36af732015-02-26 15:20:24 +020049 bool ignore_digit_sync_lost;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030050
Laurent Pinchartf933a3a2016-04-18 02:54:31 +030051 bool enabled;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030052 bool pending;
53 wait_queue_head_t pending_wait;
Laurent Pinchart577d3982016-04-19 01:15:11 +030054 struct drm_pending_vblank_event *event;
Rob Clarkcd5351f2011-11-12 12:09:40 -060055};
56
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020057/* -----------------------------------------------------------------------------
58 * Helper Functions
59 */
60
Peter Ujfalusi4520ff22016-09-22 14:07:03 +030061struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020062{
63 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030064 return &omap_crtc->vm;
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020065}
66
67enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
68{
69 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
70 return omap_crtc->channel;
71}
72
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030073static bool omap_crtc_is_pending(struct drm_crtc *crtc)
74{
75 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
76 unsigned long flags;
77 bool pending;
78
79 spin_lock_irqsave(&crtc->dev->event_lock, flags);
80 pending = omap_crtc->pending;
81 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
82
83 return pending;
84}
85
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030086int omap_crtc_wait_pending(struct drm_crtc *crtc)
87{
88 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
89
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020090 /*
91 * Timeout is set to a "sufficiently" high value, which should cover
92 * a single frame refresh even on slower displays.
93 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030094 return wait_event_timeout(omap_crtc->pending_wait,
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030095 !omap_crtc_is_pending(crtc),
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020096 msecs_to_jiffies(250));
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030097}
98
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020099/* -----------------------------------------------------------------------------
100 * DSS Manager Functions
101 */
102
Rob Clarkf5f94542012-12-04 13:59:12 -0600103/*
104 * Manager-ops, callbacks from output when they need to configure
105 * the upstream part of the video pipe.
106 *
107 * Most of these we can ignore until we add support for command-mode
108 * panels.. for video-mode the crtc-helpers already do an adequate
109 * job of sequencing the setup of the video pipe in the proper order
110 */
111
112/* we can probably ignore these until we support command-mode panels: */
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200113static void omap_crtc_dss_start_update(struct omap_drm_private *priv,
114 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600115{
116}
117
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300118/* Called only from the encoder enable/disable and suspend/resume handlers. */
Laurent Pinchart8472b572015-01-15 00:45:17 +0200119static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
120{
121 struct drm_device *dev = crtc->dev;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200122 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200123 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
124 enum omap_channel channel = omap_crtc->channel;
125 struct omap_irq_wait *wait;
126 u32 framedone_irq, vsync_irq;
127 int ret;
128
Laurent Pinchart03af8152016-04-18 03:09:48 +0300129 if (WARN_ON(omap_crtc->enabled == enable))
130 return;
131
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +0200132 if (omap_crtc->pipe->output->output_type == OMAP_DISPLAY_TYPE_HDMI) {
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200133 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300134 omap_crtc->enabled = enable;
Tomi Valkeinen4e4b53c2015-03-24 15:46:35 +0200135 return;
136 }
137
Tomi Valkeinenef422282015-02-26 15:20:25 +0200138 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
139 /*
140 * Digit output produces some sync lost interrupts during the
141 * first frame when enabling, so we need to ignore those.
142 */
143 omap_crtc->ignore_digit_sync_lost = true;
144 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200145
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200146 framedone_irq = priv->dispc_ops->mgr_get_framedone_irq(priv->dispc,
147 channel);
148 vsync_irq = priv->dispc_ops->mgr_get_vsync_irq(priv->dispc, channel);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200149
150 if (enable) {
151 wait = omap_irq_wait_init(dev, vsync_irq, 1);
152 } else {
153 /*
154 * When we disable the digit output, we need to wait for
155 * FRAMEDONE to know that DISPC has finished with the output.
156 *
157 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
158 * that case we need to use vsync interrupt, and wait for both
159 * even and odd frames.
160 */
161
162 if (framedone_irq)
163 wait = omap_irq_wait_init(dev, framedone_irq, 1);
164 else
165 wait = omap_irq_wait_init(dev, vsync_irq, 2);
166 }
167
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200168 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300169 omap_crtc->enabled = enable;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200170
171 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
172 if (ret) {
173 dev_err(dev->dev, "%s: timeout waiting for %s\n",
174 omap_crtc->name, enable ? "enable" : "disable");
175 }
176
Tomi Valkeinenef422282015-02-26 15:20:25 +0200177 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
178 omap_crtc->ignore_digit_sync_lost = false;
179 /* make sure the irq handler sees the value above */
180 mb();
181 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200182}
183
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300184
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200185static int omap_crtc_dss_enable(struct omap_drm_private *priv,
186 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600187{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200188 struct drm_crtc *crtc = priv->channels[channel]->crtc;
189 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300190
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200191 priv->dispc_ops->mgr_set_timings(priv->dispc, omap_crtc->channel,
192 &omap_crtc->vm);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200193 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300194
Rob Clarkf5f94542012-12-04 13:59:12 -0600195 return 0;
196}
197
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200198static void omap_crtc_dss_disable(struct omap_drm_private *priv,
199 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600200{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200201 struct drm_crtc *crtc = priv->channels[channel]->crtc;
202 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300203
Laurent Pinchart8472b572015-01-15 00:45:17 +0200204 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600205}
206
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200207static void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
208 enum omap_channel channel,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300209 const struct videomode *vm)
Rob Clarkf5f94542012-12-04 13:59:12 -0600210{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200211 struct drm_crtc *crtc = priv->channels[channel]->crtc;
212 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
213
Rob Clarkf5f94542012-12-04 13:59:12 -0600214 DBG("%s", omap_crtc->name);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300215 omap_crtc->vm = *vm;
Rob Clarkf5f94542012-12-04 13:59:12 -0600216}
217
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200218static void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
219 enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600220 const struct dss_lcd_mgr_config *config)
221{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200222 struct drm_crtc *crtc = priv->channels[channel]->crtc;
223 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200224
Rob Clarkf5f94542012-12-04 13:59:12 -0600225 DBG("%s", omap_crtc->name);
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200226 priv->dispc_ops->mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
227 config);
Rob Clarkf5f94542012-12-04 13:59:12 -0600228}
229
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200230static int omap_crtc_dss_register_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200231 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600232 void (*handler)(void *), void *data)
233{
234 return 0;
235}
236
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200237static void omap_crtc_dss_unregister_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200238 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600239 void (*handler)(void *), void *data)
240{
241}
242
243static const struct dss_mgr_ops mgr_ops = {
Laurent Pinchart4343f0f2015-03-05 22:01:02 +0200244 .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 Pinchartdfe9cfc2018-02-11 15:07:33 +0200257void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200258{
Laurent Pincharte0519af2015-05-28 00:21:29 +0300259 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200260
261 if (omap_crtc->ignore_digit_sync_lost) {
262 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
263 if (!irqstatus)
264 return;
265 }
266
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200267 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200268}
269
Laurent Pinchart14389a32016-04-19 01:43:03 +0300270void omap_crtc_vblank_irq(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200271{
Laurent Pinchart14389a32016-04-19 01:43:03 +0300272 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200273 struct drm_device *dev = omap_crtc->base.dev;
274 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart14389a32016-04-19 01:43:03 +0300275 bool pending;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200276
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300277 spin_lock(&crtc->dev->event_lock);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300278 /*
279 * If the dispc is busy we're racing the flush operation. Try again on
280 * the next vblank interrupt.
281 */
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200282 if (priv->dispc_ops->mgr_go_busy(priv->dispc, omap_crtc->channel)) {
Laurent Pinchart14389a32016-04-19 01:43:03 +0300283 spin_unlock(&crtc->dev->event_lock);
284 return;
285 }
286
287 /* Send the vblank event if one has been requested. */
288 if (omap_crtc->event) {
289 drm_crtc_send_vblank_event(crtc, omap_crtc->event);
290 omap_crtc->event = NULL;
291 }
292
293 pending = omap_crtc->pending;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300294 omap_crtc->pending = false;
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300295 spin_unlock(&crtc->dev->event_lock);
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300296
Laurent Pinchart14389a32016-04-19 01:43:03 +0300297 if (pending)
298 drm_crtc_vblank_put(crtc);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200299
Laurent Pinchart14389a32016-04-19 01:43:03 +0300300 /* Wake up omap_atomic_complete. */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300301 wake_up(&omap_crtc->pending_wait);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300302
303 DBG("%s: apply done", omap_crtc->name);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200304}
305
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300306static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
307{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200308 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300309 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
310 struct omap_overlay_manager_info info;
311
312 memset(&info, 0, sizeof(info));
313
314 info.default_color = 0x000000;
315 info.trans_enabled = false;
316 info.partial_alpha_enabled = false;
317 info.cpr_enable = false;
318
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200319 priv->dispc_ops->mgr_setup(priv->dispc, omap_crtc->channel, &info);
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300320}
321
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200322/* -----------------------------------------------------------------------------
323 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600324 */
325
Rob Clarkcd5351f2011-11-12 12:09:40 -0600326static void omap_crtc_destroy(struct drm_crtc *crtc)
327{
328 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600329
330 DBG("%s", omap_crtc->name);
331
Rob Clarkcd5351f2011-11-12 12:09:40 -0600332 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600333
Rob Clarkcd5351f2011-11-12 12:09:40 -0600334 kfree(omap_crtc);
335}
336
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300337static void omap_crtc_arm_event(struct drm_crtc *crtc)
338{
339 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
340
341 WARN_ON(omap_crtc->pending);
342 omap_crtc->pending = true;
343
344 if (crtc->state->event) {
345 omap_crtc->event = crtc->state->event;
346 crtc->state->event = NULL;
347 }
348}
349
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300350static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
351 struct drm_crtc_state *old_state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200352{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200353 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200354 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300355 int ret;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200356
357 DBG("%s", omap_crtc->name);
358
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200359 priv->dispc_ops->runtime_get(priv->dispc);
360
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300361 spin_lock_irq(&crtc->dev->event_lock);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300362 drm_crtc_vblank_on(crtc);
363 ret = drm_crtc_vblank_get(crtc);
364 WARN_ON(ret != 0);
365
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300366 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300367 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200368}
369
Laurent Pinchart64581712017-06-30 12:36:45 +0300370static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
371 struct drm_crtc_state *old_state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200372{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200373 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200374 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200375
376 DBG("%s", omap_crtc->name);
377
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300378 spin_lock_irq(&crtc->dev->event_lock);
379 if (crtc->state->event) {
380 drm_crtc_send_vblank_event(crtc, crtc->state->event);
381 crtc->state->event = NULL;
382 }
383 spin_unlock_irq(&crtc->dev->event_lock);
384
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200385 drm_crtc_vblank_off(crtc);
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200386
387 priv->dispc_ops->runtime_put(priv->dispc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200388}
389
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200390static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
391 const struct drm_display_mode *mode)
392{
393 struct omap_drm_private *priv = crtc->dev->dev_private;
394
395 /* Check for bandwidth limit */
396 if (priv->max_bandwidth) {
397 /*
398 * Estimation for the bandwidth need of a given mode with one
399 * full screen plane:
400 * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
401 * ^^ Refresh rate ^^
402 *
403 * The interlaced mode is taken into account by using the
404 * pixelclock in the calculation.
405 *
406 * The equation is rearranged for 64bit arithmetic.
407 */
408 uint64_t bandwidth = mode->clock * 1000;
409 unsigned int bpp = 4;
410
411 bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
412 bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
413
414 /*
415 * Reject modes which would need more bandwidth if used with one
416 * full resolution plane (most common use case).
417 */
418 if (priv->max_bandwidth < bandwidth)
419 return MODE_BAD;
420 }
421
422 return MODE_OK;
423}
424
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200425static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600426{
427 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200428 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Rob Clarkf5f94542012-12-04 13:59:12 -0600429
Shayenne Mourac39ff7e2018-12-20 10:26:10 -0200430 DBG("%s: set mode: " DRM_MODE_FMT,
431 omap_crtc->name, DRM_MODE_ARG(mode));
Rob Clarkf5f94542012-12-04 13:59:12 -0600432
Laurent Pinchart8e9c1c62018-06-07 18:32:16 +0300433 drm_display_mode_to_videomode(mode, &omap_crtc->vm);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600434}
435
Jyri Sarha492a4262016-06-07 15:09:17 +0300436static int omap_crtc_atomic_check(struct drm_crtc *crtc,
437 struct drm_crtc_state *state)
438{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200439 struct drm_plane_state *pri_state;
440
Jyri Sarha492a4262016-06-07 15:09:17 +0300441 if (state->color_mgmt_changed && state->gamma_lut) {
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200442 unsigned int length = state->gamma_lut->length /
Jyri Sarha492a4262016-06-07 15:09:17 +0300443 sizeof(struct drm_color_lut);
444
445 if (length < 2)
446 return -EINVAL;
447 }
448
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200449 pri_state = drm_atomic_get_new_plane_state(state->state, crtc->primary);
450 if (pri_state) {
451 struct omap_crtc_state *omap_crtc_state =
452 to_omap_crtc_state(state);
453
454 /* Mirror new values for zpos and rotation in omap_crtc_state */
455 omap_crtc_state->zpos = pri_state->zpos;
456 omap_crtc_state->rotation = pri_state->rotation;
457 }
458
Jyri Sarha492a4262016-06-07 15:09:17 +0300459 return 0;
460}
461
Daniel Vetterc201d002015-08-06 14:09:35 +0200462static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
Laurent Pinchart577d3982016-04-19 01:15:11 +0300463 struct drm_crtc_state *old_crtc_state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200464{
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200465}
466
Daniel Vetterc201d002015-08-06 14:09:35 +0200467static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
Laurent Pinchart577d3982016-04-19 01:15:11 +0300468 struct drm_crtc_state *old_crtc_state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200469{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200470 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300471 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300472 int ret;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300473
Jyri Sarha492a4262016-06-07 15:09:17 +0300474 if (crtc->state->color_mgmt_changed) {
475 struct drm_color_lut *lut = NULL;
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200476 unsigned int length = 0;
Jyri Sarha492a4262016-06-07 15:09:17 +0300477
478 if (crtc->state->gamma_lut) {
479 lut = (struct drm_color_lut *)
480 crtc->state->gamma_lut->data;
481 length = crtc->state->gamma_lut->length /
482 sizeof(*lut);
483 }
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200484 priv->dispc_ops->mgr_set_gamma(priv->dispc, omap_crtc->channel,
485 lut, length);
Jyri Sarha492a4262016-06-07 15:09:17 +0300486 }
487
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300488 omap_crtc_write_crtc_properties(crtc);
489
Jyri Sarhae025d382017-01-27 12:04:54 +0200490 /* Only flush the CRTC if it is currently enabled. */
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300491 if (!omap_crtc->enabled)
492 return;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300493
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300494 DBG("%s: GO", omap_crtc->name);
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300495
Laurent Pinchart14389a32016-04-19 01:43:03 +0300496 ret = drm_crtc_vblank_get(crtc);
497 WARN_ON(ret != 0);
498
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300499 spin_lock_irq(&crtc->dev->event_lock);
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200500 priv->dispc_ops->mgr_go(priv->dispc, omap_crtc->channel);
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300501 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300502 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200503}
504
Laurent Pinchartafc34932015-03-06 18:35:16 +0200505static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
506 struct drm_crtc_state *state,
507 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200508 u64 val)
Rob Clark3c810c62012-08-15 15:18:01 -0500509{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200510 struct omap_drm_private *priv = crtc->dev->dev_private;
511 struct drm_plane_state *plane_state;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200512
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200513 /*
514 * Delegate property set to the primary plane. Get the plane state and
515 * set the property directly, the shadow copy will be assigned in the
516 * omap_crtc_atomic_check callback. This way updates to plane state will
517 * always be mirrored in the crtc state correctly.
518 */
519 plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
520 if (IS_ERR(plane_state))
521 return PTR_ERR(plane_state);
Laurent Pinchartafc34932015-03-06 18:35:16 +0200522
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200523 if (property == crtc->primary->rotation_property)
524 plane_state->rotation = val;
525 else if (property == priv->zorder_prop)
526 plane_state->zpos = val;
527 else
528 return -EINVAL;
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200529
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200530 return 0;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200531}
532
533static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
534 const struct drm_crtc_state *state,
535 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200536 u64 *val)
Laurent Pinchartafc34932015-03-06 18:35:16 +0200537{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200538 struct omap_drm_private *priv = crtc->dev->dev_private;
539 struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200540
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200541 if (property == crtc->primary->rotation_property)
542 *val = omap_state->rotation;
543 else if (property == priv->zorder_prop)
544 *val = omap_state->zpos;
545 else
546 return -EINVAL;
547
548 return 0;
549}
550
551static void omap_crtc_reset(struct drm_crtc *crtc)
552{
553 if (crtc->state)
554 __drm_atomic_helper_crtc_destroy_state(crtc->state);
555
556 kfree(crtc->state);
557 crtc->state = kzalloc(sizeof(struct omap_crtc_state), GFP_KERNEL);
558
559 if (crtc->state)
560 crtc->state->crtc = crtc;
561}
562
563static struct drm_crtc_state *
564omap_crtc_duplicate_state(struct drm_crtc *crtc)
565{
566 struct omap_crtc_state *state, *current_state;
567
568 if (WARN_ON(!crtc->state))
569 return NULL;
570
571 current_state = to_omap_crtc_state(crtc->state);
572
573 state = kmalloc(sizeof(*state), GFP_KERNEL);
Dan Carpenter24196722017-08-11 23:16:06 +0300574 if (!state)
575 return NULL;
576
577 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200578
579 state->zpos = current_state->zpos;
580 state->rotation = current_state->rotation;
581
582 return &state->base;
Rob Clark3c810c62012-08-15 15:18:01 -0500583}
584
Rob Clarkcd5351f2011-11-12 12:09:40 -0600585static const struct drm_crtc_funcs omap_crtc_funcs = {
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200586 .reset = omap_crtc_reset,
Laurent Pinchart9416c9d2015-03-05 21:54:54 +0200587 .set_config = drm_atomic_helper_set_config,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600588 .destroy = omap_crtc_destroy,
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200589 .page_flip = drm_atomic_helper_page_flip,
Jyri Sarha492a4262016-06-07 15:09:17 +0300590 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200591 .atomic_duplicate_state = omap_crtc_duplicate_state,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200592 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200593 .atomic_set_property = omap_crtc_atomic_set_property,
594 .atomic_get_property = omap_crtc_atomic_get_property,
Tomi Valkeinen03961622017-02-08 13:26:00 +0200595 .enable_vblank = omap_irq_enable_vblank,
596 .disable_vblank = omap_irq_disable_vblank,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600597};
598
599static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200600 .mode_set_nofb = omap_crtc_mode_set_nofb,
Jyri Sarha492a4262016-06-07 15:09:17 +0300601 .atomic_check = omap_crtc_atomic_check,
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200602 .atomic_begin = omap_crtc_atomic_begin,
603 .atomic_flush = omap_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300604 .atomic_enable = omap_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300605 .atomic_disable = omap_crtc_atomic_disable,
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200606 .mode_valid = omap_crtc_mode_valid,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600607};
608
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200609/* -----------------------------------------------------------------------------
610 * Init and Cleanup
611 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300612
Rob Clarkf5f94542012-12-04 13:59:12 -0600613static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200614 [OMAP_DSS_CHANNEL_LCD] = "lcd",
615 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
616 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
617 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600618};
619
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200620void omap_crtc_pre_init(struct omap_drm_private *priv)
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300621{
Laurent Pinchart845417b2018-03-02 03:05:10 +0200622 dss_install_mgr_ops(priv->dss, &mgr_ops, priv);
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300623}
624
Laurent Pinchart845417b2018-03-02 03:05:10 +0200625void omap_crtc_pre_uninit(struct omap_drm_private *priv)
Archit Taneja3a01ab22014-01-02 14:49:51 +0530626{
Laurent Pinchart845417b2018-03-02 03:05:10 +0200627 dss_uninstall_mgr_ops(priv->dss);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530628}
629
Rob Clarkcd5351f2011-11-12 12:09:40 -0600630/* initialize crtc */
631struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200632 struct omap_drm_pipeline *pipe,
633 struct drm_plane *plane)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600634{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200635 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600636 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600637 struct omap_crtc *omap_crtc;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200638 enum omap_channel channel;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200639 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600640
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200641 channel = pipe->output->dispc_channel;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200642
Rob Clarkf5f94542012-12-04 13:59:12 -0600643 DBG("%s", channel_names[channel]);
644
645 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800646 if (!omap_crtc)
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200647 return ERR_PTR(-ENOMEM);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600648
Rob Clarkcd5351f2011-11-12 12:09:40 -0600649 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600650
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300651 init_waitqueue_head(&omap_crtc->pending_wait);
Rob Clarkf5f94542012-12-04 13:59:12 -0600652
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +0200653 omap_crtc->pipe = pipe;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530654 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530655 omap_crtc->name = channel_names[channel];
Archit Taneja0d8f3712013-03-26 19:15:19 +0530656
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200657 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200658 &omap_crtc_funcs, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200659 if (ret < 0) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200660 dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200661 __func__, pipe->display->name);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200662 kfree(omap_crtc);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200663 return ERR_PTR(ret);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200664 }
665
Rob Clarkcd5351f2011-11-12 12:09:40 -0600666 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
667
Jyri Sarha492a4262016-06-07 15:09:17 +0300668 /* The dispc API adapts to what ever size, but the HW supports
669 * 256 element gamma table for LCDs and 1024 element table for
670 * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
671 * tables so lets use that. Size of HW gamma table can be
672 * extracted with dispc_mgr_gamma_size(). If it returns 0
673 * gamma table is not supprted.
674 */
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200675 if (priv->dispc_ops->mgr_gamma_size(priv->dispc, channel)) {
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200676 unsigned int gamma_lut_size = 256;
Jyri Sarha492a4262016-06-07 15:09:17 +0300677
678 drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
679 drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
680 }
681
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200682 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500683
Rob Clarkcd5351f2011-11-12 12:09:40 -0600684 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600685}