blob: 7593f629c9fb9e2c6ab2d8945e3da20eed7334f6 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_fimd.c
2 *
3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
4 * Authors:
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Inki Dae <inki.dae@samsung.com>
7 *
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.
12 *
13 */
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/drmP.h>
Inki Dae1c248b72011-10-04 19:19:01 +090015
16#include <linux/kernel.h>
Inki Dae1c248b72011-10-04 19:19:01 +090017#include <linux/platform_device.h>
18#include <linux/clk.h>
Sachin Kamat3f1c7812013-08-14 16:38:01 +053019#include <linux/of.h>
Joonyoung Shimd636ead2012-12-14 15:48:25 +090020#include <linux/of_device.h>
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +090021#include <linux/pm_runtime.h>
Inki Daef37cd5e2014-05-09 14:25:20 +090022#include <linux/component.h>
YoungJun Cho3854fab2014-07-17 18:01:21 +090023#include <linux/mfd/syscon.h>
24#include <linux/regmap.h>
Inki Dae1c248b72011-10-04 19:19:01 +090025
Vikas Sajjan7f4596f2013-03-07 12:15:21 +053026#include <video/of_display_timing.h>
Andrzej Hajda111e6052013-08-21 16:22:01 +020027#include <video/of_videomode.h>
Leela Krishna Amudala5a213a52012-08-08 09:44:49 +090028#include <video/samsung_fimd.h>
Inki Dae1c248b72011-10-04 19:19:01 +090029#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090030
31#include "exynos_drm_drv.h"
32#include "exynos_drm_fbdev.h"
33#include "exynos_drm_crtc.h"
Inki Daebcc5cd1c2012-10-19 17:16:36 +090034#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090035
36/*
Sachin Kamatb8654b32013-09-19 10:39:44 +053037 * FIMD stands for Fully Interactive Mobile Display and
Inki Dae1c248b72011-10-04 19:19:01 +090038 * as a display controller, it transfers contents drawn on memory
39 * to a LCD Panel through Display Interfaces such as RGB or
40 * CPU Interface.
41 */
42
Andrzej Hajda111e6052013-08-21 16:22:01 +020043#define FIMD_DEFAULT_FRAMERATE 60
Rahul Sharma66367462014-05-07 16:55:22 +053044#define MIN_FB_WIDTH_FOR_16WORD_BURST 128
Andrzej Hajda111e6052013-08-21 16:22:01 +020045
Inki Dae1c248b72011-10-04 19:19:01 +090046/* 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)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050049/*
50 * size control register for hardware windows 0 and alpha control register
51 * for hardware windows 1 ~ 4
52 */
53#define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
54/* size control register for hardware windows 1 ~ 2. */
Inki Dae1c248b72011-10-04 19:19:01 +090055#define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
56
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)
60
61/* color key control register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050062#define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090063/* color key value register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050064#define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090065
YoungJun Cho3854fab2014-07-17 18:01:21 +090066/* I80 / RGB trigger control register */
67#define TRIGCON 0x1A4
68#define TRGMODE_I80_RGB_ENABLE_I80 (1 << 0)
69#define SWTRGCMD_I80_RGB_ENABLE (1 << 1)
70
71/* display mode change control register except exynos4 */
72#define VIDOUT_CON 0x000
73#define VIDOUT_CON_F_I80_LDI0 (0x2 << 8)
74
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)
83
Inki Dae1c248b72011-10-04 19:19:01 +090084/* FIMD has totally five hardware windows. */
85#define WINDOWS_NR 5
86
Sean Paulbb7704d2014-01-30 16:19:06 -050087#define get_fimd_manager(mgr) platform_get_drvdata(to_platform_device(dev))
Inki Dae1c248b72011-10-04 19:19:01 +090088
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053089struct fimd_driver_data {
90 unsigned int timing_base;
YoungJun Cho3854fab2014-07-17 18:01:21 +090091 unsigned int lcdblk_offset;
92 unsigned int lcdblk_vt_shift;
93 unsigned int lcdblk_bypass_shift;
Tomasz Figade7af102013-05-01 21:02:27 +020094
95 unsigned int has_shadowcon:1;
Tomasz Figa411d9ed2013-05-01 21:02:28 +020096 unsigned int has_clksel:1;
Inki Dae5cc46212013-08-20 14:28:56 +090097 unsigned int has_limited_fmt:1;
YoungJun Cho3854fab2014-07-17 18:01:21 +090098 unsigned int has_vidoutcon:1;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053099};
100
Tomasz Figa725ddea2013-05-01 21:02:29 +0200101static struct fimd_driver_data s3c64xx_fimd_driver_data = {
102 .timing_base = 0x0,
103 .has_clksel = 1,
Inki Dae5cc46212013-08-20 14:28:56 +0900104 .has_limited_fmt = 1,
Tomasz Figa725ddea2013-05-01 21:02:29 +0200105};
106
Inki Daed6ce7b52014-08-18 16:53:19 +0900107static struct fimd_driver_data exynos3_fimd_driver_data = {
108 .timing_base = 0x20000,
109 .lcdblk_offset = 0x210,
110 .lcdblk_bypass_shift = 1,
111 .has_shadowcon = 1,
112 .has_vidoutcon = 1,
113};
114
Sachin Kamat6ecf18f2012-11-19 15:22:54 +0530115static struct fimd_driver_data exynos4_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530116 .timing_base = 0x0,
YoungJun Cho3854fab2014-07-17 18:01:21 +0900117 .lcdblk_offset = 0x210,
118 .lcdblk_vt_shift = 10,
119 .lcdblk_bypass_shift = 1,
Tomasz Figade7af102013-05-01 21:02:27 +0200120 .has_shadowcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530121};
122
YoungJun Chodcb622a2014-11-07 15:12:25 +0900123static 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,
128 .has_shadowcon = 1,
129 .has_vidoutcon = 1,
130};
131
Sachin Kamat6ecf18f2012-11-19 15:22:54 +0530132static struct fimd_driver_data exynos5_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530133 .timing_base = 0x20000,
YoungJun Cho3854fab2014-07-17 18:01:21 +0900134 .lcdblk_offset = 0x214,
135 .lcdblk_vt_shift = 24,
136 .lcdblk_bypass_shift = 15,
Tomasz Figade7af102013-05-01 21:02:27 +0200137 .has_shadowcon = 1,
YoungJun Cho3854fab2014-07-17 18:01:21 +0900138 .has_vidoutcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530139};
140
Inki Dae1c248b72011-10-04 19:19:01 +0900141struct fimd_win_data {
142 unsigned int offset_x;
143 unsigned int offset_y;
Inki Dae19c8b832011-10-14 13:29:46 +0900144 unsigned int ovl_width;
145 unsigned int ovl_height;
146 unsigned int fb_width;
147 unsigned int fb_height;
Inki Dae1c248b72011-10-04 19:19:01 +0900148 unsigned int bpp;
Inki Daea4f38a82013-08-20 13:51:02 +0900149 unsigned int pixel_format;
Inki Dae2c871122011-11-12 15:23:32 +0900150 dma_addr_t dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900151 unsigned int buf_offsize;
152 unsigned int line_size; /* bytes */
Inki Daeec05da92011-12-06 11:06:54 +0900153 bool enabled;
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530154 bool resume;
Inki Dae1c248b72011-10-04 19:19:01 +0900155};
156
157struct fimd_context {
Sean Paulbb7704d2014-01-30 16:19:06 -0500158 struct device *dev;
Sean Paul40c8ab42014-01-30 16:19:04 -0500159 struct drm_device *drm_dev;
Inki Dae1c248b72011-10-04 19:19:01 +0900160 struct clk *bus_clk;
161 struct clk *lcd_clk;
Inki Dae1c248b72011-10-04 19:19:01 +0900162 void __iomem *regs;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900163 struct regmap *sysreg;
Sean Paula968e722014-01-30 16:19:20 -0500164 struct drm_display_mode mode;
Inki Dae1c248b72011-10-04 19:19:01 +0900165 struct fimd_win_data win_data[WINDOWS_NR];
Inki Dae1c248b72011-10-04 19:19:01 +0900166 unsigned int default_win;
167 unsigned long irq_flags;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900168 u32 vidcon0;
Inki Dae1c248b72011-10-04 19:19:01 +0900169 u32 vidcon1;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900170 u32 vidout_con;
171 u32 i80ifcon;
172 bool i80_if;
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900173 bool suspended;
Sean Paul080be03d2014-02-19 21:02:55 +0900174 int pipe;
Prathyush K01ce1132012-12-06 20:16:04 +0530175 wait_queue_head_t wait_vsync_queue;
176 atomic_t wait_vsync_event;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900177 atomic_t win_updated;
178 atomic_t triggering;
Inki Dae1c248b72011-10-04 19:19:01 +0900179
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200180 struct exynos_drm_panel_info panel;
Tomasz Figa18873462013-05-01 21:02:26 +0200181 struct fimd_driver_data *driver_data;
Andrzej Hajda000cc922014-04-03 16:26:00 +0200182 struct exynos_drm_display *display;
Inki Dae1c248b72011-10-04 19:19:01 +0900183};
184
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900185static const struct of_device_id fimd_driver_dt_match[] = {
Tomasz Figa725ddea2013-05-01 21:02:29 +0200186 { .compatible = "samsung,s3c6400-fimd",
187 .data = &s3c64xx_fimd_driver_data },
Inki Daed6ce7b52014-08-18 16:53:19 +0900188 { .compatible = "samsung,exynos3250-fimd",
189 .data = &exynos3_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530190 { .compatible = "samsung,exynos4210-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900191 .data = &exynos4_fimd_driver_data },
YoungJun Chodcb622a2014-11-07 15:12:25 +0900192 { .compatible = "samsung,exynos4415-fimd",
193 .data = &exynos4415_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530194 { .compatible = "samsung,exynos5250-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900195 .data = &exynos5_fimd_driver_data },
196 {},
197};
Sjoerd Simons0262cee2014-07-30 11:28:31 +0900198MODULE_DEVICE_TABLE(of, fimd_driver_dt_match);
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900199
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530200static inline struct fimd_driver_data *drm_fimd_get_driver_data(
201 struct platform_device *pdev)
202{
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900203 const struct of_device_id *of_id =
204 of_match_device(fimd_driver_dt_match, &pdev->dev);
205
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530206 return (struct fimd_driver_data *)of_id->data;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530207}
208
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900209static void fimd_wait_for_vblank(struct exynos_drm_manager *mgr)
210{
211 struct fimd_context *ctx = mgr->ctx;
212
213 if (ctx->suspended)
214 return;
215
216 atomic_set(&ctx->wait_vsync_event, 1);
217
218 /*
219 * wait for FIMD to signal VSYNC interrupt or return after
220 * timeout which is set to 50ms (refresh rate of 20).
221 */
222 if (!wait_event_timeout(ctx->wait_vsync_queue,
223 !atomic_read(&ctx->wait_vsync_event),
224 HZ/20))
225 DRM_DEBUG_KMS("vblank wait timed out.\n");
226}
227
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900228static void fimd_clear_channel(struct exynos_drm_manager *mgr)
229{
230 struct fimd_context *ctx = mgr->ctx;
231 int win, ch_enabled = 0;
232
233 DRM_DEBUG_KMS("%s\n", __FILE__);
234
235 /* Check if any channel is enabled. */
236 for (win = 0; win < WINDOWS_NR; win++) {
Marek Szyprowskieb8a3bf2014-09-01 22:27:10 +0900237 u32 val = readl(ctx->regs + WINCON(win));
238
239 if (val & WINCONx_ENWIN) {
240 /* wincon */
241 val &= ~WINCONx_ENWIN;
242 writel(val, ctx->regs + WINCON(win));
243
244 /* unprotect windows */
245 if (ctx->driver_data->has_shadowcon) {
246 val = readl(ctx->regs + SHADOWCON);
247 val &= ~SHADOWCON_CHx_ENABLE(win);
248 writel(val, ctx->regs + SHADOWCON);
249 }
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900250 ch_enabled = 1;
251 }
252 }
253
254 /* Wait for vsync, as disable channel takes effect at next vsync */
Marek Szyprowskieb8a3bf2014-09-01 22:27:10 +0900255 if (ch_enabled) {
256 unsigned int state = ctx->suspended;
257
258 ctx->suspended = 0;
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900259 fimd_wait_for_vblank(mgr);
Marek Szyprowskieb8a3bf2014-09-01 22:27:10 +0900260 ctx->suspended = state;
261 }
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900262}
263
Sean Paulbb7704d2014-01-30 16:19:06 -0500264static int fimd_mgr_initialize(struct exynos_drm_manager *mgr,
Inki Daef37cd5e2014-05-09 14:25:20 +0900265 struct drm_device *drm_dev)
Sean Paul40c8ab42014-01-30 16:19:04 -0500266{
Sean Paulbb7704d2014-01-30 16:19:06 -0500267 struct fimd_context *ctx = mgr->ctx;
Inki Daef37cd5e2014-05-09 14:25:20 +0900268 struct exynos_drm_private *priv;
269 priv = drm_dev->dev_private;
Sean Paul40c8ab42014-01-30 16:19:04 -0500270
Inki Daef37cd5e2014-05-09 14:25:20 +0900271 mgr->drm_dev = ctx->drm_dev = drm_dev;
272 mgr->pipe = ctx->pipe = priv->pipe++;
Sean Paul080be03d2014-02-19 21:02:55 +0900273
Sean Paul080be03d2014-02-19 21:02:55 +0900274 /* attach this sub driver to iommu mapping if supported. */
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900275 if (is_drm_iommu_supported(ctx->drm_dev)) {
276 /*
277 * If any channel is already active, iommu will throw
278 * a PAGE FAULT when enabled. So clear any channel if enabled.
279 */
280 fimd_clear_channel(mgr);
Sean Paul080be03d2014-02-19 21:02:55 +0900281 drm_iommu_attach_device(ctx->drm_dev, ctx->dev);
Akshu Agrawalf13bdbd2014-04-28 21:26:39 +0900282 }
Sean Paul40c8ab42014-01-30 16:19:04 -0500283
284 return 0;
285}
286
Sean Paul080be03d2014-02-19 21:02:55 +0900287static void fimd_mgr_remove(struct exynos_drm_manager *mgr)
Inki Daeec05da92011-12-06 11:06:54 +0900288{
Sean Paulbb7704d2014-01-30 16:19:06 -0500289 struct fimd_context *ctx = mgr->ctx;
Inki Daec32b06e2011-12-16 21:49:03 +0900290
Sean Paul080be03d2014-02-19 21:02:55 +0900291 /* detach this sub driver from iommu mapping if supported. */
292 if (is_drm_iommu_supported(ctx->drm_dev))
293 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
Inki Daeec05da92011-12-06 11:06:54 +0900294}
295
Sean Paula968e722014-01-30 16:19:20 -0500296static u32 fimd_calc_clkdiv(struct fimd_context *ctx,
297 const struct drm_display_mode *mode)
298{
299 unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
300 u32 clkdiv;
301
YoungJun Cho3854fab2014-07-17 18:01:21 +0900302 if (ctx->i80_if) {
303 /*
304 * The frame done interrupt should be occurred prior to the
305 * next TE signal.
306 */
307 ideal_clk *= 2;
308 }
309
Sean Paula968e722014-01-30 16:19:20 -0500310 /* Find the clock divider value that gets us closest to ideal_clk */
311 clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->lcd_clk), ideal_clk);
312
313 return (clkdiv < 0x100) ? clkdiv : 0xff;
314}
315
316static bool fimd_mode_fixup(struct exynos_drm_manager *mgr,
317 const struct drm_display_mode *mode,
318 struct drm_display_mode *adjusted_mode)
319{
320 if (adjusted_mode->vrefresh == 0)
321 adjusted_mode->vrefresh = FIMD_DEFAULT_FRAMERATE;
322
323 return true;
324}
325
326static void fimd_mode_set(struct exynos_drm_manager *mgr,
327 const struct drm_display_mode *in_mode)
328{
329 struct fimd_context *ctx = mgr->ctx;
330
331 drm_mode_copy(&ctx->mode, in_mode);
332}
333
Sean Paulbb7704d2014-01-30 16:19:06 -0500334static void fimd_commit(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900335{
Sean Paulbb7704d2014-01-30 16:19:06 -0500336 struct fimd_context *ctx = mgr->ctx;
Sean Paula968e722014-01-30 16:19:20 -0500337 struct drm_display_mode *mode = &ctx->mode;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900338 struct fimd_driver_data *driver_data = ctx->driver_data;
339 void *timing_base = ctx->regs + driver_data->timing_base;
340 u32 val, clkdiv;
Inki Dae1c248b72011-10-04 19:19:01 +0900341
Inki Daee30d4bc2011-12-12 16:35:20 +0900342 if (ctx->suspended)
343 return;
344
Sean Paula968e722014-01-30 16:19:20 -0500345 /* nothing to do if we haven't set the mode yet */
346 if (mode->htotal == 0 || mode->vtotal == 0)
347 return;
348
YoungJun Cho3854fab2014-07-17 18:01:21 +0900349 if (ctx->i80_if) {
350 val = ctx->i80ifcon | I80IFEN_ENABLE;
351 writel(val, timing_base + I80IFCONFAx(0));
Inki Dae1c248b72011-10-04 19:19:01 +0900352
YoungJun Cho3854fab2014-07-17 18:01:21 +0900353 /* disable auto frame rate */
354 writel(0, timing_base + I80IFCONFBx(0));
Sean Paula968e722014-01-30 16:19:20 -0500355
YoungJun Cho3854fab2014-07-17 18:01:21 +0900356 /* set video type selection to I80 interface */
357 if (ctx->sysreg && regmap_update_bits(ctx->sysreg,
358 driver_data->lcdblk_offset,
359 0x3 << driver_data->lcdblk_vt_shift,
360 0x1 << driver_data->lcdblk_vt_shift)) {
361 DRM_ERROR("Failed to update sysreg for I80 i/f.\n");
362 return;
363 }
364 } else {
365 int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
366 u32 vidcon1;
Inki Dae1c248b72011-10-04 19:19:01 +0900367
YoungJun Cho3854fab2014-07-17 18:01:21 +0900368 /* setup polarity values */
369 vidcon1 = ctx->vidcon1;
370 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
371 vidcon1 |= VIDCON1_INV_VSYNC;
372 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
373 vidcon1 |= VIDCON1_INV_HSYNC;
374 writel(vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
Sean Paula968e722014-01-30 16:19:20 -0500375
YoungJun Cho3854fab2014-07-17 18:01:21 +0900376 /* setup vertical timing values. */
377 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
378 vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
379 vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
380
381 val = VIDTCON0_VBPD(vbpd - 1) |
382 VIDTCON0_VFPD(vfpd - 1) |
383 VIDTCON0_VSPW(vsync_len - 1);
384 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
385
386 /* setup horizontal timing values. */
387 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
388 hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
389 hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
390
391 val = VIDTCON1_HBPD(hbpd - 1) |
392 VIDTCON1_HFPD(hfpd - 1) |
393 VIDTCON1_HSPW(hsync_len - 1);
394 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
395 }
396
397 if (driver_data->has_vidoutcon)
398 writel(ctx->vidout_con, timing_base + VIDOUT_CON);
399
400 /* set bypass selection */
401 if (ctx->sysreg && regmap_update_bits(ctx->sysreg,
402 driver_data->lcdblk_offset,
403 0x1 << driver_data->lcdblk_bypass_shift,
404 0x1 << driver_data->lcdblk_bypass_shift)) {
405 DRM_ERROR("Failed to update sysreg for bypass setting.\n");
406 return;
407 }
Inki Dae1c248b72011-10-04 19:19:01 +0900408
409 /* setup horizontal and vertical display size. */
Sean Paula968e722014-01-30 16:19:20 -0500410 val = VIDTCON2_LINEVAL(mode->vdisplay - 1) |
411 VIDTCON2_HOZVAL(mode->hdisplay - 1) |
412 VIDTCON2_LINEVAL_E(mode->vdisplay - 1) |
413 VIDTCON2_HOZVAL_E(mode->hdisplay - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530414 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
Inki Dae1c248b72011-10-04 19:19:01 +0900415
Inki Dae1c248b72011-10-04 19:19:01 +0900416 /*
417 * fields of register with prefix '_F' would be updated
418 * at vsync(same as dma start)
419 */
YoungJun Cho3854fab2014-07-17 18:01:21 +0900420 val = ctx->vidcon0;
421 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
Andrzej Hajda1d531062014-03-20 17:09:00 +0900422
423 if (ctx->driver_data->has_clksel)
424 val |= VIDCON0_CLKSEL_LCD;
425
426 clkdiv = fimd_calc_clkdiv(ctx, mode);
427 if (clkdiv > 1)
428 val |= VIDCON0_CLKVAL_F(clkdiv - 1) | VIDCON0_CLKDIR;
429
Inki Dae1c248b72011-10-04 19:19:01 +0900430 writel(val, ctx->regs + VIDCON0);
431}
432
Sean Paulbb7704d2014-01-30 16:19:06 -0500433static int fimd_enable_vblank(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900434{
Sean Paulbb7704d2014-01-30 16:19:06 -0500435 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900436 u32 val;
437
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900438 if (ctx->suspended)
439 return -EPERM;
440
Inki Dae1c248b72011-10-04 19:19:01 +0900441 if (!test_and_set_bit(0, &ctx->irq_flags)) {
442 val = readl(ctx->regs + VIDINTCON0);
443
444 val |= VIDINTCON0_INT_ENABLE;
445 val |= VIDINTCON0_INT_FRAME;
446
447 val &= ~VIDINTCON0_FRAMESEL0_MASK;
448 val |= VIDINTCON0_FRAMESEL0_VSYNC;
449 val &= ~VIDINTCON0_FRAMESEL1_MASK;
450 val |= VIDINTCON0_FRAMESEL1_NONE;
451
452 writel(val, ctx->regs + VIDINTCON0);
453 }
454
455 return 0;
456}
457
Sean Paulbb7704d2014-01-30 16:19:06 -0500458static void fimd_disable_vblank(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900459{
Sean Paulbb7704d2014-01-30 16:19:06 -0500460 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900461 u32 val;
462
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900463 if (ctx->suspended)
464 return;
465
Inki Dae1c248b72011-10-04 19:19:01 +0900466 if (test_and_clear_bit(0, &ctx->irq_flags)) {
467 val = readl(ctx->regs + VIDINTCON0);
468
469 val &= ~VIDINTCON0_INT_FRAME;
470 val &= ~VIDINTCON0_INT_ENABLE;
471
472 writel(val, ctx->regs + VIDINTCON0);
473 }
474}
475
Sean Paulbb7704d2014-01-30 16:19:06 -0500476static void fimd_win_mode_set(struct exynos_drm_manager *mgr,
477 struct exynos_drm_overlay *overlay)
Inki Dae1c248b72011-10-04 19:19:01 +0900478{
Sean Paulbb7704d2014-01-30 16:19:06 -0500479 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900480 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900481 int win;
Inki Dae19c8b832011-10-14 13:29:46 +0900482 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900483
Inki Dae1c248b72011-10-04 19:19:01 +0900484 if (!overlay) {
Sean Paulbb7704d2014-01-30 16:19:06 -0500485 DRM_ERROR("overlay is NULL\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900486 return;
487 }
488
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900489 win = overlay->zpos;
490 if (win == DEFAULT_ZPOS)
491 win = ctx->default_win;
492
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200493 if (win < 0 || win >= WINDOWS_NR)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900494 return;
495
Inki Dae19c8b832011-10-14 13:29:46 +0900496 offset = overlay->fb_x * (overlay->bpp >> 3);
497 offset += overlay->fb_y * overlay->pitch;
498
499 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
500
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900501 win_data = &ctx->win_data[win];
Inki Dae1c248b72011-10-04 19:19:01 +0900502
Inki Dae19c8b832011-10-14 13:29:46 +0900503 win_data->offset_x = overlay->crtc_x;
504 win_data->offset_y = overlay->crtc_y;
505 win_data->ovl_width = overlay->crtc_width;
506 win_data->ovl_height = overlay->crtc_height;
507 win_data->fb_width = overlay->fb_width;
508 win_data->fb_height = overlay->fb_height;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900509 win_data->dma_addr = overlay->dma_addr[0] + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900510 win_data->bpp = overlay->bpp;
Inki Daea4f38a82013-08-20 13:51:02 +0900511 win_data->pixel_format = overlay->pixel_format;
Inki Dae19c8b832011-10-14 13:29:46 +0900512 win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
513 (overlay->bpp >> 3);
514 win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
515
516 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
517 win_data->offset_x, win_data->offset_y);
518 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
519 win_data->ovl_width, win_data->ovl_height);
YoungJun Choddd8e952012-12-10 15:44:58 +0900520 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
Inki Dae19c8b832011-10-14 13:29:46 +0900521 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
522 overlay->fb_width, overlay->crtc_width);
Inki Dae1c248b72011-10-04 19:19:01 +0900523}
524
Sean Paulbb7704d2014-01-30 16:19:06 -0500525static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win)
Inki Dae1c248b72011-10-04 19:19:01 +0900526{
Inki Dae1c248b72011-10-04 19:19:01 +0900527 struct fimd_win_data *win_data = &ctx->win_data[win];
528 unsigned long val;
529
Inki Dae1c248b72011-10-04 19:19:01 +0900530 val = WINCONx_ENWIN;
531
Inki Dae5cc46212013-08-20 14:28:56 +0900532 /*
533 * In case of s3c64xx, window 0 doesn't support alpha channel.
534 * So the request format is ARGB8888 then change it to XRGB8888.
535 */
536 if (ctx->driver_data->has_limited_fmt && !win) {
537 if (win_data->pixel_format == DRM_FORMAT_ARGB8888)
538 win_data->pixel_format = DRM_FORMAT_XRGB8888;
539 }
540
Inki Daea4f38a82013-08-20 13:51:02 +0900541 switch (win_data->pixel_format) {
542 case DRM_FORMAT_C8:
Inki Dae1c248b72011-10-04 19:19:01 +0900543 val |= WINCON0_BPPMODE_8BPP_PALETTE;
544 val |= WINCONx_BURSTLEN_8WORD;
545 val |= WINCONx_BYTSWP;
546 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900547 case DRM_FORMAT_XRGB1555:
548 val |= WINCON0_BPPMODE_16BPP_1555;
549 val |= WINCONx_HAWSWP;
550 val |= WINCONx_BURSTLEN_16WORD;
551 break;
552 case DRM_FORMAT_RGB565:
Inki Dae1c248b72011-10-04 19:19:01 +0900553 val |= WINCON0_BPPMODE_16BPP_565;
554 val |= WINCONx_HAWSWP;
555 val |= WINCONx_BURSTLEN_16WORD;
556 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900557 case DRM_FORMAT_XRGB8888:
Inki Dae1c248b72011-10-04 19:19:01 +0900558 val |= WINCON0_BPPMODE_24BPP_888;
559 val |= WINCONx_WSWP;
560 val |= WINCONx_BURSTLEN_16WORD;
561 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900562 case DRM_FORMAT_ARGB8888:
563 val |= WINCON1_BPPMODE_25BPP_A1888
Inki Dae1c248b72011-10-04 19:19:01 +0900564 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
565 val |= WINCONx_WSWP;
566 val |= WINCONx_BURSTLEN_16WORD;
567 break;
568 default:
569 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
570
571 val |= WINCON0_BPPMODE_24BPP_888;
572 val |= WINCONx_WSWP;
573 val |= WINCONx_BURSTLEN_16WORD;
574 break;
575 }
576
577 DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
578
Rahul Sharma66367462014-05-07 16:55:22 +0530579 /*
580 * In case of exynos, setting dma-burst to 16Word causes permanent
581 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
582 * switching which is based on overlay size is not recommended as
583 * overlay size varies alot towards the end of the screen and rapid
584 * movement causes unstable DMA which results into iommu crash/tear.
585 */
586
587 if (win_data->fb_width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
588 val &= ~WINCONx_BURSTLEN_MASK;
589 val |= WINCONx_BURSTLEN_4WORD;
590 }
591
Inki Dae1c248b72011-10-04 19:19:01 +0900592 writel(val, ctx->regs + WINCON(win));
593}
594
Sean Paulbb7704d2014-01-30 16:19:06 -0500595static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
Inki Dae1c248b72011-10-04 19:19:01 +0900596{
Inki Dae1c248b72011-10-04 19:19:01 +0900597 unsigned int keycon0 = 0, keycon1 = 0;
598
Inki Dae1c248b72011-10-04 19:19:01 +0900599 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
600 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
601
602 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
603
604 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
605 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
606}
607
Tomasz Figade7af102013-05-01 21:02:27 +0200608/**
609 * shadow_protect_win() - disable updating values from shadow registers at vsync
610 *
611 * @win: window to protect registers for
612 * @protect: 1 to protect (disable updates)
613 */
614static void fimd_shadow_protect_win(struct fimd_context *ctx,
615 int win, bool protect)
616{
617 u32 reg, bits, val;
618
619 if (ctx->driver_data->has_shadowcon) {
620 reg = SHADOWCON;
621 bits = SHADOWCON_WINx_PROTECT(win);
622 } else {
623 reg = PRTCON;
624 bits = PRTCON_PROTECT;
625 }
626
627 val = readl(ctx->regs + reg);
628 if (protect)
629 val |= bits;
630 else
631 val &= ~bits;
632 writel(val, ctx->regs + reg);
633}
634
Sean Paulbb7704d2014-01-30 16:19:06 -0500635static void fimd_win_commit(struct exynos_drm_manager *mgr, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900636{
Sean Paulbb7704d2014-01-30 16:19:06 -0500637 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900638 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900639 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900640 unsigned long val, alpha, size;
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900641 unsigned int last_x;
642 unsigned int last_y;
Inki Dae1c248b72011-10-04 19:19:01 +0900643
Inki Daee30d4bc2011-12-12 16:35:20 +0900644 if (ctx->suspended)
645 return;
646
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900647 if (win == DEFAULT_ZPOS)
648 win = ctx->default_win;
649
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200650 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900651 return;
652
653 win_data = &ctx->win_data[win];
654
Sean Paula43b9332014-01-30 16:19:26 -0500655 /* If suspended, enable this on resume */
656 if (ctx->suspended) {
657 win_data->resume = true;
658 return;
659 }
660
Inki Dae1c248b72011-10-04 19:19:01 +0900661 /*
Tomasz Figade7af102013-05-01 21:02:27 +0200662 * SHADOWCON/PRTCON register is used for enabling timing.
Inki Dae1c248b72011-10-04 19:19:01 +0900663 *
664 * for example, once only width value of a register is set,
665 * if the dma is started then fimd hardware could malfunction so
666 * with protect window setting, the register fields with prefix '_F'
667 * wouldn't be updated at vsync also but updated once unprotect window
668 * is set.
669 */
670
671 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200672 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900673
674 /* buffer start address */
Inki Dae2c871122011-11-12 15:23:32 +0900675 val = (unsigned long)win_data->dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900676 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
677
678 /* buffer end address */
Inki Dae19c8b832011-10-14 13:29:46 +0900679 size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
Inki Dae2c871122011-11-12 15:23:32 +0900680 val = (unsigned long)(win_data->dma_addr + size);
Inki Dae1c248b72011-10-04 19:19:01 +0900681 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
682
683 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
Inki Dae2c871122011-11-12 15:23:32 +0900684 (unsigned long)win_data->dma_addr, val, size);
Inki Dae19c8b832011-10-14 13:29:46 +0900685 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
686 win_data->ovl_width, win_data->ovl_height);
Inki Dae1c248b72011-10-04 19:19:01 +0900687
688 /* buffer size */
689 val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900690 VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
691 VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
692 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
Inki Dae1c248b72011-10-04 19:19:01 +0900693 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
694
695 /* OSD position */
696 val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900697 VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
698 VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
699 VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900700 writel(val, ctx->regs + VIDOSD_A(win));
701
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900702 last_x = win_data->offset_x + win_data->ovl_width;
703 if (last_x)
704 last_x--;
705 last_y = win_data->offset_y + win_data->ovl_height;
706 if (last_y)
707 last_y--;
708
Joonyoung Shimca555e52012-12-14 15:48:24 +0900709 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
710 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
711
Inki Dae1c248b72011-10-04 19:19:01 +0900712 writel(val, ctx->regs + VIDOSD_B(win));
713
Inki Dae19c8b832011-10-14 13:29:46 +0900714 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900715 win_data->offset_x, win_data->offset_y, last_x, last_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900716
717 /* hardware window 0 doesn't support alpha channel. */
718 if (win != 0) {
719 /* OSD alpha */
720 alpha = VIDISD14C_ALPHA1_R(0xf) |
721 VIDISD14C_ALPHA1_G(0xf) |
722 VIDISD14C_ALPHA1_B(0xf);
723
724 writel(alpha, ctx->regs + VIDOSD_C(win));
725 }
726
727 /* OSD size */
728 if (win != 3 && win != 4) {
729 u32 offset = VIDOSD_D(win);
730 if (win == 0)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -0500731 offset = VIDOSD_C(win);
Inki Dae19c8b832011-10-14 13:29:46 +0900732 val = win_data->ovl_width * win_data->ovl_height;
Inki Dae1c248b72011-10-04 19:19:01 +0900733 writel(val, ctx->regs + offset);
734
735 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
736 }
737
Sean Paulbb7704d2014-01-30 16:19:06 -0500738 fimd_win_set_pixfmt(ctx, win);
Inki Dae1c248b72011-10-04 19:19:01 +0900739
740 /* hardware window 0 doesn't support color key. */
741 if (win != 0)
Sean Paulbb7704d2014-01-30 16:19:06 -0500742 fimd_win_set_colkey(ctx, win);
Inki Dae1c248b72011-10-04 19:19:01 +0900743
Inki Daeec05da92011-12-06 11:06:54 +0900744 /* wincon */
745 val = readl(ctx->regs + WINCON(win));
746 val |= WINCONx_ENWIN;
747 writel(val, ctx->regs + WINCON(win));
748
Inki Dae1c248b72011-10-04 19:19:01 +0900749 /* Enable DMA channel and unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200750 fimd_shadow_protect_win(ctx, win, false);
751
752 if (ctx->driver_data->has_shadowcon) {
753 val = readl(ctx->regs + SHADOWCON);
754 val |= SHADOWCON_CHx_ENABLE(win);
755 writel(val, ctx->regs + SHADOWCON);
756 }
Inki Daeec05da92011-12-06 11:06:54 +0900757
758 win_data->enabled = true;
YoungJun Cho3854fab2014-07-17 18:01:21 +0900759
760 if (ctx->i80_if)
761 atomic_set(&ctx->win_updated, 1);
Inki Dae1c248b72011-10-04 19:19:01 +0900762}
763
Sean Paulbb7704d2014-01-30 16:19:06 -0500764static void fimd_win_disable(struct exynos_drm_manager *mgr, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900765{
Sean Paulbb7704d2014-01-30 16:19:06 -0500766 struct fimd_context *ctx = mgr->ctx;
Inki Daeec05da92011-12-06 11:06:54 +0900767 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900768 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900769 u32 val;
770
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900771 if (win == DEFAULT_ZPOS)
772 win = ctx->default_win;
773
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200774 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900775 return;
776
Inki Daeec05da92011-12-06 11:06:54 +0900777 win_data = &ctx->win_data[win];
778
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530779 if (ctx->suspended) {
780 /* do not resume this window*/
781 win_data->resume = false;
782 return;
783 }
784
Inki Dae1c248b72011-10-04 19:19:01 +0900785 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200786 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900787
788 /* wincon */
789 val = readl(ctx->regs + WINCON(win));
790 val &= ~WINCONx_ENWIN;
791 writel(val, ctx->regs + WINCON(win));
792
793 /* unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200794 if (ctx->driver_data->has_shadowcon) {
795 val = readl(ctx->regs + SHADOWCON);
796 val &= ~SHADOWCON_CHx_ENABLE(win);
797 writel(val, ctx->regs + SHADOWCON);
798 }
799
800 fimd_shadow_protect_win(ctx, win, false);
Inki Daeec05da92011-12-06 11:06:54 +0900801
802 win_data->enabled = false;
Inki Dae1c248b72011-10-04 19:19:01 +0900803}
804
Sean Paula43b9332014-01-30 16:19:26 -0500805static void fimd_window_suspend(struct exynos_drm_manager *mgr)
806{
807 struct fimd_context *ctx = mgr->ctx;
808 struct fimd_win_data *win_data;
809 int i;
810
811 for (i = 0; i < WINDOWS_NR; i++) {
812 win_data = &ctx->win_data[i];
813 win_data->resume = win_data->enabled;
814 if (win_data->enabled)
815 fimd_win_disable(mgr, i);
816 }
Sean Paula43b9332014-01-30 16:19:26 -0500817}
818
819static void fimd_window_resume(struct exynos_drm_manager *mgr)
820{
821 struct fimd_context *ctx = mgr->ctx;
822 struct fimd_win_data *win_data;
823 int i;
824
825 for (i = 0; i < WINDOWS_NR; i++) {
826 win_data = &ctx->win_data[i];
827 win_data->enabled = win_data->resume;
828 win_data->resume = false;
829 }
830}
831
832static void fimd_apply(struct exynos_drm_manager *mgr)
833{
834 struct fimd_context *ctx = mgr->ctx;
835 struct fimd_win_data *win_data;
836 int i;
837
838 for (i = 0; i < WINDOWS_NR; i++) {
839 win_data = &ctx->win_data[i];
840 if (win_data->enabled)
841 fimd_win_commit(mgr, i);
Andrzej Hajdad9b68d82014-06-09 16:10:59 +0200842 else
843 fimd_win_disable(mgr, i);
Sean Paula43b9332014-01-30 16:19:26 -0500844 }
845
846 fimd_commit(mgr);
847}
848
849static int fimd_poweron(struct exynos_drm_manager *mgr)
850{
851 struct fimd_context *ctx = mgr->ctx;
852 int ret;
853
854 if (!ctx->suspended)
855 return 0;
856
857 ctx->suspended = false;
858
Sean Paulaf65c802014-01-30 16:19:27 -0500859 pm_runtime_get_sync(ctx->dev);
860
Sean Paula43b9332014-01-30 16:19:26 -0500861 ret = clk_prepare_enable(ctx->bus_clk);
862 if (ret < 0) {
863 DRM_ERROR("Failed to prepare_enable the bus clk [%d]\n", ret);
864 goto bus_clk_err;
865 }
866
867 ret = clk_prepare_enable(ctx->lcd_clk);
868 if (ret < 0) {
869 DRM_ERROR("Failed to prepare_enable the lcd clk [%d]\n", ret);
870 goto lcd_clk_err;
871 }
872
873 /* if vblank was enabled status, enable it again. */
874 if (test_and_clear_bit(0, &ctx->irq_flags)) {
875 ret = fimd_enable_vblank(mgr);
876 if (ret) {
877 DRM_ERROR("Failed to re-enable vblank [%d]\n", ret);
878 goto enable_vblank_err;
879 }
880 }
881
882 fimd_window_resume(mgr);
883
884 fimd_apply(mgr);
885
886 return 0;
887
888enable_vblank_err:
889 clk_disable_unprepare(ctx->lcd_clk);
890lcd_clk_err:
891 clk_disable_unprepare(ctx->bus_clk);
892bus_clk_err:
893 ctx->suspended = true;
894 return ret;
895}
896
897static int fimd_poweroff(struct exynos_drm_manager *mgr)
898{
899 struct fimd_context *ctx = mgr->ctx;
900
901 if (ctx->suspended)
902 return 0;
903
904 /*
905 * We need to make sure that all windows are disabled before we
906 * suspend that connector. Otherwise we might try to scan from
907 * a destroyed buffer later.
908 */
909 fimd_window_suspend(mgr);
910
911 clk_disable_unprepare(ctx->lcd_clk);
912 clk_disable_unprepare(ctx->bus_clk);
913
Sean Paulaf65c802014-01-30 16:19:27 -0500914 pm_runtime_put_sync(ctx->dev);
915
Sean Paula43b9332014-01-30 16:19:26 -0500916 ctx->suspended = true;
917 return 0;
918}
919
Sean Paul080be03d2014-02-19 21:02:55 +0900920static void fimd_dpms(struct exynos_drm_manager *mgr, int mode)
921{
Sean Paulaf65c802014-01-30 16:19:27 -0500922 DRM_DEBUG_KMS("%s, %d\n", __FILE__, mode);
Sean Paul080be03d2014-02-19 21:02:55 +0900923
Sean Paul080be03d2014-02-19 21:02:55 +0900924 switch (mode) {
925 case DRM_MODE_DPMS_ON:
Sean Paulaf65c802014-01-30 16:19:27 -0500926 fimd_poweron(mgr);
Sean Paul080be03d2014-02-19 21:02:55 +0900927 break;
928 case DRM_MODE_DPMS_STANDBY:
929 case DRM_MODE_DPMS_SUSPEND:
930 case DRM_MODE_DPMS_OFF:
Sean Paulaf65c802014-01-30 16:19:27 -0500931 fimd_poweroff(mgr);
Sean Paul080be03d2014-02-19 21:02:55 +0900932 break;
933 default:
934 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
935 break;
936 }
Sean Paul080be03d2014-02-19 21:02:55 +0900937}
938
YoungJun Cho3854fab2014-07-17 18:01:21 +0900939static void fimd_trigger(struct device *dev)
940{
941 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
942 struct fimd_context *ctx = mgr->ctx;
943 struct fimd_driver_data *driver_data = ctx->driver_data;
944 void *timing_base = ctx->regs + driver_data->timing_base;
945 u32 reg;
946
947 atomic_set(&ctx->triggering, 1);
948
949 reg = readl(ctx->regs + VIDINTCON0);
950 reg |= (VIDINTCON0_INT_ENABLE | VIDINTCON0_INT_I80IFDONE |
951 VIDINTCON0_INT_SYSMAINCON);
952 writel(reg, ctx->regs + VIDINTCON0);
953
954 reg = readl(timing_base + TRIGCON);
955 reg |= (TRGMODE_I80_RGB_ENABLE_I80 | SWTRGCMD_I80_RGB_ENABLE);
956 writel(reg, timing_base + TRIGCON);
957}
958
959static void fimd_te_handler(struct exynos_drm_manager *mgr)
960{
961 struct fimd_context *ctx = mgr->ctx;
962
963 /* Checks the crtc is detached already from encoder */
964 if (ctx->pipe < 0 || !ctx->drm_dev)
965 return;
966
967 /*
968 * Skips to trigger if in triggering state, because multiple triggering
969 * requests can cause panel reset.
970 */
971 if (atomic_read(&ctx->triggering))
972 return;
973
974 /*
975 * If there is a page flip request, triggers and handles the page flip
976 * event so that current fb can be updated into panel GRAM.
977 */
978 if (atomic_add_unless(&ctx->win_updated, -1, 0))
979 fimd_trigger(ctx->dev);
980
981 /* Wakes up vsync event queue */
982 if (atomic_read(&ctx->wait_vsync_event)) {
983 atomic_set(&ctx->wait_vsync_event, 0);
984 wake_up(&ctx->wait_vsync_queue);
YoungJun Cho3854fab2014-07-17 18:01:21 +0900985 }
YoungJun Chob301ae22014-10-01 15:19:10 +0900986
987 if (!atomic_read(&ctx->triggering))
988 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
YoungJun Cho3854fab2014-07-17 18:01:21 +0900989}
990
Sean Paul1c6244c2014-01-30 16:19:02 -0500991static struct exynos_drm_manager_ops fimd_manager_ops = {
992 .dpms = fimd_dpms,
Sean Paula968e722014-01-30 16:19:20 -0500993 .mode_fixup = fimd_mode_fixup,
994 .mode_set = fimd_mode_set,
Sean Paul1c6244c2014-01-30 16:19:02 -0500995 .commit = fimd_commit,
996 .enable_vblank = fimd_enable_vblank,
997 .disable_vblank = fimd_disable_vblank,
998 .wait_for_vblank = fimd_wait_for_vblank,
999 .win_mode_set = fimd_win_mode_set,
1000 .win_commit = fimd_win_commit,
1001 .win_disable = fimd_win_disable,
YoungJun Cho3854fab2014-07-17 18:01:21 +09001002 .te_handler = fimd_te_handler,
Inki Dae1c248b72011-10-04 19:19:01 +09001003};
1004
Joonyoung Shim677e84c2012-04-05 20:49:27 +09001005static struct exynos_drm_manager fimd_manager = {
Sean Paul080be03d2014-02-19 21:02:55 +09001006 .type = EXYNOS_DISPLAY_TYPE_LCD,
1007 .ops = &fimd_manager_ops,
Joonyoung Shim677e84c2012-04-05 20:49:27 +09001008};
1009
Inki Dae1c248b72011-10-04 19:19:01 +09001010static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
1011{
1012 struct fimd_context *ctx = (struct fimd_context *)dev_id;
YoungJun Cho3854fab2014-07-17 18:01:21 +09001013 u32 val, clear_bit;
Inki Dae1c248b72011-10-04 19:19:01 +09001014
1015 val = readl(ctx->regs + VIDINTCON1);
1016
YoungJun Cho3854fab2014-07-17 18:01:21 +09001017 clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
1018 if (val & clear_bit)
1019 writel(clear_bit, ctx->regs + VIDINTCON1);
Inki Dae1c248b72011-10-04 19:19:01 +09001020
Inki Daeec05da92011-12-06 11:06:54 +09001021 /* check the crtc is detached already from encoder */
Sean Paul080be03d2014-02-19 21:02:55 +09001022 if (ctx->pipe < 0 || !ctx->drm_dev)
Inki Daeec05da92011-12-06 11:06:54 +09001023 goto out;
Inki Dae483b88f2011-11-11 21:28:00 +09001024
YoungJun Cho3854fab2014-07-17 18:01:21 +09001025 if (ctx->i80_if) {
1026 /* unset I80 frame done interrupt */
1027 val = readl(ctx->regs + VIDINTCON0);
1028 val &= ~(VIDINTCON0_INT_I80IFDONE | VIDINTCON0_INT_SYSMAINCON);
1029 writel(val, ctx->regs + VIDINTCON0);
Inki Dae1c248b72011-10-04 19:19:01 +09001030
YoungJun Cho3854fab2014-07-17 18:01:21 +09001031 /* exit triggering mode */
1032 atomic_set(&ctx->triggering, 0);
1033
1034 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
1035 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
1036 } else {
1037 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
1038 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
1039
1040 /* set wait vsync event to zero and wake up queue. */
1041 if (atomic_read(&ctx->wait_vsync_event)) {
1042 atomic_set(&ctx->wait_vsync_event, 0);
1043 wake_up(&ctx->wait_vsync_queue);
1044 }
Prathyush K01ce1132012-12-06 20:16:04 +05301045 }
YoungJun Cho3854fab2014-07-17 18:01:21 +09001046
Inki Daeec05da92011-12-06 11:06:54 +09001047out:
Inki Dae1c248b72011-10-04 19:19:01 +09001048 return IRQ_HANDLED;
1049}
1050
Inki Daef37cd5e2014-05-09 14:25:20 +09001051static int fimd_bind(struct device *dev, struct device *master, void *data)
Andrzej Hajda562ad9f2013-08-21 16:22:03 +02001052{
Andrzej Hajda000cc922014-04-03 16:26:00 +02001053 struct fimd_context *ctx = fimd_manager.ctx;
Inki Daef37cd5e2014-05-09 14:25:20 +09001054 struct drm_device *drm_dev = data;
Andrzej Hajda000cc922014-04-03 16:26:00 +02001055
1056 fimd_mgr_initialize(&fimd_manager, drm_dev);
1057 exynos_drm_crtc_create(&fimd_manager);
1058 if (ctx->display)
1059 exynos_drm_create_enc_conn(drm_dev, ctx->display);
1060
Andrzej Hajda000cc922014-04-03 16:26:00 +02001061 return 0;
1062
1063}
1064
1065static void fimd_unbind(struct device *dev, struct device *master,
1066 void *data)
1067{
1068 struct exynos_drm_manager *mgr = dev_get_drvdata(dev);
1069 struct fimd_context *ctx = fimd_manager.ctx;
Andrzej Hajda000cc922014-04-03 16:26:00 +02001070
1071 fimd_dpms(mgr, DRM_MODE_DPMS_OFF);
1072
1073 if (ctx->display)
1074 exynos_dpi_remove(dev);
1075
1076 fimd_mgr_remove(mgr);
Andrzej Hajda000cc922014-04-03 16:26:00 +02001077}
1078
1079static const struct component_ops fimd_component_ops = {
1080 .bind = fimd_bind,
1081 .unbind = fimd_unbind,
1082};
1083
1084static int fimd_probe(struct platform_device *pdev)
1085{
1086 struct device *dev = &pdev->dev;
1087 struct fimd_context *ctx;
YoungJun Cho3854fab2014-07-17 18:01:21 +09001088 struct device_node *i80_if_timings;
Andrzej Hajda000cc922014-04-03 16:26:00 +02001089 struct resource *res;
Andrzej Hajda562ad9f2013-08-21 16:22:03 +02001090 int ret = -EINVAL;
Inki Dae1c248b72011-10-04 19:19:01 +09001091
Inki Daedf5225b2014-05-29 18:28:02 +09001092 ret = exynos_drm_component_add(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC,
1093 fimd_manager.type);
1094 if (ret)
1095 return ret;
1096
1097 if (!dev->of_node) {
1098 ret = -ENODEV;
1099 goto err_del_component;
1100 }
Sachin Kamat2d3f1732013-08-28 10:47:58 +05301101
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001102 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Inki Daedf5225b2014-05-29 18:28:02 +09001103 if (!ctx) {
1104 ret = -ENOMEM;
1105 goto err_del_component;
1106 }
Inki Dae1c248b72011-10-04 19:19:01 +09001107
Sean Paulbb7704d2014-01-30 16:19:06 -05001108 ctx->dev = dev;
Sean Paula43b9332014-01-30 16:19:26 -05001109 ctx->suspended = true;
YoungJun Cho3854fab2014-07-17 18:01:21 +09001110 ctx->driver_data = drm_fimd_get_driver_data(pdev);
Sean Paulbb7704d2014-01-30 16:19:06 -05001111
Sean Paul1417f102014-01-30 16:19:23 -05001112 if (of_property_read_bool(dev->of_node, "samsung,invert-vden"))
1113 ctx->vidcon1 |= VIDCON1_INV_VDEN;
1114 if (of_property_read_bool(dev->of_node, "samsung,invert-vclk"))
1115 ctx->vidcon1 |= VIDCON1_INV_VCLK;
Andrzej Hajda562ad9f2013-08-21 16:22:03 +02001116
YoungJun Cho3854fab2014-07-17 18:01:21 +09001117 i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
1118 if (i80_if_timings) {
1119 u32 val;
1120
1121 ctx->i80_if = true;
1122
1123 if (ctx->driver_data->has_vidoutcon)
1124 ctx->vidout_con |= VIDOUT_CON_F_I80_LDI0;
1125 else
1126 ctx->vidcon0 |= VIDCON0_VIDOUT_I80_LDI0;
1127 /*
1128 * The user manual describes that this "DSI_EN" bit is required
1129 * to enable I80 24-bit data interface.
1130 */
1131 ctx->vidcon0 |= VIDCON0_DSI_EN;
1132
1133 if (of_property_read_u32(i80_if_timings, "cs-setup", &val))
1134 val = 0;
1135 ctx->i80ifcon = LCD_CS_SETUP(val);
1136 if (of_property_read_u32(i80_if_timings, "wr-setup", &val))
1137 val = 0;
1138 ctx->i80ifcon |= LCD_WR_SETUP(val);
1139 if (of_property_read_u32(i80_if_timings, "wr-active", &val))
1140 val = 1;
1141 ctx->i80ifcon |= LCD_WR_ACTIVE(val);
1142 if (of_property_read_u32(i80_if_timings, "wr-hold", &val))
1143 val = 0;
1144 ctx->i80ifcon |= LCD_WR_HOLD(val);
1145 }
1146 of_node_put(i80_if_timings);
1147
1148 ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
1149 "samsung,sysreg");
1150 if (IS_ERR(ctx->sysreg)) {
1151 dev_warn(dev, "failed to get system register.\n");
1152 ctx->sysreg = NULL;
1153 }
1154
Sean Paula968e722014-01-30 16:19:20 -05001155 ctx->bus_clk = devm_clk_get(dev, "fimd");
1156 if (IS_ERR(ctx->bus_clk)) {
1157 dev_err(dev, "failed to get bus clock\n");
Inki Daedf5225b2014-05-29 18:28:02 +09001158 ret = PTR_ERR(ctx->bus_clk);
1159 goto err_del_component;
Sean Paula968e722014-01-30 16:19:20 -05001160 }
1161
1162 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
1163 if (IS_ERR(ctx->lcd_clk)) {
1164 dev_err(dev, "failed to get lcd clock\n");
Inki Daedf5225b2014-05-29 18:28:02 +09001165 ret = PTR_ERR(ctx->lcd_clk);
1166 goto err_del_component;
Sean Paula968e722014-01-30 16:19:20 -05001167 }
Inki Dae1c248b72011-10-04 19:19:01 +09001168
Inki Dae1c248b72011-10-04 19:19:01 +09001169 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Inki Dae1c248b72011-10-04 19:19:01 +09001170
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001171 ctx->regs = devm_ioremap_resource(dev, res);
Inki Daedf5225b2014-05-29 18:28:02 +09001172 if (IS_ERR(ctx->regs)) {
1173 ret = PTR_ERR(ctx->regs);
1174 goto err_del_component;
1175 }
Inki Dae1c248b72011-10-04 19:19:01 +09001176
YoungJun Cho3854fab2014-07-17 18:01:21 +09001177 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1178 ctx->i80_if ? "lcd_sys" : "vsync");
Inki Dae1c248b72011-10-04 19:19:01 +09001179 if (!res) {
1180 dev_err(dev, "irq request failed.\n");
Inki Daedf5225b2014-05-29 18:28:02 +09001181 ret = -ENXIO;
1182 goto err_del_component;
Inki Dae1c248b72011-10-04 19:19:01 +09001183 }
1184
Sean Paul055e0c02014-01-30 16:19:21 -05001185 ret = devm_request_irq(dev, res->start, fimd_irq_handler,
Sachin Kamatedc57262012-06-19 11:47:39 +05301186 0, "drm_fimd", ctx);
1187 if (ret) {
Inki Dae1c248b72011-10-04 19:19:01 +09001188 dev_err(dev, "irq request failed.\n");
Inki Daedf5225b2014-05-29 18:28:02 +09001189 goto err_del_component;
Inki Dae1c248b72011-10-04 19:19:01 +09001190 }
1191
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001192 init_waitqueue_head(&ctx->wait_vsync_queue);
Prathyush K01ce1132012-12-06 20:16:04 +05301193 atomic_set(&ctx->wait_vsync_event, 0);
Inki Dae1c248b72011-10-04 19:19:01 +09001194
Sean Paulbb7704d2014-01-30 16:19:06 -05001195 platform_set_drvdata(pdev, &fimd_manager);
Inki Daec32b06e2011-12-16 21:49:03 +09001196
Sean Paul080be03d2014-02-19 21:02:55 +09001197 fimd_manager.ctx = ctx;
Sean Paul080be03d2014-02-19 21:02:55 +09001198
Andrzej Hajda000cc922014-04-03 16:26:00 +02001199 ctx->display = exynos_dpi_probe(dev);
1200 if (IS_ERR(ctx->display))
1201 return PTR_ERR(ctx->display);
Inki Daef37cd5e2014-05-09 14:25:20 +09001202
1203 pm_runtime_enable(&pdev->dev);
1204
Inki Daedf5225b2014-05-29 18:28:02 +09001205 ret = component_add(&pdev->dev, &fimd_component_ops);
1206 if (ret)
1207 goto err_disable_pm_runtime;
1208
1209 return ret;
1210
1211err_disable_pm_runtime:
1212 pm_runtime_disable(&pdev->dev);
1213
1214err_del_component:
1215 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
1216 return ret;
Inki Daef37cd5e2014-05-09 14:25:20 +09001217}
1218
1219static int fimd_remove(struct platform_device *pdev)
1220{
Sean Paulaf65c802014-01-30 16:19:27 -05001221 pm_runtime_disable(&pdev->dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001222
Inki Daedf5225b2014-05-29 18:28:02 +09001223 component_del(&pdev->dev, &fimd_component_ops);
1224 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
1225
Inki Dae1c248b72011-10-04 19:19:01 +09001226 return 0;
1227}
1228
Joonyoung Shim132a5b92012-03-16 18:47:08 +09001229struct platform_driver fimd_driver = {
Inki Dae1c248b72011-10-04 19:19:01 +09001230 .probe = fimd_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001231 .remove = fimd_remove,
Inki Dae1c248b72011-10-04 19:19:01 +09001232 .driver = {
1233 .name = "exynos4-fb",
1234 .owner = THIS_MODULE,
Sachin Kamat2d3f1732013-08-28 10:47:58 +05301235 .of_match_table = fimd_driver_dt_match,
Inki Dae1c248b72011-10-04 19:19:01 +09001236 },
1237};