2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_flip_work.h>
22 #include <drm/drm_plane_helper.h>
23 #include <linux/workqueue.h>
25 #include "tilcdc_drv.h"
26 #include "tilcdc_regs.h"
28 #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
33 struct drm_plane primary;
34 const struct tilcdc_panel_info *info;
35 struct drm_pending_vblank_event *event;
37 wait_queue_head_t frame_done_wq;
41 unsigned int lcd_fck_rate;
45 struct drm_framebuffer *curr_fb;
46 struct drm_framebuffer *next_fb;
48 /* for deferred fb unref's: */
49 struct drm_flip_work unref_work;
51 /* Only set if an external encoder is connected */
52 bool simulate_vesa_sync;
57 #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
59 static void unref_worker(struct drm_flip_work *work, void *val)
61 struct tilcdc_crtc *tilcdc_crtc =
62 container_of(work, struct tilcdc_crtc, unref_work);
63 struct drm_device *dev = tilcdc_crtc->base.dev;
65 mutex_lock(&dev->mode_config.mutex);
66 drm_framebuffer_unreference(val);
67 mutex_unlock(&dev->mode_config.mutex);
70 static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
72 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
73 struct drm_device *dev = crtc->dev;
74 struct drm_gem_cma_object *gem;
75 unsigned int depth, bpp;
76 dma_addr_t start, end;
77 u64 dma_base_and_ceiling;
79 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
80 gem = drm_fb_cma_get_gem_obj(fb, 0);
82 start = gem->paddr + fb->offsets[0] +
83 crtc->y * fb->pitches[0] +
86 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
88 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
89 * with a single insruction, if available. This should make it more
90 * unlikely that LCDC would fetch the DMA addresses in the middle of
93 dma_base_and_ceiling = (u64)(end - 1) << 32 | start;
94 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
96 if (tilcdc_crtc->curr_fb)
97 drm_flip_work_queue(&tilcdc_crtc->unref_work,
98 tilcdc_crtc->curr_fb);
100 tilcdc_crtc->curr_fb = fb;
103 static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
105 struct tilcdc_drm_private *priv = dev->dev_private;
107 tilcdc_clear_irqstatus(dev, 0xffffffff);
109 if (priv->rev == 1) {
110 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
111 LCDC_V1_UNDERFLOW_INT_ENA);
112 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
113 LCDC_V1_END_OF_FRAME_INT_ENA);
115 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
116 LCDC_V2_UNDERFLOW_INT_ENA |
117 LCDC_V2_END_OF_FRAME0_INT_ENA |
118 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
122 static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
124 struct tilcdc_drm_private *priv = dev->dev_private;
126 /* disable irqs that we might have enabled: */
127 if (priv->rev == 1) {
128 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
129 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
130 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
131 LCDC_V1_END_OF_FRAME_INT_ENA);
133 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
134 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
135 LCDC_V2_END_OF_FRAME0_INT_ENA |
136 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
140 static void reset(struct drm_crtc *crtc)
142 struct drm_device *dev = crtc->dev;
143 struct tilcdc_drm_private *priv = dev->dev_private;
148 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
149 usleep_range(250, 1000);
150 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
153 static void tilcdc_crtc_enable(struct drm_crtc *crtc)
155 struct drm_device *dev = crtc->dev;
156 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
158 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
160 if (tilcdc_crtc->enabled)
163 pm_runtime_get_sync(dev->dev);
167 tilcdc_crtc_enable_irqs(dev);
169 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
170 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
171 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
173 drm_crtc_vblank_on(crtc);
175 tilcdc_crtc->enabled = true;
178 void tilcdc_crtc_disable(struct drm_crtc *crtc)
180 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
181 struct drm_device *dev = crtc->dev;
182 struct tilcdc_drm_private *priv = dev->dev_private;
184 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
186 if (!tilcdc_crtc->enabled)
189 tilcdc_crtc->frame_done = false;
190 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
193 * if necessary wait for framedone irq which will still come
194 * before putting things to sleep..
196 if (priv->rev == 2) {
197 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
198 tilcdc_crtc->frame_done,
199 msecs_to_jiffies(500));
201 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
205 drm_crtc_vblank_off(crtc);
207 tilcdc_crtc_disable_irqs(dev);
209 pm_runtime_put_sync(dev->dev);
211 if (tilcdc_crtc->next_fb) {
212 drm_flip_work_queue(&tilcdc_crtc->unref_work,
213 tilcdc_crtc->next_fb);
214 tilcdc_crtc->next_fb = NULL;
217 if (tilcdc_crtc->curr_fb) {
218 drm_flip_work_queue(&tilcdc_crtc->unref_work,
219 tilcdc_crtc->curr_fb);
220 tilcdc_crtc->curr_fb = NULL;
223 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
224 tilcdc_crtc->last_vblank = ktime_set(0, 0);
226 tilcdc_crtc->enabled = false;
229 static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
231 return crtc->state && crtc->state->enable && crtc->state->active;
234 static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
236 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
237 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
239 drm_modeset_lock_crtc(crtc, NULL);
240 tilcdc_crtc_disable(crtc);
241 drm_modeset_unlock_crtc(crtc);
243 flush_workqueue(priv->wq);
245 of_node_put(crtc->port);
246 drm_crtc_cleanup(crtc);
247 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
250 int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
251 struct drm_framebuffer *fb,
252 struct drm_pending_vblank_event *event)
254 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
255 struct drm_device *dev = crtc->dev;
258 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
260 if (tilcdc_crtc->event) {
261 dev_err(dev->dev, "already pending page flip!\n");
265 drm_framebuffer_reference(fb);
267 crtc->primary->fb = fb;
269 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
271 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
275 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
276 1000000 / crtc->hwmode.vrefresh);
278 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
280 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
281 tilcdc_crtc->next_fb = fb;
284 if (tilcdc_crtc->next_fb != fb)
285 set_scanout(crtc, fb);
287 tilcdc_crtc->event = event;
289 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
294 static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
295 const struct drm_display_mode *mode,
296 struct drm_display_mode *adjusted_mode)
298 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
300 if (!tilcdc_crtc->simulate_vesa_sync)
304 * tilcdc does not generate VESA-compliant sync but aligns
305 * VS on the second edge of HS instead of first edge.
306 * We use adjusted_mode, to fixup sync by aligning both rising
307 * edges and add HSKEW offset to fix the sync.
309 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
310 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
312 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
313 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
314 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
316 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
317 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
323 static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
325 struct drm_device *dev = crtc->dev;
326 struct tilcdc_drm_private *priv = dev->dev_private;
327 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
328 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
331 /* mode.clock is in KHz, set_rate wants parameter in Hz */
332 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
334 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
339 tilcdc_crtc->lcd_fck_rate = clk_get_rate(priv->clk);
341 DBG("lcd_clk=%u, mode clock=%d, div=%u",
342 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
344 /* Configure the LCD clock divisor. */
345 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
349 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
350 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
351 LCDC_V2_CORE_CLK_EN);
354 static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
356 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
357 struct drm_device *dev = crtc->dev;
358 struct tilcdc_drm_private *priv = dev->dev_private;
359 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
360 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
361 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
362 struct drm_framebuffer *fb = crtc->primary->state->fb;
364 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
372 /* Configure the Burst Size and fifo threshold of DMA: */
373 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
374 switch (info->dma_burst_sz) {
376 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
379 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
382 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
385 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
388 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
391 dev_err(dev->dev, "invalid burst size\n");
394 reg |= (info->fifo_th << 8);
395 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
397 /* Configure timings: */
398 hbp = mode->htotal - mode->hsync_end;
399 hfp = mode->hsync_start - mode->hdisplay;
400 hsw = mode->hsync_end - mode->hsync_start;
401 vbp = mode->vtotal - mode->vsync_end;
402 vfp = mode->vsync_start - mode->vdisplay;
403 vsw = mode->vsync_end - mode->vsync_start;
405 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
406 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
408 /* Set AC Bias Period and Number of Transitions per Interrupt: */
409 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
410 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
411 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
414 * subtract one from hfp, hbp, hsw because the hardware uses
417 if (priv->rev == 2) {
418 /* clear bits we're going to set */
420 reg |= ((hfp-1) & 0x300) >> 8;
421 reg |= ((hbp-1) & 0x300) >> 4;
422 reg |= ((hsw-1) & 0x3c0) << 21;
424 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
426 reg = (((mode->hdisplay >> 4) - 1) << 4) |
427 (((hbp-1) & 0xff) << 24) |
428 (((hfp-1) & 0xff) << 16) |
429 (((hsw-1) & 0x3f) << 10);
431 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
432 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
434 reg = ((mode->vdisplay - 1) & 0x3ff) |
435 ((vbp & 0xff) << 24) |
436 ((vfp & 0xff) << 16) |
437 (((vsw-1) & 0x3f) << 10);
438 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
441 * be sure to set Bit 10 for the V2 LCDC controller,
442 * otherwise limited to 1024 pixels width, stopping
443 * 1920x1080 being supported.
445 if (priv->rev == 2) {
446 if ((mode->vdisplay - 1) & 0x400) {
447 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
450 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
455 /* Configure display type: */
456 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
457 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
458 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
459 0x000ff000 /* Palette Loading Delay bits */);
460 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
461 if (info->tft_alt_mode)
462 reg |= LCDC_TFT_ALT_ENABLE;
463 if (priv->rev == 2) {
464 unsigned int depth, bpp;
466 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
471 reg |= LCDC_V2_TFT_24BPP_UNPACK;
474 reg |= LCDC_V2_TFT_24BPP_MODE;
477 dev_err(dev->dev, "invalid pixel format\n");
481 reg |= info->fdd < 12;
482 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
484 if (info->invert_pxl_clk)
485 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
487 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
490 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
492 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
495 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
497 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
499 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
500 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
502 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
504 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
505 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
507 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
509 if (info->raster_order)
510 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
512 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
514 drm_framebuffer_reference(fb);
516 set_scanout(crtc, fb);
518 tilcdc_crtc_set_clk(crtc);
520 crtc->hwmode = crtc->state->adjusted_mode;
523 static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
524 struct drm_crtc_state *state)
526 struct drm_display_mode *mode = &state->mode;
529 /* If we are not active we don't care */
533 if (state->state->planes[0].ptr != crtc->primary ||
534 state->state->planes[0].state == NULL ||
535 state->state->planes[0].state->crtc != crtc) {
536 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
540 ret = tilcdc_crtc_mode_valid(crtc, mode);
542 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
549 static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
550 .destroy = tilcdc_crtc_destroy,
551 .set_config = drm_atomic_helper_set_config,
552 .page_flip = drm_atomic_helper_page_flip,
553 .reset = drm_atomic_helper_crtc_reset,
554 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
555 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
558 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
559 .mode_fixup = tilcdc_crtc_mode_fixup,
560 .enable = tilcdc_crtc_enable,
561 .disable = tilcdc_crtc_disable,
562 .atomic_check = tilcdc_crtc_atomic_check,
563 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
566 int tilcdc_crtc_max_width(struct drm_crtc *crtc)
568 struct drm_device *dev = crtc->dev;
569 struct tilcdc_drm_private *priv = dev->dev_private;
574 else if (priv->rev == 2)
580 int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
582 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
583 unsigned int bandwidth;
584 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
587 * check to see if the width is within the range that
588 * the LCD Controller physically supports
590 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
591 return MODE_VIRTUAL_X;
593 /* width must be multiple of 16 */
594 if (mode->hdisplay & 0xf)
595 return MODE_VIRTUAL_X;
597 if (mode->vdisplay > 2048)
598 return MODE_VIRTUAL_Y;
600 DBG("Processing mode %dx%d@%d with pixel clock %d",
601 mode->hdisplay, mode->vdisplay,
602 drm_mode_vrefresh(mode), mode->clock);
604 hbp = mode->htotal - mode->hsync_end;
605 hfp = mode->hsync_start - mode->hdisplay;
606 hsw = mode->hsync_end - mode->hsync_start;
607 vbp = mode->vtotal - mode->vsync_end;
608 vfp = mode->vsync_start - mode->vdisplay;
609 vsw = mode->vsync_end - mode->vsync_start;
611 if ((hbp-1) & ~0x3ff) {
612 DBG("Pruning mode: Horizontal Back Porch out of range");
613 return MODE_HBLANK_WIDE;
616 if ((hfp-1) & ~0x3ff) {
617 DBG("Pruning mode: Horizontal Front Porch out of range");
618 return MODE_HBLANK_WIDE;
621 if ((hsw-1) & ~0x3ff) {
622 DBG("Pruning mode: Horizontal Sync Width out of range");
623 return MODE_HSYNC_WIDE;
627 DBG("Pruning mode: Vertical Back Porch out of range");
628 return MODE_VBLANK_WIDE;
632 DBG("Pruning mode: Vertical Front Porch out of range");
633 return MODE_VBLANK_WIDE;
636 if ((vsw-1) & ~0x3f) {
637 DBG("Pruning mode: Vertical Sync Width out of range");
638 return MODE_VSYNC_WIDE;
642 * some devices have a maximum allowed pixel clock
643 * configured from the DT
645 if (mode->clock > priv->max_pixelclock) {
646 DBG("Pruning mode: pixel clock too high");
647 return MODE_CLOCK_HIGH;
651 * some devices further limit the max horizontal resolution
652 * configured from the DT
654 if (mode->hdisplay > priv->max_width)
655 return MODE_BAD_WIDTH;
657 /* filter out modes that would require too much memory bandwidth: */
658 bandwidth = mode->hdisplay * mode->vdisplay *
659 drm_mode_vrefresh(mode);
660 if (bandwidth > priv->max_bandwidth) {
661 DBG("Pruning mode: exceeds defined bandwidth limit");
668 void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
669 const struct tilcdc_panel_info *info)
671 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
672 tilcdc_crtc->info = info;
675 void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
676 bool simulate_vesa_sync)
678 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
680 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
683 void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
685 struct drm_device *dev = crtc->dev;
686 struct tilcdc_drm_private *priv = dev->dev_private;
687 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
689 drm_modeset_lock_crtc(crtc, NULL);
690 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
691 if (tilcdc_crtc_is_on(crtc)) {
692 pm_runtime_get_sync(dev->dev);
693 tilcdc_crtc_disable(crtc);
695 tilcdc_crtc_set_clk(crtc);
697 tilcdc_crtc_enable(crtc);
698 pm_runtime_put_sync(dev->dev);
701 drm_modeset_unlock_crtc(crtc);
704 #define SYNC_LOST_COUNT_LIMIT 50
706 irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
708 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
709 struct drm_device *dev = crtc->dev;
710 struct tilcdc_drm_private *priv = dev->dev_private;
713 stat = tilcdc_read_irqstatus(dev);
714 tilcdc_clear_irqstatus(dev, stat);
716 if (stat & LCDC_END_OF_FRAME0) {
718 bool skip_event = false;
723 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
725 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
727 tilcdc_crtc->last_vblank = now;
729 if (tilcdc_crtc->next_fb) {
730 set_scanout(crtc, tilcdc_crtc->next_fb);
731 tilcdc_crtc->next_fb = NULL;
735 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
737 drm_crtc_handle_vblank(crtc);
740 struct drm_pending_vblank_event *event;
742 spin_lock_irqsave(&dev->event_lock, flags);
744 event = tilcdc_crtc->event;
745 tilcdc_crtc->event = NULL;
747 drm_crtc_send_vblank_event(crtc, event);
749 spin_unlock_irqrestore(&dev->event_lock, flags);
752 if (tilcdc_crtc->frame_intact)
753 tilcdc_crtc->sync_lost_count = 0;
755 tilcdc_crtc->frame_intact = true;
758 if (stat & LCDC_FIFO_UNDERFLOW)
759 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
762 /* For revision 2 only */
763 if (priv->rev == 2) {
764 if (stat & LCDC_FRAME_DONE) {
765 tilcdc_crtc->frame_done = true;
766 wake_up(&tilcdc_crtc->frame_done_wq);
769 if (stat & LCDC_SYNC_LOST) {
770 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
772 tilcdc_crtc->frame_intact = false;
773 if (tilcdc_crtc->sync_lost_count++ >
774 SYNC_LOST_COUNT_LIMIT) {
775 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
776 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
781 /* Indicate to LCDC that the interrupt service routine has
782 * completed, see 13.3.6.1.6 in AM335x TRM.
784 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
790 struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
792 struct tilcdc_drm_private *priv = dev->dev_private;
793 struct tilcdc_crtc *tilcdc_crtc;
794 struct drm_crtc *crtc;
797 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
799 dev_err(dev->dev, "allocation failed\n");
803 crtc = &tilcdc_crtc->base;
805 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
809 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
811 drm_flip_work_init(&tilcdc_crtc->unref_work,
812 "unref", unref_worker);
814 spin_lock_init(&tilcdc_crtc->irq_lock);
816 ret = drm_crtc_init_with_planes(dev, crtc,
817 &tilcdc_crtc->primary,
824 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
826 if (priv->is_componentized) {
827 struct device_node *ports =
828 of_get_child_by_name(dev->dev->of_node, "ports");
831 crtc->port = of_get_child_by_name(ports, "port");
835 of_get_child_by_name(dev->dev->of_node, "port");
837 if (!crtc->port) { /* This should never happen */
838 dev_err(dev->dev, "Port node not found in %s\n",
839 dev->dev->of_node->full_name);
847 tilcdc_crtc_destroy(crtc);