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>
24 #include "tilcdc_drv.h"
25 #include "tilcdc_regs.h"
27 #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
32 struct drm_plane primary;
33 const struct tilcdc_panel_info *info;
34 struct drm_pending_vblank_event *event;
36 wait_queue_head_t frame_done_wq;
42 struct drm_framebuffer *curr_fb;
43 struct drm_framebuffer *next_fb;
45 /* for deferred fb unref's: */
46 struct drm_flip_work unref_work;
48 /* Only set if an external encoder is connected */
49 bool simulate_vesa_sync;
54 #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
56 static void unref_worker(struct drm_flip_work *work, void *val)
58 struct tilcdc_crtc *tilcdc_crtc =
59 container_of(work, struct tilcdc_crtc, unref_work);
60 struct drm_device *dev = tilcdc_crtc->base.dev;
62 mutex_lock(&dev->mode_config.mutex);
63 drm_framebuffer_unreference(val);
64 mutex_unlock(&dev->mode_config.mutex);
67 static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
69 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
70 struct drm_device *dev = crtc->dev;
71 struct drm_gem_cma_object *gem;
72 unsigned int depth, bpp;
73 dma_addr_t start, end;
74 u64 dma_base_and_ceiling;
76 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
77 gem = drm_fb_cma_get_gem_obj(fb, 0);
79 start = gem->paddr + fb->offsets[0] +
80 crtc->y * fb->pitches[0] +
83 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
85 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
86 * with a single insruction, if available. This should make it more
87 * unlikely that LCDC would fetch the DMA addresses in the middle of
90 dma_base_and_ceiling = (u64)(end - 1) << 32 | start;
91 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
93 if (tilcdc_crtc->curr_fb)
94 drm_flip_work_queue(&tilcdc_crtc->unref_work,
95 tilcdc_crtc->curr_fb);
97 tilcdc_crtc->curr_fb = fb;
100 static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
102 struct tilcdc_drm_private *priv = dev->dev_private;
104 tilcdc_clear_irqstatus(dev, 0xffffffff);
106 if (priv->rev == 1) {
107 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
108 LCDC_V1_UNDERFLOW_INT_ENA);
109 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
110 LCDC_V1_END_OF_FRAME_INT_ENA);
112 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
113 LCDC_V2_UNDERFLOW_INT_ENA |
114 LCDC_V2_END_OF_FRAME0_INT_ENA |
115 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
119 static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
121 struct tilcdc_drm_private *priv = dev->dev_private;
123 /* disable irqs that we might have enabled: */
124 if (priv->rev == 1) {
125 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
126 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
127 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
128 LCDC_V1_END_OF_FRAME_INT_ENA);
130 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
131 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
132 LCDC_V2_END_OF_FRAME0_INT_ENA |
133 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
137 static void reset(struct drm_crtc *crtc)
139 struct drm_device *dev = crtc->dev;
140 struct tilcdc_drm_private *priv = dev->dev_private;
145 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
146 usleep_range(250, 1000);
147 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
150 static void tilcdc_crtc_enable(struct drm_crtc *crtc)
152 struct drm_device *dev = crtc->dev;
153 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
155 if (tilcdc_crtc->enabled)
158 pm_runtime_get_sync(dev->dev);
162 tilcdc_crtc_enable_irqs(dev);
164 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
165 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
166 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
168 drm_crtc_vblank_on(crtc);
170 tilcdc_crtc->enabled = true;
173 void tilcdc_crtc_disable(struct drm_crtc *crtc)
175 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
176 struct drm_device *dev = crtc->dev;
177 struct tilcdc_drm_private *priv = dev->dev_private;
179 if (!tilcdc_crtc->enabled)
182 tilcdc_crtc->frame_done = false;
183 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
186 * if necessary wait for framedone irq which will still come
187 * before putting things to sleep..
189 if (priv->rev == 2) {
190 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
191 tilcdc_crtc->frame_done,
192 msecs_to_jiffies(500));
194 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
198 drm_crtc_vblank_off(crtc);
200 tilcdc_crtc_disable_irqs(dev);
202 pm_runtime_put_sync(dev->dev);
204 if (tilcdc_crtc->next_fb) {
205 drm_flip_work_queue(&tilcdc_crtc->unref_work,
206 tilcdc_crtc->next_fb);
207 tilcdc_crtc->next_fb = NULL;
210 if (tilcdc_crtc->curr_fb) {
211 drm_flip_work_queue(&tilcdc_crtc->unref_work,
212 tilcdc_crtc->curr_fb);
213 tilcdc_crtc->curr_fb = NULL;
216 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
217 tilcdc_crtc->last_vblank = ktime_set(0, 0);
219 tilcdc_crtc->enabled = false;
222 static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
224 return crtc->state && crtc->state->enable && crtc->state->active;
227 static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
229 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
231 tilcdc_crtc_disable(crtc);
233 of_node_put(crtc->port);
234 drm_crtc_cleanup(crtc);
235 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
238 int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
239 struct drm_framebuffer *fb,
240 struct drm_pending_vblank_event *event)
242 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
243 struct drm_device *dev = crtc->dev;
246 if (tilcdc_crtc->event) {
247 dev_err(dev->dev, "already pending page flip!\n");
251 drm_framebuffer_reference(fb);
253 crtc->primary->fb = fb;
255 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
257 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
261 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
262 1000000 / crtc->hwmode.vrefresh);
264 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
266 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
267 tilcdc_crtc->next_fb = fb;
270 if (tilcdc_crtc->next_fb != fb)
271 set_scanout(crtc, fb);
273 tilcdc_crtc->event = event;
275 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
280 static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
281 const struct drm_display_mode *mode,
282 struct drm_display_mode *adjusted_mode)
284 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
286 if (!tilcdc_crtc->simulate_vesa_sync)
290 * tilcdc does not generate VESA-compliant sync but aligns
291 * VS on the second edge of HS instead of first edge.
292 * We use adjusted_mode, to fixup sync by aligning both rising
293 * edges and add HSKEW offset to fix the sync.
295 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
296 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
298 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
299 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
300 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
302 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
303 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
309 static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
311 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
312 struct drm_device *dev = crtc->dev;
313 struct tilcdc_drm_private *priv = dev->dev_private;
314 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
315 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
316 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
317 struct drm_framebuffer *fb = crtc->primary->state->fb;
325 /* Configure the Burst Size and fifo threshold of DMA: */
326 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
327 switch (info->dma_burst_sz) {
329 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
332 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
335 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
338 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
341 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
344 dev_err(dev->dev, "invalid burst size\n");
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 /* Set 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 supported.
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 |
412 0x000ff000 /* Palette Loading Delay bits */);
413 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
414 if (info->tft_alt_mode)
415 reg |= LCDC_TFT_ALT_ENABLE;
416 if (priv->rev == 2) {
417 unsigned int depth, bpp;
419 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
424 reg |= LCDC_V2_TFT_24BPP_UNPACK;
427 reg |= LCDC_V2_TFT_24BPP_MODE;
430 dev_err(dev->dev, "invalid pixel format\n");
434 reg |= info->fdd < 12;
435 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
437 if (info->invert_pxl_clk)
438 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
440 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
443 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
445 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
448 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
450 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
452 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
453 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
455 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
457 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
458 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
460 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
462 if (info->raster_order)
463 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
465 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
467 drm_framebuffer_reference(fb);
469 set_scanout(crtc, fb);
471 tilcdc_crtc_update_clk(crtc);
473 crtc->hwmode = crtc->state->adjusted_mode;
476 static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
477 struct drm_crtc_state *state)
479 struct drm_display_mode *mode = &state->mode;
482 /* If we are not active we don't care */
486 if (state->state->planes[0].ptr != crtc->primary ||
487 state->state->planes[0].state == NULL ||
488 state->state->planes[0].state->crtc != crtc) {
489 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
493 ret = tilcdc_crtc_mode_valid(crtc, mode);
495 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
502 static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
503 .destroy = tilcdc_crtc_destroy,
504 .set_config = drm_atomic_helper_set_config,
505 .page_flip = drm_atomic_helper_page_flip,
506 .reset = drm_atomic_helper_crtc_reset,
507 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
508 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
511 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
512 .mode_fixup = tilcdc_crtc_mode_fixup,
513 .enable = tilcdc_crtc_enable,
514 .disable = tilcdc_crtc_disable,
515 .atomic_check = tilcdc_crtc_atomic_check,
516 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
519 int tilcdc_crtc_max_width(struct drm_crtc *crtc)
521 struct drm_device *dev = crtc->dev;
522 struct tilcdc_drm_private *priv = dev->dev_private;
527 else if (priv->rev == 2)
533 int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
535 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
536 unsigned int bandwidth;
537 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
540 * check to see if the width is within the range that
541 * the LCD Controller physically supports
543 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
544 return MODE_VIRTUAL_X;
546 /* width must be multiple of 16 */
547 if (mode->hdisplay & 0xf)
548 return MODE_VIRTUAL_X;
550 if (mode->vdisplay > 2048)
551 return MODE_VIRTUAL_Y;
553 DBG("Processing mode %dx%d@%d with pixel clock %d",
554 mode->hdisplay, mode->vdisplay,
555 drm_mode_vrefresh(mode), mode->clock);
557 hbp = mode->htotal - mode->hsync_end;
558 hfp = mode->hsync_start - mode->hdisplay;
559 hsw = mode->hsync_end - mode->hsync_start;
560 vbp = mode->vtotal - mode->vsync_end;
561 vfp = mode->vsync_start - mode->vdisplay;
562 vsw = mode->vsync_end - mode->vsync_start;
564 if ((hbp-1) & ~0x3ff) {
565 DBG("Pruning mode: Horizontal Back Porch out of range");
566 return MODE_HBLANK_WIDE;
569 if ((hfp-1) & ~0x3ff) {
570 DBG("Pruning mode: Horizontal Front Porch out of range");
571 return MODE_HBLANK_WIDE;
574 if ((hsw-1) & ~0x3ff) {
575 DBG("Pruning mode: Horizontal Sync Width out of range");
576 return MODE_HSYNC_WIDE;
580 DBG("Pruning mode: Vertical Back Porch out of range");
581 return MODE_VBLANK_WIDE;
585 DBG("Pruning mode: Vertical Front Porch out of range");
586 return MODE_VBLANK_WIDE;
589 if ((vsw-1) & ~0x3f) {
590 DBG("Pruning mode: Vertical Sync Width out of range");
591 return MODE_VSYNC_WIDE;
595 * some devices have a maximum allowed pixel clock
596 * configured from the DT
598 if (mode->clock > priv->max_pixelclock) {
599 DBG("Pruning mode: pixel clock too high");
600 return MODE_CLOCK_HIGH;
604 * some devices further limit the max horizontal resolution
605 * configured from the DT
607 if (mode->hdisplay > priv->max_width)
608 return MODE_BAD_WIDTH;
610 /* filter out modes that would require too much memory bandwidth: */
611 bandwidth = mode->hdisplay * mode->vdisplay *
612 drm_mode_vrefresh(mode);
613 if (bandwidth > priv->max_bandwidth) {
614 DBG("Pruning mode: exceeds defined bandwidth limit");
621 void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
622 const struct tilcdc_panel_info *info)
624 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
625 tilcdc_crtc->info = info;
628 void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
629 bool simulate_vesa_sync)
631 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
633 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
636 void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
638 struct drm_device *dev = crtc->dev;
639 struct tilcdc_drm_private *priv = dev->dev_private;
640 unsigned long lcd_clk;
641 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
644 pm_runtime_get_sync(dev->dev);
646 tilcdc_crtc_disable(crtc);
648 /* mode.clock is in KHz, set_rate wants parameter in Hz */
649 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
651 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
656 lcd_clk = clk_get_rate(priv->clk);
658 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
659 lcd_clk, crtc->mode.clock, clkdiv);
661 /* Configure the LCD clock divisor. */
662 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
666 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
667 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
668 LCDC_V2_CORE_CLK_EN);
670 if (tilcdc_crtc_is_on(crtc))
671 tilcdc_crtc_enable(crtc);
674 pm_runtime_put_sync(dev->dev);
677 #define SYNC_LOST_COUNT_LIMIT 50
679 irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
681 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
682 struct drm_device *dev = crtc->dev;
683 struct tilcdc_drm_private *priv = dev->dev_private;
686 stat = tilcdc_read_irqstatus(dev);
687 tilcdc_clear_irqstatus(dev, stat);
689 if (stat & LCDC_END_OF_FRAME0) {
691 bool skip_event = false;
696 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
698 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
700 tilcdc_crtc->last_vblank = now;
702 if (tilcdc_crtc->next_fb) {
703 set_scanout(crtc, tilcdc_crtc->next_fb);
704 tilcdc_crtc->next_fb = NULL;
708 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
710 drm_crtc_handle_vblank(crtc);
713 struct drm_pending_vblank_event *event;
715 spin_lock_irqsave(&dev->event_lock, flags);
717 event = tilcdc_crtc->event;
718 tilcdc_crtc->event = NULL;
720 drm_crtc_send_vblank_event(crtc, event);
722 spin_unlock_irqrestore(&dev->event_lock, flags);
725 if (tilcdc_crtc->frame_intact)
726 tilcdc_crtc->sync_lost_count = 0;
728 tilcdc_crtc->frame_intact = true;
731 if (stat & LCDC_FIFO_UNDERFLOW)
732 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
735 /* For revision 2 only */
736 if (priv->rev == 2) {
737 if (stat & LCDC_FRAME_DONE) {
738 tilcdc_crtc->frame_done = true;
739 wake_up(&tilcdc_crtc->frame_done_wq);
742 if (stat & LCDC_SYNC_LOST) {
743 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
745 tilcdc_crtc->frame_intact = false;
746 if (tilcdc_crtc->sync_lost_count++ >
747 SYNC_LOST_COUNT_LIMIT) {
748 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
749 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
754 /* Indicate to LCDC that the interrupt service routine has
755 * completed, see 13.3.6.1.6 in AM335x TRM.
757 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
763 struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
765 struct tilcdc_drm_private *priv = dev->dev_private;
766 struct tilcdc_crtc *tilcdc_crtc;
767 struct drm_crtc *crtc;
770 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
772 dev_err(dev->dev, "allocation failed\n");
776 crtc = &tilcdc_crtc->base;
778 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
782 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
784 drm_flip_work_init(&tilcdc_crtc->unref_work,
785 "unref", unref_worker);
787 spin_lock_init(&tilcdc_crtc->irq_lock);
789 ret = drm_crtc_init_with_planes(dev, crtc,
790 &tilcdc_crtc->primary,
797 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
799 if (priv->is_componentized) {
800 struct device_node *ports =
801 of_get_child_by_name(dev->dev->of_node, "ports");
804 crtc->port = of_get_child_by_name(ports, "port");
808 of_get_child_by_name(dev->dev->of_node, "port");
810 if (!crtc->port) { /* This should never happen */
811 dev_err(dev->dev, "Port node not found in %s\n",
812 dev->dev->of_node->full_name);
820 tilcdc_crtc_destroy(crtc);