1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_mode.h>
11 #include <drm/drm_plane_helper.h>
12 #include <linux/math64.h>
16 #define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
18 struct omap_crtc_state {
20 struct drm_crtc_state base;
21 /* Shadow values for legacy userspace support. */
22 unsigned int rotation;
26 #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
32 struct omap_drm_pipeline *pipe;
33 enum omap_channel channel;
37 bool ignore_digit_sync_lost;
41 wait_queue_head_t pending_wait;
42 struct drm_pending_vblank_event *event;
45 /* -----------------------------------------------------------------------------
49 struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
51 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
52 return &omap_crtc->vm;
55 enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
57 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
58 return omap_crtc->channel;
61 static bool omap_crtc_is_pending(struct drm_crtc *crtc)
63 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
67 spin_lock_irqsave(&crtc->dev->event_lock, flags);
68 pending = omap_crtc->pending;
69 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
74 int omap_crtc_wait_pending(struct drm_crtc *crtc)
76 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
79 * Timeout is set to a "sufficiently" high value, which should cover
80 * a single frame refresh even on slower displays.
82 return wait_event_timeout(omap_crtc->pending_wait,
83 !omap_crtc_is_pending(crtc),
84 msecs_to_jiffies(250));
87 /* -----------------------------------------------------------------------------
88 * DSS Manager Functions
92 * Manager-ops, callbacks from output when they need to configure
93 * the upstream part of the video pipe.
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
100 /* we can probably ignore these until we support command-mode panels: */
101 static void omap_crtc_dss_start_update(struct omap_drm_private *priv,
102 enum omap_channel channel)
106 /* Called only from the encoder enable/disable and suspend/resume handlers. */
107 static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
109 struct drm_device *dev = crtc->dev;
110 struct omap_drm_private *priv = dev->dev_private;
111 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;
117 if (WARN_ON(omap_crtc->enabled == enable))
120 if (omap_crtc->pipe->output->type == OMAP_DISPLAY_TYPE_HDMI) {
121 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
122 omap_crtc->enabled = enable;
126 if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
128 * Digit output produces some sync lost interrupts during the
129 * first frame when enabling, so we need to ignore those.
131 omap_crtc->ignore_digit_sync_lost = true;
134 framedone_irq = priv->dispc_ops->mgr_get_framedone_irq(priv->dispc,
136 vsync_irq = priv->dispc_ops->mgr_get_vsync_irq(priv->dispc, channel);
139 wait = omap_irq_wait_init(dev, vsync_irq, 1);
142 * When we disable the digit output, we need to wait for
143 * FRAMEDONE to know that DISPC has finished with the output.
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.
151 wait = omap_irq_wait_init(dev, framedone_irq, 1);
153 wait = omap_irq_wait_init(dev, vsync_irq, 2);
156 priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
157 omap_crtc->enabled = enable;
159 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
161 dev_err(dev->dev, "%s: timeout waiting for %s\n",
162 omap_crtc->name, enable ? "enable" : "disable");
165 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 */
173 static int omap_crtc_dss_enable(struct omap_drm_private *priv,
174 enum omap_channel channel)
176 struct drm_crtc *crtc = priv->channels[channel]->crtc;
177 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
179 priv->dispc_ops->mgr_set_timings(priv->dispc, omap_crtc->channel,
181 omap_crtc_set_enabled(&omap_crtc->base, true);
186 static void omap_crtc_dss_disable(struct omap_drm_private *priv,
187 enum omap_channel channel)
189 struct drm_crtc *crtc = priv->channels[channel]->crtc;
190 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
192 omap_crtc_set_enabled(&omap_crtc->base, false);
195 static void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
196 enum omap_channel channel,
197 const struct videomode *vm)
199 struct drm_crtc *crtc = priv->channels[channel]->crtc;
200 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
202 DBG("%s", omap_crtc->name);
206 static void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
207 enum omap_channel channel,
208 const struct dss_lcd_mgr_config *config)
210 struct drm_crtc *crtc = priv->channels[channel]->crtc;
211 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
213 DBG("%s", omap_crtc->name);
214 priv->dispc_ops->mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
218 static int omap_crtc_dss_register_framedone(
219 struct omap_drm_private *priv, enum omap_channel channel,
220 void (*handler)(void *), void *data)
225 static void omap_crtc_dss_unregister_framedone(
226 struct omap_drm_private *priv, enum omap_channel channel,
227 void (*handler)(void *), void *data)
231 static const struct dss_mgr_ops mgr_ops = {
232 .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,
241 /* -----------------------------------------------------------------------------
242 * Setup, Flush and Page Flip
245 void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
247 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
249 if (omap_crtc->ignore_digit_sync_lost) {
250 irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
255 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
258 void omap_crtc_vblank_irq(struct drm_crtc *crtc)
260 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
261 struct drm_device *dev = omap_crtc->base.dev;
262 struct omap_drm_private *priv = dev->dev_private;
265 spin_lock(&crtc->dev->event_lock);
267 * If the dispc is busy we're racing the flush operation. Try again on
268 * the next vblank interrupt.
270 if (priv->dispc_ops->mgr_go_busy(priv->dispc, omap_crtc->channel)) {
271 spin_unlock(&crtc->dev->event_lock);
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;
281 pending = omap_crtc->pending;
282 omap_crtc->pending = false;
283 spin_unlock(&crtc->dev->event_lock);
286 drm_crtc_vblank_put(crtc);
288 /* Wake up omap_atomic_complete. */
289 wake_up(&omap_crtc->pending_wait);
291 DBG("%s: apply done", omap_crtc->name);
294 static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
296 struct omap_drm_private *priv = crtc->dev->dev_private;
297 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
298 struct omap_overlay_manager_info info;
300 memset(&info, 0, sizeof(info));
302 info.default_color = 0x000000;
303 info.trans_enabled = false;
304 info.partial_alpha_enabled = false;
305 info.cpr_enable = false;
307 priv->dispc_ops->mgr_setup(priv->dispc, omap_crtc->channel, &info);
310 /* -----------------------------------------------------------------------------
314 static void omap_crtc_destroy(struct drm_crtc *crtc)
316 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
318 DBG("%s", omap_crtc->name);
320 drm_crtc_cleanup(crtc);
325 static void omap_crtc_arm_event(struct drm_crtc *crtc)
327 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
329 WARN_ON(omap_crtc->pending);
330 omap_crtc->pending = true;
332 if (crtc->state->event) {
333 omap_crtc->event = crtc->state->event;
334 crtc->state->event = NULL;
338 static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
339 struct drm_crtc_state *old_state)
341 struct omap_drm_private *priv = crtc->dev->dev_private;
342 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
345 DBG("%s", omap_crtc->name);
347 priv->dispc_ops->runtime_get(priv->dispc);
349 spin_lock_irq(&crtc->dev->event_lock);
350 drm_crtc_vblank_on(crtc);
351 ret = drm_crtc_vblank_get(crtc);
354 omap_crtc_arm_event(crtc);
355 spin_unlock_irq(&crtc->dev->event_lock);
358 static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
359 struct drm_crtc_state *old_state)
361 struct omap_drm_private *priv = crtc->dev->dev_private;
362 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
364 DBG("%s", omap_crtc->name);
366 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;
371 spin_unlock_irq(&crtc->dev->event_lock);
373 drm_crtc_vblank_off(crtc);
375 priv->dispc_ops->runtime_put(priv->dispc);
378 static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
379 const struct drm_display_mode *mode)
381 struct omap_drm_private *priv = crtc->dev->dev_private;
382 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
383 struct videomode vm = {0};
386 drm_display_mode_to_videomode(mode, &vm);
387 r = priv->dispc_ops->mgr_check_timings(priv->dispc, omap_crtc->channel,
392 /* Check for bandwidth limit */
393 if (priv->max_bandwidth) {
395 * Estimation for the bandwidth need of a given mode with one
397 * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
400 * The interlaced mode is taken into account by using the
401 * pixelclock in the calculation.
403 * The equation is rearranged for 64bit arithmetic.
405 uint64_t bandwidth = mode->clock * 1000;
406 unsigned int bpp = 4;
408 bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
409 bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
412 * Reject modes which would need more bandwidth if used with one
413 * full resolution plane (most common use case).
415 if (priv->max_bandwidth < bandwidth)
422 static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
424 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
425 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
427 DBG("%s: set mode: " DRM_MODE_FMT,
428 omap_crtc->name, DRM_MODE_ARG(mode));
430 drm_display_mode_to_videomode(mode, &omap_crtc->vm);
433 static int omap_crtc_atomic_check(struct drm_crtc *crtc,
434 struct drm_crtc_state *state)
436 struct drm_plane_state *pri_state;
438 if (state->color_mgmt_changed && state->gamma_lut) {
439 unsigned int length = state->gamma_lut->length /
440 sizeof(struct drm_color_lut);
446 pri_state = drm_atomic_get_new_plane_state(state->state, crtc->primary);
448 struct omap_crtc_state *omap_crtc_state =
449 to_omap_crtc_state(state);
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;
459 static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
460 struct drm_crtc_state *old_crtc_state)
464 static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
465 struct drm_crtc_state *old_crtc_state)
467 struct omap_drm_private *priv = crtc->dev->dev_private;
468 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
471 if (crtc->state->color_mgmt_changed) {
472 struct drm_color_lut *lut = NULL;
473 unsigned int length = 0;
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 /
481 priv->dispc_ops->mgr_set_gamma(priv->dispc, omap_crtc->channel,
485 omap_crtc_write_crtc_properties(crtc);
487 /* Only flush the CRTC if it is currently enabled. */
488 if (!omap_crtc->enabled)
491 DBG("%s: GO", omap_crtc->name);
493 ret = drm_crtc_vblank_get(crtc);
496 spin_lock_irq(&crtc->dev->event_lock);
497 priv->dispc_ops->mgr_go(priv->dispc, omap_crtc->channel);
498 omap_crtc_arm_event(crtc);
499 spin_unlock_irq(&crtc->dev->event_lock);
502 static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
503 struct drm_crtc_state *state,
504 struct drm_property *property,
507 struct omap_drm_private *priv = crtc->dev->dev_private;
508 struct drm_plane_state *plane_state;
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.
516 plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
517 if (IS_ERR(plane_state))
518 return PTR_ERR(plane_state);
520 if (property == crtc->primary->rotation_property)
521 plane_state->rotation = val;
522 else if (property == priv->zorder_prop)
523 plane_state->zpos = val;
530 static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
531 const struct drm_crtc_state *state,
532 struct drm_property *property,
535 struct omap_drm_private *priv = crtc->dev->dev_private;
536 struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
538 if (property == crtc->primary->rotation_property)
539 *val = omap_state->rotation;
540 else if (property == priv->zorder_prop)
541 *val = omap_state->zpos;
548 static void omap_crtc_reset(struct drm_crtc *crtc)
551 __drm_atomic_helper_crtc_destroy_state(crtc->state);
554 crtc->state = kzalloc(sizeof(struct omap_crtc_state), GFP_KERNEL);
557 crtc->state->crtc = crtc;
560 static struct drm_crtc_state *
561 omap_crtc_duplicate_state(struct drm_crtc *crtc)
563 struct omap_crtc_state *state, *current_state;
565 if (WARN_ON(!crtc->state))
568 current_state = to_omap_crtc_state(crtc->state);
570 state = kmalloc(sizeof(*state), GFP_KERNEL);
574 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
576 state->zpos = current_state->zpos;
577 state->rotation = current_state->rotation;
582 static const struct drm_crtc_funcs omap_crtc_funcs = {
583 .reset = omap_crtc_reset,
584 .set_config = drm_atomic_helper_set_config,
585 .destroy = omap_crtc_destroy,
586 .page_flip = drm_atomic_helper_page_flip,
587 .gamma_set = drm_atomic_helper_legacy_gamma_set,
588 .atomic_duplicate_state = omap_crtc_duplicate_state,
589 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
590 .atomic_set_property = omap_crtc_atomic_set_property,
591 .atomic_get_property = omap_crtc_atomic_get_property,
592 .enable_vblank = omap_irq_enable_vblank,
593 .disable_vblank = omap_irq_disable_vblank,
596 static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
597 .mode_set_nofb = omap_crtc_mode_set_nofb,
598 .atomic_check = omap_crtc_atomic_check,
599 .atomic_begin = omap_crtc_atomic_begin,
600 .atomic_flush = omap_crtc_atomic_flush,
601 .atomic_enable = omap_crtc_atomic_enable,
602 .atomic_disable = omap_crtc_atomic_disable,
603 .mode_valid = omap_crtc_mode_valid,
606 /* -----------------------------------------------------------------------------
610 static const char *channel_names[] = {
611 [OMAP_DSS_CHANNEL_LCD] = "lcd",
612 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
613 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
614 [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
617 void omap_crtc_pre_init(struct omap_drm_private *priv)
619 dss_install_mgr_ops(priv->dss, &mgr_ops, priv);
622 void omap_crtc_pre_uninit(struct omap_drm_private *priv)
624 dss_uninstall_mgr_ops(priv->dss);
627 /* initialize crtc */
628 struct drm_crtc *omap_crtc_init(struct drm_device *dev,
629 struct omap_drm_pipeline *pipe,
630 struct drm_plane *plane)
632 struct omap_drm_private *priv = dev->dev_private;
633 struct drm_crtc *crtc = NULL;
634 struct omap_crtc *omap_crtc;
635 enum omap_channel channel;
638 channel = pipe->output->dispc_channel;
640 DBG("%s", channel_names[channel]);
642 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
644 return ERR_PTR(-ENOMEM);
646 crtc = &omap_crtc->base;
648 init_waitqueue_head(&omap_crtc->pending_wait);
650 omap_crtc->pipe = pipe;
651 omap_crtc->channel = channel;
652 omap_crtc->name = channel_names[channel];
654 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
655 &omap_crtc_funcs, NULL);
657 dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
658 __func__, pipe->output->name);
663 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
665 /* 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.
672 if (priv->dispc_ops->mgr_gamma_size(priv->dispc, channel)) {
673 unsigned int gamma_lut_size = 256;
675 drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
676 drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
679 omap_plane_install_properties(crtc->primary, &crtc->base);