3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Inki Dae <inki.dae@samsung.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
20 #include <linux/of_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/component.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/regmap.h>
26 #include <video/of_display_timing.h>
27 #include <video/of_videomode.h>
28 #include <video/samsung_fimd.h>
29 #include <drm/exynos_drm.h>
31 #include "exynos_drm_drv.h"
32 #include "exynos_drm_fbdev.h"
33 #include "exynos_drm_crtc.h"
34 #include "exynos_drm_iommu.h"
37 * FIMD stands for Fully Interactive Mobile Display and
38 * as a display controller, it transfers contents drawn on memory
39 * to a LCD Panel through Display Interfaces such as RGB or
43 #define FIMD_DEFAULT_FRAMERATE 60
44 #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
46 /* position control register for hardware window 0, 2 ~ 4.*/
47 #define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
48 #define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
50 * size control register for hardware windows 0 and alpha control register
51 * for hardware windows 1 ~ 4
53 #define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
54 /* size control register for hardware windows 1 ~ 2. */
55 #define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
57 #define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
58 #define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
59 #define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
61 /* color key control register for hardware window 1 ~ 4. */
62 #define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
63 /* color key value register for hardware window 1 ~ 4. */
64 #define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
66 /* I80 / RGB trigger control register */
68 #define TRGMODE_I80_RGB_ENABLE_I80 (1 << 0)
69 #define SWTRGCMD_I80_RGB_ENABLE (1 << 1)
71 /* display mode change control register except exynos4 */
72 #define VIDOUT_CON 0x000
73 #define VIDOUT_CON_F_I80_LDI0 (0x2 << 8)
75 /* I80 interface control for main LDI register */
76 #define I80IFCONFAx(x) (0x1B0 + (x) * 4)
77 #define I80IFCONFBx(x) (0x1B8 + (x) * 4)
78 #define LCD_CS_SETUP(x) ((x) << 16)
79 #define LCD_WR_SETUP(x) ((x) << 12)
80 #define LCD_WR_ACTIVE(x) ((x) << 8)
81 #define LCD_WR_HOLD(x) ((x) << 4)
82 #define I80IFEN_ENABLE (1 << 0)
84 /* FIMD has totally five hardware windows. */
87 struct fimd_driver_data {
88 unsigned int timing_base;
89 unsigned int lcdblk_offset;
90 unsigned int lcdblk_vt_shift;
91 unsigned int lcdblk_bypass_shift;
93 unsigned int has_shadowcon:1;
94 unsigned int has_clksel:1;
95 unsigned int has_limited_fmt:1;
96 unsigned int has_vidoutcon:1;
97 unsigned int has_vtsel:1;
100 static struct fimd_driver_data s3c64xx_fimd_driver_data = {
103 .has_limited_fmt = 1,
106 static struct fimd_driver_data exynos3_fimd_driver_data = {
107 .timing_base = 0x20000,
108 .lcdblk_offset = 0x210,
109 .lcdblk_bypass_shift = 1,
114 static struct fimd_driver_data exynos4_fimd_driver_data = {
116 .lcdblk_offset = 0x210,
117 .lcdblk_vt_shift = 10,
118 .lcdblk_bypass_shift = 1,
123 static struct fimd_driver_data exynos4415_fimd_driver_data = {
124 .timing_base = 0x20000,
125 .lcdblk_offset = 0x210,
126 .lcdblk_vt_shift = 10,
127 .lcdblk_bypass_shift = 1,
133 static struct fimd_driver_data exynos5_fimd_driver_data = {
134 .timing_base = 0x20000,
135 .lcdblk_offset = 0x214,
136 .lcdblk_vt_shift = 24,
137 .lcdblk_bypass_shift = 15,
143 struct fimd_win_data {
144 unsigned int offset_x;
145 unsigned int offset_y;
146 unsigned int ovl_width;
147 unsigned int ovl_height;
148 unsigned int fb_width;
149 unsigned int fb_height;
151 unsigned int pixel_format;
153 unsigned int buf_offsize;
154 unsigned int line_size; /* bytes */
159 struct fimd_context {
161 struct drm_device *drm_dev;
162 struct exynos_drm_crtc *crtc;
166 struct regmap *sysreg;
167 struct fimd_win_data win_data[WINDOWS_NR];
168 unsigned int default_win;
169 unsigned long irq_flags;
177 wait_queue_head_t wait_vsync_queue;
178 atomic_t wait_vsync_event;
179 atomic_t win_updated;
182 struct exynos_drm_panel_info panel;
183 struct fimd_driver_data *driver_data;
184 struct exynos_drm_display *display;
187 static const struct of_device_id fimd_driver_dt_match[] = {
188 { .compatible = "samsung,s3c6400-fimd",
189 .data = &s3c64xx_fimd_driver_data },
190 { .compatible = "samsung,exynos3250-fimd",
191 .data = &exynos3_fimd_driver_data },
192 { .compatible = "samsung,exynos4210-fimd",
193 .data = &exynos4_fimd_driver_data },
194 { .compatible = "samsung,exynos4415-fimd",
195 .data = &exynos4415_fimd_driver_data },
196 { .compatible = "samsung,exynos5250-fimd",
197 .data = &exynos5_fimd_driver_data },
200 MODULE_DEVICE_TABLE(of, fimd_driver_dt_match);
202 static inline struct fimd_driver_data *drm_fimd_get_driver_data(
203 struct platform_device *pdev)
205 const struct of_device_id *of_id =
206 of_match_device(fimd_driver_dt_match, &pdev->dev);
208 return (struct fimd_driver_data *)of_id->data;
211 static void fimd_wait_for_vblank(struct exynos_drm_crtc *crtc)
213 struct fimd_context *ctx = crtc->ctx;
218 atomic_set(&ctx->wait_vsync_event, 1);
221 * wait for FIMD to signal VSYNC interrupt or return after
222 * timeout which is set to 50ms (refresh rate of 20).
224 if (!wait_event_timeout(ctx->wait_vsync_queue,
225 !atomic_read(&ctx->wait_vsync_event),
227 DRM_DEBUG_KMS("vblank wait timed out.\n");
230 static void fimd_enable_video_output(struct fimd_context *ctx, int win,
233 u32 val = readl(ctx->regs + WINCON(win));
236 val |= WINCONx_ENWIN;
238 val &= ~WINCONx_ENWIN;
240 writel(val, ctx->regs + WINCON(win));
243 static void fimd_enable_shadow_channel_path(struct fimd_context *ctx, int win,
246 u32 val = readl(ctx->regs + SHADOWCON);
249 val |= SHADOWCON_CHx_ENABLE(win);
251 val &= ~SHADOWCON_CHx_ENABLE(win);
253 writel(val, ctx->regs + SHADOWCON);
256 static void fimd_clear_channel(struct fimd_context *ctx)
258 int win, ch_enabled = 0;
260 DRM_DEBUG_KMS("%s\n", __FILE__);
262 /* Check if any channel is enabled. */
263 for (win = 0; win < WINDOWS_NR; win++) {
264 u32 val = readl(ctx->regs + WINCON(win));
266 if (val & WINCONx_ENWIN) {
267 fimd_enable_video_output(ctx, win, false);
269 if (ctx->driver_data->has_shadowcon)
270 fimd_enable_shadow_channel_path(ctx, win,
277 /* Wait for vsync, as disable channel takes effect at next vsync */
279 unsigned int state = ctx->suspended;
282 fimd_wait_for_vblank(ctx->crtc);
283 ctx->suspended = state;
287 static int fimd_iommu_attach_devices(struct fimd_context *ctx,
288 struct drm_device *drm_dev)
291 /* attach this sub driver to iommu mapping if supported. */
292 if (is_drm_iommu_supported(ctx->drm_dev)) {
296 * If any channel is already active, iommu will throw
297 * a PAGE FAULT when enabled. So clear any channel if enabled.
299 fimd_clear_channel(ctx);
300 ret = drm_iommu_attach_device(ctx->drm_dev, ctx->dev);
302 DRM_ERROR("drm_iommu_attach failed.\n");
311 static void fimd_iommu_detach_devices(struct fimd_context *ctx)
313 /* detach this sub driver from iommu mapping if supported. */
314 if (is_drm_iommu_supported(ctx->drm_dev))
315 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
318 static u32 fimd_calc_clkdiv(struct fimd_context *ctx,
319 const struct drm_display_mode *mode)
321 unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
326 * The frame done interrupt should be occurred prior to the
332 /* Find the clock divider value that gets us closest to ideal_clk */
333 clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->lcd_clk), ideal_clk);
335 return (clkdiv < 0x100) ? clkdiv : 0xff;
338 static bool fimd_mode_fixup(struct exynos_drm_crtc *crtc,
339 const struct drm_display_mode *mode,
340 struct drm_display_mode *adjusted_mode)
342 if (adjusted_mode->vrefresh == 0)
343 adjusted_mode->vrefresh = FIMD_DEFAULT_FRAMERATE;
348 static void fimd_commit(struct exynos_drm_crtc *crtc)
350 struct fimd_context *ctx = crtc->ctx;
351 struct drm_display_mode *mode = &crtc->base.mode;
352 struct fimd_driver_data *driver_data = ctx->driver_data;
353 void *timing_base = ctx->regs + driver_data->timing_base;
359 /* nothing to do if we haven't set the mode yet */
360 if (mode->htotal == 0 || mode->vtotal == 0)
364 val = ctx->i80ifcon | I80IFEN_ENABLE;
365 writel(val, timing_base + I80IFCONFAx(0));
367 /* disable auto frame rate */
368 writel(0, timing_base + I80IFCONFBx(0));
370 /* set video type selection to I80 interface */
371 if (driver_data->has_vtsel && ctx->sysreg &&
372 regmap_update_bits(ctx->sysreg,
373 driver_data->lcdblk_offset,
374 0x3 << driver_data->lcdblk_vt_shift,
375 0x1 << driver_data->lcdblk_vt_shift)) {
376 DRM_ERROR("Failed to update sysreg for I80 i/f.\n");
380 int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
383 /* setup polarity values */
384 vidcon1 = ctx->vidcon1;
385 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
386 vidcon1 |= VIDCON1_INV_VSYNC;
387 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
388 vidcon1 |= VIDCON1_INV_HSYNC;
389 writel(vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
391 /* setup vertical timing values. */
392 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
393 vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
394 vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
396 val = VIDTCON0_VBPD(vbpd - 1) |
397 VIDTCON0_VFPD(vfpd - 1) |
398 VIDTCON0_VSPW(vsync_len - 1);
399 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
401 /* setup horizontal timing values. */
402 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
403 hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
404 hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
406 val = VIDTCON1_HBPD(hbpd - 1) |
407 VIDTCON1_HFPD(hfpd - 1) |
408 VIDTCON1_HSPW(hsync_len - 1);
409 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
412 if (driver_data->has_vidoutcon)
413 writel(ctx->vidout_con, timing_base + VIDOUT_CON);
415 /* set bypass selection */
416 if (ctx->sysreg && regmap_update_bits(ctx->sysreg,
417 driver_data->lcdblk_offset,
418 0x1 << driver_data->lcdblk_bypass_shift,
419 0x1 << driver_data->lcdblk_bypass_shift)) {
420 DRM_ERROR("Failed to update sysreg for bypass setting.\n");
424 /* setup horizontal and vertical display size. */
425 val = VIDTCON2_LINEVAL(mode->vdisplay - 1) |
426 VIDTCON2_HOZVAL(mode->hdisplay - 1) |
427 VIDTCON2_LINEVAL_E(mode->vdisplay - 1) |
428 VIDTCON2_HOZVAL_E(mode->hdisplay - 1);
429 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
432 * fields of register with prefix '_F' would be updated
433 * at vsync(same as dma start)
436 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
438 if (ctx->driver_data->has_clksel)
439 val |= VIDCON0_CLKSEL_LCD;
441 clkdiv = fimd_calc_clkdiv(ctx, mode);
443 val |= VIDCON0_CLKVAL_F(clkdiv - 1) | VIDCON0_CLKDIR;
445 writel(val, ctx->regs + VIDCON0);
448 static int fimd_enable_vblank(struct exynos_drm_crtc *crtc)
450 struct fimd_context *ctx = crtc->ctx;
456 if (!test_and_set_bit(0, &ctx->irq_flags)) {
457 val = readl(ctx->regs + VIDINTCON0);
459 val |= VIDINTCON0_INT_ENABLE;
462 val |= VIDINTCON0_INT_I80IFDONE;
463 val |= VIDINTCON0_INT_SYSMAINCON;
464 val &= ~VIDINTCON0_INT_SYSSUBCON;
466 val |= VIDINTCON0_INT_FRAME;
468 val &= ~VIDINTCON0_FRAMESEL0_MASK;
469 val |= VIDINTCON0_FRAMESEL0_VSYNC;
470 val &= ~VIDINTCON0_FRAMESEL1_MASK;
471 val |= VIDINTCON0_FRAMESEL1_NONE;
474 writel(val, ctx->regs + VIDINTCON0);
480 static void fimd_disable_vblank(struct exynos_drm_crtc *crtc)
482 struct fimd_context *ctx = crtc->ctx;
488 if (test_and_clear_bit(0, &ctx->irq_flags)) {
489 val = readl(ctx->regs + VIDINTCON0);
491 val &= ~VIDINTCON0_INT_ENABLE;
494 val &= ~VIDINTCON0_INT_I80IFDONE;
495 val &= ~VIDINTCON0_INT_SYSMAINCON;
496 val &= ~VIDINTCON0_INT_SYSSUBCON;
498 val &= ~VIDINTCON0_INT_FRAME;
500 writel(val, ctx->regs + VIDINTCON0);
504 static void fimd_win_mode_set(struct exynos_drm_crtc *crtc,
505 struct exynos_drm_plane *plane)
507 struct fimd_context *ctx = crtc->ctx;
508 struct fimd_win_data *win_data;
510 unsigned long offset;
513 DRM_ERROR("plane is NULL\n");
518 if (win == DEFAULT_ZPOS)
519 win = ctx->default_win;
521 if (win < 0 || win >= WINDOWS_NR)
524 offset = plane->fb_x * (plane->bpp >> 3);
525 offset += plane->fb_y * plane->pitch;
527 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, plane->pitch);
529 win_data = &ctx->win_data[win];
531 win_data->offset_x = plane->crtc_x;
532 win_data->offset_y = plane->crtc_y;
533 win_data->ovl_width = plane->crtc_width;
534 win_data->ovl_height = plane->crtc_height;
535 win_data->fb_width = plane->fb_width;
536 win_data->fb_height = plane->fb_height;
537 win_data->dma_addr = plane->dma_addr[0] + offset;
538 win_data->bpp = plane->bpp;
539 win_data->pixel_format = plane->pixel_format;
540 win_data->buf_offsize = (plane->fb_width - plane->crtc_width) *
542 win_data->line_size = plane->crtc_width * (plane->bpp >> 3);
544 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
545 win_data->offset_x, win_data->offset_y);
546 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
547 win_data->ovl_width, win_data->ovl_height);
548 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
549 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
550 plane->fb_width, plane->crtc_width);
553 static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win)
555 struct fimd_win_data *win_data = &ctx->win_data[win];
561 * In case of s3c64xx, window 0 doesn't support alpha channel.
562 * So the request format is ARGB8888 then change it to XRGB8888.
564 if (ctx->driver_data->has_limited_fmt && !win) {
565 if (win_data->pixel_format == DRM_FORMAT_ARGB8888)
566 win_data->pixel_format = DRM_FORMAT_XRGB8888;
569 switch (win_data->pixel_format) {
571 val |= WINCON0_BPPMODE_8BPP_PALETTE;
572 val |= WINCONx_BURSTLEN_8WORD;
573 val |= WINCONx_BYTSWP;
575 case DRM_FORMAT_XRGB1555:
576 val |= WINCON0_BPPMODE_16BPP_1555;
577 val |= WINCONx_HAWSWP;
578 val |= WINCONx_BURSTLEN_16WORD;
580 case DRM_FORMAT_RGB565:
581 val |= WINCON0_BPPMODE_16BPP_565;
582 val |= WINCONx_HAWSWP;
583 val |= WINCONx_BURSTLEN_16WORD;
585 case DRM_FORMAT_XRGB8888:
586 val |= WINCON0_BPPMODE_24BPP_888;
588 val |= WINCONx_BURSTLEN_16WORD;
590 case DRM_FORMAT_ARGB8888:
591 val |= WINCON1_BPPMODE_25BPP_A1888
592 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
594 val |= WINCONx_BURSTLEN_16WORD;
597 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
599 val |= WINCON0_BPPMODE_24BPP_888;
601 val |= WINCONx_BURSTLEN_16WORD;
605 DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
608 * In case of exynos, setting dma-burst to 16Word causes permanent
609 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
610 * switching which is based on plane size is not recommended as
611 * plane size varies alot towards the end of the screen and rapid
612 * movement causes unstable DMA which results into iommu crash/tear.
615 if (win_data->fb_width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
616 val &= ~WINCONx_BURSTLEN_MASK;
617 val |= WINCONx_BURSTLEN_4WORD;
620 writel(val, ctx->regs + WINCON(win));
623 static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
625 unsigned int keycon0 = 0, keycon1 = 0;
627 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
628 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
630 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
632 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
633 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
637 * shadow_protect_win() - disable updating values from shadow registers at vsync
639 * @win: window to protect registers for
640 * @protect: 1 to protect (disable updates)
642 static void fimd_shadow_protect_win(struct fimd_context *ctx,
643 int win, bool protect)
647 if (ctx->driver_data->has_shadowcon) {
649 bits = SHADOWCON_WINx_PROTECT(win);
652 bits = PRTCON_PROTECT;
655 val = readl(ctx->regs + reg);
660 writel(val, ctx->regs + reg);
663 static void fimd_win_commit(struct exynos_drm_crtc *crtc, int zpos)
665 struct fimd_context *ctx = crtc->ctx;
666 struct fimd_win_data *win_data;
668 unsigned long val, alpha, size;
675 if (win == DEFAULT_ZPOS)
676 win = ctx->default_win;
678 if (win < 0 || win >= WINDOWS_NR)
681 win_data = &ctx->win_data[win];
683 /* If suspended, enable this on resume */
684 if (ctx->suspended) {
685 win_data->resume = true;
690 * SHADOWCON/PRTCON register is used for enabling timing.
692 * for example, once only width value of a register is set,
693 * if the dma is started then fimd hardware could malfunction so
694 * with protect window setting, the register fields with prefix '_F'
695 * wouldn't be updated at vsync also but updated once unprotect window
699 /* protect windows */
700 fimd_shadow_protect_win(ctx, win, true);
702 /* buffer start address */
703 val = (unsigned long)win_data->dma_addr;
704 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
706 /* buffer end address */
707 size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
708 val = (unsigned long)(win_data->dma_addr + size);
709 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
711 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
712 (unsigned long)win_data->dma_addr, val, size);
713 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
714 win_data->ovl_width, win_data->ovl_height);
717 val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
718 VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
719 VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
720 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
721 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
724 val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
725 VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
726 VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
727 VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
728 writel(val, ctx->regs + VIDOSD_A(win));
730 last_x = win_data->offset_x + win_data->ovl_width;
733 last_y = win_data->offset_y + win_data->ovl_height;
737 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
738 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
740 writel(val, ctx->regs + VIDOSD_B(win));
742 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
743 win_data->offset_x, win_data->offset_y, last_x, last_y);
745 /* hardware window 0 doesn't support alpha channel. */
748 alpha = VIDISD14C_ALPHA1_R(0xf) |
749 VIDISD14C_ALPHA1_G(0xf) |
750 VIDISD14C_ALPHA1_B(0xf);
752 writel(alpha, ctx->regs + VIDOSD_C(win));
756 if (win != 3 && win != 4) {
757 u32 offset = VIDOSD_D(win);
759 offset = VIDOSD_C(win);
760 val = win_data->ovl_width * win_data->ovl_height;
761 writel(val, ctx->regs + offset);
763 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
766 fimd_win_set_pixfmt(ctx, win);
768 /* hardware window 0 doesn't support color key. */
770 fimd_win_set_colkey(ctx, win);
772 fimd_enable_video_output(ctx, win, true);
774 if (ctx->driver_data->has_shadowcon)
775 fimd_enable_shadow_channel_path(ctx, win, true);
777 /* Enable DMA channel and unprotect windows */
778 fimd_shadow_protect_win(ctx, win, false);
780 win_data->enabled = true;
783 atomic_set(&ctx->win_updated, 1);
786 static void fimd_win_disable(struct exynos_drm_crtc *crtc, int zpos)
788 struct fimd_context *ctx = crtc->ctx;
789 struct fimd_win_data *win_data;
792 if (win == DEFAULT_ZPOS)
793 win = ctx->default_win;
795 if (win < 0 || win >= WINDOWS_NR)
798 win_data = &ctx->win_data[win];
800 if (ctx->suspended) {
801 /* do not resume this window*/
802 win_data->resume = false;
806 /* protect windows */
807 fimd_shadow_protect_win(ctx, win, true);
809 fimd_enable_video_output(ctx, win, false);
811 if (ctx->driver_data->has_shadowcon)
812 fimd_enable_shadow_channel_path(ctx, win, false);
814 /* unprotect windows */
815 fimd_shadow_protect_win(ctx, win, false);
817 win_data->enabled = false;
820 static void fimd_window_suspend(struct fimd_context *ctx)
822 struct fimd_win_data *win_data;
825 for (i = 0; i < WINDOWS_NR; i++) {
826 win_data = &ctx->win_data[i];
827 win_data->resume = win_data->enabled;
828 if (win_data->enabled)
829 fimd_win_disable(ctx->crtc, i);
833 static void fimd_window_resume(struct fimd_context *ctx)
835 struct fimd_win_data *win_data;
838 for (i = 0; i < WINDOWS_NR; i++) {
839 win_data = &ctx->win_data[i];
840 win_data->enabled = win_data->resume;
841 win_data->resume = false;
845 static void fimd_apply(struct fimd_context *ctx)
847 struct fimd_win_data *win_data;
850 for (i = 0; i < WINDOWS_NR; i++) {
851 win_data = &ctx->win_data[i];
852 if (win_data->enabled)
853 fimd_win_commit(ctx->crtc, i);
855 fimd_win_disable(ctx->crtc, i);
858 fimd_commit(ctx->crtc);
861 static int fimd_poweron(struct fimd_context *ctx)
868 ctx->suspended = false;
870 pm_runtime_get_sync(ctx->dev);
872 ret = clk_prepare_enable(ctx->bus_clk);
874 DRM_ERROR("Failed to prepare_enable the bus clk [%d]\n", ret);
878 ret = clk_prepare_enable(ctx->lcd_clk);
880 DRM_ERROR("Failed to prepare_enable the lcd clk [%d]\n", ret);
884 /* if vblank was enabled status, enable it again. */
885 if (test_and_clear_bit(0, &ctx->irq_flags)) {
886 ret = fimd_enable_vblank(ctx->crtc);
888 DRM_ERROR("Failed to re-enable vblank [%d]\n", ret);
889 goto enable_vblank_err;
893 fimd_window_resume(ctx);
900 clk_disable_unprepare(ctx->lcd_clk);
902 clk_disable_unprepare(ctx->bus_clk);
904 ctx->suspended = true;
908 static int fimd_poweroff(struct fimd_context *ctx)
914 * We need to make sure that all windows are disabled before we
915 * suspend that connector. Otherwise we might try to scan from
916 * a destroyed buffer later.
918 fimd_window_suspend(ctx);
920 clk_disable_unprepare(ctx->lcd_clk);
921 clk_disable_unprepare(ctx->bus_clk);
923 pm_runtime_put_sync(ctx->dev);
925 ctx->suspended = true;
929 static void fimd_dpms(struct exynos_drm_crtc *crtc, int mode)
931 DRM_DEBUG_KMS("%s, %d\n", __FILE__, mode);
934 case DRM_MODE_DPMS_ON:
935 fimd_poweron(crtc->ctx);
937 case DRM_MODE_DPMS_STANDBY:
938 case DRM_MODE_DPMS_SUSPEND:
939 case DRM_MODE_DPMS_OFF:
940 fimd_poweroff(crtc->ctx);
943 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
948 static void fimd_trigger(struct device *dev)
950 struct fimd_context *ctx = dev_get_drvdata(dev);
951 struct fimd_driver_data *driver_data = ctx->driver_data;
952 void *timing_base = ctx->regs + driver_data->timing_base;
956 * Skips triggering if in triggering state, because multiple triggering
957 * requests can cause panel reset.
959 if (atomic_read(&ctx->triggering))
962 /* Enters triggering mode */
963 atomic_set(&ctx->triggering, 1);
965 reg = readl(timing_base + TRIGCON);
966 reg |= (TRGMODE_I80_RGB_ENABLE_I80 | SWTRGCMD_I80_RGB_ENABLE);
967 writel(reg, timing_base + TRIGCON);
970 * Exits triggering mode if vblank is not enabled yet, because when the
971 * VIDINTCON0 register is not set, it can not exit from triggering mode.
973 if (!test_bit(0, &ctx->irq_flags))
974 atomic_set(&ctx->triggering, 0);
977 static void fimd_te_handler(struct exynos_drm_crtc *crtc)
979 struct fimd_context *ctx = crtc->ctx;
981 /* Checks the crtc is detached already from encoder */
982 if (ctx->pipe < 0 || !ctx->drm_dev)
986 * If there is a page flip request, triggers and handles the page flip
987 * event so that current fb can be updated into panel GRAM.
989 if (atomic_add_unless(&ctx->win_updated, -1, 0))
990 fimd_trigger(ctx->dev);
992 /* Wakes up vsync event queue */
993 if (atomic_read(&ctx->wait_vsync_event)) {
994 atomic_set(&ctx->wait_vsync_event, 0);
995 wake_up(&ctx->wait_vsync_queue);
998 if (test_bit(0, &ctx->irq_flags))
999 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
1002 static struct exynos_drm_crtc_ops fimd_crtc_ops = {
1004 .mode_fixup = fimd_mode_fixup,
1005 .commit = fimd_commit,
1006 .enable_vblank = fimd_enable_vblank,
1007 .disable_vblank = fimd_disable_vblank,
1008 .wait_for_vblank = fimd_wait_for_vblank,
1009 .win_mode_set = fimd_win_mode_set,
1010 .win_commit = fimd_win_commit,
1011 .win_disable = fimd_win_disable,
1012 .te_handler = fimd_te_handler,
1015 static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
1017 struct fimd_context *ctx = (struct fimd_context *)dev_id;
1020 val = readl(ctx->regs + VIDINTCON1);
1022 clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
1023 if (val & clear_bit)
1024 writel(clear_bit, ctx->regs + VIDINTCON1);
1026 /* check the crtc is detached already from encoder */
1027 if (ctx->pipe < 0 || !ctx->drm_dev)
1031 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
1033 /* Exits triggering mode */
1034 atomic_set(&ctx->triggering, 0);
1036 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
1037 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
1039 /* set wait vsync event to zero and wake up queue. */
1040 if (atomic_read(&ctx->wait_vsync_event)) {
1041 atomic_set(&ctx->wait_vsync_event, 0);
1042 wake_up(&ctx->wait_vsync_queue);
1050 static int fimd_bind(struct device *dev, struct device *master, void *data)
1052 struct fimd_context *ctx = dev_get_drvdata(dev);
1053 struct drm_device *drm_dev = data;
1054 struct exynos_drm_private *priv = drm_dev->dev_private;
1057 ctx->drm_dev = drm_dev;
1058 ctx->pipe = priv->pipe++;
1060 ctx->crtc = exynos_drm_crtc_create(drm_dev, ctx->pipe,
1061 EXYNOS_DISPLAY_TYPE_LCD,
1062 &fimd_crtc_ops, ctx);
1065 exynos_drm_create_enc_conn(drm_dev, ctx->display);
1067 ret = fimd_iommu_attach_devices(ctx, drm_dev);
1075 static void fimd_unbind(struct device *dev, struct device *master,
1078 struct fimd_context *ctx = dev_get_drvdata(dev);
1080 fimd_dpms(ctx->crtc, DRM_MODE_DPMS_OFF);
1082 fimd_iommu_detach_devices(ctx);
1085 exynos_dpi_remove(ctx->display);
1088 static const struct component_ops fimd_component_ops = {
1090 .unbind = fimd_unbind,
1093 static int fimd_probe(struct platform_device *pdev)
1095 struct device *dev = &pdev->dev;
1096 struct fimd_context *ctx;
1097 struct device_node *i80_if_timings;
1098 struct resource *res;
1104 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1108 ret = exynos_drm_component_add(dev, EXYNOS_DEVICE_TYPE_CRTC,
1109 EXYNOS_DISPLAY_TYPE_LCD);
1114 ctx->suspended = true;
1115 ctx->driver_data = drm_fimd_get_driver_data(pdev);
1117 if (of_property_read_bool(dev->of_node, "samsung,invert-vden"))
1118 ctx->vidcon1 |= VIDCON1_INV_VDEN;
1119 if (of_property_read_bool(dev->of_node, "samsung,invert-vclk"))
1120 ctx->vidcon1 |= VIDCON1_INV_VCLK;
1122 i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
1123 if (i80_if_timings) {
1128 if (ctx->driver_data->has_vidoutcon)
1129 ctx->vidout_con |= VIDOUT_CON_F_I80_LDI0;
1131 ctx->vidcon0 |= VIDCON0_VIDOUT_I80_LDI0;
1133 * The user manual describes that this "DSI_EN" bit is required
1134 * to enable I80 24-bit data interface.
1136 ctx->vidcon0 |= VIDCON0_DSI_EN;
1138 if (of_property_read_u32(i80_if_timings, "cs-setup", &val))
1140 ctx->i80ifcon = LCD_CS_SETUP(val);
1141 if (of_property_read_u32(i80_if_timings, "wr-setup", &val))
1143 ctx->i80ifcon |= LCD_WR_SETUP(val);
1144 if (of_property_read_u32(i80_if_timings, "wr-active", &val))
1146 ctx->i80ifcon |= LCD_WR_ACTIVE(val);
1147 if (of_property_read_u32(i80_if_timings, "wr-hold", &val))
1149 ctx->i80ifcon |= LCD_WR_HOLD(val);
1151 of_node_put(i80_if_timings);
1153 ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
1155 if (IS_ERR(ctx->sysreg)) {
1156 dev_warn(dev, "failed to get system register.\n");
1160 ctx->bus_clk = devm_clk_get(dev, "fimd");
1161 if (IS_ERR(ctx->bus_clk)) {
1162 dev_err(dev, "failed to get bus clock\n");
1163 ret = PTR_ERR(ctx->bus_clk);
1164 goto err_del_component;
1167 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
1168 if (IS_ERR(ctx->lcd_clk)) {
1169 dev_err(dev, "failed to get lcd clock\n");
1170 ret = PTR_ERR(ctx->lcd_clk);
1171 goto err_del_component;
1174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1176 ctx->regs = devm_ioremap_resource(dev, res);
1177 if (IS_ERR(ctx->regs)) {
1178 ret = PTR_ERR(ctx->regs);
1179 goto err_del_component;
1182 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1183 ctx->i80_if ? "lcd_sys" : "vsync");
1185 dev_err(dev, "irq request failed.\n");
1187 goto err_del_component;
1190 ret = devm_request_irq(dev, res->start, fimd_irq_handler,
1191 0, "drm_fimd", ctx);
1193 dev_err(dev, "irq request failed.\n");
1194 goto err_del_component;
1197 init_waitqueue_head(&ctx->wait_vsync_queue);
1198 atomic_set(&ctx->wait_vsync_event, 0);
1200 platform_set_drvdata(pdev, ctx);
1202 ctx->display = exynos_dpi_probe(dev);
1203 if (IS_ERR(ctx->display)) {
1204 ret = PTR_ERR(ctx->display);
1205 goto err_del_component;
1208 pm_runtime_enable(dev);
1210 ret = component_add(dev, &fimd_component_ops);
1212 goto err_disable_pm_runtime;
1216 err_disable_pm_runtime:
1217 pm_runtime_disable(dev);
1220 exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CRTC);
1224 static int fimd_remove(struct platform_device *pdev)
1226 pm_runtime_disable(&pdev->dev);
1228 component_del(&pdev->dev, &fimd_component_ops);
1229 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
1234 struct platform_driver fimd_driver = {
1235 .probe = fimd_probe,
1236 .remove = fimd_remove,
1238 .name = "exynos4-fb",
1239 .owner = THIS_MODULE,
1240 .of_match_table = fimd_driver_dt_match,