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_flip_work.h"
19 #include <drm/drm_plane_helper.h>
21 #include "tilcdc_drv.h"
22 #include "tilcdc_regs.h"
24 #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
29 const struct tilcdc_panel_info *info;
30 struct drm_pending_vblank_event *event;
32 wait_queue_head_t frame_done_wq;
38 struct drm_framebuffer *curr_fb;
39 struct drm_framebuffer *next_fb;
41 /* for deferred fb unref's: */
42 struct drm_flip_work unref_work;
44 /* Only set if an external encoder is connected */
45 bool simulate_vesa_sync;
50 #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
52 static void unref_worker(struct drm_flip_work *work, void *val)
54 struct tilcdc_crtc *tilcdc_crtc =
55 container_of(work, struct tilcdc_crtc, unref_work);
56 struct drm_device *dev = tilcdc_crtc->base.dev;
58 mutex_lock(&dev->mode_config.mutex);
59 drm_framebuffer_unreference(val);
60 mutex_unlock(&dev->mode_config.mutex);
63 static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
65 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
66 struct drm_device *dev = crtc->dev;
67 struct drm_gem_cma_object *gem;
68 unsigned int depth, bpp;
69 dma_addr_t start, end;
71 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
72 gem = drm_fb_cma_get_gem_obj(fb, 0);
74 start = gem->paddr + fb->offsets[0] +
75 crtc->y * fb->pitches[0] +
78 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
80 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
81 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
83 if (tilcdc_crtc->curr_fb)
84 drm_flip_work_queue(&tilcdc_crtc->unref_work,
85 tilcdc_crtc->curr_fb);
87 tilcdc_crtc->curr_fb = fb;
90 static void reset(struct drm_crtc *crtc)
92 struct drm_device *dev = crtc->dev;
93 struct tilcdc_drm_private *priv = dev->dev_private;
98 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
99 usleep_range(250, 1000);
100 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
103 static void start(struct drm_crtc *crtc)
105 struct drm_device *dev = crtc->dev;
109 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
110 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
111 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
113 drm_crtc_vblank_on(crtc);
116 static void stop(struct drm_crtc *crtc)
118 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
119 struct drm_device *dev = crtc->dev;
120 struct tilcdc_drm_private *priv = dev->dev_private;
122 tilcdc_crtc->frame_done = false;
123 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
126 * if necessary wait for framedone irq which will still come
127 * before putting things to sleep..
129 if (priv->rev == 2) {
130 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
131 tilcdc_crtc->frame_done,
132 msecs_to_jiffies(500));
134 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
138 drm_crtc_vblank_off(crtc);
141 static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
143 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
145 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
147 of_node_put(crtc->port);
148 drm_crtc_cleanup(crtc);
149 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
152 static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
154 struct drm_device *dev = crtc->dev;
155 unsigned int depth, bpp;
157 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
159 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
161 "Invalid pitch: fb and crtc widths must be the same");
168 int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
169 struct drm_framebuffer *fb,
170 struct drm_pending_vblank_event *event,
171 uint32_t page_flip_flags)
173 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
174 struct drm_device *dev = crtc->dev;
180 r = tilcdc_verify_fb(crtc, fb);
184 if (tilcdc_crtc->event) {
185 dev_err(dev->dev, "already pending page flip!\n");
189 drm_framebuffer_reference(fb);
191 crtc->primary->fb = fb;
193 pm_runtime_get_sync(dev->dev);
195 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
197 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
198 1000000 / crtc->hwmode.vrefresh);
200 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
202 if (tdiff >= TILCDC_VBLANK_SAFETY_THRESHOLD_US)
203 set_scanout(crtc, fb);
205 tilcdc_crtc->next_fb = fb;
207 tilcdc_crtc->event = event;
209 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
211 pm_runtime_put_sync(dev->dev);
216 void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
218 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
219 struct drm_device *dev = crtc->dev;
220 struct tilcdc_drm_private *priv = dev->dev_private;
222 /* we really only care about on or off: */
223 if (mode != DRM_MODE_DPMS_ON)
224 mode = DRM_MODE_DPMS_OFF;
226 if (tilcdc_crtc->dpms == mode)
229 tilcdc_crtc->dpms = mode;
231 if (mode == DRM_MODE_DPMS_ON) {
232 pm_runtime_get_sync(dev->dev);
236 pm_runtime_put_sync(dev->dev);
238 if (tilcdc_crtc->next_fb) {
239 drm_flip_work_queue(&tilcdc_crtc->unref_work,
240 tilcdc_crtc->next_fb);
241 tilcdc_crtc->next_fb = NULL;
244 if (tilcdc_crtc->curr_fb) {
245 drm_flip_work_queue(&tilcdc_crtc->unref_work,
246 tilcdc_crtc->curr_fb);
247 tilcdc_crtc->curr_fb = NULL;
250 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
254 int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc)
256 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
258 return tilcdc_crtc->dpms;
261 static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
262 const struct drm_display_mode *mode,
263 struct drm_display_mode *adjusted_mode)
265 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
267 if (!tilcdc_crtc->simulate_vesa_sync)
271 * tilcdc does not generate VESA-compliant sync but aligns
272 * VS on the second edge of HS instead of first edge.
273 * We use adjusted_mode, to fixup sync by aligning both rising
274 * edges and add HSKEW offset to fix the sync.
276 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
277 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
279 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
280 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
281 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
283 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
284 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
290 static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
292 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
295 static void tilcdc_crtc_commit(struct drm_crtc *crtc)
297 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
300 static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
301 struct drm_display_mode *mode,
302 struct drm_display_mode *adjusted_mode,
304 struct drm_framebuffer *old_fb)
306 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
307 struct drm_device *dev = crtc->dev;
308 struct tilcdc_drm_private *priv = dev->dev_private;
309 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
310 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
313 ret = tilcdc_crtc_mode_valid(crtc, mode);
320 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
324 pm_runtime_get_sync(dev->dev);
326 /* Configure the Burst Size and fifo threshold of DMA: */
327 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
328 switch (info->dma_burst_sz) {
330 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
333 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
336 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
339 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
342 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
347 reg |= (info->fifo_th << 8);
348 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
350 /* Configure timings: */
351 hbp = mode->htotal - mode->hsync_end;
352 hfp = mode->hsync_start - mode->hdisplay;
353 hsw = mode->hsync_end - mode->hsync_start;
354 vbp = mode->vtotal - mode->vsync_end;
355 vfp = mode->vsync_start - mode->vdisplay;
356 vsw = mode->vsync_end - mode->vsync_start;
358 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
359 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
361 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
362 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
363 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
364 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
367 * subtract one from hfp, hbp, hsw because the hardware uses
370 if (priv->rev == 2) {
371 /* clear bits we're going to set */
373 reg |= ((hfp-1) & 0x300) >> 8;
374 reg |= ((hbp-1) & 0x300) >> 4;
375 reg |= ((hsw-1) & 0x3c0) << 21;
377 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
379 reg = (((mode->hdisplay >> 4) - 1) << 4) |
380 (((hbp-1) & 0xff) << 24) |
381 (((hfp-1) & 0xff) << 16) |
382 (((hsw-1) & 0x3f) << 10);
384 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
385 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
387 reg = ((mode->vdisplay - 1) & 0x3ff) |
388 ((vbp & 0xff) << 24) |
389 ((vfp & 0xff) << 16) |
390 (((vsw-1) & 0x3f) << 10);
391 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
394 * be sure to set Bit 10 for the V2 LCDC controller,
395 * otherwise limited to 1024 pixels width, stopping
396 * 1920x1080 being suppoted.
398 if (priv->rev == 2) {
399 if ((mode->vdisplay - 1) & 0x400) {
400 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
403 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
408 /* Configure display type: */
409 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
410 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
411 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
412 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
413 if (info->tft_alt_mode)
414 reg |= LCDC_TFT_ALT_ENABLE;
415 if (priv->rev == 2) {
416 unsigned int depth, bpp;
418 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
423 reg |= LCDC_V2_TFT_24BPP_UNPACK;
426 reg |= LCDC_V2_TFT_24BPP_MODE;
429 dev_err(dev->dev, "invalid pixel format\n");
433 reg |= info->fdd < 12;
434 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
436 if (info->invert_pxl_clk)
437 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
439 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
442 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
444 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
447 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
449 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
452 * use value from adjusted_mode here as this might have been
453 * changed as part of the fixup for slave encoders to solve the
454 * issue where tilcdc timings are not VESA compliant
456 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
457 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
459 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
461 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
462 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
464 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
466 if (info->raster_order)
467 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
469 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
471 drm_framebuffer_reference(crtc->primary->fb);
473 set_scanout(crtc, crtc->primary->fb);
475 tilcdc_crtc_update_clk(crtc);
477 pm_runtime_put_sync(dev->dev);
482 static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
483 struct drm_framebuffer *old_fb)
485 struct drm_device *dev = crtc->dev;
488 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
492 drm_framebuffer_reference(crtc->primary->fb);
494 pm_runtime_get_sync(dev->dev);
496 set_scanout(crtc, crtc->primary->fb);
498 pm_runtime_put_sync(dev->dev);
503 static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
504 .destroy = tilcdc_crtc_destroy,
505 .set_config = drm_crtc_helper_set_config,
506 .page_flip = tilcdc_crtc_page_flip,
509 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
510 .dpms = tilcdc_crtc_dpms,
511 .mode_fixup = tilcdc_crtc_mode_fixup,
512 .prepare = tilcdc_crtc_prepare,
513 .commit = tilcdc_crtc_commit,
514 .mode_set = tilcdc_crtc_mode_set,
515 .mode_set_base = tilcdc_crtc_mode_set_base,
518 int tilcdc_crtc_max_width(struct drm_crtc *crtc)
520 struct drm_device *dev = crtc->dev;
521 struct tilcdc_drm_private *priv = dev->dev_private;
526 else if (priv->rev == 2)
532 int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
534 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
535 unsigned int bandwidth;
536 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
539 * check to see if the width is within the range that
540 * the LCD Controller physically supports
542 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
543 return MODE_VIRTUAL_X;
545 /* width must be multiple of 16 */
546 if (mode->hdisplay & 0xf)
547 return MODE_VIRTUAL_X;
549 if (mode->vdisplay > 2048)
550 return MODE_VIRTUAL_Y;
552 DBG("Processing mode %dx%d@%d with pixel clock %d",
553 mode->hdisplay, mode->vdisplay,
554 drm_mode_vrefresh(mode), mode->clock);
556 hbp = mode->htotal - mode->hsync_end;
557 hfp = mode->hsync_start - mode->hdisplay;
558 hsw = mode->hsync_end - mode->hsync_start;
559 vbp = mode->vtotal - mode->vsync_end;
560 vfp = mode->vsync_start - mode->vdisplay;
561 vsw = mode->vsync_end - mode->vsync_start;
563 if ((hbp-1) & ~0x3ff) {
564 DBG("Pruning mode: Horizontal Back Porch out of range");
565 return MODE_HBLANK_WIDE;
568 if ((hfp-1) & ~0x3ff) {
569 DBG("Pruning mode: Horizontal Front Porch out of range");
570 return MODE_HBLANK_WIDE;
573 if ((hsw-1) & ~0x3ff) {
574 DBG("Pruning mode: Horizontal Sync Width out of range");
575 return MODE_HSYNC_WIDE;
579 DBG("Pruning mode: Vertical Back Porch out of range");
580 return MODE_VBLANK_WIDE;
584 DBG("Pruning mode: Vertical Front Porch out of range");
585 return MODE_VBLANK_WIDE;
588 if ((vsw-1) & ~0x3f) {
589 DBG("Pruning mode: Vertical Sync Width out of range");
590 return MODE_VSYNC_WIDE;
594 * some devices have a maximum allowed pixel clock
595 * configured from the DT
597 if (mode->clock > priv->max_pixelclock) {
598 DBG("Pruning mode: pixel clock too high");
599 return MODE_CLOCK_HIGH;
603 * some devices further limit the max horizontal resolution
604 * configured from the DT
606 if (mode->hdisplay > priv->max_width)
607 return MODE_BAD_WIDTH;
609 /* filter out modes that would require too much memory bandwidth: */
610 bandwidth = mode->hdisplay * mode->vdisplay *
611 drm_mode_vrefresh(mode);
612 if (bandwidth > priv->max_bandwidth) {
613 DBG("Pruning mode: exceeds defined bandwidth limit");
620 void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
621 const struct tilcdc_panel_info *info)
623 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
624 tilcdc_crtc->info = info;
627 void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
628 bool simulate_vesa_sync)
630 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
632 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
635 void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
637 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
638 struct drm_device *dev = crtc->dev;
639 struct tilcdc_drm_private *priv = dev->dev_private;
640 int dpms = tilcdc_crtc->dpms;
641 unsigned long lcd_clk;
642 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
645 pm_runtime_get_sync(dev->dev);
647 if (dpms == DRM_MODE_DPMS_ON)
648 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
650 /* mode.clock is in KHz, set_rate wants parameter in Hz */
651 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
653 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
658 lcd_clk = clk_get_rate(priv->clk);
660 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
661 lcd_clk, crtc->mode.clock, clkdiv);
663 /* Configure the LCD clock divisor. */
664 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
668 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
669 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
670 LCDC_V2_CORE_CLK_EN);
672 if (dpms == DRM_MODE_DPMS_ON)
673 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
676 pm_runtime_put_sync(dev->dev);
679 #define SYNC_LOST_COUNT_LIMIT 50
681 irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
683 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
684 struct drm_device *dev = crtc->dev;
685 struct tilcdc_drm_private *priv = dev->dev_private;
688 stat = tilcdc_read_irqstatus(dev);
689 tilcdc_clear_irqstatus(dev, stat);
691 if (stat & LCDC_END_OF_FRAME0) {
693 bool skip_event = false;
698 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
700 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
702 tilcdc_crtc->last_vblank = now;
704 if (tilcdc_crtc->next_fb) {
705 set_scanout(crtc, tilcdc_crtc->next_fb);
706 tilcdc_crtc->next_fb = NULL;
710 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
712 drm_crtc_handle_vblank(crtc);
715 struct drm_pending_vblank_event *event;
717 spin_lock_irqsave(&dev->event_lock, flags);
719 event = tilcdc_crtc->event;
720 tilcdc_crtc->event = NULL;
722 drm_crtc_send_vblank_event(crtc, event);
724 spin_unlock_irqrestore(&dev->event_lock, flags);
727 if (tilcdc_crtc->frame_intact)
728 tilcdc_crtc->sync_lost_count = 0;
730 tilcdc_crtc->frame_intact = true;
733 if (stat & LCDC_FIFO_UNDERFLOW)
734 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
737 /* For revision 2 only */
738 if (priv->rev == 2) {
739 if (stat & LCDC_FRAME_DONE) {
740 tilcdc_crtc->frame_done = true;
741 wake_up(&tilcdc_crtc->frame_done_wq);
744 if (stat & LCDC_SYNC_LOST) {
745 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
747 tilcdc_crtc->frame_intact = false;
748 if (tilcdc_crtc->sync_lost_count++ >
749 SYNC_LOST_COUNT_LIMIT) {
750 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
751 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
756 /* Indicate to LCDC that the interrupt service routine has
757 * completed, see 13.3.6.1.6 in AM335x TRM.
759 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
765 struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
767 struct tilcdc_drm_private *priv = dev->dev_private;
768 struct tilcdc_crtc *tilcdc_crtc;
769 struct drm_crtc *crtc;
772 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
774 dev_err(dev->dev, "allocation failed\n");
778 crtc = &tilcdc_crtc->base;
780 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
781 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
783 drm_flip_work_init(&tilcdc_crtc->unref_work,
784 "unref", unref_worker);
786 spin_lock_init(&tilcdc_crtc->irq_lock);
788 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
792 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
794 if (priv->is_componentized) {
795 struct device_node *ports =
796 of_get_child_by_name(dev->dev->of_node, "ports");
799 crtc->port = of_get_child_by_name(ports, "port");
803 of_get_child_by_name(dev->dev->of_node, "port");
805 if (!crtc->port) { /* This should never happen */
806 dev_err(dev->dev, "Port node not found in %s\n",
807 dev->dev->of_node->full_name);
815 tilcdc_crtc_destroy(crtc);