blob: 06a719c104f4e492a7b58f7b71bf7e09e0183479 [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/*
Alexander A. Klimov1b409fd2020-07-13 14:28:59 +02003 * Copyright (C) 2011 Texas Instruments Incorporated - https://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
Sam Ravnborg81f61562019-07-16 08:42:10 +02007#include <linux/math64.h>
8
Laurent Pinchart69a12262015-03-05 21:38:16 +02009#include <drm/drm_atomic.h>
10#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020011#include <drm/drm_crtc.h>
Andy Grossb9ed9f02012-10-16 00:17:40 -050012#include <drm/drm_mode.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010013#include <drm/drm_plane_helper.h>
Sam Ravnborg81f61562019-07-16 08:42:10 +020014#include <drm/drm_vblank.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020015
16#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060017
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +020018#define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
19
20struct omap_crtc_state {
21 /* Must be first. */
22 struct drm_crtc_state base;
23 /* Shadow values for legacy userspace support. */
24 unsigned int rotation;
25 unsigned int zpos;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +020026 bool manually_updated;
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +020027};
28
Rob Clarkcd5351f2011-11-12 12:09:40 -060029#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
30
31struct omap_crtc {
32 struct drm_crtc base;
Rob Clarkf5f94542012-12-04 13:59:12 -060033
Rob Clarkbb5c2d92012-01-16 12:51:16 -060034 const char *name;
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +020035 struct omap_drm_pipeline *pipe;
Rob Clarkf5f94542012-12-04 13:59:12 -060036 enum omap_channel channel;
Rob Clarkf5f94542012-12-04 13:59:12 -060037
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030038 struct videomode vm;
Rob Clarkf5f94542012-12-04 13:59:12 -060039
Tomi Valkeinena36af732015-02-26 15:20:24 +020040 bool ignore_digit_sync_lost;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030041
Laurent Pinchartf933a3a2016-04-18 02:54:31 +030042 bool enabled;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030043 bool pending;
44 wait_queue_head_t pending_wait;
Laurent Pinchart577d3982016-04-19 01:15:11 +030045 struct drm_pending_vblank_event *event;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +020046 struct delayed_work update_work;
Sebastian Reichel47103a82019-05-23 22:07:55 +020047
48 void (*framedone_handler)(void *);
49 void *framedone_handler_data;
Rob Clarkcd5351f2011-11-12 12:09:40 -060050};
51
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020052/* -----------------------------------------------------------------------------
53 * Helper Functions
54 */
55
Peter Ujfalusi4520ff22016-09-22 14:07:03 +030056struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020057{
58 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030059 return &omap_crtc->vm;
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020060}
61
62enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
63{
64 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
65 return omap_crtc->channel;
66}
67
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030068static bool omap_crtc_is_pending(struct drm_crtc *crtc)
69{
70 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
71 unsigned long flags;
72 bool pending;
73
74 spin_lock_irqsave(&crtc->dev->event_lock, flags);
75 pending = omap_crtc->pending;
76 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
77
78 return pending;
79}
80
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030081int omap_crtc_wait_pending(struct drm_crtc *crtc)
82{
83 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
84
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020085 /*
86 * Timeout is set to a "sufficiently" high value, which should cover
87 * a single frame refresh even on slower displays.
88 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030089 return wait_event_timeout(omap_crtc->pending_wait,
Laurent Pinchartd173d3d2016-04-19 01:31:21 +030090 !omap_crtc_is_pending(crtc),
Tomi Valkeinen61f3c402015-11-19 17:31:25 +020091 msecs_to_jiffies(250));
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030092}
93
Laurent Pinchart971fb3e2015-01-18 01:12:59 +020094/* -----------------------------------------------------------------------------
95 * DSS Manager Functions
96 */
97
Rob Clarkf5f94542012-12-04 13:59:12 -060098/*
99 * Manager-ops, callbacks from output when they need to configure
100 * the upstream part of the video pipe.
Rob Clarkf5f94542012-12-04 13:59:12 -0600101 */
102
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200103void omap_crtc_dss_start_update(struct omap_drm_private *priv,
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200104 enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600105{
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200106 dispc_mgr_enable(priv->dispc, channel, true);
Rob Clarkf5f94542012-12-04 13:59:12 -0600107}
108
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300109/* Called only from the encoder enable/disable and suspend/resume handlers. */
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200110void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
Laurent Pinchart8472b572015-01-15 00:45:17 +0200111{
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200112 struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200113 struct drm_device *dev = crtc->dev;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200114 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200115 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
116 enum omap_channel channel = omap_crtc->channel;
117 struct omap_irq_wait *wait;
118 u32 framedone_irq, vsync_irq;
119 int ret;
120
Laurent Pinchart03af8152016-04-18 03:09:48 +0300121 if (WARN_ON(omap_crtc->enabled == enable))
122 return;
123
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200124 if (omap_state->manually_updated) {
125 omap_irq_enable_framedone(crtc, enable);
126 omap_crtc->enabled = enable;
127 return;
128 }
129
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200130 if (omap_crtc->pipe->output->type == OMAP_DISPLAY_TYPE_HDMI) {
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200131 dispc_mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300132 omap_crtc->enabled = enable;
Tomi Valkeinen4e4b53c2015-03-24 15:46:35 +0200133 return;
134 }
135
Tomi Valkeinenef422282015-02-26 15:20:25 +0200136 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
137 /*
138 * Digit output produces some sync lost interrupts during the
139 * first frame when enabling, so we need to ignore those.
140 */
141 omap_crtc->ignore_digit_sync_lost = true;
142 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200143
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200144 framedone_irq = dispc_mgr_get_framedone_irq(priv->dispc,
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200145 channel);
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200146 vsync_irq = dispc_mgr_get_vsync_irq(priv->dispc, channel);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200147
148 if (enable) {
149 wait = omap_irq_wait_init(dev, vsync_irq, 1);
150 } else {
151 /*
152 * When we disable the digit output, we need to wait for
153 * FRAMEDONE to know that DISPC has finished with the output.
154 *
155 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
156 * that case we need to use vsync interrupt, and wait for both
157 * even and odd frames.
158 */
159
160 if (framedone_irq)
161 wait = omap_irq_wait_init(dev, framedone_irq, 1);
162 else
163 wait = omap_irq_wait_init(dev, vsync_irq, 2);
164 }
165
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200166 dispc_mgr_enable(priv->dispc, channel, enable);
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300167 omap_crtc->enabled = enable;
Laurent Pinchart8472b572015-01-15 00:45:17 +0200168
169 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
170 if (ret) {
171 dev_err(dev->dev, "%s: timeout waiting for %s\n",
172 omap_crtc->name, enable ? "enable" : "disable");
173 }
174
Tomi Valkeinenef422282015-02-26 15:20:25 +0200175 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
176 omap_crtc->ignore_digit_sync_lost = false;
177 /* make sure the irq handler sees the value above */
178 mb();
179 }
Laurent Pinchart8472b572015-01-15 00:45:17 +0200180}
181
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300182
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200183int omap_crtc_dss_enable(struct omap_drm_private *priv, enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600184{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200185 struct drm_crtc *crtc = priv->channels[channel]->crtc;
186 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300187
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200188 dispc_mgr_set_timings(priv->dispc, omap_crtc->channel,
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200189 &omap_crtc->vm);
Laurent Pinchart8472b572015-01-15 00:45:17 +0200190 omap_crtc_set_enabled(&omap_crtc->base, true);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300191
Rob Clarkf5f94542012-12-04 13:59:12 -0600192 return 0;
193}
194
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200195void omap_crtc_dss_disable(struct omap_drm_private *priv, enum omap_channel channel)
Rob Clarkf5f94542012-12-04 13:59:12 -0600196{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200197 struct drm_crtc *crtc = priv->channels[channel]->crtc;
198 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen506096a2014-04-03 13:11:54 +0300199
Laurent Pinchart8472b572015-01-15 00:45:17 +0200200 omap_crtc_set_enabled(&omap_crtc->base, false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600201}
202
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200203void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200204 enum omap_channel channel,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300205 const struct videomode *vm)
Rob Clarkf5f94542012-12-04 13:59:12 -0600206{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200207 struct drm_crtc *crtc = priv->channels[channel]->crtc;
208 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
209
Rob Clarkf5f94542012-12-04 13:59:12 -0600210 DBG("%s", omap_crtc->name);
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300211 omap_crtc->vm = *vm;
Rob Clarkf5f94542012-12-04 13:59:12 -0600212}
213
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200214void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200215 enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600216 const struct dss_lcd_mgr_config *config)
217{
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200218 struct drm_crtc *crtc = priv->channels[channel]->crtc;
219 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200220
Rob Clarkf5f94542012-12-04 13:59:12 -0600221 DBG("%s", omap_crtc->name);
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200222 dispc_mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200223 config);
Rob Clarkf5f94542012-12-04 13:59:12 -0600224}
225
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200226int omap_crtc_dss_register_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200227 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600228 void (*handler)(void *), void *data)
229{
Sebastian Reichel47103a82019-05-23 22:07:55 +0200230 struct drm_crtc *crtc = priv->channels[channel]->crtc;
231 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
232 struct drm_device *dev = omap_crtc->base.dev;
233
234 if (omap_crtc->framedone_handler)
235 return -EBUSY;
236
237 dev_dbg(dev->dev, "register framedone %s", omap_crtc->name);
238
239 omap_crtc->framedone_handler = handler;
240 omap_crtc->framedone_handler_data = data;
241
Rob Clarkf5f94542012-12-04 13:59:12 -0600242 return 0;
243}
244
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200245void omap_crtc_dss_unregister_framedone(
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200246 struct omap_drm_private *priv, enum omap_channel channel,
Rob Clarkf5f94542012-12-04 13:59:12 -0600247 void (*handler)(void *), void *data)
248{
Sebastian Reichel47103a82019-05-23 22:07:55 +0200249 struct drm_crtc *crtc = priv->channels[channel]->crtc;
250 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
251 struct drm_device *dev = omap_crtc->base.dev;
252
253 dev_dbg(dev->dev, "unregister framedone %s", omap_crtc->name);
254
255 WARN_ON(omap_crtc->framedone_handler != handler);
256 WARN_ON(omap_crtc->framedone_handler_data != data);
257
258 omap_crtc->framedone_handler = NULL;
259 omap_crtc->framedone_handler_data = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600260}
261
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200262/* -----------------------------------------------------------------------------
Laurent Pinchart1d5e5ea2015-01-18 16:57:36 +0200263 * Setup, Flush and Page Flip
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200264 */
265
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200266void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200267{
Laurent Pincharte0519af2015-05-28 00:21:29 +0300268 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinena36af732015-02-26 15:20:24 +0200269
270 if (omap_crtc->ignore_digit_sync_lost) {
271 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
272 if (!irqstatus)
273 return;
274 }
275
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200276 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200277}
278
Laurent Pinchart14389a32016-04-19 01:43:03 +0300279void omap_crtc_vblank_irq(struct drm_crtc *crtc)
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200280{
Laurent Pinchart14389a32016-04-19 01:43:03 +0300281 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200282 struct drm_device *dev = omap_crtc->base.dev;
283 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart14389a32016-04-19 01:43:03 +0300284 bool pending;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200285
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300286 spin_lock(&crtc->dev->event_lock);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300287 /*
288 * If the dispc is busy we're racing the flush operation. Try again on
289 * the next vblank interrupt.
290 */
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200291 if (dispc_mgr_go_busy(priv->dispc, omap_crtc->channel)) {
Laurent Pinchart14389a32016-04-19 01:43:03 +0300292 spin_unlock(&crtc->dev->event_lock);
293 return;
294 }
295
296 /* Send the vblank event if one has been requested. */
297 if (omap_crtc->event) {
298 drm_crtc_send_vblank_event(crtc, omap_crtc->event);
299 omap_crtc->event = NULL;
300 }
301
302 pending = omap_crtc->pending;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300303 omap_crtc->pending = false;
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300304 spin_unlock(&crtc->dev->event_lock);
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300305
Laurent Pinchart14389a32016-04-19 01:43:03 +0300306 if (pending)
307 drm_crtc_vblank_put(crtc);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200308
Laurent Pinchart14389a32016-04-19 01:43:03 +0300309 /* Wake up omap_atomic_complete. */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300310 wake_up(&omap_crtc->pending_wait);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300311
312 DBG("%s: apply done", omap_crtc->name);
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200313}
314
Sebastian Reichel47103a82019-05-23 22:07:55 +0200315void omap_crtc_framedone_irq(struct drm_crtc *crtc, uint32_t irqstatus)
316{
317 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
318
319 if (!omap_crtc->framedone_handler)
320 return;
321
322 omap_crtc->framedone_handler(omap_crtc->framedone_handler_data);
323
324 spin_lock(&crtc->dev->event_lock);
325 /* Send the vblank event if one has been requested. */
326 if (omap_crtc->event) {
327 drm_crtc_send_vblank_event(crtc, omap_crtc->event);
328 omap_crtc->event = NULL;
329 }
330 omap_crtc->pending = false;
331 spin_unlock(&crtc->dev->event_lock);
332
333 /* Wake up omap_atomic_complete. */
334 wake_up(&omap_crtc->pending_wait);
335}
336
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200337void omap_crtc_flush(struct drm_crtc *crtc)
338{
339 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
340 struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
341
342 if (!omap_state->manually_updated)
343 return;
344
345 if (!delayed_work_pending(&omap_crtc->update_work))
346 schedule_delayed_work(&omap_crtc->update_work, 0);
347}
348
349static void omap_crtc_manual_display_update(struct work_struct *data)
350{
351 struct omap_crtc *omap_crtc =
352 container_of(data, struct omap_crtc, update_work.work);
Sebastian Reichel2a4703c2020-12-15 12:46:02 +0200353 struct omap_dss_device *dssdev = omap_crtc->pipe->output;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200354 struct drm_device *dev = omap_crtc->base.dev;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200355 int ret;
356
Sebastian Reichel94d73322020-12-15 12:46:20 +0200357 if (!dssdev || !dssdev->dsi_ops || !dssdev->dsi_ops->update)
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200358 return;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200359
Sebastian Reichel94d73322020-12-15 12:46:20 +0200360 ret = dssdev->dsi_ops->update(dssdev);
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200361 if (ret < 0) {
362 spin_lock_irq(&dev->event_lock);
363 omap_crtc->pending = false;
364 spin_unlock_irq(&dev->event_lock);
365 wake_up(&omap_crtc->pending_wait);
366 }
367}
368
Jyri Sarhaf18f4392020-11-03 10:03:08 +0200369static s16 omap_crtc_s31_32_to_s2_8(s64 coef)
370{
371 u64 sign_bit = 1ULL << 63;
372 u64 cbits = (u64)coef;
373
374 s16 ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x1ff);
375
376 if (cbits & sign_bit)
377 ret = -ret;
378
379 return ret;
380}
381
382static void omap_crtc_cpr_coefs_from_ctm(const struct drm_color_ctm *ctm,
383 struct omap_dss_cpr_coefs *cpr)
384{
385 cpr->rr = omap_crtc_s31_32_to_s2_8(ctm->matrix[0]);
386 cpr->rg = omap_crtc_s31_32_to_s2_8(ctm->matrix[1]);
387 cpr->rb = omap_crtc_s31_32_to_s2_8(ctm->matrix[2]);
388 cpr->gr = omap_crtc_s31_32_to_s2_8(ctm->matrix[3]);
389 cpr->gg = omap_crtc_s31_32_to_s2_8(ctm->matrix[4]);
390 cpr->gb = omap_crtc_s31_32_to_s2_8(ctm->matrix[5]);
391 cpr->br = omap_crtc_s31_32_to_s2_8(ctm->matrix[6]);
392 cpr->bg = omap_crtc_s31_32_to_s2_8(ctm->matrix[7]);
393 cpr->bb = omap_crtc_s31_32_to_s2_8(ctm->matrix[8]);
394}
395
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300396static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
397{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200398 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300399 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
400 struct omap_overlay_manager_info info;
401
402 memset(&info, 0, sizeof(info));
403
404 info.default_color = 0x000000;
405 info.trans_enabled = false;
406 info.partial_alpha_enabled = false;
Jyri Sarhaf18f4392020-11-03 10:03:08 +0200407
408 if (crtc->state->ctm) {
409 struct drm_color_ctm *ctm = crtc->state->ctm->data;
410
411 info.cpr_enable = true;
412 omap_crtc_cpr_coefs_from_ctm(ctm, &info.cpr_coefs);
413 } else {
414 info.cpr_enable = false;
415 }
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300416
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200417 dispc_mgr_setup(priv->dispc, omap_crtc->channel, &info);
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300418}
419
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200420/* -----------------------------------------------------------------------------
421 * CRTC Functions
Rob Clarkf5f94542012-12-04 13:59:12 -0600422 */
423
Rob Clarkcd5351f2011-11-12 12:09:40 -0600424static void omap_crtc_destroy(struct drm_crtc *crtc)
425{
426 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600427
428 DBG("%s", omap_crtc->name);
429
Rob Clarkcd5351f2011-11-12 12:09:40 -0600430 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600431
Rob Clarkcd5351f2011-11-12 12:09:40 -0600432 kfree(omap_crtc);
433}
434
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300435static void omap_crtc_arm_event(struct drm_crtc *crtc)
436{
437 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
438
439 WARN_ON(omap_crtc->pending);
440 omap_crtc->pending = true;
441
442 if (crtc->state->event) {
443 omap_crtc->event = crtc->state->event;
444 crtc->state->event = NULL;
445 }
446}
447
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300448static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
Maxime Ripard351f9502020-10-08 14:44:08 +0200449 struct drm_atomic_state *state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200450{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200451 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200452 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200453 struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300454 int ret;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200455
456 DBG("%s", omap_crtc->name);
457
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200458 dispc_runtime_get(priv->dispc);
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200459
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200460 /* manual updated display will not trigger vsync irq */
461 if (omap_state->manually_updated)
462 return;
463
Laurent Pinchart14389a32016-04-19 01:43:03 +0300464 drm_crtc_vblank_on(crtc);
Tomi Valkeinen7fd5b252020-08-19 13:30:21 +0300465
Laurent Pinchart14389a32016-04-19 01:43:03 +0300466 ret = drm_crtc_vblank_get(crtc);
467 WARN_ON(ret != 0);
468
Tomi Valkeinen7fd5b252020-08-19 13:30:21 +0300469 spin_lock_irq(&crtc->dev->event_lock);
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300470 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300471 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200472}
473
Laurent Pinchart64581712017-06-30 12:36:45 +0300474static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
Maxime Ripard351f9502020-10-08 14:44:08 +0200475 struct drm_atomic_state *state)
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200476{
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200477 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200478 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200479 struct drm_device *dev = crtc->dev;
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200480
481 DBG("%s", omap_crtc->name);
482
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300483 spin_lock_irq(&crtc->dev->event_lock);
484 if (crtc->state->event) {
485 drm_crtc_send_vblank_event(crtc, crtc->state->event);
486 crtc->state->event = NULL;
487 }
488 spin_unlock_irq(&crtc->dev->event_lock);
489
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200490 cancel_delayed_work(&omap_crtc->update_work);
491
492 if (!omap_crtc_wait_pending(crtc))
493 dev_warn(dev->dev, "manual display update did not finish!");
494
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200495 drm_crtc_vblank_off(crtc);
Laurent Pinchart24ec84e2018-11-10 13:16:54 +0200496
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200497 dispc_runtime_put(priv->dispc);
Laurent Pinchartf1d57fb2015-03-05 22:13:22 +0200498}
499
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200500static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
501 const struct drm_display_mode *mode)
502{
503 struct omap_drm_private *priv = crtc->dev->dev_private;
Laurent Pinchart116c7722018-09-20 00:17:42 +0300504 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
505 struct videomode vm = {0};
506 int r;
507
508 drm_display_mode_to_videomode(mode, &vm);
Sebastian Reichelad9df7d2019-05-23 22:07:54 +0200509
510 /*
511 * DSI might not call this, since the supplied mode is not a
512 * valid DISPC mode. DSI will calculate and configure the
513 * proper DISPC mode later.
514 */
Sebastian Reichele4869b02020-12-15 12:46:04 +0200515 if (omap_crtc->pipe->output->type != OMAP_DISPLAY_TYPE_DSI) {
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200516 r = dispc_mgr_check_timings(priv->dispc,
Sebastian Reichelad9df7d2019-05-23 22:07:54 +0200517 omap_crtc->channel,
518 &vm);
519 if (r)
520 return r;
521 }
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200522
523 /* Check for bandwidth limit */
524 if (priv->max_bandwidth) {
525 /*
526 * Estimation for the bandwidth need of a given mode with one
527 * full screen plane:
528 * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
529 * ^^ Refresh rate ^^
530 *
531 * The interlaced mode is taken into account by using the
532 * pixelclock in the calculation.
533 *
534 * The equation is rearranged for 64bit arithmetic.
535 */
536 uint64_t bandwidth = mode->clock * 1000;
537 unsigned int bpp = 4;
538
539 bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
540 bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
541
542 /*
543 * Reject modes which would need more bandwidth if used with one
544 * full resolution plane (most common use case).
545 */
546 if (priv->max_bandwidth < bandwidth)
547 return MODE_BAD;
548 }
549
550 return MODE_OK;
551}
552
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200553static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600554{
555 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200556 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Rob Clarkf5f94542012-12-04 13:59:12 -0600557
Shayenne Mourac39ff7e2018-12-20 10:26:10 -0200558 DBG("%s: set mode: " DRM_MODE_FMT,
559 omap_crtc->name, DRM_MODE_ARG(mode));
Rob Clarkf5f94542012-12-04 13:59:12 -0600560
Laurent Pinchart8e9c1c62018-06-07 18:32:16 +0300561 drm_display_mode_to_videomode(mode, &omap_crtc->vm);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600562}
563
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200564static bool omap_crtc_is_manually_updated(struct drm_crtc *crtc)
565{
566 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Sebastian Reichele4869b02020-12-15 12:46:04 +0200567 struct omap_dss_device *dssdev = omap_crtc->pipe->output;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200568
Sebastian Reichel94d73322020-12-15 12:46:20 +0200569 if (!dssdev || !dssdev->dsi_ops || !dssdev->dsi_ops->is_video_mode)
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200570 return false;
571
Sebastian Reichel94d73322020-12-15 12:46:20 +0200572 if (dssdev->dsi_ops->is_video_mode(dssdev))
Sebastian Reichele4869b02020-12-15 12:46:04 +0200573 return false;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200574
Sebastian Reichele4869b02020-12-15 12:46:04 +0200575 DBG("detected manually updated display!");
576 return true;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200577}
578
Jyri Sarha492a4262016-06-07 15:09:17 +0300579static int omap_crtc_atomic_check(struct drm_crtc *crtc,
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100580 struct drm_atomic_state *state)
Jyri Sarha492a4262016-06-07 15:09:17 +0300581{
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100582 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
583 crtc);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200584 struct drm_plane_state *pri_state;
585
Tomi Valkeinen3fcd70c2020-11-03 10:03:07 +0200586 if (crtc_state->color_mgmt_changed && crtc_state->degamma_lut) {
587 unsigned int length = crtc_state->degamma_lut->length /
Jyri Sarha492a4262016-06-07 15:09:17 +0300588 sizeof(struct drm_color_lut);
589
590 if (length < 2)
591 return -EINVAL;
592 }
593
Maxime Ripardd74252b2020-11-02 14:38:34 +0100594 pri_state = drm_atomic_get_new_plane_state(state,
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100595 crtc->primary);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200596 if (pri_state) {
597 struct omap_crtc_state *omap_crtc_state =
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100598 to_omap_crtc_state(crtc_state);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200599
600 /* Mirror new values for zpos and rotation in omap_crtc_state */
601 omap_crtc_state->zpos = pri_state->zpos;
602 omap_crtc_state->rotation = pri_state->rotation;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200603
604 /* Check if this CRTC is for a manually updated display */
605 omap_crtc_state->manually_updated = omap_crtc_is_manually_updated(crtc);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200606 }
607
Jyri Sarha492a4262016-06-07 15:09:17 +0300608 return 0;
609}
610
Daniel Vetterc201d002015-08-06 14:09:35 +0200611static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +0100612 struct drm_atomic_state *state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200613{
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200614}
615
Daniel Vetterc201d002015-08-06 14:09:35 +0200616static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +0100617 struct drm_atomic_state *state)
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200618{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200619 struct omap_drm_private *priv = crtc->dev->dev_private;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300620 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200621 struct omap_crtc_state *omap_crtc_state = to_omap_crtc_state(crtc->state);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300622 int ret;
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300623
Jyri Sarha492a4262016-06-07 15:09:17 +0300624 if (crtc->state->color_mgmt_changed) {
625 struct drm_color_lut *lut = NULL;
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200626 unsigned int length = 0;
Jyri Sarha492a4262016-06-07 15:09:17 +0300627
Tomi Valkeinen3fcd70c2020-11-03 10:03:07 +0200628 if (crtc->state->degamma_lut) {
Jyri Sarha492a4262016-06-07 15:09:17 +0300629 lut = (struct drm_color_lut *)
Tomi Valkeinen3fcd70c2020-11-03 10:03:07 +0200630 crtc->state->degamma_lut->data;
631 length = crtc->state->degamma_lut->length /
Jyri Sarha492a4262016-06-07 15:09:17 +0300632 sizeof(*lut);
633 }
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200634 dispc_mgr_set_gamma(priv->dispc, omap_crtc->channel,
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200635 lut, length);
Jyri Sarha492a4262016-06-07 15:09:17 +0300636 }
637
Tomi Valkeinen7e3d9272015-08-10 12:08:50 +0300638 omap_crtc_write_crtc_properties(crtc);
639
Jyri Sarhae025d382017-01-27 12:04:54 +0200640 /* Only flush the CRTC if it is currently enabled. */
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300641 if (!omap_crtc->enabled)
642 return;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300643
Laurent Pinchartf933a3a2016-04-18 02:54:31 +0300644 DBG("%s: GO", omap_crtc->name);
Tomi Valkeinen6646dfd2015-06-08 13:08:25 +0300645
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200646 if (omap_crtc_state->manually_updated) {
647 /* send new image for page flips and modeset changes */
648 spin_lock_irq(&crtc->dev->event_lock);
649 omap_crtc_flush(crtc);
650 omap_crtc_arm_event(crtc);
651 spin_unlock_irq(&crtc->dev->event_lock);
652 return;
653 }
654
Laurent Pinchart14389a32016-04-19 01:43:03 +0300655 ret = drm_crtc_vblank_get(crtc);
656 WARN_ON(ret != 0);
657
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300658 spin_lock_irq(&crtc->dev->event_lock);
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200659 dispc_mgr_go(priv->dispc, omap_crtc->channel);
Laurent Pinchartce9a8f12017-05-09 01:27:09 +0300660 omap_crtc_arm_event(crtc);
Laurent Pinchartd173d3d2016-04-19 01:31:21 +0300661 spin_unlock_irq(&crtc->dev->event_lock);
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200662}
663
Laurent Pinchartafc34932015-03-06 18:35:16 +0200664static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
665 struct drm_crtc_state *state,
666 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200667 u64 val)
Rob Clark3c810c62012-08-15 15:18:01 -0500668{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200669 struct omap_drm_private *priv = crtc->dev->dev_private;
670 struct drm_plane_state *plane_state;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200671
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200672 /*
673 * Delegate property set to the primary plane. Get the plane state and
674 * set the property directly, the shadow copy will be assigned in the
675 * omap_crtc_atomic_check callback. This way updates to plane state will
676 * always be mirrored in the crtc state correctly.
677 */
678 plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
679 if (IS_ERR(plane_state))
680 return PTR_ERR(plane_state);
Laurent Pinchartafc34932015-03-06 18:35:16 +0200681
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200682 if (property == crtc->primary->rotation_property)
683 plane_state->rotation = val;
684 else if (property == priv->zorder_prop)
685 plane_state->zpos = val;
686 else
687 return -EINVAL;
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200688
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200689 return 0;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200690}
691
692static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
693 const struct drm_crtc_state *state,
694 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200695 u64 *val)
Laurent Pinchartafc34932015-03-06 18:35:16 +0200696{
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200697 struct omap_drm_private *priv = crtc->dev->dev_private;
698 struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
Tomi Valkeinen6bdad6c2016-02-18 18:47:14 +0200699
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200700 if (property == crtc->primary->rotation_property)
701 *val = omap_state->rotation;
702 else if (property == priv->zorder_prop)
703 *val = omap_state->zpos;
704 else
705 return -EINVAL;
706
707 return 0;
708}
709
710static void omap_crtc_reset(struct drm_crtc *crtc)
711{
Daniel Vetter51f644b2020-06-12 18:00:49 +0200712 struct omap_crtc_state *state;
713
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200714 if (crtc->state)
715 __drm_atomic_helper_crtc_destroy_state(crtc->state);
716
717 kfree(crtc->state);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200718
Daniel Vetter51f644b2020-06-12 18:00:49 +0200719 state = kzalloc(sizeof(*state), GFP_KERNEL);
720 if (state)
721 __drm_atomic_helper_crtc_reset(crtc, &state->base);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200722}
723
724static struct drm_crtc_state *
725omap_crtc_duplicate_state(struct drm_crtc *crtc)
726{
727 struct omap_crtc_state *state, *current_state;
728
729 if (WARN_ON(!crtc->state))
730 return NULL;
731
732 current_state = to_omap_crtc_state(crtc->state);
733
734 state = kmalloc(sizeof(*state), GFP_KERNEL);
Dan Carpenter24196722017-08-11 23:16:06 +0300735 if (!state)
736 return NULL;
737
738 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200739
740 state->zpos = current_state->zpos;
741 state->rotation = current_state->rotation;
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200742 state->manually_updated = current_state->manually_updated;
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200743
744 return &state->base;
Rob Clark3c810c62012-08-15 15:18:01 -0500745}
746
Rob Clarkcd5351f2011-11-12 12:09:40 -0600747static const struct drm_crtc_funcs omap_crtc_funcs = {
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200748 .reset = omap_crtc_reset,
Laurent Pinchart9416c9d2015-03-05 21:54:54 +0200749 .set_config = drm_atomic_helper_set_config,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600750 .destroy = omap_crtc_destroy,
Laurent Pinchartfa16d262015-03-06 16:01:53 +0200751 .page_flip = drm_atomic_helper_page_flip,
Maarten Lankhorst3dfeb632017-08-07 12:20:06 +0200752 .atomic_duplicate_state = omap_crtc_duplicate_state,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200753 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200754 .atomic_set_property = omap_crtc_atomic_set_property,
755 .atomic_get_property = omap_crtc_atomic_get_property,
Tomi Valkeinen03961622017-02-08 13:26:00 +0200756 .enable_vblank = omap_irq_enable_vblank,
757 .disable_vblank = omap_irq_disable_vblank,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600758};
759
760static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
Laurent Pinchartf7a73b62015-03-05 13:45:14 +0200761 .mode_set_nofb = omap_crtc_mode_set_nofb,
Jyri Sarha492a4262016-06-07 15:09:17 +0300762 .atomic_check = omap_crtc_atomic_check,
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200763 .atomic_begin = omap_crtc_atomic_begin,
764 .atomic_flush = omap_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300765 .atomic_enable = omap_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300766 .atomic_disable = omap_crtc_atomic_disable,
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200767 .mode_valid = omap_crtc_mode_valid,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600768};
769
Laurent Pinchart971fb3e2015-01-18 01:12:59 +0200770/* -----------------------------------------------------------------------------
771 * Init and Cleanup
772 */
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300773
Rob Clarkf5f94542012-12-04 13:59:12 -0600774static const char *channel_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200775 [OMAP_DSS_CHANNEL_LCD] = "lcd",
776 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
777 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
778 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600779};
780
Rob Clarkcd5351f2011-11-12 12:09:40 -0600781/* initialize crtc */
782struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200783 struct omap_drm_pipeline *pipe,
784 struct drm_plane *plane)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600785{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200786 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600787 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600788 struct omap_crtc *omap_crtc;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200789 enum omap_channel channel;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200790 int ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600791
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200792 channel = pipe->output->dispc_channel;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200793
Rob Clarkf5f94542012-12-04 13:59:12 -0600794 DBG("%s", channel_names[channel]);
795
796 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800797 if (!omap_crtc)
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200798 return ERR_PTR(-ENOMEM);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600799
Rob Clarkcd5351f2011-11-12 12:09:40 -0600800 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600801
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300802 init_waitqueue_head(&omap_crtc->pending_wait);
Rob Clarkf5f94542012-12-04 13:59:12 -0600803
Laurent Pinchart67dfd2d2018-03-06 23:38:21 +0200804 omap_crtc->pipe = pipe;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530805 omap_crtc->channel = channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530806 omap_crtc->name = channel_names[channel];
Archit Taneja0d8f3712013-03-26 19:15:19 +0530807
Sebastian Reichel1bb418b2019-05-23 22:07:56 +0200808 /*
809 * We want to refresh manually updated displays from dirty callback,
810 * which is called quite often (e.g. for each drawn line). This will
811 * be used to do the display update asynchronously to avoid blocking
812 * the rendering process and merges multiple dirty calls into one
813 * update if they arrive very fast. We also call this function for
814 * atomic display updates (e.g. for page flips), which means we do
815 * not need extra locking. Atomic updates should be synchronous, but
816 * need to wait for the framedone interrupt anyways.
817 */
818 INIT_DELAYED_WORK(&omap_crtc->update_work,
819 omap_crtc_manual_display_update);
820
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200821 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200822 &omap_crtc_funcs, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200823 if (ret < 0) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200824 dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
Laurent Pinchart79107f22018-09-23 12:58:15 +0300825 __func__, pipe->output->name);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200826 kfree(omap_crtc);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200827 return ERR_PTR(ret);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200828 }
829
Rob Clarkcd5351f2011-11-12 12:09:40 -0600830 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
831
Jyri Sarha492a4262016-06-07 15:09:17 +0300832 /* The dispc API adapts to what ever size, but the HW supports
833 * 256 element gamma table for LCDs and 1024 element table for
834 * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
835 * tables so lets use that. Size of HW gamma table can be
836 * extracted with dispc_mgr_gamma_size(). If it returns 0
Kieran Binghambdc19ba2019-12-09 12:33:19 +0000837 * gamma table is not supported.
Jyri Sarha492a4262016-06-07 15:09:17 +0300838 */
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200839 if (dispc_mgr_gamma_size(priv->dispc, channel)) {
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200840 unsigned int gamma_lut_size = 256;
Jyri Sarha492a4262016-06-07 15:09:17 +0300841
Jyri Sarhaf18f4392020-11-03 10:03:08 +0200842 drm_crtc_enable_color_mgmt(crtc, gamma_lut_size, true, 0);
Jyri Sarha492a4262016-06-07 15:09:17 +0300843 drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
844 }
845
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200846 omap_plane_install_properties(crtc->primary, &crtc->base);
Rob Clark3c810c62012-08-15 15:18:01 -0500847
Rob Clarkcd5351f2011-11-12 12:09:40 -0600848 return crtc;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600849}