blob: dc8c5e4aa23500554abf0cd5f21aa827f970b2b6 [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 Dae1c248b72011-10-04 19:19:01 +090022
Vikas Sajjan7f4596f2013-03-07 12:15:21 +053023#include <video/of_display_timing.h>
Andrzej Hajda111e6052013-08-21 16:22:01 +020024#include <video/of_videomode.h>
Leela Krishna Amudala5a213a52012-08-08 09:44:49 +090025#include <video/samsung_fimd.h>
Inki Dae1c248b72011-10-04 19:19:01 +090026#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090027
28#include "exynos_drm_drv.h"
29#include "exynos_drm_fbdev.h"
30#include "exynos_drm_crtc.h"
Inki Daebcc5cd12012-10-19 17:16:36 +090031#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090032
33/*
Sachin Kamatb8654b32013-09-19 10:39:44 +053034 * FIMD stands for Fully Interactive Mobile Display and
Inki Dae1c248b72011-10-04 19:19:01 +090035 * as a display controller, it transfers contents drawn on memory
36 * to a LCD Panel through Display Interfaces such as RGB or
37 * CPU Interface.
38 */
39
Andrzej Hajda111e6052013-08-21 16:22:01 +020040#define FIMD_DEFAULT_FRAMERATE 60
41
Inki Dae1c248b72011-10-04 19:19:01 +090042/* position control register for hardware window 0, 2 ~ 4.*/
43#define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
44#define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050045/*
46 * size control register for hardware windows 0 and alpha control register
47 * for hardware windows 1 ~ 4
48 */
49#define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
50/* size control register for hardware windows 1 ~ 2. */
Inki Dae1c248b72011-10-04 19:19:01 +090051#define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
52
53#define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
54#define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
55#define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
56
57/* color key control register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050058#define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090059/* color key value register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050060#define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090061
62/* FIMD has totally five hardware windows. */
63#define WINDOWS_NR 5
64
Sean Paulbb7704d2014-01-30 16:19:06 -050065#define get_fimd_manager(mgr) platform_get_drvdata(to_platform_device(dev))
Inki Dae1c248b72011-10-04 19:19:01 +090066
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053067struct fimd_driver_data {
68 unsigned int timing_base;
Tomasz Figade7af102013-05-01 21:02:27 +020069
70 unsigned int has_shadowcon:1;
Tomasz Figa411d9ed2013-05-01 21:02:28 +020071 unsigned int has_clksel:1;
Inki Dae5cc46212013-08-20 14:28:56 +090072 unsigned int has_limited_fmt:1;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053073};
74
Tomasz Figa725ddea2013-05-01 21:02:29 +020075static struct fimd_driver_data s3c64xx_fimd_driver_data = {
76 .timing_base = 0x0,
77 .has_clksel = 1,
Inki Dae5cc46212013-08-20 14:28:56 +090078 .has_limited_fmt = 1,
Tomasz Figa725ddea2013-05-01 21:02:29 +020079};
80
Sachin Kamat6ecf18f2012-11-19 15:22:54 +053081static struct fimd_driver_data exynos4_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053082 .timing_base = 0x0,
Tomasz Figade7af102013-05-01 21:02:27 +020083 .has_shadowcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053084};
85
Sachin Kamat6ecf18f2012-11-19 15:22:54 +053086static struct fimd_driver_data exynos5_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053087 .timing_base = 0x20000,
Tomasz Figade7af102013-05-01 21:02:27 +020088 .has_shadowcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053089};
90
Inki Dae1c248b72011-10-04 19:19:01 +090091struct fimd_win_data {
92 unsigned int offset_x;
93 unsigned int offset_y;
Inki Dae19c8b832011-10-14 13:29:46 +090094 unsigned int ovl_width;
95 unsigned int ovl_height;
96 unsigned int fb_width;
97 unsigned int fb_height;
Inki Dae1c248b72011-10-04 19:19:01 +090098 unsigned int bpp;
Inki Daea4f38a82013-08-20 13:51:02 +090099 unsigned int pixel_format;
Inki Dae2c871122011-11-12 15:23:32 +0900100 dma_addr_t dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900101 unsigned int buf_offsize;
102 unsigned int line_size; /* bytes */
Inki Daeec05da92011-12-06 11:06:54 +0900103 bool enabled;
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530104 bool resume;
Inki Dae1c248b72011-10-04 19:19:01 +0900105};
106
107struct fimd_context {
Sean Paulbb7704d2014-01-30 16:19:06 -0500108 struct device *dev;
Sean Paul40c8ab42014-01-30 16:19:04 -0500109 struct drm_device *drm_dev;
Inki Dae1c248b72011-10-04 19:19:01 +0900110 int irq;
111 struct drm_crtc *crtc;
112 struct clk *bus_clk;
113 struct clk *lcd_clk;
Inki Dae1c248b72011-10-04 19:19:01 +0900114 void __iomem *regs;
115 struct fimd_win_data win_data[WINDOWS_NR];
116 unsigned int clkdiv;
117 unsigned int default_win;
118 unsigned long irq_flags;
119 u32 vidcon0;
120 u32 vidcon1;
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900121 bool suspended;
Sean Paul080be03d2014-02-19 21:02:55 +0900122 int pipe;
Inki Daec32b06e2011-12-16 21:49:03 +0900123 struct mutex lock;
Prathyush K01ce1132012-12-06 20:16:04 +0530124 wait_queue_head_t wait_vsync_queue;
125 atomic_t wait_vsync_event;
Inki Dae1c248b72011-10-04 19:19:01 +0900126
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200127 struct exynos_drm_panel_info panel;
Tomasz Figa18873462013-05-01 21:02:26 +0200128 struct fimd_driver_data *driver_data;
Inki Dae1c248b72011-10-04 19:19:01 +0900129};
130
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900131static const struct of_device_id fimd_driver_dt_match[] = {
Tomasz Figa725ddea2013-05-01 21:02:29 +0200132 { .compatible = "samsung,s3c6400-fimd",
133 .data = &s3c64xx_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530134 { .compatible = "samsung,exynos4210-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900135 .data = &exynos4_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530136 { .compatible = "samsung,exynos5250-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900137 .data = &exynos5_fimd_driver_data },
138 {},
139};
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900140
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530141static inline struct fimd_driver_data *drm_fimd_get_driver_data(
142 struct platform_device *pdev)
143{
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900144 const struct of_device_id *of_id =
145 of_match_device(fimd_driver_dt_match, &pdev->dev);
146
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530147 return (struct fimd_driver_data *)of_id->data;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530148}
149
Sean Paul080be03d2014-02-19 21:02:55 +0900150static bool fimd_display_is_connected(struct exynos_drm_display *display)
Inki Dae1c248b72011-10-04 19:19:01 +0900151{
Inki Dae1c248b72011-10-04 19:19:01 +0900152 /* TODO. */
153
154 return true;
155}
156
Sean Paul080be03d2014-02-19 21:02:55 +0900157static void *fimd_get_panel(struct exynos_drm_display *display)
Inki Dae1c248b72011-10-04 19:19:01 +0900158{
Sean Paul080be03d2014-02-19 21:02:55 +0900159 struct fimd_context *ctx = display->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900160
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200161 return &ctx->panel;
Inki Dae1c248b72011-10-04 19:19:01 +0900162}
163
Sean Paul080be03d2014-02-19 21:02:55 +0900164static int fimd_check_mode(struct exynos_drm_display *display,
165 struct drm_display_mode *mode)
Inki Dae1c248b72011-10-04 19:19:01 +0900166{
Inki Dae1c248b72011-10-04 19:19:01 +0900167 /* TODO. */
168
169 return 0;
170}
171
Inki Dae74ccc532011-10-19 17:23:07 +0900172static struct exynos_drm_display_ops fimd_display_ops = {
Inki Dae1c248b72011-10-04 19:19:01 +0900173 .is_connected = fimd_display_is_connected,
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900174 .get_panel = fimd_get_panel,
Rahul Sharma16844fb2013-06-10 14:50:00 +0530175 .check_mode = fimd_check_mode,
Inki Dae1c248b72011-10-04 19:19:01 +0900176};
177
Sean Paul080be03d2014-02-19 21:02:55 +0900178static struct exynos_drm_display fimd_display = {
179 .type = EXYNOS_DISPLAY_TYPE_LCD,
180 .ops = &fimd_display_ops,
181};
182
Sean Paulbb7704d2014-01-30 16:19:06 -0500183static int fimd_mgr_initialize(struct exynos_drm_manager *mgr,
Sean Paul080be03d2014-02-19 21:02:55 +0900184 struct drm_device *drm_dev, int pipe)
Sean Paul40c8ab42014-01-30 16:19:04 -0500185{
Sean Paulbb7704d2014-01-30 16:19:06 -0500186 struct fimd_context *ctx = mgr->ctx;
Sean Paul40c8ab42014-01-30 16:19:04 -0500187
188 ctx->drm_dev = drm_dev;
Sean Paul080be03d2014-02-19 21:02:55 +0900189 ctx->pipe = pipe;
190
191 /*
192 * enable drm irq mode.
193 * - with irq_enabled = true, we can use the vblank feature.
194 *
195 * P.S. note that we wouldn't use drm irq handler but
196 * just specific driver own one instead because
197 * drm framework supports only one irq handler.
198 */
199 drm_dev->irq_enabled = true;
200
201 /*
202 * with vblank_disable_allowed = true, vblank interrupt will be disabled
203 * by drm timer once a current process gives up ownership of
204 * vblank event.(after drm_vblank_put function is called)
205 */
206 drm_dev->vblank_disable_allowed = true;
207
208 /* attach this sub driver to iommu mapping if supported. */
209 if (is_drm_iommu_supported(ctx->drm_dev))
210 drm_iommu_attach_device(ctx->drm_dev, ctx->dev);
Sean Paul40c8ab42014-01-30 16:19:04 -0500211
212 return 0;
213}
214
Sean Paul080be03d2014-02-19 21:02:55 +0900215static void fimd_mgr_remove(struct exynos_drm_manager *mgr)
Inki Daeec05da92011-12-06 11:06:54 +0900216{
Sean Paulbb7704d2014-01-30 16:19:06 -0500217 struct fimd_context *ctx = mgr->ctx;
Inki Daec32b06e2011-12-16 21:49:03 +0900218
Sean Paul080be03d2014-02-19 21:02:55 +0900219 /* detach this sub driver from iommu mapping if supported. */
220 if (is_drm_iommu_supported(ctx->drm_dev))
221 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
Inki Daeec05da92011-12-06 11:06:54 +0900222}
223
Sean Paulbb7704d2014-01-30 16:19:06 -0500224static void fimd_commit(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900225{
Sean Paulbb7704d2014-01-30 16:19:06 -0500226 struct fimd_context *ctx = mgr->ctx;
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200227 struct exynos_drm_panel_info *panel = &ctx->panel;
Andrzej Hajda111e6052013-08-21 16:22:01 +0200228 struct videomode *vm = &panel->vm;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530229 struct fimd_driver_data *driver_data;
Inki Dae1c248b72011-10-04 19:19:01 +0900230 u32 val;
231
Tomasz Figa18873462013-05-01 21:02:26 +0200232 driver_data = ctx->driver_data;
Inki Daee30d4bc2011-12-12 16:35:20 +0900233 if (ctx->suspended)
234 return;
235
Inki Dae1c248b72011-10-04 19:19:01 +0900236 /* setup polarity values from machine code. */
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530237 writel(ctx->vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
Inki Dae1c248b72011-10-04 19:19:01 +0900238
239 /* setup vertical timing values. */
Andrzej Hajda111e6052013-08-21 16:22:01 +0200240 val = VIDTCON0_VBPD(vm->vback_porch - 1) |
241 VIDTCON0_VFPD(vm->vfront_porch - 1) |
242 VIDTCON0_VSPW(vm->vsync_len - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530243 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
Inki Dae1c248b72011-10-04 19:19:01 +0900244
245 /* setup horizontal timing values. */
Andrzej Hajda111e6052013-08-21 16:22:01 +0200246 val = VIDTCON1_HBPD(vm->hback_porch - 1) |
247 VIDTCON1_HFPD(vm->hfront_porch - 1) |
248 VIDTCON1_HSPW(vm->hsync_len - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530249 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
Inki Dae1c248b72011-10-04 19:19:01 +0900250
251 /* setup horizontal and vertical display size. */
Andrzej Hajda111e6052013-08-21 16:22:01 +0200252 val = VIDTCON2_LINEVAL(vm->vactive - 1) |
253 VIDTCON2_HOZVAL(vm->hactive - 1) |
254 VIDTCON2_LINEVAL_E(vm->vactive - 1) |
255 VIDTCON2_HOZVAL_E(vm->hactive - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530256 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
Inki Dae1c248b72011-10-04 19:19:01 +0900257
258 /* setup clock source, clock divider, enable dma. */
259 val = ctx->vidcon0;
260 val &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
261
Tomasz Figa411d9ed2013-05-01 21:02:28 +0200262 if (ctx->driver_data->has_clksel) {
263 val &= ~VIDCON0_CLKSEL_MASK;
264 val |= VIDCON0_CLKSEL_LCD;
265 }
266
Inki Dae1c248b72011-10-04 19:19:01 +0900267 if (ctx->clkdiv > 1)
268 val |= VIDCON0_CLKVAL_F(ctx->clkdiv - 1) | VIDCON0_CLKDIR;
269 else
270 val &= ~VIDCON0_CLKDIR; /* 1:1 clock */
271
272 /*
273 * fields of register with prefix '_F' would be updated
274 * at vsync(same as dma start)
275 */
276 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
277 writel(val, ctx->regs + VIDCON0);
278}
279
Sean Paulbb7704d2014-01-30 16:19:06 -0500280static int fimd_enable_vblank(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900281{
Sean Paulbb7704d2014-01-30 16:19:06 -0500282 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900283 u32 val;
284
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900285 if (ctx->suspended)
286 return -EPERM;
287
Inki Dae1c248b72011-10-04 19:19:01 +0900288 if (!test_and_set_bit(0, &ctx->irq_flags)) {
289 val = readl(ctx->regs + VIDINTCON0);
290
291 val |= VIDINTCON0_INT_ENABLE;
292 val |= VIDINTCON0_INT_FRAME;
293
294 val &= ~VIDINTCON0_FRAMESEL0_MASK;
295 val |= VIDINTCON0_FRAMESEL0_VSYNC;
296 val &= ~VIDINTCON0_FRAMESEL1_MASK;
297 val |= VIDINTCON0_FRAMESEL1_NONE;
298
299 writel(val, ctx->regs + VIDINTCON0);
300 }
301
302 return 0;
303}
304
Sean Paulbb7704d2014-01-30 16:19:06 -0500305static void fimd_disable_vblank(struct exynos_drm_manager *mgr)
Inki Dae1c248b72011-10-04 19:19:01 +0900306{
Sean Paulbb7704d2014-01-30 16:19:06 -0500307 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900308 u32 val;
309
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900310 if (ctx->suspended)
311 return;
312
Inki Dae1c248b72011-10-04 19:19:01 +0900313 if (test_and_clear_bit(0, &ctx->irq_flags)) {
314 val = readl(ctx->regs + VIDINTCON0);
315
316 val &= ~VIDINTCON0_INT_FRAME;
317 val &= ~VIDINTCON0_INT_ENABLE;
318
319 writel(val, ctx->regs + VIDINTCON0);
320 }
321}
322
Sean Paulbb7704d2014-01-30 16:19:06 -0500323static void fimd_wait_for_vblank(struct exynos_drm_manager *mgr)
Prathyush K07033972012-12-06 20:16:02 +0530324{
Sean Paulbb7704d2014-01-30 16:19:06 -0500325 struct fimd_context *ctx = mgr->ctx;
Prathyush K07033972012-12-06 20:16:02 +0530326
Prathyush K01ce1132012-12-06 20:16:04 +0530327 if (ctx->suspended)
328 return;
329
330 atomic_set(&ctx->wait_vsync_event, 1);
331
332 /*
333 * wait for FIMD to signal VSYNC interrupt or return after
334 * timeout which is set to 50ms (refresh rate of 20).
335 */
336 if (!wait_event_timeout(ctx->wait_vsync_queue,
337 !atomic_read(&ctx->wait_vsync_event),
Seung-Woo Kim8dd9ad52013-12-30 16:01:29 +0900338 HZ/20))
Prathyush K07033972012-12-06 20:16:02 +0530339 DRM_DEBUG_KMS("vblank wait timed out.\n");
340}
341
Sean Paulbb7704d2014-01-30 16:19:06 -0500342static void fimd_win_mode_set(struct exynos_drm_manager *mgr,
343 struct exynos_drm_overlay *overlay)
Inki Dae1c248b72011-10-04 19:19:01 +0900344{
Sean Paulbb7704d2014-01-30 16:19:06 -0500345 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900346 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900347 int win;
Inki Dae19c8b832011-10-14 13:29:46 +0900348 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900349
Inki Dae1c248b72011-10-04 19:19:01 +0900350 if (!overlay) {
Sean Paulbb7704d2014-01-30 16:19:06 -0500351 DRM_ERROR("overlay is NULL\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900352 return;
353 }
354
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900355 win = overlay->zpos;
356 if (win == DEFAULT_ZPOS)
357 win = ctx->default_win;
358
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200359 if (win < 0 || win >= WINDOWS_NR)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900360 return;
361
Inki Dae19c8b832011-10-14 13:29:46 +0900362 offset = overlay->fb_x * (overlay->bpp >> 3);
363 offset += overlay->fb_y * overlay->pitch;
364
365 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
366
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900367 win_data = &ctx->win_data[win];
Inki Dae1c248b72011-10-04 19:19:01 +0900368
Inki Dae19c8b832011-10-14 13:29:46 +0900369 win_data->offset_x = overlay->crtc_x;
370 win_data->offset_y = overlay->crtc_y;
371 win_data->ovl_width = overlay->crtc_width;
372 win_data->ovl_height = overlay->crtc_height;
373 win_data->fb_width = overlay->fb_width;
374 win_data->fb_height = overlay->fb_height;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900375 win_data->dma_addr = overlay->dma_addr[0] + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900376 win_data->bpp = overlay->bpp;
Inki Daea4f38a82013-08-20 13:51:02 +0900377 win_data->pixel_format = overlay->pixel_format;
Inki Dae19c8b832011-10-14 13:29:46 +0900378 win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
379 (overlay->bpp >> 3);
380 win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
381
382 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
383 win_data->offset_x, win_data->offset_y);
384 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
385 win_data->ovl_width, win_data->ovl_height);
YoungJun Choddd8e952012-12-10 15:44:58 +0900386 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
Inki Dae19c8b832011-10-14 13:29:46 +0900387 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
388 overlay->fb_width, overlay->crtc_width);
Inki Dae1c248b72011-10-04 19:19:01 +0900389}
390
Sean Paulbb7704d2014-01-30 16:19:06 -0500391static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win)
Inki Dae1c248b72011-10-04 19:19:01 +0900392{
Inki Dae1c248b72011-10-04 19:19:01 +0900393 struct fimd_win_data *win_data = &ctx->win_data[win];
394 unsigned long val;
395
Inki Dae1c248b72011-10-04 19:19:01 +0900396 val = WINCONx_ENWIN;
397
Inki Dae5cc46212013-08-20 14:28:56 +0900398 /*
399 * In case of s3c64xx, window 0 doesn't support alpha channel.
400 * So the request format is ARGB8888 then change it to XRGB8888.
401 */
402 if (ctx->driver_data->has_limited_fmt && !win) {
403 if (win_data->pixel_format == DRM_FORMAT_ARGB8888)
404 win_data->pixel_format = DRM_FORMAT_XRGB8888;
405 }
406
Inki Daea4f38a82013-08-20 13:51:02 +0900407 switch (win_data->pixel_format) {
408 case DRM_FORMAT_C8:
Inki Dae1c248b72011-10-04 19:19:01 +0900409 val |= WINCON0_BPPMODE_8BPP_PALETTE;
410 val |= WINCONx_BURSTLEN_8WORD;
411 val |= WINCONx_BYTSWP;
412 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900413 case DRM_FORMAT_XRGB1555:
414 val |= WINCON0_BPPMODE_16BPP_1555;
415 val |= WINCONx_HAWSWP;
416 val |= WINCONx_BURSTLEN_16WORD;
417 break;
418 case DRM_FORMAT_RGB565:
Inki Dae1c248b72011-10-04 19:19:01 +0900419 val |= WINCON0_BPPMODE_16BPP_565;
420 val |= WINCONx_HAWSWP;
421 val |= WINCONx_BURSTLEN_16WORD;
422 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900423 case DRM_FORMAT_XRGB8888:
Inki Dae1c248b72011-10-04 19:19:01 +0900424 val |= WINCON0_BPPMODE_24BPP_888;
425 val |= WINCONx_WSWP;
426 val |= WINCONx_BURSTLEN_16WORD;
427 break;
Inki Daea4f38a82013-08-20 13:51:02 +0900428 case DRM_FORMAT_ARGB8888:
429 val |= WINCON1_BPPMODE_25BPP_A1888
Inki Dae1c248b72011-10-04 19:19:01 +0900430 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
431 val |= WINCONx_WSWP;
432 val |= WINCONx_BURSTLEN_16WORD;
433 break;
434 default:
435 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
436
437 val |= WINCON0_BPPMODE_24BPP_888;
438 val |= WINCONx_WSWP;
439 val |= WINCONx_BURSTLEN_16WORD;
440 break;
441 }
442
443 DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
444
445 writel(val, ctx->regs + WINCON(win));
446}
447
Sean Paulbb7704d2014-01-30 16:19:06 -0500448static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
Inki Dae1c248b72011-10-04 19:19:01 +0900449{
Inki Dae1c248b72011-10-04 19:19:01 +0900450 unsigned int keycon0 = 0, keycon1 = 0;
451
Inki Dae1c248b72011-10-04 19:19:01 +0900452 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
453 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
454
455 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
456
457 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
458 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
459}
460
Tomasz Figade7af102013-05-01 21:02:27 +0200461/**
462 * shadow_protect_win() - disable updating values from shadow registers at vsync
463 *
464 * @win: window to protect registers for
465 * @protect: 1 to protect (disable updates)
466 */
467static void fimd_shadow_protect_win(struct fimd_context *ctx,
468 int win, bool protect)
469{
470 u32 reg, bits, val;
471
472 if (ctx->driver_data->has_shadowcon) {
473 reg = SHADOWCON;
474 bits = SHADOWCON_WINx_PROTECT(win);
475 } else {
476 reg = PRTCON;
477 bits = PRTCON_PROTECT;
478 }
479
480 val = readl(ctx->regs + reg);
481 if (protect)
482 val |= bits;
483 else
484 val &= ~bits;
485 writel(val, ctx->regs + reg);
486}
487
Sean Paulbb7704d2014-01-30 16:19:06 -0500488static void fimd_win_commit(struct exynos_drm_manager *mgr, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900489{
Sean Paulbb7704d2014-01-30 16:19:06 -0500490 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900491 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900492 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900493 unsigned long val, alpha, size;
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900494 unsigned int last_x;
495 unsigned int last_y;
Inki Dae1c248b72011-10-04 19:19:01 +0900496
Inki Daee30d4bc2011-12-12 16:35:20 +0900497 if (ctx->suspended)
498 return;
499
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900500 if (win == DEFAULT_ZPOS)
501 win = ctx->default_win;
502
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200503 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900504 return;
505
506 win_data = &ctx->win_data[win];
507
508 /*
Tomasz Figade7af102013-05-01 21:02:27 +0200509 * SHADOWCON/PRTCON register is used for enabling timing.
Inki Dae1c248b72011-10-04 19:19:01 +0900510 *
511 * for example, once only width value of a register is set,
512 * if the dma is started then fimd hardware could malfunction so
513 * with protect window setting, the register fields with prefix '_F'
514 * wouldn't be updated at vsync also but updated once unprotect window
515 * is set.
516 */
517
518 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200519 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900520
521 /* buffer start address */
Inki Dae2c871122011-11-12 15:23:32 +0900522 val = (unsigned long)win_data->dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900523 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
524
525 /* buffer end address */
Inki Dae19c8b832011-10-14 13:29:46 +0900526 size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
Inki Dae2c871122011-11-12 15:23:32 +0900527 val = (unsigned long)(win_data->dma_addr + size);
Inki Dae1c248b72011-10-04 19:19:01 +0900528 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
529
530 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
Inki Dae2c871122011-11-12 15:23:32 +0900531 (unsigned long)win_data->dma_addr, val, size);
Inki Dae19c8b832011-10-14 13:29:46 +0900532 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
533 win_data->ovl_width, win_data->ovl_height);
Inki Dae1c248b72011-10-04 19:19:01 +0900534
535 /* buffer size */
536 val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900537 VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
538 VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
539 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
Inki Dae1c248b72011-10-04 19:19:01 +0900540 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
541
542 /* OSD position */
543 val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900544 VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
545 VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
546 VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900547 writel(val, ctx->regs + VIDOSD_A(win));
548
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900549 last_x = win_data->offset_x + win_data->ovl_width;
550 if (last_x)
551 last_x--;
552 last_y = win_data->offset_y + win_data->ovl_height;
553 if (last_y)
554 last_y--;
555
Joonyoung Shimca555e52012-12-14 15:48:24 +0900556 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
557 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
558
Inki Dae1c248b72011-10-04 19:19:01 +0900559 writel(val, ctx->regs + VIDOSD_B(win));
560
Inki Dae19c8b832011-10-14 13:29:46 +0900561 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900562 win_data->offset_x, win_data->offset_y, last_x, last_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900563
564 /* hardware window 0 doesn't support alpha channel. */
565 if (win != 0) {
566 /* OSD alpha */
567 alpha = VIDISD14C_ALPHA1_R(0xf) |
568 VIDISD14C_ALPHA1_G(0xf) |
569 VIDISD14C_ALPHA1_B(0xf);
570
571 writel(alpha, ctx->regs + VIDOSD_C(win));
572 }
573
574 /* OSD size */
575 if (win != 3 && win != 4) {
576 u32 offset = VIDOSD_D(win);
577 if (win == 0)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -0500578 offset = VIDOSD_C(win);
Inki Dae19c8b832011-10-14 13:29:46 +0900579 val = win_data->ovl_width * win_data->ovl_height;
Inki Dae1c248b72011-10-04 19:19:01 +0900580 writel(val, ctx->regs + offset);
581
582 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
583 }
584
Sean Paulbb7704d2014-01-30 16:19:06 -0500585 fimd_win_set_pixfmt(ctx, win);
Inki Dae1c248b72011-10-04 19:19:01 +0900586
587 /* hardware window 0 doesn't support color key. */
588 if (win != 0)
Sean Paulbb7704d2014-01-30 16:19:06 -0500589 fimd_win_set_colkey(ctx, win);
Inki Dae1c248b72011-10-04 19:19:01 +0900590
Inki Daeec05da92011-12-06 11:06:54 +0900591 /* wincon */
592 val = readl(ctx->regs + WINCON(win));
593 val |= WINCONx_ENWIN;
594 writel(val, ctx->regs + WINCON(win));
595
Inki Dae1c248b72011-10-04 19:19:01 +0900596 /* Enable DMA channel and unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200597 fimd_shadow_protect_win(ctx, win, false);
598
599 if (ctx->driver_data->has_shadowcon) {
600 val = readl(ctx->regs + SHADOWCON);
601 val |= SHADOWCON_CHx_ENABLE(win);
602 writel(val, ctx->regs + SHADOWCON);
603 }
Inki Daeec05da92011-12-06 11:06:54 +0900604
605 win_data->enabled = true;
Inki Dae1c248b72011-10-04 19:19:01 +0900606}
607
Sean Paulbb7704d2014-01-30 16:19:06 -0500608static void fimd_win_disable(struct exynos_drm_manager *mgr, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900609{
Sean Paulbb7704d2014-01-30 16:19:06 -0500610 struct fimd_context *ctx = mgr->ctx;
Inki Daeec05da92011-12-06 11:06:54 +0900611 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900612 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900613 u32 val;
614
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900615 if (win == DEFAULT_ZPOS)
616 win = ctx->default_win;
617
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200618 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900619 return;
620
Inki Daeec05da92011-12-06 11:06:54 +0900621 win_data = &ctx->win_data[win];
622
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530623 if (ctx->suspended) {
624 /* do not resume this window*/
625 win_data->resume = false;
626 return;
627 }
628
Inki Dae1c248b72011-10-04 19:19:01 +0900629 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200630 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900631
632 /* wincon */
633 val = readl(ctx->regs + WINCON(win));
634 val &= ~WINCONx_ENWIN;
635 writel(val, ctx->regs + WINCON(win));
636
637 /* unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200638 if (ctx->driver_data->has_shadowcon) {
639 val = readl(ctx->regs + SHADOWCON);
640 val &= ~SHADOWCON_CHx_ENABLE(win);
641 writel(val, ctx->regs + SHADOWCON);
642 }
643
644 fimd_shadow_protect_win(ctx, win, false);
Inki Daeec05da92011-12-06 11:06:54 +0900645
646 win_data->enabled = false;
Inki Dae1c248b72011-10-04 19:19:01 +0900647}
648
Sean Paul080be03d2014-02-19 21:02:55 +0900649static void fimd_dpms(struct exynos_drm_manager *mgr, int mode)
650{
651 struct fimd_context *ctx = mgr->ctx;
652
653 DRM_DEBUG_KMS("%d\n", mode);
654
655 mutex_lock(&ctx->lock);
656
657 switch (mode) {
658 case DRM_MODE_DPMS_ON:
659 /*
660 * enable fimd hardware only if suspended status.
661 *
662 * P.S. fimd_dpms function would be called at booting time so
663 * clk_enable could be called double time.
664 */
665 if (ctx->suspended)
666 pm_runtime_get_sync(ctx->dev);
667 break;
668 case DRM_MODE_DPMS_STANDBY:
669 case DRM_MODE_DPMS_SUSPEND:
670 case DRM_MODE_DPMS_OFF:
671 if (!ctx->suspended)
672 pm_runtime_put_sync(ctx->dev);
673 break;
674 default:
675 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
676 break;
677 }
678
679 mutex_unlock(&ctx->lock);
680}
681
Sean Paul1c6244c2014-01-30 16:19:02 -0500682static struct exynos_drm_manager_ops fimd_manager_ops = {
Sean Paul40c8ab42014-01-30 16:19:04 -0500683 .initialize = fimd_mgr_initialize,
Sean Paul080be03d2014-02-19 21:02:55 +0900684 .remove = fimd_mgr_remove,
Sean Paul1c6244c2014-01-30 16:19:02 -0500685 .dpms = fimd_dpms,
Sean Paul1c6244c2014-01-30 16:19:02 -0500686 .commit = fimd_commit,
687 .enable_vblank = fimd_enable_vblank,
688 .disable_vblank = fimd_disable_vblank,
689 .wait_for_vblank = fimd_wait_for_vblank,
690 .win_mode_set = fimd_win_mode_set,
691 .win_commit = fimd_win_commit,
692 .win_disable = fimd_win_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900693};
694
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900695static struct exynos_drm_manager fimd_manager = {
Sean Paul080be03d2014-02-19 21:02:55 +0900696 .type = EXYNOS_DISPLAY_TYPE_LCD,
697 .ops = &fimd_manager_ops,
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900698};
699
Inki Dae1c248b72011-10-04 19:19:01 +0900700static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
701{
702 struct fimd_context *ctx = (struct fimd_context *)dev_id;
Inki Dae1c248b72011-10-04 19:19:01 +0900703 u32 val;
704
705 val = readl(ctx->regs + VIDINTCON1);
706
707 if (val & VIDINTCON1_INT_FRAME)
708 /* VSYNC interrupt */
709 writel(VIDINTCON1_INT_FRAME, ctx->regs + VIDINTCON1);
710
Inki Daeec05da92011-12-06 11:06:54 +0900711 /* check the crtc is detached already from encoder */
Sean Paul080be03d2014-02-19 21:02:55 +0900712 if (ctx->pipe < 0 || !ctx->drm_dev)
Inki Daeec05da92011-12-06 11:06:54 +0900713 goto out;
Inki Dae483b88f2011-11-11 21:28:00 +0900714
Sean Paul080be03d2014-02-19 21:02:55 +0900715 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
716 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
Inki Dae1c248b72011-10-04 19:19:01 +0900717
Prathyush K01ce1132012-12-06 20:16:04 +0530718 /* set wait vsync event to zero and wake up queue. */
719 if (atomic_read(&ctx->wait_vsync_event)) {
720 atomic_set(&ctx->wait_vsync_event, 0);
Seung-Woo Kim8dd9ad52013-12-30 16:01:29 +0900721 wake_up(&ctx->wait_vsync_queue);
Prathyush K01ce1132012-12-06 20:16:04 +0530722 }
Inki Daeec05da92011-12-06 11:06:54 +0900723out:
Inki Dae1c248b72011-10-04 19:19:01 +0900724 return IRQ_HANDLED;
725}
726
Andrzej Hajda111e6052013-08-21 16:22:01 +0200727static int fimd_configure_clocks(struct fimd_context *ctx, struct device *dev)
Inki Dae1c248b72011-10-04 19:19:01 +0900728{
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200729 struct videomode *vm = &ctx->panel.vm;
Andrzej Hajda111e6052013-08-21 16:22:01 +0200730 unsigned long clk;
Inki Dae1c248b72011-10-04 19:19:01 +0900731
Andrzej Hajda111e6052013-08-21 16:22:01 +0200732 ctx->bus_clk = devm_clk_get(dev, "fimd");
733 if (IS_ERR(ctx->bus_clk)) {
734 dev_err(dev, "failed to get bus clock\n");
735 return PTR_ERR(ctx->bus_clk);
Inki Dae1c248b72011-10-04 19:19:01 +0900736 }
737
Andrzej Hajda111e6052013-08-21 16:22:01 +0200738 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
739 if (IS_ERR(ctx->lcd_clk)) {
740 dev_err(dev, "failed to get lcd clock\n");
741 return PTR_ERR(ctx->lcd_clk);
742 }
743
744 clk = clk_get_rate(ctx->lcd_clk);
745 if (clk == 0) {
746 dev_err(dev, "error getting sclk_fimd clock rate\n");
747 return -EINVAL;
748 }
749
750 if (vm->pixelclock == 0) {
751 unsigned long c;
752 c = vm->hactive + vm->hback_porch + vm->hfront_porch +
753 vm->hsync_len;
754 c *= vm->vactive + vm->vback_porch + vm->vfront_porch +
755 vm->vsync_len;
756 vm->pixelclock = c * FIMD_DEFAULT_FRAMERATE;
757 if (vm->pixelclock == 0) {
758 dev_err(dev, "incorrect display timings\n");
759 return -EINVAL;
760 }
761 dev_warn(dev, "pixel clock recalculated to %luHz (%dHz frame rate)\n",
762 vm->pixelclock, FIMD_DEFAULT_FRAMERATE);
763 }
764 ctx->clkdiv = DIV_ROUND_UP(clk, vm->pixelclock);
765 if (ctx->clkdiv > 256) {
766 dev_warn(dev, "calculated pixel clock divider too high (%u), lowered to 256\n",
767 ctx->clkdiv);
768 ctx->clkdiv = 256;
769 }
770 vm->pixelclock = clk / ctx->clkdiv;
771 DRM_DEBUG_KMS("pixel clock = %lu, clkdiv = %d\n", vm->pixelclock,
772 ctx->clkdiv);
773
774 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900775}
776
777static void fimd_clear_win(struct fimd_context *ctx, int win)
778{
Inki Dae1c248b72011-10-04 19:19:01 +0900779 writel(0, ctx->regs + WINCON(win));
780 writel(0, ctx->regs + VIDOSD_A(win));
781 writel(0, ctx->regs + VIDOSD_B(win));
782 writel(0, ctx->regs + VIDOSD_C(win));
783
784 if (win == 1 || win == 2)
785 writel(0, ctx->regs + VIDOSD_D(win));
786
Tomasz Figade7af102013-05-01 21:02:27 +0200787 fimd_shadow_protect_win(ctx, win, false);
Inki Dae1c248b72011-10-04 19:19:01 +0900788}
789
Inki Dae5d553932012-08-17 17:08:04 +0900790static int fimd_clock(struct fimd_context *ctx, bool enable)
Inki Dae373af0c2012-01-27 11:54:58 +0900791{
Inki Dae373af0c2012-01-27 11:54:58 +0900792 if (enable) {
793 int ret;
794
Vikas Sajjan11963a62013-04-23 17:19:50 +0530795 ret = clk_prepare_enable(ctx->bus_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900796 if (ret < 0)
797 return ret;
798
Vikas Sajjan11963a62013-04-23 17:19:50 +0530799 ret = clk_prepare_enable(ctx->lcd_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900800 if (ret < 0) {
Vikas Sajjan11963a62013-04-23 17:19:50 +0530801 clk_disable_unprepare(ctx->bus_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900802 return ret;
803 }
Inki Dae5d553932012-08-17 17:08:04 +0900804 } else {
Vikas Sajjan11963a62013-04-23 17:19:50 +0530805 clk_disable_unprepare(ctx->lcd_clk);
806 clk_disable_unprepare(ctx->bus_clk);
Inki Dae5d553932012-08-17 17:08:04 +0900807 }
808
809 return 0;
810}
811
Sean Paul080be03d2014-02-19 21:02:55 +0900812static void fimd_window_suspend(struct exynos_drm_manager *mgr)
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530813{
Sean Paulbb7704d2014-01-30 16:19:06 -0500814 struct fimd_context *ctx = mgr->ctx;
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530815 struct fimd_win_data *win_data;
816 int i;
817
818 for (i = 0; i < WINDOWS_NR; i++) {
819 win_data = &ctx->win_data[i];
820 win_data->resume = win_data->enabled;
Sean Paulbb7704d2014-01-30 16:19:06 -0500821 fimd_win_disable(mgr, i);
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530822 }
Sean Paulbb7704d2014-01-30 16:19:06 -0500823 fimd_wait_for_vblank(mgr);
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530824}
825
Sean Paul080be03d2014-02-19 21:02:55 +0900826static void fimd_window_resume(struct exynos_drm_manager *mgr)
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530827{
Sean Paulbb7704d2014-01-30 16:19:06 -0500828 struct fimd_context *ctx = mgr->ctx;
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530829 struct fimd_win_data *win_data;
830 int i;
831
832 for (i = 0; i < WINDOWS_NR; i++) {
833 win_data = &ctx->win_data[i];
834 win_data->enabled = win_data->resume;
835 win_data->resume = false;
836 }
837}
838
Sean Paul080be03d2014-02-19 21:02:55 +0900839static void fimd_apply(struct exynos_drm_manager *mgr)
840{
841 struct fimd_context *ctx = mgr->ctx;
842 struct fimd_win_data *win_data;
843 int i;
844
845 for (i = 0; i < WINDOWS_NR; i++) {
846 win_data = &ctx->win_data[i];
847 if (win_data->enabled)
848 fimd_win_commit(mgr, i);
849 }
850
851 fimd_commit(mgr);
852}
853
Sean Paulbb7704d2014-01-30 16:19:06 -0500854static int fimd_activate(struct exynos_drm_manager *mgr, bool enable)
Inki Dae5d553932012-08-17 17:08:04 +0900855{
Sean Paulbb7704d2014-01-30 16:19:06 -0500856 struct fimd_context *ctx = mgr->ctx;
Sean Paulbb7704d2014-01-30 16:19:06 -0500857
Inki Dae5d553932012-08-17 17:08:04 +0900858 if (enable) {
859 int ret;
Inki Dae5d553932012-08-17 17:08:04 +0900860
861 ret = fimd_clock(ctx, true);
862 if (ret < 0)
863 return ret;
Inki Dae373af0c2012-01-27 11:54:58 +0900864
865 ctx->suspended = false;
866
867 /* if vblank was enabled status, enable it again. */
868 if (test_and_clear_bit(0, &ctx->irq_flags))
Sean Paulbb7704d2014-01-30 16:19:06 -0500869 fimd_enable_vblank(mgr);
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530870
Sean Paul080be03d2014-02-19 21:02:55 +0900871 fimd_window_resume(mgr);
Sean Paul87244fa2014-01-30 16:19:07 -0500872
873 fimd_apply(mgr);
Inki Dae373af0c2012-01-27 11:54:58 +0900874 } else {
Sean Paul080be03d2014-02-19 21:02:55 +0900875 fimd_window_suspend(mgr);
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530876
Inki Dae5d553932012-08-17 17:08:04 +0900877 fimd_clock(ctx, false);
Inki Dae373af0c2012-01-27 11:54:58 +0900878 ctx->suspended = true;
879 }
880
881 return 0;
882}
883
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200884static int fimd_get_platform_data(struct fimd_context *ctx, struct device *dev)
Inki Dae1c248b72011-10-04 19:19:01 +0900885{
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530886 struct videomode *vm;
887 int ret;
Vikas Sajjan7f4596f2013-03-07 12:15:21 +0530888
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530889 vm = &ctx->panel.vm;
890 ret = of_get_videomode(dev->of_node, vm, OF_USE_NATIVE_MODE);
891 if (ret) {
892 DRM_ERROR("failed: of_get_videomode() : %d\n", ret);
893 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900894 }
895
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530896 if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
897 ctx->vidcon1 |= VIDCON1_INV_VSYNC;
898 if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
899 ctx->vidcon1 |= VIDCON1_INV_HSYNC;
900 if (vm->flags & DISPLAY_FLAGS_DE_LOW)
901 ctx->vidcon1 |= VIDCON1_INV_VDEN;
902 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
903 ctx->vidcon1 |= VIDCON1_INV_VCLK;
904
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200905 return 0;
906}
907
908static int fimd_probe(struct platform_device *pdev)
909{
910 struct device *dev = &pdev->dev;
911 struct fimd_context *ctx;
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200912 struct resource *res;
913 int win;
914 int ret = -EINVAL;
Inki Dae1c248b72011-10-04 19:19:01 +0900915
Sachin Kamat2d3f1732013-08-28 10:47:58 +0530916 if (!dev->of_node)
917 return -ENODEV;
918
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900919 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Inki Dae1c248b72011-10-04 19:19:01 +0900920 if (!ctx)
921 return -ENOMEM;
922
Sean Paulbb7704d2014-01-30 16:19:06 -0500923 ctx->dev = dev;
924
Andrzej Hajda562ad9f2013-08-21 16:22:03 +0200925 ret = fimd_get_platform_data(ctx, dev);
926 if (ret)
927 return ret;
928
Andrzej Hajda111e6052013-08-21 16:22:01 +0200929 ret = fimd_configure_clocks(ctx, dev);
930 if (ret)
931 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900932
Inki Dae1c248b72011-10-04 19:19:01 +0900933 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Inki Dae1c248b72011-10-04 19:19:01 +0900934
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900935 ctx->regs = devm_ioremap_resource(dev, res);
Thierry Redingd4ed6022013-01-21 11:09:02 +0100936 if (IS_ERR(ctx->regs))
937 return PTR_ERR(ctx->regs);
Inki Dae1c248b72011-10-04 19:19:01 +0900938
Vikas Sajjan1977e6d2013-04-02 17:53:01 +0900939 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "vsync");
Inki Dae1c248b72011-10-04 19:19:01 +0900940 if (!res) {
941 dev_err(dev, "irq request failed.\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530942 return -ENXIO;
Inki Dae1c248b72011-10-04 19:19:01 +0900943 }
944
945 ctx->irq = res->start;
946
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900947 ret = devm_request_irq(dev, ctx->irq, fimd_irq_handler,
Sachin Kamatedc57262012-06-19 11:47:39 +0530948 0, "drm_fimd", ctx);
949 if (ret) {
Inki Dae1c248b72011-10-04 19:19:01 +0900950 dev_err(dev, "irq request failed.\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530951 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900952 }
953
Tomasz Figa18873462013-05-01 21:02:26 +0200954 ctx->driver_data = drm_fimd_get_driver_data(pdev);
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100955 init_waitqueue_head(&ctx->wait_vsync_queue);
Prathyush K01ce1132012-12-06 20:16:04 +0530956 atomic_set(&ctx->wait_vsync_event, 0);
Inki Dae1c248b72011-10-04 19:19:01 +0900957
Inki Daec32b06e2011-12-16 21:49:03 +0900958 mutex_init(&ctx->lock);
959
Sean Paulbb7704d2014-01-30 16:19:06 -0500960 platform_set_drvdata(pdev, &fimd_manager);
Inki Daec32b06e2011-12-16 21:49:03 +0900961
Sean Paul080be03d2014-02-19 21:02:55 +0900962 fimd_manager.ctx = ctx;
963 exynos_drm_manager_register(&fimd_manager);
964
965 fimd_display.ctx = ctx;
966 exynos_drm_display_register(&fimd_display);
967
Inki Daec32b06e2011-12-16 21:49:03 +0900968 pm_runtime_enable(dev);
969 pm_runtime_get_sync(dev);
970
971 for (win = 0; win < WINDOWS_NR; win++)
972 fimd_clear_win(ctx, win);
973
Inki Dae1c248b72011-10-04 19:19:01 +0900974 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900975}
976
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800977static int fimd_remove(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900978{
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900979 struct device *dev = &pdev->dev;
Sean Paulbb7704d2014-01-30 16:19:06 -0500980 struct exynos_drm_manager *mgr = platform_get_drvdata(pdev);
981 struct fimd_context *ctx = mgr->ctx;
Inki Dae1c248b72011-10-04 19:19:01 +0900982
Sean Paul080be03d2014-02-19 21:02:55 +0900983 exynos_drm_display_unregister(&fimd_display);
984 exynos_drm_manager_unregister(&fimd_manager);
Inki Dae1c248b72011-10-04 19:19:01 +0900985
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900986 if (ctx->suspended)
987 goto out;
988
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900989 pm_runtime_set_suspended(dev);
990 pm_runtime_put_sync(dev);
991
992out:
993 pm_runtime_disable(dev);
994
Inki Dae1c248b72011-10-04 19:19:01 +0900995 return 0;
996}
997
Inki Daee30d4bc2011-12-12 16:35:20 +0900998#ifdef CONFIG_PM_SLEEP
999static int fimd_suspend(struct device *dev)
1000{
Sean Paulbb7704d2014-01-30 16:19:06 -05001001 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
Inki Daee30d4bc2011-12-12 16:35:20 +09001002
Inki Dae373af0c2012-01-27 11:54:58 +09001003 /*
1004 * do not use pm_runtime_suspend(). if pm_runtime_suspend() is
1005 * called here, an error would be returned by that interface
1006 * because the usage_count of pm runtime is more than 1.
1007 */
Inki Dae5d553932012-08-17 17:08:04 +09001008 if (!pm_runtime_suspended(dev))
Sean Paulbb7704d2014-01-30 16:19:06 -05001009 return fimd_activate(mgr, false);
Inki Dae5d553932012-08-17 17:08:04 +09001010
1011 return 0;
Inki Daee30d4bc2011-12-12 16:35:20 +09001012}
1013
1014static int fimd_resume(struct device *dev)
1015{
Sean Paulbb7704d2014-01-30 16:19:06 -05001016 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
Inki Daee30d4bc2011-12-12 16:35:20 +09001017
Inki Dae373af0c2012-01-27 11:54:58 +09001018 /*
1019 * if entered to sleep when lcd panel was on, the usage_count
1020 * of pm runtime would still be 1 so in this case, fimd driver
1021 * should be on directly not drawing on pm runtime interface.
1022 */
Sean Paul87244fa2014-01-30 16:19:07 -05001023 if (pm_runtime_suspended(dev))
1024 return 0;
Inki Dae5d553932012-08-17 17:08:04 +09001025
Sean Paul87244fa2014-01-30 16:19:07 -05001026 return fimd_activate(mgr, true);
Inki Daee30d4bc2011-12-12 16:35:20 +09001027}
1028#endif
1029
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001030#ifdef CONFIG_PM_RUNTIME
1031static int fimd_runtime_suspend(struct device *dev)
1032{
Sean Paulbb7704d2014-01-30 16:19:06 -05001033 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001034
Sean Paulbb7704d2014-01-30 16:19:06 -05001035 return fimd_activate(mgr, false);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001036}
1037
1038static int fimd_runtime_resume(struct device *dev)
1039{
Sean Paulbb7704d2014-01-30 16:19:06 -05001040 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001041
Sean Paulbb7704d2014-01-30 16:19:06 -05001042 return fimd_activate(mgr, true);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001043}
1044#endif
1045
1046static const struct dev_pm_ops fimd_pm_ops = {
Inki Daee30d4bc2011-12-12 16:35:20 +09001047 SET_SYSTEM_SLEEP_PM_OPS(fimd_suspend, fimd_resume)
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001048 SET_RUNTIME_PM_OPS(fimd_runtime_suspend, fimd_runtime_resume, NULL)
1049};
1050
Joonyoung Shim132a5b92012-03-16 18:47:08 +09001051struct platform_driver fimd_driver = {
Inki Dae1c248b72011-10-04 19:19:01 +09001052 .probe = fimd_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001053 .remove = fimd_remove,
Inki Dae1c248b72011-10-04 19:19:01 +09001054 .driver = {
1055 .name = "exynos4-fb",
1056 .owner = THIS_MODULE,
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001057 .pm = &fimd_pm_ops,
Sachin Kamat2d3f1732013-08-28 10:47:58 +05301058 .of_match_table = fimd_driver_dt_match,
Inki Dae1c248b72011-10-04 19:19:01 +09001059 },
1060};