vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_fimd.c
blobb7f56935a46bc2a33fec03c18854774cb331f70d
1 /* exynos_drm_fimd.c
3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
4 * Authors:
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.
14 #include <drm/drmP.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/of.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_fb.h"
33 #include "exynos_drm_crtc.h"
34 #include "exynos_drm_plane.h"
35 #include "exynos_drm_iommu.h"
38 * FIMD stands for Fully Interactive Mobile Display and
39 * as a display controller, it transfers contents drawn on memory
40 * to a LCD Panel through Display Interfaces such as RGB or
41 * CPU Interface.
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 VIDWnALPHA0(win) (VIDW_ALPHA + 0x00 + (win) * 8)
58 #define VIDWnALPHA1(win) (VIDW_ALPHA + 0x04 + (win) * 8)
60 #define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
61 #define VIDWx_BUF_START_S(win, buf) (VIDW_BUF_START_S(buf) + (win) * 8)
62 #define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
63 #define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
65 /* color key control register for hardware window 1 ~ 4. */
66 #define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
67 /* color key value register for hardware window 1 ~ 4. */
68 #define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
70 /* I80 trigger control register */
71 #define TRIGCON 0x1A4
72 #define TRGMODE_ENABLE (1 << 0)
73 #define SWTRGCMD_ENABLE (1 << 1)
74 /* Exynos3250, 3472, 5260 5410, 5420 and 5422 only supported. */
75 #define HWTRGEN_ENABLE (1 << 3)
76 #define HWTRGMASK_ENABLE (1 << 4)
77 /* Exynos3250, 3472, 5260, 5420 and 5422 only supported. */
78 #define HWTRIGEN_PER_ENABLE (1 << 31)
80 /* display mode change control register except exynos4 */
81 #define VIDOUT_CON 0x000
82 #define VIDOUT_CON_F_I80_LDI0 (0x2 << 8)
84 /* I80 interface control for main LDI register */
85 #define I80IFCONFAx(x) (0x1B0 + (x) * 4)
86 #define I80IFCONFBx(x) (0x1B8 + (x) * 4)
87 #define LCD_CS_SETUP(x) ((x) << 16)
88 #define LCD_WR_SETUP(x) ((x) << 12)
89 #define LCD_WR_ACTIVE(x) ((x) << 8)
90 #define LCD_WR_HOLD(x) ((x) << 4)
91 #define I80IFEN_ENABLE (1 << 0)
93 /* FIMD has totally five hardware windows. */
94 #define WINDOWS_NR 5
96 /* HW trigger flag on i80 panel. */
97 #define I80_HW_TRG (1 << 1)
99 struct fimd_driver_data {
100 unsigned int timing_base;
101 unsigned int lcdblk_offset;
102 unsigned int lcdblk_vt_shift;
103 unsigned int lcdblk_bypass_shift;
104 unsigned int lcdblk_mic_bypass_shift;
105 unsigned int trg_type;
107 unsigned int has_shadowcon:1;
108 unsigned int has_clksel:1;
109 unsigned int has_limited_fmt:1;
110 unsigned int has_vidoutcon:1;
111 unsigned int has_vtsel:1;
112 unsigned int has_mic_bypass:1;
113 unsigned int has_dp_clk:1;
114 unsigned int has_hw_trigger:1;
115 unsigned int has_trigger_per_te:1;
118 static struct fimd_driver_data s3c64xx_fimd_driver_data = {
119 .timing_base = 0x0,
120 .has_clksel = 1,
121 .has_limited_fmt = 1,
124 static struct fimd_driver_data s5pv210_fimd_driver_data = {
125 .timing_base = 0x0,
126 .has_shadowcon = 1,
127 .has_clksel = 1,
130 static struct fimd_driver_data exynos3_fimd_driver_data = {
131 .timing_base = 0x20000,
132 .lcdblk_offset = 0x210,
133 .lcdblk_bypass_shift = 1,
134 .has_shadowcon = 1,
135 .has_vidoutcon = 1,
138 static struct fimd_driver_data exynos4_fimd_driver_data = {
139 .timing_base = 0x0,
140 .lcdblk_offset = 0x210,
141 .lcdblk_vt_shift = 10,
142 .lcdblk_bypass_shift = 1,
143 .has_shadowcon = 1,
144 .has_vtsel = 1,
147 static struct fimd_driver_data exynos5_fimd_driver_data = {
148 .timing_base = 0x20000,
149 .lcdblk_offset = 0x214,
150 .lcdblk_vt_shift = 24,
151 .lcdblk_bypass_shift = 15,
152 .has_shadowcon = 1,
153 .has_vidoutcon = 1,
154 .has_vtsel = 1,
155 .has_dp_clk = 1,
158 static struct fimd_driver_data exynos5420_fimd_driver_data = {
159 .timing_base = 0x20000,
160 .lcdblk_offset = 0x214,
161 .lcdblk_vt_shift = 24,
162 .lcdblk_bypass_shift = 15,
163 .lcdblk_mic_bypass_shift = 11,
164 .has_shadowcon = 1,
165 .has_vidoutcon = 1,
166 .has_vtsel = 1,
167 .has_mic_bypass = 1,
168 .has_dp_clk = 1,
171 struct fimd_context {
172 struct device *dev;
173 struct drm_device *drm_dev;
174 struct exynos_drm_crtc *crtc;
175 struct exynos_drm_plane planes[WINDOWS_NR];
176 struct exynos_drm_plane_config configs[WINDOWS_NR];
177 struct clk *bus_clk;
178 struct clk *lcd_clk;
179 void __iomem *regs;
180 struct regmap *sysreg;
181 unsigned long irq_flags;
182 u32 vidcon0;
183 u32 vidcon1;
184 u32 vidout_con;
185 u32 i80ifcon;
186 bool i80_if;
187 bool suspended;
188 wait_queue_head_t wait_vsync_queue;
189 atomic_t wait_vsync_event;
190 atomic_t win_updated;
191 atomic_t triggering;
192 u32 clkdiv;
194 const struct fimd_driver_data *driver_data;
195 struct drm_encoder *encoder;
196 struct exynos_drm_clk dp_clk;
199 static const struct of_device_id fimd_driver_dt_match[] = {
200 { .compatible = "samsung,s3c6400-fimd",
201 .data = &s3c64xx_fimd_driver_data },
202 { .compatible = "samsung,s5pv210-fimd",
203 .data = &s5pv210_fimd_driver_data },
204 { .compatible = "samsung,exynos3250-fimd",
205 .data = &exynos3_fimd_driver_data },
206 { .compatible = "samsung,exynos4210-fimd",
207 .data = &exynos4_fimd_driver_data },
208 { .compatible = "samsung,exynos5250-fimd",
209 .data = &exynos5_fimd_driver_data },
210 { .compatible = "samsung,exynos5420-fimd",
211 .data = &exynos5420_fimd_driver_data },
214 MODULE_DEVICE_TABLE(of, fimd_driver_dt_match);
216 static const enum drm_plane_type fimd_win_types[WINDOWS_NR] = {
217 DRM_PLANE_TYPE_PRIMARY,
218 DRM_PLANE_TYPE_OVERLAY,
219 DRM_PLANE_TYPE_OVERLAY,
220 DRM_PLANE_TYPE_OVERLAY,
221 DRM_PLANE_TYPE_CURSOR,
224 static const uint32_t fimd_formats[] = {
225 DRM_FORMAT_C8,
226 DRM_FORMAT_XRGB1555,
227 DRM_FORMAT_RGB565,
228 DRM_FORMAT_XRGB8888,
229 DRM_FORMAT_ARGB8888,
232 static int fimd_enable_vblank(struct exynos_drm_crtc *crtc)
234 struct fimd_context *ctx = crtc->ctx;
235 u32 val;
237 if (ctx->suspended)
238 return -EPERM;
240 if (!test_and_set_bit(0, &ctx->irq_flags)) {
241 val = readl(ctx->regs + VIDINTCON0);
243 val |= VIDINTCON0_INT_ENABLE;
245 if (ctx->i80_if) {
246 val |= VIDINTCON0_INT_I80IFDONE;
247 val |= VIDINTCON0_INT_SYSMAINCON;
248 val &= ~VIDINTCON0_INT_SYSSUBCON;
249 } else {
250 val |= VIDINTCON0_INT_FRAME;
252 val &= ~VIDINTCON0_FRAMESEL0_MASK;
253 val |= VIDINTCON0_FRAMESEL0_FRONTPORCH;
254 val &= ~VIDINTCON0_FRAMESEL1_MASK;
255 val |= VIDINTCON0_FRAMESEL1_NONE;
258 writel(val, ctx->regs + VIDINTCON0);
261 return 0;
264 static void fimd_disable_vblank(struct exynos_drm_crtc *crtc)
266 struct fimd_context *ctx = crtc->ctx;
267 u32 val;
269 if (ctx->suspended)
270 return;
272 if (test_and_clear_bit(0, &ctx->irq_flags)) {
273 val = readl(ctx->regs + VIDINTCON0);
275 val &= ~VIDINTCON0_INT_ENABLE;
277 if (ctx->i80_if) {
278 val &= ~VIDINTCON0_INT_I80IFDONE;
279 val &= ~VIDINTCON0_INT_SYSMAINCON;
280 val &= ~VIDINTCON0_INT_SYSSUBCON;
281 } else
282 val &= ~VIDINTCON0_INT_FRAME;
284 writel(val, ctx->regs + VIDINTCON0);
288 static void fimd_wait_for_vblank(struct exynos_drm_crtc *crtc)
290 struct fimd_context *ctx = crtc->ctx;
292 if (ctx->suspended)
293 return;
295 atomic_set(&ctx->wait_vsync_event, 1);
298 * wait for FIMD to signal VSYNC interrupt or return after
299 * timeout which is set to 50ms (refresh rate of 20).
301 if (!wait_event_timeout(ctx->wait_vsync_queue,
302 !atomic_read(&ctx->wait_vsync_event),
303 HZ/20))
304 DRM_DEBUG_KMS("vblank wait timed out.\n");
307 static void fimd_enable_video_output(struct fimd_context *ctx, unsigned int win,
308 bool enable)
310 u32 val = readl(ctx->regs + WINCON(win));
312 if (enable)
313 val |= WINCONx_ENWIN;
314 else
315 val &= ~WINCONx_ENWIN;
317 writel(val, ctx->regs + WINCON(win));
320 static void fimd_enable_shadow_channel_path(struct fimd_context *ctx,
321 unsigned int win,
322 bool enable)
324 u32 val = readl(ctx->regs + SHADOWCON);
326 if (enable)
327 val |= SHADOWCON_CHx_ENABLE(win);
328 else
329 val &= ~SHADOWCON_CHx_ENABLE(win);
331 writel(val, ctx->regs + SHADOWCON);
334 static void fimd_clear_channels(struct exynos_drm_crtc *crtc)
336 struct fimd_context *ctx = crtc->ctx;
337 unsigned int win, ch_enabled = 0;
339 DRM_DEBUG_KMS("%s\n", __FILE__);
341 /* Hardware is in unknown state, so ensure it gets enabled properly */
342 pm_runtime_get_sync(ctx->dev);
344 clk_prepare_enable(ctx->bus_clk);
345 clk_prepare_enable(ctx->lcd_clk);
347 /* Check if any channel is enabled. */
348 for (win = 0; win < WINDOWS_NR; win++) {
349 u32 val = readl(ctx->regs + WINCON(win));
351 if (val & WINCONx_ENWIN) {
352 fimd_enable_video_output(ctx, win, false);
354 if (ctx->driver_data->has_shadowcon)
355 fimd_enable_shadow_channel_path(ctx, win,
356 false);
358 ch_enabled = 1;
362 /* Wait for vsync, as disable channel takes effect at next vsync */
363 if (ch_enabled) {
364 ctx->suspended = false;
366 fimd_enable_vblank(ctx->crtc);
367 fimd_wait_for_vblank(ctx->crtc);
368 fimd_disable_vblank(ctx->crtc);
370 ctx->suspended = true;
373 clk_disable_unprepare(ctx->lcd_clk);
374 clk_disable_unprepare(ctx->bus_clk);
376 pm_runtime_put(ctx->dev);
380 static int fimd_atomic_check(struct exynos_drm_crtc *crtc,
381 struct drm_crtc_state *state)
383 struct drm_display_mode *mode = &state->adjusted_mode;
384 struct fimd_context *ctx = crtc->ctx;
385 unsigned long ideal_clk, lcd_rate;
386 u32 clkdiv;
388 if (mode->clock == 0) {
389 DRM_INFO("Mode has zero clock value.\n");
390 return -EINVAL;
393 ideal_clk = mode->clock * 1000;
395 if (ctx->i80_if) {
397 * The frame done interrupt should be occurred prior to the
398 * next TE signal.
400 ideal_clk *= 2;
403 lcd_rate = clk_get_rate(ctx->lcd_clk);
404 if (2 * lcd_rate < ideal_clk) {
405 DRM_INFO("sclk_fimd clock too low(%lu) for requested pixel clock(%lu)\n",
406 lcd_rate, ideal_clk);
407 return -EINVAL;
410 /* Find the clock divider value that gets us closest to ideal_clk */
411 clkdiv = DIV_ROUND_CLOSEST(lcd_rate, ideal_clk);
412 if (clkdiv >= 0x200) {
413 DRM_INFO("requested pixel clock(%lu) too low\n", ideal_clk);
414 return -EINVAL;
417 ctx->clkdiv = (clkdiv < 0x100) ? clkdiv : 0xff;
419 return 0;
422 static void fimd_setup_trigger(struct fimd_context *ctx)
424 void __iomem *timing_base = ctx->regs + ctx->driver_data->timing_base;
425 u32 trg_type = ctx->driver_data->trg_type;
426 u32 val = readl(timing_base + TRIGCON);
428 val &= ~(TRGMODE_ENABLE);
430 if (trg_type == I80_HW_TRG) {
431 if (ctx->driver_data->has_hw_trigger)
432 val |= HWTRGEN_ENABLE | HWTRGMASK_ENABLE;
433 if (ctx->driver_data->has_trigger_per_te)
434 val |= HWTRIGEN_PER_ENABLE;
435 } else {
436 val |= TRGMODE_ENABLE;
439 writel(val, timing_base + TRIGCON);
442 static void fimd_commit(struct exynos_drm_crtc *crtc)
444 struct fimd_context *ctx = crtc->ctx;
445 struct drm_display_mode *mode = &crtc->base.state->adjusted_mode;
446 const struct fimd_driver_data *driver_data = ctx->driver_data;
447 void *timing_base = ctx->regs + driver_data->timing_base;
448 u32 val;
450 if (ctx->suspended)
451 return;
453 /* nothing to do if we haven't set the mode yet */
454 if (mode->htotal == 0 || mode->vtotal == 0)
455 return;
457 if (ctx->i80_if) {
458 val = ctx->i80ifcon | I80IFEN_ENABLE;
459 writel(val, timing_base + I80IFCONFAx(0));
461 /* disable auto frame rate */
462 writel(0, timing_base + I80IFCONFBx(0));
464 /* set video type selection to I80 interface */
465 if (driver_data->has_vtsel && ctx->sysreg &&
466 regmap_update_bits(ctx->sysreg,
467 driver_data->lcdblk_offset,
468 0x3 << driver_data->lcdblk_vt_shift,
469 0x1 << driver_data->lcdblk_vt_shift)) {
470 DRM_ERROR("Failed to update sysreg for I80 i/f.\n");
471 return;
473 } else {
474 int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
475 u32 vidcon1;
477 /* setup polarity values */
478 vidcon1 = ctx->vidcon1;
479 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
480 vidcon1 |= VIDCON1_INV_VSYNC;
481 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
482 vidcon1 |= VIDCON1_INV_HSYNC;
483 writel(vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
485 /* setup vertical timing values. */
486 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
487 vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
488 vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
490 val = VIDTCON0_VBPD(vbpd - 1) |
491 VIDTCON0_VFPD(vfpd - 1) |
492 VIDTCON0_VSPW(vsync_len - 1);
493 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
495 /* setup horizontal timing values. */
496 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
497 hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
498 hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
500 val = VIDTCON1_HBPD(hbpd - 1) |
501 VIDTCON1_HFPD(hfpd - 1) |
502 VIDTCON1_HSPW(hsync_len - 1);
503 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
506 if (driver_data->has_vidoutcon)
507 writel(ctx->vidout_con, timing_base + VIDOUT_CON);
509 /* set bypass selection */
510 if (ctx->sysreg && regmap_update_bits(ctx->sysreg,
511 driver_data->lcdblk_offset,
512 0x1 << driver_data->lcdblk_bypass_shift,
513 0x1 << driver_data->lcdblk_bypass_shift)) {
514 DRM_ERROR("Failed to update sysreg for bypass setting.\n");
515 return;
518 /* TODO: When MIC is enabled for display path, the lcdblk_mic_bypass
519 * bit should be cleared.
521 if (driver_data->has_mic_bypass && ctx->sysreg &&
522 regmap_update_bits(ctx->sysreg,
523 driver_data->lcdblk_offset,
524 0x1 << driver_data->lcdblk_mic_bypass_shift,
525 0x1 << driver_data->lcdblk_mic_bypass_shift)) {
526 DRM_ERROR("Failed to update sysreg for bypass mic.\n");
527 return;
530 /* setup horizontal and vertical display size. */
531 val = VIDTCON2_LINEVAL(mode->vdisplay - 1) |
532 VIDTCON2_HOZVAL(mode->hdisplay - 1) |
533 VIDTCON2_LINEVAL_E(mode->vdisplay - 1) |
534 VIDTCON2_HOZVAL_E(mode->hdisplay - 1);
535 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
537 fimd_setup_trigger(ctx);
540 * fields of register with prefix '_F' would be updated
541 * at vsync(same as dma start)
543 val = ctx->vidcon0;
544 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
546 if (ctx->driver_data->has_clksel)
547 val |= VIDCON0_CLKSEL_LCD;
549 if (ctx->clkdiv > 1)
550 val |= VIDCON0_CLKVAL_F(ctx->clkdiv - 1) | VIDCON0_CLKDIR;
552 writel(val, ctx->regs + VIDCON0);
556 static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win,
557 uint32_t pixel_format, int width)
559 unsigned long val;
561 val = WINCONx_ENWIN;
564 * In case of s3c64xx, window 0 doesn't support alpha channel.
565 * So the request format is ARGB8888 then change it to XRGB8888.
567 if (ctx->driver_data->has_limited_fmt && !win) {
568 if (pixel_format == DRM_FORMAT_ARGB8888)
569 pixel_format = DRM_FORMAT_XRGB8888;
572 switch (pixel_format) {
573 case DRM_FORMAT_C8:
574 val |= WINCON0_BPPMODE_8BPP_PALETTE;
575 val |= WINCONx_BURSTLEN_8WORD;
576 val |= WINCONx_BYTSWP;
577 break;
578 case DRM_FORMAT_XRGB1555:
579 val |= WINCON0_BPPMODE_16BPP_1555;
580 val |= WINCONx_HAWSWP;
581 val |= WINCONx_BURSTLEN_16WORD;
582 break;
583 case DRM_FORMAT_RGB565:
584 val |= WINCON0_BPPMODE_16BPP_565;
585 val |= WINCONx_HAWSWP;
586 val |= WINCONx_BURSTLEN_16WORD;
587 break;
588 case DRM_FORMAT_XRGB8888:
589 val |= WINCON0_BPPMODE_24BPP_888;
590 val |= WINCONx_WSWP;
591 val |= WINCONx_BURSTLEN_16WORD;
592 break;
593 case DRM_FORMAT_ARGB8888:
594 default:
595 val |= WINCON1_BPPMODE_25BPP_A1888
596 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
597 val |= WINCONx_WSWP;
598 val |= WINCONx_BURSTLEN_16WORD;
599 break;
603 * Setting dma-burst to 16Word causes permanent tearing for very small
604 * buffers, e.g. cursor buffer. Burst Mode switching which based on
605 * plane size is not recommended as plane size varies alot towards the
606 * end of the screen and rapid movement causes unstable DMA, but it is
607 * still better to change dma-burst than displaying garbage.
610 if (width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
611 val &= ~WINCONx_BURSTLEN_MASK;
612 val |= WINCONx_BURSTLEN_4WORD;
615 writel(val, ctx->regs + WINCON(win));
617 /* hardware window 0 doesn't support alpha channel. */
618 if (win != 0) {
619 /* OSD alpha */
620 val = VIDISD14C_ALPHA0_R(0xf) |
621 VIDISD14C_ALPHA0_G(0xf) |
622 VIDISD14C_ALPHA0_B(0xf) |
623 VIDISD14C_ALPHA1_R(0xf) |
624 VIDISD14C_ALPHA1_G(0xf) |
625 VIDISD14C_ALPHA1_B(0xf);
627 writel(val, ctx->regs + VIDOSD_C(win));
629 val = VIDW_ALPHA_R(0xf) | VIDW_ALPHA_G(0xf) |
630 VIDW_ALPHA_G(0xf);
631 writel(val, ctx->regs + VIDWnALPHA0(win));
632 writel(val, ctx->regs + VIDWnALPHA1(win));
636 static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
638 unsigned int keycon0 = 0, keycon1 = 0;
640 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
641 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
643 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
645 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
646 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
650 * shadow_protect_win() - disable updating values from shadow registers at vsync
652 * @win: window to protect registers for
653 * @protect: 1 to protect (disable updates)
655 static void fimd_shadow_protect_win(struct fimd_context *ctx,
656 unsigned int win, bool protect)
658 u32 reg, bits, val;
661 * SHADOWCON/PRTCON register is used for enabling timing.
663 * for example, once only width value of a register is set,
664 * if the dma is started then fimd hardware could malfunction so
665 * with protect window setting, the register fields with prefix '_F'
666 * wouldn't be updated at vsync also but updated once unprotect window
667 * is set.
670 if (ctx->driver_data->has_shadowcon) {
671 reg = SHADOWCON;
672 bits = SHADOWCON_WINx_PROTECT(win);
673 } else {
674 reg = PRTCON;
675 bits = PRTCON_PROTECT;
678 val = readl(ctx->regs + reg);
679 if (protect)
680 val |= bits;
681 else
682 val &= ~bits;
683 writel(val, ctx->regs + reg);
686 static void fimd_atomic_begin(struct exynos_drm_crtc *crtc)
688 struct fimd_context *ctx = crtc->ctx;
689 int i;
691 if (ctx->suspended)
692 return;
694 for (i = 0; i < WINDOWS_NR; i++)
695 fimd_shadow_protect_win(ctx, i, true);
698 static void fimd_atomic_flush(struct exynos_drm_crtc *crtc)
700 struct fimd_context *ctx = crtc->ctx;
701 int i;
703 if (ctx->suspended)
704 return;
706 for (i = 0; i < WINDOWS_NR; i++)
707 fimd_shadow_protect_win(ctx, i, false);
709 exynos_crtc_handle_event(crtc);
712 static void fimd_update_plane(struct exynos_drm_crtc *crtc,
713 struct exynos_drm_plane *plane)
715 struct exynos_drm_plane_state *state =
716 to_exynos_plane_state(plane->base.state);
717 struct fimd_context *ctx = crtc->ctx;
718 struct drm_framebuffer *fb = state->base.fb;
719 dma_addr_t dma_addr;
720 unsigned long val, size, offset;
721 unsigned int last_x, last_y, buf_offsize, line_size;
722 unsigned int win = plane->index;
723 unsigned int cpp = fb->format->cpp[0];
724 unsigned int pitch = fb->pitches[0];
726 if (ctx->suspended)
727 return;
729 offset = state->src.x * cpp;
730 offset += state->src.y * pitch;
732 /* buffer start address */
733 dma_addr = exynos_drm_fb_dma_addr(fb, 0) + offset;
734 val = (unsigned long)dma_addr;
735 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
737 /* buffer end address */
738 size = pitch * state->crtc.h;
739 val = (unsigned long)(dma_addr + size);
740 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
742 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
743 (unsigned long)dma_addr, val, size);
744 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
745 state->crtc.w, state->crtc.h);
747 /* buffer size */
748 buf_offsize = pitch - (state->crtc.w * cpp);
749 line_size = state->crtc.w * cpp;
750 val = VIDW_BUF_SIZE_OFFSET(buf_offsize) |
751 VIDW_BUF_SIZE_PAGEWIDTH(line_size) |
752 VIDW_BUF_SIZE_OFFSET_E(buf_offsize) |
753 VIDW_BUF_SIZE_PAGEWIDTH_E(line_size);
754 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
756 /* OSD position */
757 val = VIDOSDxA_TOPLEFT_X(state->crtc.x) |
758 VIDOSDxA_TOPLEFT_Y(state->crtc.y) |
759 VIDOSDxA_TOPLEFT_X_E(state->crtc.x) |
760 VIDOSDxA_TOPLEFT_Y_E(state->crtc.y);
761 writel(val, ctx->regs + VIDOSD_A(win));
763 last_x = state->crtc.x + state->crtc.w;
764 if (last_x)
765 last_x--;
766 last_y = state->crtc.y + state->crtc.h;
767 if (last_y)
768 last_y--;
770 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
771 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
773 writel(val, ctx->regs + VIDOSD_B(win));
775 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
776 state->crtc.x, state->crtc.y, last_x, last_y);
778 /* OSD size */
779 if (win != 3 && win != 4) {
780 u32 offset = VIDOSD_D(win);
781 if (win == 0)
782 offset = VIDOSD_C(win);
783 val = state->crtc.w * state->crtc.h;
784 writel(val, ctx->regs + offset);
786 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
789 fimd_win_set_pixfmt(ctx, win, fb->format->format, state->src.w);
791 /* hardware window 0 doesn't support color key. */
792 if (win != 0)
793 fimd_win_set_colkey(ctx, win);
795 fimd_enable_video_output(ctx, win, true);
797 if (ctx->driver_data->has_shadowcon)
798 fimd_enable_shadow_channel_path(ctx, win, true);
800 if (ctx->i80_if)
801 atomic_set(&ctx->win_updated, 1);
804 static void fimd_disable_plane(struct exynos_drm_crtc *crtc,
805 struct exynos_drm_plane *plane)
807 struct fimd_context *ctx = crtc->ctx;
808 unsigned int win = plane->index;
810 if (ctx->suspended)
811 return;
813 fimd_enable_video_output(ctx, win, false);
815 if (ctx->driver_data->has_shadowcon)
816 fimd_enable_shadow_channel_path(ctx, win, false);
819 static void fimd_enable(struct exynos_drm_crtc *crtc)
821 struct fimd_context *ctx = crtc->ctx;
823 if (!ctx->suspended)
824 return;
826 ctx->suspended = false;
828 pm_runtime_get_sync(ctx->dev);
830 /* if vblank was enabled status, enable it again. */
831 if (test_and_clear_bit(0, &ctx->irq_flags))
832 fimd_enable_vblank(ctx->crtc);
834 fimd_commit(ctx->crtc);
837 static void fimd_disable(struct exynos_drm_crtc *crtc)
839 struct fimd_context *ctx = crtc->ctx;
840 int i;
842 if (ctx->suspended)
843 return;
846 * We need to make sure that all windows are disabled before we
847 * suspend that connector. Otherwise we might try to scan from
848 * a destroyed buffer later.
850 for (i = 0; i < WINDOWS_NR; i++)
851 fimd_disable_plane(crtc, &ctx->planes[i]);
853 fimd_enable_vblank(crtc);
854 fimd_wait_for_vblank(crtc);
855 fimd_disable_vblank(crtc);
857 writel(0, ctx->regs + VIDCON0);
859 pm_runtime_put_sync(ctx->dev);
860 ctx->suspended = true;
863 static void fimd_trigger(struct device *dev)
865 struct fimd_context *ctx = dev_get_drvdata(dev);
866 const struct fimd_driver_data *driver_data = ctx->driver_data;
867 void *timing_base = ctx->regs + driver_data->timing_base;
868 u32 reg;
871 * Skips triggering if in triggering state, because multiple triggering
872 * requests can cause panel reset.
874 if (atomic_read(&ctx->triggering))
875 return;
877 /* Enters triggering mode */
878 atomic_set(&ctx->triggering, 1);
880 reg = readl(timing_base + TRIGCON);
881 reg |= (TRGMODE_ENABLE | SWTRGCMD_ENABLE);
882 writel(reg, timing_base + TRIGCON);
885 * Exits triggering mode if vblank is not enabled yet, because when the
886 * VIDINTCON0 register is not set, it can not exit from triggering mode.
888 if (!test_bit(0, &ctx->irq_flags))
889 atomic_set(&ctx->triggering, 0);
892 static void fimd_te_handler(struct exynos_drm_crtc *crtc)
894 struct fimd_context *ctx = crtc->ctx;
895 u32 trg_type = ctx->driver_data->trg_type;
897 /* Checks the crtc is detached already from encoder */
898 if (!ctx->drm_dev)
899 return;
901 if (trg_type == I80_HW_TRG)
902 goto out;
905 * If there is a page flip request, triggers and handles the page flip
906 * event so that current fb can be updated into panel GRAM.
908 if (atomic_add_unless(&ctx->win_updated, -1, 0))
909 fimd_trigger(ctx->dev);
911 out:
912 /* Wakes up vsync event queue */
913 if (atomic_read(&ctx->wait_vsync_event)) {
914 atomic_set(&ctx->wait_vsync_event, 0);
915 wake_up(&ctx->wait_vsync_queue);
918 if (test_bit(0, &ctx->irq_flags))
919 drm_crtc_handle_vblank(&ctx->crtc->base);
922 static void fimd_dp_clock_enable(struct exynos_drm_clk *clk, bool enable)
924 struct fimd_context *ctx = container_of(clk, struct fimd_context,
925 dp_clk);
926 u32 val = enable ? DP_MIE_CLK_DP_ENABLE : DP_MIE_CLK_DISABLE;
927 writel(val, ctx->regs + DP_MIE_CLKCON);
930 static const struct exynos_drm_crtc_ops fimd_crtc_ops = {
931 .enable = fimd_enable,
932 .disable = fimd_disable,
933 .enable_vblank = fimd_enable_vblank,
934 .disable_vblank = fimd_disable_vblank,
935 .atomic_begin = fimd_atomic_begin,
936 .update_plane = fimd_update_plane,
937 .disable_plane = fimd_disable_plane,
938 .atomic_flush = fimd_atomic_flush,
939 .atomic_check = fimd_atomic_check,
940 .te_handler = fimd_te_handler,
943 static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
945 struct fimd_context *ctx = (struct fimd_context *)dev_id;
946 u32 val, clear_bit;
948 val = readl(ctx->regs + VIDINTCON1);
950 clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
951 if (val & clear_bit)
952 writel(clear_bit, ctx->regs + VIDINTCON1);
954 /* check the crtc is detached already from encoder */
955 if (!ctx->drm_dev)
956 goto out;
958 if (!ctx->i80_if)
959 drm_crtc_handle_vblank(&ctx->crtc->base);
961 if (ctx->i80_if) {
962 /* Exits triggering mode */
963 atomic_set(&ctx->triggering, 0);
964 } else {
965 /* set wait vsync event to zero and wake up queue. */
966 if (atomic_read(&ctx->wait_vsync_event)) {
967 atomic_set(&ctx->wait_vsync_event, 0);
968 wake_up(&ctx->wait_vsync_queue);
972 out:
973 return IRQ_HANDLED;
976 static int fimd_bind(struct device *dev, struct device *master, void *data)
978 struct fimd_context *ctx = dev_get_drvdata(dev);
979 struct drm_device *drm_dev = data;
980 struct exynos_drm_plane *exynos_plane;
981 unsigned int i;
982 int ret;
984 ctx->drm_dev = drm_dev;
986 for (i = 0; i < WINDOWS_NR; i++) {
987 ctx->configs[i].pixel_formats = fimd_formats;
988 ctx->configs[i].num_pixel_formats = ARRAY_SIZE(fimd_formats);
989 ctx->configs[i].zpos = i;
990 ctx->configs[i].type = fimd_win_types[i];
991 ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
992 &ctx->configs[i]);
993 if (ret)
994 return ret;
997 exynos_plane = &ctx->planes[DEFAULT_WIN];
998 ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
999 EXYNOS_DISPLAY_TYPE_LCD, &fimd_crtc_ops, ctx);
1000 if (IS_ERR(ctx->crtc))
1001 return PTR_ERR(ctx->crtc);
1003 if (ctx->driver_data->has_dp_clk) {
1004 ctx->dp_clk.enable = fimd_dp_clock_enable;
1005 ctx->crtc->pipe_clk = &ctx->dp_clk;
1008 if (ctx->encoder)
1009 exynos_dpi_bind(drm_dev, ctx->encoder);
1011 if (is_drm_iommu_supported(drm_dev))
1012 fimd_clear_channels(ctx->crtc);
1014 return drm_iommu_attach_device(drm_dev, dev);
1017 static void fimd_unbind(struct device *dev, struct device *master,
1018 void *data)
1020 struct fimd_context *ctx = dev_get_drvdata(dev);
1022 fimd_disable(ctx->crtc);
1024 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
1026 if (ctx->encoder)
1027 exynos_dpi_remove(ctx->encoder);
1030 static const struct component_ops fimd_component_ops = {
1031 .bind = fimd_bind,
1032 .unbind = fimd_unbind,
1035 static int fimd_probe(struct platform_device *pdev)
1037 struct device *dev = &pdev->dev;
1038 struct fimd_context *ctx;
1039 struct device_node *i80_if_timings;
1040 struct resource *res;
1041 int ret;
1043 if (!dev->of_node)
1044 return -ENODEV;
1046 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1047 if (!ctx)
1048 return -ENOMEM;
1050 ctx->dev = dev;
1051 ctx->suspended = true;
1052 ctx->driver_data = of_device_get_match_data(dev);
1054 if (of_property_read_bool(dev->of_node, "samsung,invert-vden"))
1055 ctx->vidcon1 |= VIDCON1_INV_VDEN;
1056 if (of_property_read_bool(dev->of_node, "samsung,invert-vclk"))
1057 ctx->vidcon1 |= VIDCON1_INV_VCLK;
1059 i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
1060 if (i80_if_timings) {
1061 u32 val;
1063 ctx->i80_if = true;
1065 if (ctx->driver_data->has_vidoutcon)
1066 ctx->vidout_con |= VIDOUT_CON_F_I80_LDI0;
1067 else
1068 ctx->vidcon0 |= VIDCON0_VIDOUT_I80_LDI0;
1070 * The user manual describes that this "DSI_EN" bit is required
1071 * to enable I80 24-bit data interface.
1073 ctx->vidcon0 |= VIDCON0_DSI_EN;
1075 if (of_property_read_u32(i80_if_timings, "cs-setup", &val))
1076 val = 0;
1077 ctx->i80ifcon = LCD_CS_SETUP(val);
1078 if (of_property_read_u32(i80_if_timings, "wr-setup", &val))
1079 val = 0;
1080 ctx->i80ifcon |= LCD_WR_SETUP(val);
1081 if (of_property_read_u32(i80_if_timings, "wr-active", &val))
1082 val = 1;
1083 ctx->i80ifcon |= LCD_WR_ACTIVE(val);
1084 if (of_property_read_u32(i80_if_timings, "wr-hold", &val))
1085 val = 0;
1086 ctx->i80ifcon |= LCD_WR_HOLD(val);
1088 of_node_put(i80_if_timings);
1090 ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
1091 "samsung,sysreg");
1092 if (IS_ERR(ctx->sysreg)) {
1093 dev_warn(dev, "failed to get system register.\n");
1094 ctx->sysreg = NULL;
1097 ctx->bus_clk = devm_clk_get(dev, "fimd");
1098 if (IS_ERR(ctx->bus_clk)) {
1099 dev_err(dev, "failed to get bus clock\n");
1100 return PTR_ERR(ctx->bus_clk);
1103 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
1104 if (IS_ERR(ctx->lcd_clk)) {
1105 dev_err(dev, "failed to get lcd clock\n");
1106 return PTR_ERR(ctx->lcd_clk);
1109 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1111 ctx->regs = devm_ioremap_resource(dev, res);
1112 if (IS_ERR(ctx->regs))
1113 return PTR_ERR(ctx->regs);
1115 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1116 ctx->i80_if ? "lcd_sys" : "vsync");
1117 if (!res) {
1118 dev_err(dev, "irq request failed.\n");
1119 return -ENXIO;
1122 ret = devm_request_irq(dev, res->start, fimd_irq_handler,
1123 0, "drm_fimd", ctx);
1124 if (ret) {
1125 dev_err(dev, "irq request failed.\n");
1126 return ret;
1129 init_waitqueue_head(&ctx->wait_vsync_queue);
1130 atomic_set(&ctx->wait_vsync_event, 0);
1132 platform_set_drvdata(pdev, ctx);
1134 ctx->encoder = exynos_dpi_probe(dev);
1135 if (IS_ERR(ctx->encoder))
1136 return PTR_ERR(ctx->encoder);
1138 pm_runtime_enable(dev);
1140 ret = component_add(dev, &fimd_component_ops);
1141 if (ret)
1142 goto err_disable_pm_runtime;
1144 return ret;
1146 err_disable_pm_runtime:
1147 pm_runtime_disable(dev);
1149 return ret;
1152 static int fimd_remove(struct platform_device *pdev)
1154 pm_runtime_disable(&pdev->dev);
1156 component_del(&pdev->dev, &fimd_component_ops);
1158 return 0;
1161 #ifdef CONFIG_PM
1162 static int exynos_fimd_suspend(struct device *dev)
1164 struct fimd_context *ctx = dev_get_drvdata(dev);
1166 clk_disable_unprepare(ctx->lcd_clk);
1167 clk_disable_unprepare(ctx->bus_clk);
1169 return 0;
1172 static int exynos_fimd_resume(struct device *dev)
1174 struct fimd_context *ctx = dev_get_drvdata(dev);
1175 int ret;
1177 ret = clk_prepare_enable(ctx->bus_clk);
1178 if (ret < 0) {
1179 DRM_ERROR("Failed to prepare_enable the bus clk [%d]\n", ret);
1180 return ret;
1183 ret = clk_prepare_enable(ctx->lcd_clk);
1184 if (ret < 0) {
1185 DRM_ERROR("Failed to prepare_enable the lcd clk [%d]\n", ret);
1186 return ret;
1189 return 0;
1191 #endif
1193 static const struct dev_pm_ops exynos_fimd_pm_ops = {
1194 SET_RUNTIME_PM_OPS(exynos_fimd_suspend, exynos_fimd_resume, NULL)
1195 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1196 pm_runtime_force_resume)
1199 struct platform_driver fimd_driver = {
1200 .probe = fimd_probe,
1201 .remove = fimd_remove,
1202 .driver = {
1203 .name = "exynos4-fb",
1204 .owner = THIS_MODULE,
1205 .pm = &exynos_fimd_pm_ops,
1206 .of_match_table = fimd_driver_dt_match,