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>
20 #include <drm/drm_atomic_helper.h>
22 #include "tilcdc_drv.h"
23 #include "tilcdc_regs.h"
25 #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
30 struct drm_plane primary;
31 const struct tilcdc_panel_info *info;
32 struct drm_pending_vblank_event *event;
34 wait_queue_head_t frame_done_wq;
40 struct drm_framebuffer *curr_fb;
41 struct drm_framebuffer *next_fb;
43 /* for deferred fb unref's: */
44 struct drm_flip_work unref_work;
46 /* Only set if an external encoder is connected */
47 bool simulate_vesa_sync;
52 #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
54 static void unref_worker(struct drm_flip_work *work, void *val)
56 struct tilcdc_crtc *tilcdc_crtc =
57 container_of(work, struct tilcdc_crtc, unref_work);
58 struct drm_device *dev = tilcdc_crtc->base.dev;
60 mutex_lock(&dev->mode_config.mutex);
61 drm_framebuffer_unreference(val);
62 mutex_unlock(&dev->mode_config.mutex);
65 static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
67 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
68 struct drm_device *dev = crtc->dev;
69 struct drm_gem_cma_object *gem;
70 unsigned int depth, bpp;
71 dma_addr_t start, end;
73 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
74 gem = drm_fb_cma_get_gem_obj(fb, 0);
76 start = gem->paddr + fb->offsets[0] +
77 crtc->y * fb->pitches[0] +
80 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
82 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
83 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
85 if (tilcdc_crtc->curr_fb)
86 drm_flip_work_queue(&tilcdc_crtc->unref_work,
87 tilcdc_crtc->curr_fb);
89 tilcdc_crtc->curr_fb = fb;
92 static void reset(struct drm_crtc *crtc)
94 struct drm_device *dev = crtc->dev;
95 struct tilcdc_drm_private *priv = dev->dev_private;
100 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
101 usleep_range(250, 1000);
102 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
105 static void start(struct drm_crtc *crtc)
107 struct drm_device *dev = crtc->dev;
111 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
112 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
113 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
115 drm_crtc_vblank_on(crtc);
118 static void stop(struct drm_crtc *crtc)
120 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
121 struct drm_device *dev = crtc->dev;
122 struct tilcdc_drm_private *priv = dev->dev_private;
124 tilcdc_crtc->frame_done = false;
125 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
128 * if necessary wait for framedone irq which will still come
129 * before putting things to sleep..
131 if (priv->rev == 2) {
132 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
133 tilcdc_crtc->frame_done,
134 msecs_to_jiffies(500));
136 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
140 drm_crtc_vblank_off(crtc);
143 static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
145 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
147 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
149 of_node_put(crtc->port);
150 drm_crtc_cleanup(crtc);
151 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
154 static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
156 struct drm_device *dev = crtc->dev;
157 unsigned int depth, bpp;
159 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
161 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
163 "Invalid pitch: fb and crtc widths must be the same");
170 int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
171 struct drm_framebuffer *fb,
172 struct drm_pending_vblank_event *event,
173 uint32_t page_flip_flags)
175 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
176 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 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
201 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
202 1000000 / crtc->hwmode.vrefresh);
204 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
206 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
207 tilcdc_crtc->next_fb = fb;
210 if (tilcdc_crtc->next_fb != fb)
211 set_scanout(crtc, fb);
213 tilcdc_crtc->event = event;
215 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
217 pm_runtime_put_sync(dev->dev);
222 void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
224 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
225 struct drm_device *dev = crtc->dev;
226 struct tilcdc_drm_private *priv = dev->dev_private;
228 /* we really only care about on or off: */
229 if (mode != DRM_MODE_DPMS_ON)
230 mode = DRM_MODE_DPMS_OFF;
232 if (tilcdc_crtc->dpms == mode)
235 tilcdc_crtc->dpms = mode;
237 if (mode == DRM_MODE_DPMS_ON) {
238 pm_runtime_get_sync(dev->dev);
242 pm_runtime_put_sync(dev->dev);
244 if (tilcdc_crtc->next_fb) {
245 drm_flip_work_queue(&tilcdc_crtc->unref_work,
246 tilcdc_crtc->next_fb);
247 tilcdc_crtc->next_fb = NULL;
250 if (tilcdc_crtc->curr_fb) {
251 drm_flip_work_queue(&tilcdc_crtc->unref_work,
252 tilcdc_crtc->curr_fb);
253 tilcdc_crtc->curr_fb = NULL;
256 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
257 tilcdc_crtc->last_vblank = ktime_set(0, 0);
261 int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc)
263 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
265 return tilcdc_crtc->dpms;
268 static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
269 const struct drm_display_mode *mode,
270 struct drm_display_mode *adjusted_mode)
272 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
274 if (!tilcdc_crtc->simulate_vesa_sync)
278 * tilcdc does not generate VESA-compliant sync but aligns
279 * VS on the second edge of HS instead of first edge.
280 * We use adjusted_mode, to fixup sync by aligning both rising
281 * edges and add HSKEW offset to fix the sync.
283 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
284 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
286 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
287 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
288 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
290 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
291 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
297 static void tilcdc_crtc_disable(struct drm_crtc *crtc)
299 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
302 static void tilcdc_crtc_enable(struct drm_crtc *crtc)
304 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
307 static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
309 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
310 struct drm_device *dev = crtc->dev;
311 struct tilcdc_drm_private *priv = dev->dev_private;
312 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
313 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
314 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
315 struct drm_framebuffer *fb = crtc->primary->state->fb;
323 pm_runtime_get_sync(dev->dev);
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 pm_runtime_put_sync(dev->dev);
475 crtc->hwmode = crtc->state->adjusted_mode;
478 static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
479 struct drm_crtc_state *state)
481 struct drm_display_mode *mode = &state->mode;
484 /* If we are not active we don't care */
488 if (state->state->planes[0].ptr != crtc->primary ||
489 state->state->planes[0].state == NULL ||
490 state->state->planes[0].state->crtc != crtc) {
491 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
495 ret = tilcdc_crtc_mode_valid(crtc, mode);
497 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
504 static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
505 struct drm_display_mode *mode,
506 struct drm_display_mode *adjusted_mode,
508 struct drm_framebuffer *old_fb)
510 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
511 struct drm_device *dev = crtc->dev;
512 struct tilcdc_drm_private *priv = dev->dev_private;
513 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
514 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
517 ret = tilcdc_crtc_mode_valid(crtc, mode);
524 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
528 pm_runtime_get_sync(dev->dev);
530 /* Configure the Burst Size and fifo threshold of DMA: */
531 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
532 switch (info->dma_burst_sz) {
534 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
537 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
540 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
543 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
546 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
551 reg |= (info->fifo_th << 8);
552 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
554 /* Configure timings: */
555 hbp = mode->htotal - mode->hsync_end;
556 hfp = mode->hsync_start - mode->hdisplay;
557 hsw = mode->hsync_end - mode->hsync_start;
558 vbp = mode->vtotal - mode->vsync_end;
559 vfp = mode->vsync_start - mode->vdisplay;
560 vsw = mode->vsync_end - mode->vsync_start;
562 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
563 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
565 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
566 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
567 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
568 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
571 * subtract one from hfp, hbp, hsw because the hardware uses
574 if (priv->rev == 2) {
575 /* clear bits we're going to set */
577 reg |= ((hfp-1) & 0x300) >> 8;
578 reg |= ((hbp-1) & 0x300) >> 4;
579 reg |= ((hsw-1) & 0x3c0) << 21;
581 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
583 reg = (((mode->hdisplay >> 4) - 1) << 4) |
584 (((hbp-1) & 0xff) << 24) |
585 (((hfp-1) & 0xff) << 16) |
586 (((hsw-1) & 0x3f) << 10);
588 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
589 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
591 reg = ((mode->vdisplay - 1) & 0x3ff) |
592 ((vbp & 0xff) << 24) |
593 ((vfp & 0xff) << 16) |
594 (((vsw-1) & 0x3f) << 10);
595 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
598 * be sure to set Bit 10 for the V2 LCDC controller,
599 * otherwise limited to 1024 pixels width, stopping
600 * 1920x1080 being suppoted.
602 if (priv->rev == 2) {
603 if ((mode->vdisplay - 1) & 0x400) {
604 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
607 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
612 /* Configure display type: */
613 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
614 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
615 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
616 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
617 if (info->tft_alt_mode)
618 reg |= LCDC_TFT_ALT_ENABLE;
619 if (priv->rev == 2) {
620 unsigned int depth, bpp;
622 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
627 reg |= LCDC_V2_TFT_24BPP_UNPACK;
630 reg |= LCDC_V2_TFT_24BPP_MODE;
633 dev_err(dev->dev, "invalid pixel format\n");
637 reg |= info->fdd < 12;
638 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
640 if (info->invert_pxl_clk)
641 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
643 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
646 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
648 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
651 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
653 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
656 * use value from adjusted_mode here as this might have been
657 * changed as part of the fixup for slave encoders to solve the
658 * issue where tilcdc timings are not VESA compliant
660 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
661 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
663 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
665 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
666 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
668 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
670 if (info->raster_order)
671 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
673 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
675 drm_framebuffer_reference(crtc->primary->fb);
677 set_scanout(crtc, crtc->primary->fb);
679 tilcdc_crtc_update_clk(crtc);
681 pm_runtime_put_sync(dev->dev);
686 static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
687 struct drm_framebuffer *old_fb)
689 struct drm_device *dev = crtc->dev;
692 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
696 drm_framebuffer_reference(crtc->primary->fb);
698 pm_runtime_get_sync(dev->dev);
700 set_scanout(crtc, crtc->primary->fb);
702 pm_runtime_put_sync(dev->dev);
707 static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
708 .destroy = tilcdc_crtc_destroy,
709 .set_config = drm_atomic_helper_set_config,
710 .page_flip = drm_atomic_helper_page_flip,
711 .reset = drm_atomic_helper_crtc_reset,
712 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
713 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
716 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
717 .dpms = tilcdc_crtc_dpms,
718 .mode_fixup = tilcdc_crtc_mode_fixup,
719 .prepare = tilcdc_crtc_disable,
720 .commit = tilcdc_crtc_enable,
721 .mode_set = tilcdc_crtc_mode_set,
722 .mode_set_base = tilcdc_crtc_mode_set_base,
723 .enable = tilcdc_crtc_enable,
724 .disable = tilcdc_crtc_disable,
725 .atomic_check = tilcdc_crtc_atomic_check,
726 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
729 int tilcdc_crtc_max_width(struct drm_crtc *crtc)
731 struct drm_device *dev = crtc->dev;
732 struct tilcdc_drm_private *priv = dev->dev_private;
737 else if (priv->rev == 2)
743 int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
745 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
746 unsigned int bandwidth;
747 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
750 * check to see if the width is within the range that
751 * the LCD Controller physically supports
753 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
754 return MODE_VIRTUAL_X;
756 /* width must be multiple of 16 */
757 if (mode->hdisplay & 0xf)
758 return MODE_VIRTUAL_X;
760 if (mode->vdisplay > 2048)
761 return MODE_VIRTUAL_Y;
763 DBG("Processing mode %dx%d@%d with pixel clock %d",
764 mode->hdisplay, mode->vdisplay,
765 drm_mode_vrefresh(mode), mode->clock);
767 hbp = mode->htotal - mode->hsync_end;
768 hfp = mode->hsync_start - mode->hdisplay;
769 hsw = mode->hsync_end - mode->hsync_start;
770 vbp = mode->vtotal - mode->vsync_end;
771 vfp = mode->vsync_start - mode->vdisplay;
772 vsw = mode->vsync_end - mode->vsync_start;
774 if ((hbp-1) & ~0x3ff) {
775 DBG("Pruning mode: Horizontal Back Porch out of range");
776 return MODE_HBLANK_WIDE;
779 if ((hfp-1) & ~0x3ff) {
780 DBG("Pruning mode: Horizontal Front Porch out of range");
781 return MODE_HBLANK_WIDE;
784 if ((hsw-1) & ~0x3ff) {
785 DBG("Pruning mode: Horizontal Sync Width out of range");
786 return MODE_HSYNC_WIDE;
790 DBG("Pruning mode: Vertical Back Porch out of range");
791 return MODE_VBLANK_WIDE;
795 DBG("Pruning mode: Vertical Front Porch out of range");
796 return MODE_VBLANK_WIDE;
799 if ((vsw-1) & ~0x3f) {
800 DBG("Pruning mode: Vertical Sync Width out of range");
801 return MODE_VSYNC_WIDE;
805 * some devices have a maximum allowed pixel clock
806 * configured from the DT
808 if (mode->clock > priv->max_pixelclock) {
809 DBG("Pruning mode: pixel clock too high");
810 return MODE_CLOCK_HIGH;
814 * some devices further limit the max horizontal resolution
815 * configured from the DT
817 if (mode->hdisplay > priv->max_width)
818 return MODE_BAD_WIDTH;
820 /* filter out modes that would require too much memory bandwidth: */
821 bandwidth = mode->hdisplay * mode->vdisplay *
822 drm_mode_vrefresh(mode);
823 if (bandwidth > priv->max_bandwidth) {
824 DBG("Pruning mode: exceeds defined bandwidth limit");
831 void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
832 const struct tilcdc_panel_info *info)
834 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
835 tilcdc_crtc->info = info;
838 void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
839 bool simulate_vesa_sync)
841 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
843 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
846 void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
848 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
849 struct drm_device *dev = crtc->dev;
850 struct tilcdc_drm_private *priv = dev->dev_private;
851 int dpms = tilcdc_crtc->dpms;
852 unsigned long lcd_clk;
853 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
856 pm_runtime_get_sync(dev->dev);
858 if (dpms == DRM_MODE_DPMS_ON)
859 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
861 /* mode.clock is in KHz, set_rate wants parameter in Hz */
862 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
864 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
869 lcd_clk = clk_get_rate(priv->clk);
871 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
872 lcd_clk, crtc->mode.clock, clkdiv);
874 /* Configure the LCD clock divisor. */
875 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
879 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
880 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
881 LCDC_V2_CORE_CLK_EN);
883 if (dpms == DRM_MODE_DPMS_ON)
884 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
887 pm_runtime_put_sync(dev->dev);
890 #define SYNC_LOST_COUNT_LIMIT 50
892 irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
894 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
895 struct drm_device *dev = crtc->dev;
896 struct tilcdc_drm_private *priv = dev->dev_private;
899 stat = tilcdc_read_irqstatus(dev);
900 tilcdc_clear_irqstatus(dev, stat);
902 if (stat & LCDC_END_OF_FRAME0) {
904 bool skip_event = false;
909 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
911 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
913 tilcdc_crtc->last_vblank = now;
915 if (tilcdc_crtc->next_fb) {
916 set_scanout(crtc, tilcdc_crtc->next_fb);
917 tilcdc_crtc->next_fb = NULL;
921 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
923 drm_crtc_handle_vblank(crtc);
926 struct drm_pending_vblank_event *event;
928 spin_lock_irqsave(&dev->event_lock, flags);
930 event = tilcdc_crtc->event;
931 tilcdc_crtc->event = NULL;
933 drm_crtc_send_vblank_event(crtc, event);
935 spin_unlock_irqrestore(&dev->event_lock, flags);
938 if (tilcdc_crtc->frame_intact)
939 tilcdc_crtc->sync_lost_count = 0;
941 tilcdc_crtc->frame_intact = true;
944 if (stat & LCDC_FIFO_UNDERFLOW)
945 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
948 /* For revision 2 only */
949 if (priv->rev == 2) {
950 if (stat & LCDC_FRAME_DONE) {
951 tilcdc_crtc->frame_done = true;
952 wake_up(&tilcdc_crtc->frame_done_wq);
955 if (stat & LCDC_SYNC_LOST) {
956 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
958 tilcdc_crtc->frame_intact = false;
959 if (tilcdc_crtc->sync_lost_count++ >
960 SYNC_LOST_COUNT_LIMIT) {
961 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
962 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
967 /* Indicate to LCDC that the interrupt service routine has
968 * completed, see 13.3.6.1.6 in AM335x TRM.
970 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
976 struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
978 struct tilcdc_drm_private *priv = dev->dev_private;
979 struct tilcdc_crtc *tilcdc_crtc;
980 struct drm_crtc *crtc;
983 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
985 dev_err(dev->dev, "allocation failed\n");
989 crtc = &tilcdc_crtc->base;
991 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
995 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
996 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
998 drm_flip_work_init(&tilcdc_crtc->unref_work,
999 "unref", unref_worker);
1001 spin_lock_init(&tilcdc_crtc->irq_lock);
1003 ret = drm_crtc_init_with_planes(dev, crtc,
1004 &tilcdc_crtc->primary,
1011 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
1013 if (priv->is_componentized) {
1014 struct device_node *ports =
1015 of_get_child_by_name(dev->dev->of_node, "ports");
1018 crtc->port = of_get_child_by_name(ports, "port");
1022 of_get_child_by_name(dev->dev->of_node, "port");
1024 if (!crtc->port) { /* This should never happen */
1025 dev_err(dev->dev, "Port node not found in %s\n",
1026 dev->dev->of_node->full_name);
1034 tilcdc_crtc_destroy(crtc);