blob: 9ecc30744c6a765e994a6fd15c7d3ac708037f93 [file] [log] [blame]
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001/*
2 * Copyright (C) 2008-2009 MontaVista Software Inc.
3 * Copyright (C) 2008-2009 Texas Instruments Inc
4 *
5 * Based on the LCD driver for TI Avalanche processors written by
6 * Ajay Singh and Shalom Hai.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option)any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/fb.h>
25#include <linux/dma-mapping.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070029#include <linux/interrupt.h>
30#include <linux/clk.h>
Chaithrika U Se04e5482009-12-15 16:46:29 -080031#include <linux/cpufreq.h>
Chaithrika U S1d3c6c72009-12-15 16:46:39 -080032#include <linux/console.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070034#include <video/da8xx-fb.h>
35
36#define DRIVER_NAME "da8xx_lcdc"
37
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053038#define LCD_VERSION_1 1
39#define LCD_VERSION_2 2
40
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070041/* LCD Status Register */
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070042#define LCD_END_OF_FRAME1 BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070043#define LCD_END_OF_FRAME0 BIT(8)
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070044#define LCD_PL_LOAD_DONE BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070045#define LCD_FIFO_UNDERFLOW BIT(5)
46#define LCD_SYNC_LOST BIT(2)
47
48/* LCD DMA Control Register */
49#define LCD_DMA_BURST_SIZE(x) ((x) << 4)
50#define LCD_DMA_BURST_1 0x0
51#define LCD_DMA_BURST_2 0x1
52#define LCD_DMA_BURST_4 0x2
53#define LCD_DMA_BURST_8 0x3
54#define LCD_DMA_BURST_16 0x4
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053055#define LCD_V1_END_OF_FRAME_INT_ENA BIT(2)
56#define LCD_V2_END_OF_FRAME0_INT_ENA BIT(8)
57#define LCD_V2_END_OF_FRAME1_INT_ENA BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070058#define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
59
60/* LCD Control Register */
61#define LCD_CLK_DIVISOR(x) ((x) << 8)
62#define LCD_RASTER_MODE 0x01
63
64/* LCD Raster Control Register */
65#define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
66#define PALETTE_AND_DATA 0x00
67#define PALETTE_ONLY 0x01
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070068#define DATA_ONLY 0x02
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070069
70#define LCD_MONO_8BIT_MODE BIT(9)
71#define LCD_RASTER_ORDER BIT(8)
72#define LCD_TFT_MODE BIT(7)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053073#define LCD_V1_UNDERFLOW_INT_ENA BIT(6)
74#define LCD_V2_UNDERFLOW_INT_ENA BIT(5)
75#define LCD_V1_PL_INT_ENA BIT(4)
76#define LCD_V2_PL_INT_ENA BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070077#define LCD_MONOCHROME_MODE BIT(1)
78#define LCD_RASTER_ENABLE BIT(0)
79#define LCD_TFT_ALT_ENABLE BIT(23)
80#define LCD_STN_565_ENABLE BIT(24)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053081#define LCD_V2_DMA_CLK_EN BIT(2)
82#define LCD_V2_LIDD_CLK_EN BIT(1)
83#define LCD_V2_CORE_CLK_EN BIT(0)
84#define LCD_V2_LPP_B10 26
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070085
86/* LCD Raster Timing 2 Register */
87#define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
88#define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
89#define LCD_SYNC_CTRL BIT(25)
90#define LCD_SYNC_EDGE BIT(24)
91#define LCD_INVERT_PIXEL_CLOCK BIT(22)
92#define LCD_INVERT_LINE_CLOCK BIT(21)
93#define LCD_INVERT_FRAME_CLOCK BIT(20)
94
95/* LCD Block */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053096#define LCD_PID_REG 0x0
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070097#define LCD_CTRL_REG 0x4
98#define LCD_STAT_REG 0x8
99#define LCD_RASTER_CTRL_REG 0x28
100#define LCD_RASTER_TIMING_0_REG 0x2C
101#define LCD_RASTER_TIMING_1_REG 0x30
102#define LCD_RASTER_TIMING_2_REG 0x34
103#define LCD_DMA_CTRL_REG 0x40
104#define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
105#define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700106#define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
107#define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
108
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530109/* Interrupt Registers available only in Version 2 */
110#define LCD_RAW_STAT_REG 0x58
111#define LCD_MASKED_STAT_REG 0x5c
112#define LCD_INT_ENABLE_SET_REG 0x60
113#define LCD_INT_ENABLE_CLR_REG 0x64
114#define LCD_END_OF_INT_IND_REG 0x68
115
116/* Clock registers available only on Version 2 */
117#define LCD_CLK_ENABLE_REG 0x6c
118#define LCD_CLK_RESET_REG 0x70
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530119#define LCD_CLK_MAIN_RESET BIT(3)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530120
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700121#define LCD_NUM_BUFFERS 2
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700122
123#define WSI_TIMEOUT 50
124#define PALETTE_SIZE 256
125#define LEFT_MARGIN 64
126#define RIGHT_MARGIN 64
127#define UPPER_MARGIN 32
128#define LOWER_MARGIN 32
129
130static resource_size_t da8xx_fb_reg_base;
131static struct resource *lcdc_regs;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530132static unsigned int lcd_revision;
133static irq_handler_t lcdc_irq_handler;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700134
135static inline unsigned int lcdc_read(unsigned int addr)
136{
137 return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
138}
139
140static inline void lcdc_write(unsigned int val, unsigned int addr)
141{
142 __raw_writel(val, da8xx_fb_reg_base + (addr));
143}
144
145struct da8xx_fb_par {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700146 resource_size_t p_palette_base;
147 unsigned char *v_palette_base;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700148 dma_addr_t vram_phys;
149 unsigned long vram_size;
150 void *vram_virt;
151 unsigned int dma_start;
152 unsigned int dma_end;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700153 struct clk *lcdc_clk;
154 int irq;
155 unsigned short pseudo_palette[16];
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700156 unsigned int palette_sz;
Chaithrika U S8097b172009-12-15 16:46:29 -0800157 unsigned int pxl_clk;
Chaithrika U S36113802009-12-15 16:46:38 -0800158 int blank;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700159 wait_queue_head_t vsync_wait;
160 int vsync_flag;
161 int vsync_timeout;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800162#ifdef CONFIG_CPU_FREQ
163 struct notifier_block freq_transition;
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +0530164 unsigned int lcd_fck_rate;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800165#endif
Chaithrika U S36113802009-12-15 16:46:38 -0800166 void (*panel_power_ctrl)(int);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700167};
168
169/* Variable Screen Information */
170static struct fb_var_screeninfo da8xx_fb_var __devinitdata = {
171 .xoffset = 0,
172 .yoffset = 0,
173 .transp = {0, 0, 0},
174 .nonstd = 0,
175 .activate = 0,
176 .height = -1,
177 .width = -1,
178 .pixclock = 46666, /* 46us - AUO display */
179 .accel_flags = 0,
180 .left_margin = LEFT_MARGIN,
181 .right_margin = RIGHT_MARGIN,
182 .upper_margin = UPPER_MARGIN,
183 .lower_margin = LOWER_MARGIN,
184 .sync = 0,
185 .vmode = FB_VMODE_NONINTERLACED
186};
187
188static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
189 .id = "DA8xx FB Drv",
190 .type = FB_TYPE_PACKED_PIXELS,
191 .type_aux = 0,
192 .visual = FB_VISUAL_PSEUDOCOLOR,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700193 .xpanstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700194 .ypanstep = 1,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700195 .ywrapstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700196 .accel = FB_ACCEL_NONE
197};
198
199struct da8xx_panel {
200 const char name[25]; /* Full name <vendor>_<model> */
201 unsigned short width;
202 unsigned short height;
203 int hfp; /* Horizontal front porch */
204 int hbp; /* Horizontal back porch */
205 int hsw; /* Horizontal Sync Pulse Width */
206 int vfp; /* Vertical front porch */
207 int vbp; /* Vertical back porch */
208 int vsw; /* Vertical Sync Pulse Width */
Chaithrika U S8097b172009-12-15 16:46:29 -0800209 unsigned int pxl_clk; /* Pixel clock */
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700210 unsigned char invert_pxl_clk; /* Invert Pixel clock */
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700211};
212
213static struct da8xx_panel known_lcd_panels[] = {
214 /* Sharp LCD035Q3DG01 */
215 [0] = {
216 .name = "Sharp_LCD035Q3DG01",
217 .width = 320,
218 .height = 240,
219 .hfp = 8,
220 .hbp = 6,
221 .hsw = 0,
222 .vfp = 2,
223 .vbp = 2,
224 .vsw = 0,
Chaithrika U S8097b172009-12-15 16:46:29 -0800225 .pxl_clk = 4608000,
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700226 .invert_pxl_clk = 1,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700227 },
228 /* Sharp LK043T1DG01 */
229 [1] = {
230 .name = "Sharp_LK043T1DG01",
231 .width = 480,
232 .height = 272,
233 .hfp = 2,
234 .hbp = 2,
235 .hsw = 41,
236 .vfp = 2,
237 .vbp = 2,
238 .vsw = 10,
Chaithrika U S8097b172009-12-15 16:46:29 -0800239 .pxl_clk = 7833600,
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700240 .invert_pxl_clk = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700241 },
242};
243
Chaithrika U S36113802009-12-15 16:46:38 -0800244/* Enable the Raster Engine of the LCD Controller */
245static inline void lcd_enable_raster(void)
246{
247 u32 reg;
248
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530249 /* Bring LCDC out of reset */
250 if (lcd_revision == LCD_VERSION_2)
251 lcdc_write(0, LCD_CLK_RESET_REG);
252
Chaithrika U S36113802009-12-15 16:46:38 -0800253 reg = lcdc_read(LCD_RASTER_CTRL_REG);
254 if (!(reg & LCD_RASTER_ENABLE))
255 lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
256}
257
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700258/* Disable the Raster Engine of the LCD Controller */
Chaithrika U S36113802009-12-15 16:46:38 -0800259static inline void lcd_disable_raster(void)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700260{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700261 u32 reg;
262
263 reg = lcdc_read(LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700264 if (reg & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700265 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530266
267 if (lcd_revision == LCD_VERSION_2)
268 /* Write 1 to reset LCDC */
269 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700270}
271
272static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
273{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700274 u32 start;
275 u32 end;
276 u32 reg_ras;
277 u32 reg_dma;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530278 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700279
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700280 /* init reg to clear PLM (loading mode) fields */
281 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
282 reg_ras &= ~(3 << 20);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700283
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700284 reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700285
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700286 if (load_mode == LOAD_DATA) {
287 start = par->dma_start;
288 end = par->dma_end;
289
290 reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530291 if (lcd_revision == LCD_VERSION_1) {
292 reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
293 } else {
294 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
295 LCD_V2_END_OF_FRAME0_INT_ENA |
296 LCD_V2_END_OF_FRAME1_INT_ENA;
297 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
298 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700299 reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
300
301 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
302 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
303 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
304 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
305 } else if (load_mode == LOAD_PALETTE) {
306 start = par->p_palette_base;
307 end = start + par->palette_sz - 1;
308
309 reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530310
311 if (lcd_revision == LCD_VERSION_1) {
312 reg_ras |= LCD_V1_PL_INT_ENA;
313 } else {
314 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
315 LCD_V2_PL_INT_ENA;
316 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
317 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700318
319 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
320 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
321 }
322
323 lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
324 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
325
326 /*
327 * The Raster enable bit must be set after all other control fields are
328 * set.
329 */
330 lcd_enable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700331}
332
333/* Configure the Burst Size of DMA */
334static int lcd_cfg_dma(int burst_size)
335{
336 u32 reg;
337
338 reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
339 switch (burst_size) {
340 case 1:
341 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
342 break;
343 case 2:
344 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
345 break;
346 case 4:
347 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
348 break;
349 case 8:
350 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
351 break;
352 case 16:
353 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
354 break;
355 default:
356 return -EINVAL;
357 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700358 lcdc_write(reg, LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700359
360 return 0;
361}
362
363static void lcd_cfg_ac_bias(int period, int transitions_per_int)
364{
365 u32 reg;
366
367 /* Set the AC Bias Period and Number of Transisitons per Interrupt */
368 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
369 reg |= LCD_AC_BIAS_FREQUENCY(period) |
370 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
371 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
372}
373
374static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
375 int front_porch)
376{
377 u32 reg;
378
379 reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
380 reg |= ((back_porch & 0xff) << 24)
381 | ((front_porch & 0xff) << 16)
382 | ((pulse_width & 0x3f) << 10);
383 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
384}
385
386static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
387 int front_porch)
388{
389 u32 reg;
390
391 reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
392 reg |= ((back_porch & 0xff) << 24)
393 | ((front_porch & 0xff) << 16)
394 | ((pulse_width & 0x3f) << 10);
395 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
396}
397
398static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
399{
400 u32 reg;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530401 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700402
403 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
404 LCD_MONO_8BIT_MODE |
405 LCD_MONOCHROME_MODE);
406
407 switch (cfg->p_disp_panel->panel_shade) {
408 case MONOCHROME:
409 reg |= LCD_MONOCHROME_MODE;
410 if (cfg->mono_8bit_mode)
411 reg |= LCD_MONO_8BIT_MODE;
412 break;
413 case COLOR_ACTIVE:
414 reg |= LCD_TFT_MODE;
415 if (cfg->tft_alt_mode)
416 reg |= LCD_TFT_ALT_ENABLE;
417 break;
418
419 case COLOR_PASSIVE:
420 if (cfg->stn_565_mode)
421 reg |= LCD_STN_565_ENABLE;
422 break;
423
424 default:
425 return -EINVAL;
426 }
427
428 /* enable additional interrupts here */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530429 if (lcd_revision == LCD_VERSION_1) {
430 reg |= LCD_V1_UNDERFLOW_INT_ENA;
431 } else {
432 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
433 LCD_V2_UNDERFLOW_INT_ENA;
434 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
435 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700436
437 lcdc_write(reg, LCD_RASTER_CTRL_REG);
438
439 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
440
441 if (cfg->sync_ctrl)
442 reg |= LCD_SYNC_CTRL;
443 else
444 reg &= ~LCD_SYNC_CTRL;
445
446 if (cfg->sync_edge)
447 reg |= LCD_SYNC_EDGE;
448 else
449 reg &= ~LCD_SYNC_EDGE;
450
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700451 if (cfg->invert_line_clock)
452 reg |= LCD_INVERT_LINE_CLOCK;
453 else
454 reg &= ~LCD_INVERT_LINE_CLOCK;
455
456 if (cfg->invert_frm_clock)
457 reg |= LCD_INVERT_FRAME_CLOCK;
458 else
459 reg &= ~LCD_INVERT_FRAME_CLOCK;
460
461 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
462
463 return 0;
464}
465
466static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
467 u32 bpp, u32 raster_order)
468{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700469 u32 reg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700470
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700471 /* Set the Panel Width */
472 /* Pixels per line = (PPL + 1)*16 */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530473 if (lcd_revision == LCD_VERSION_1) {
474 /*
475 * 0x3F in bits 4..9 gives max horizontal resolution = 1024
476 * pixels.
477 */
478 width &= 0x3f0;
479 } else {
480 /*
481 * 0x7F in bits 4..10 gives max horizontal resolution = 2048
482 * pixels.
483 */
484 width &= 0x7f0;
485 }
486
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700487 reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
488 reg &= 0xfffffc00;
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530489 if (lcd_revision == LCD_VERSION_1) {
490 reg |= ((width >> 4) - 1) << 4;
491 } else {
492 width = (width >> 4) - 1;
493 reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
494 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700495 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
496
497 /* Set the Panel Height */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530498 /* Set bits 9:0 of Lines Per Pixel */
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700499 reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
500 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
501 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
502
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530503 /* Set bit 10 of Lines Per Pixel */
504 if (lcd_revision == LCD_VERSION_2) {
505 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
506 reg |= ((height - 1) & 0x400) << 16;
507 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
508 }
509
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700510 /* Set the Raster Order of the Frame Buffer */
511 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
512 if (raster_order)
513 reg |= LCD_RASTER_ORDER;
514 lcdc_write(reg, LCD_RASTER_CTRL_REG);
515
516 switch (bpp) {
517 case 1:
518 case 2:
519 case 4:
520 case 16:
521 par->palette_sz = 16 * 2;
522 break;
523
524 case 8:
525 par->palette_sz = 256 * 2;
526 break;
527
528 default:
529 return -EINVAL;
530 }
531
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700532 return 0;
533}
534
535static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
536 unsigned blue, unsigned transp,
537 struct fb_info *info)
538{
539 struct da8xx_fb_par *par = info->par;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700540 unsigned short *palette = (unsigned short *) par->v_palette_base;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700541 u_short pal;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700542 int update_hw = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700543
544 if (regno > 255)
545 return 1;
546
547 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
548 return 1;
549
550 if (info->var.bits_per_pixel == 8) {
551 red >>= 4;
552 green >>= 8;
553 blue >>= 12;
554
555 pal = (red & 0x0f00);
556 pal |= (green & 0x00f0);
557 pal |= (blue & 0x000f);
558
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700559 if (palette[regno] != pal) {
560 update_hw = 1;
561 palette[regno] = pal;
562 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700563 } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
564 red >>= (16 - info->var.red.length);
565 red <<= info->var.red.offset;
566
567 green >>= (16 - info->var.green.length);
568 green <<= info->var.green.offset;
569
570 blue >>= (16 - info->var.blue.length);
571 blue <<= info->var.blue.offset;
572
573 par->pseudo_palette[regno] = red | green | blue;
574
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700575 if (palette[0] != 0x4000) {
576 update_hw = 1;
577 palette[0] = 0x4000;
578 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700579 }
580
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700581 /* Update the palette in the h/w as needed. */
582 if (update_hw)
583 lcd_blit(LOAD_PALETTE, par);
584
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700585 return 0;
586}
587
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700588static void lcd_reset(struct da8xx_fb_par *par)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700589{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700590 /* Disable the Raster if previously Enabled */
Chaithrika U S36113802009-12-15 16:46:38 -0800591 lcd_disable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700592
593 /* DMA has to be disabled */
594 lcdc_write(0, LCD_DMA_CTRL_REG);
595 lcdc_write(0, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530596
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530597 if (lcd_revision == LCD_VERSION_2) {
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530598 lcdc_write(0, LCD_INT_ENABLE_SET_REG);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530599 /* Write 1 to reset */
600 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
601 lcdc_write(0, LCD_CLK_RESET_REG);
602 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700603}
604
Chaithrika U S8097b172009-12-15 16:46:29 -0800605static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
606{
607 unsigned int lcd_clk, div;
608
609 lcd_clk = clk_get_rate(par->lcdc_clk);
610 div = lcd_clk / par->pxl_clk;
611
612 /* Configure the LCD clock divisor. */
613 lcdc_write(LCD_CLK_DIVISOR(div) |
614 (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530615
616 if (lcd_revision == LCD_VERSION_2)
617 lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
618 LCD_V2_CORE_CLK_EN, LCD_CLK_ENABLE_REG);
619
Chaithrika U S8097b172009-12-15 16:46:29 -0800620}
621
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700622static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
623 struct da8xx_panel *panel)
624{
625 u32 bpp;
626 int ret = 0;
627
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700628 lcd_reset(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700629
Chaithrika U S8097b172009-12-15 16:46:29 -0800630 /* Calculate the divider */
631 lcd_calc_clk_divider(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700632
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700633 if (panel->invert_pxl_clk)
634 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
635 LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
636 else
637 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
638 ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
639
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700640 /* Configure the DMA burst size. */
641 ret = lcd_cfg_dma(cfg->dma_burst_sz);
642 if (ret < 0)
643 return ret;
644
645 /* Configure the AC bias properties. */
646 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
647
648 /* Configure the vertical and horizontal sync properties. */
649 lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
650 lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
651
652 /* Configure for disply */
653 ret = lcd_cfg_display(cfg);
654 if (ret < 0)
655 return ret;
656
657 if (QVGA != cfg->p_disp_panel->panel_type)
658 return -EINVAL;
659
660 if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
661 cfg->bpp >= cfg->p_disp_panel->min_bpp)
662 bpp = cfg->bpp;
663 else
664 bpp = cfg->p_disp_panel->max_bpp;
665 if (bpp == 12)
666 bpp = 16;
667 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
668 (unsigned int)panel->height, bpp,
669 cfg->raster_order);
670 if (ret < 0)
671 return ret;
672
673 /* Configure FDD */
674 lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
675 (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
676
677 return 0;
678}
679
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530680/* IRQ handler for version 2 of LCDC */
681static irqreturn_t lcdc_irq_handler_rev02(int irq, void *arg)
682{
683 struct da8xx_fb_par *par = arg;
684 u32 stat = lcdc_read(LCD_MASKED_STAT_REG);
685 u32 reg_int;
686
687 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
688 lcd_disable_raster();
689 lcdc_write(stat, LCD_MASKED_STAT_REG);
690 lcd_enable_raster();
691 } else if (stat & LCD_PL_LOAD_DONE) {
692 /*
693 * Must disable raster before changing state of any control bit.
694 * And also must be disabled before clearing the PL loading
695 * interrupt via the following write to the status register. If
696 * this is done after then one gets multiple PL done interrupts.
697 */
698 lcd_disable_raster();
699
700 lcdc_write(stat, LCD_MASKED_STAT_REG);
701
702 /* Disable PL completion inerrupt */
703 reg_int = lcdc_read(LCD_INT_ENABLE_CLR_REG) |
704 (LCD_V2_PL_INT_ENA);
705 lcdc_write(reg_int, LCD_INT_ENABLE_CLR_REG);
706
707 /* Setup and start data loading mode */
708 lcd_blit(LOAD_DATA, par);
709 } else {
710 lcdc_write(stat, LCD_MASKED_STAT_REG);
711
712 if (stat & LCD_END_OF_FRAME0) {
713 lcdc_write(par->dma_start,
714 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
715 lcdc_write(par->dma_end,
716 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
717 par->vsync_flag = 1;
718 wake_up_interruptible(&par->vsync_wait);
719 }
720
721 if (stat & LCD_END_OF_FRAME1) {
722 lcdc_write(par->dma_start,
723 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
724 lcdc_write(par->dma_end,
725 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
726 par->vsync_flag = 1;
727 wake_up_interruptible(&par->vsync_wait);
728 }
729 }
730
731 lcdc_write(0, LCD_END_OF_INT_IND_REG);
732 return IRQ_HANDLED;
733}
734
735/* IRQ handler for version 1 LCDC */
736static irqreturn_t lcdc_irq_handler_rev01(int irq, void *arg)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700737{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700738 struct da8xx_fb_par *par = arg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700739 u32 stat = lcdc_read(LCD_STAT_REG);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700740 u32 reg_ras;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700741
742 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
Chaithrika U S36113802009-12-15 16:46:38 -0800743 lcd_disable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700744 lcdc_write(stat, LCD_STAT_REG);
Chaithrika U S36113802009-12-15 16:46:38 -0800745 lcd_enable_raster();
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700746 } else if (stat & LCD_PL_LOAD_DONE) {
747 /*
748 * Must disable raster before changing state of any control bit.
749 * And also must be disabled before clearing the PL loading
750 * interrupt via the following write to the status register. If
751 * this is done after then one gets multiple PL done interrupts.
752 */
753 lcd_disable_raster();
754
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700755 lcdc_write(stat, LCD_STAT_REG);
756
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700757 /* Disable PL completion inerrupt */
758 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530759 reg_ras &= ~LCD_V1_PL_INT_ENA;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700760 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
761
762 /* Setup and start data loading mode */
763 lcd_blit(LOAD_DATA, par);
764 } else {
765 lcdc_write(stat, LCD_STAT_REG);
766
767 if (stat & LCD_END_OF_FRAME0) {
768 lcdc_write(par->dma_start,
769 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
770 lcdc_write(par->dma_end,
771 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
772 par->vsync_flag = 1;
773 wake_up_interruptible(&par->vsync_wait);
774 }
775
776 if (stat & LCD_END_OF_FRAME1) {
777 lcdc_write(par->dma_start,
778 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
779 lcdc_write(par->dma_end,
780 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
781 par->vsync_flag = 1;
782 wake_up_interruptible(&par->vsync_wait);
783 }
784 }
785
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700786 return IRQ_HANDLED;
787}
788
789static int fb_check_var(struct fb_var_screeninfo *var,
790 struct fb_info *info)
791{
792 int err = 0;
793
794 switch (var->bits_per_pixel) {
795 case 1:
796 case 8:
797 var->red.offset = 0;
798 var->red.length = 8;
799 var->green.offset = 0;
800 var->green.length = 8;
801 var->blue.offset = 0;
802 var->blue.length = 8;
803 var->transp.offset = 0;
804 var->transp.length = 0;
805 break;
806 case 4:
807 var->red.offset = 0;
808 var->red.length = 4;
809 var->green.offset = 0;
810 var->green.length = 4;
811 var->blue.offset = 0;
812 var->blue.length = 4;
813 var->transp.offset = 0;
814 var->transp.length = 0;
815 break;
816 case 16: /* RGB 565 */
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800817 var->red.offset = 11;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700818 var->red.length = 5;
819 var->green.offset = 5;
820 var->green.length = 6;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800821 var->blue.offset = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700822 var->blue.length = 5;
823 var->transp.offset = 0;
824 var->transp.length = 0;
825 break;
826 default:
827 err = -EINVAL;
828 }
829
830 var->red.msb_right = 0;
831 var->green.msb_right = 0;
832 var->blue.msb_right = 0;
833 var->transp.msb_right = 0;
834 return err;
835}
836
Chaithrika U Se04e5482009-12-15 16:46:29 -0800837#ifdef CONFIG_CPU_FREQ
838static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
839 unsigned long val, void *data)
840{
841 struct da8xx_fb_par *par;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800842
843 par = container_of(nb, struct da8xx_fb_par, freq_transition);
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +0530844 if (val == CPUFREQ_POSTCHANGE) {
845 if (par->lcd_fck_rate != clk_get_rate(par->lcdc_clk)) {
846 par->lcd_fck_rate = clk_get_rate(par->lcdc_clk);
847 lcd_disable_raster();
848 lcd_calc_clk_divider(par);
849 lcd_enable_raster();
850 }
Chaithrika U Se04e5482009-12-15 16:46:29 -0800851 }
852
853 return 0;
854}
855
856static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
857{
858 par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
859
860 return cpufreq_register_notifier(&par->freq_transition,
861 CPUFREQ_TRANSITION_NOTIFIER);
862}
863
864static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
865{
866 cpufreq_unregister_notifier(&par->freq_transition,
867 CPUFREQ_TRANSITION_NOTIFIER);
868}
869#endif
870
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700871static int __devexit fb_remove(struct platform_device *dev)
872{
873 struct fb_info *info = dev_get_drvdata(&dev->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700874
875 if (info) {
876 struct da8xx_fb_par *par = info->par;
877
Chaithrika U Se04e5482009-12-15 16:46:29 -0800878#ifdef CONFIG_CPU_FREQ
879 lcd_da8xx_cpufreq_deregister(par);
880#endif
Chaithrika U S36113802009-12-15 16:46:38 -0800881 if (par->panel_power_ctrl)
882 par->panel_power_ctrl(0);
883
884 lcd_disable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700885 lcdc_write(0, LCD_RASTER_CTRL_REG);
886
887 /* disable DMA */
888 lcdc_write(0, LCD_DMA_CTRL_REG);
889
890 unregister_framebuffer(info);
891 fb_dealloc_cmap(&info->cmap);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700892 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
893 par->p_palette_base);
894 dma_free_coherent(NULL, par->vram_size, par->vram_virt,
895 par->vram_phys);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700896 free_irq(par->irq, par);
897 clk_disable(par->lcdc_clk);
898 clk_put(par->lcdc_clk);
899 framebuffer_release(info);
900 iounmap((void __iomem *)da8xx_fb_reg_base);
901 release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
902
903 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700904 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700905}
906
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700907/*
908 * Function to wait for vertical sync which for this LCD peripheral
909 * translates into waiting for the current raster frame to complete.
910 */
911static int fb_wait_for_vsync(struct fb_info *info)
912{
913 struct da8xx_fb_par *par = info->par;
914 int ret;
915
916 /*
917 * Set flag to 0 and wait for isr to set to 1. It would seem there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300918 * race condition here where the ISR could have occurred just before or
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700919 * just after this set. But since we are just coarsely waiting for
920 * a frame to complete then that's OK. i.e. if the frame completed
921 * just before this code executed then we have to wait another full
922 * frame time but there is no way to avoid such a situation. On the
923 * other hand if the frame completed just after then we don't need
924 * to wait long at all. Either way we are guaranteed to return to the
925 * user immediately after a frame completion which is all that is
926 * required.
927 */
928 par->vsync_flag = 0;
929 ret = wait_event_interruptible_timeout(par->vsync_wait,
930 par->vsync_flag != 0,
931 par->vsync_timeout);
932 if (ret < 0)
933 return ret;
934 if (ret == 0)
935 return -ETIMEDOUT;
936
937 return 0;
938}
939
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700940static int fb_ioctl(struct fb_info *info, unsigned int cmd,
941 unsigned long arg)
942{
943 struct lcd_sync_arg sync_arg;
944
945 switch (cmd) {
946 case FBIOGET_CONTRAST:
947 case FBIOPUT_CONTRAST:
948 case FBIGET_BRIGHTNESS:
949 case FBIPUT_BRIGHTNESS:
950 case FBIGET_COLOR:
951 case FBIPUT_COLOR:
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700952 return -ENOTTY;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700953 case FBIPUT_HSYNC:
954 if (copy_from_user(&sync_arg, (char *)arg,
955 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700956 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700957 lcd_cfg_horizontal_sync(sync_arg.back_porch,
958 sync_arg.pulse_width,
959 sync_arg.front_porch);
960 break;
961 case FBIPUT_VSYNC:
962 if (copy_from_user(&sync_arg, (char *)arg,
963 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700964 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700965 lcd_cfg_vertical_sync(sync_arg.back_porch,
966 sync_arg.pulse_width,
967 sync_arg.front_porch);
968 break;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700969 case FBIO_WAITFORVSYNC:
970 return fb_wait_for_vsync(info);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700971 default:
972 return -EINVAL;
973 }
974 return 0;
975}
976
Chaithrika U S312d9712009-12-15 16:46:39 -0800977static int cfb_blank(int blank, struct fb_info *info)
978{
979 struct da8xx_fb_par *par = info->par;
980 int ret = 0;
981
982 if (par->blank == blank)
983 return 0;
984
985 par->blank = blank;
986 switch (blank) {
987 case FB_BLANK_UNBLANK:
988 if (par->panel_power_ctrl)
989 par->panel_power_ctrl(1);
990
991 lcd_enable_raster();
992 break;
993 case FB_BLANK_POWERDOWN:
994 if (par->panel_power_ctrl)
995 par->panel_power_ctrl(0);
996
997 lcd_disable_raster();
998 break;
999 default:
1000 ret = -EINVAL;
1001 }
1002
1003 return ret;
1004}
1005
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001006/*
1007 * Set new x,y offsets in the virtual display for the visible area and switch
1008 * to the new mode.
1009 */
1010static int da8xx_pan_display(struct fb_var_screeninfo *var,
1011 struct fb_info *fbi)
1012{
1013 int ret = 0;
1014 struct fb_var_screeninfo new_var;
1015 struct da8xx_fb_par *par = fbi->par;
1016 struct fb_fix_screeninfo *fix = &fbi->fix;
1017 unsigned int end;
1018 unsigned int start;
1019
1020 if (var->xoffset != fbi->var.xoffset ||
1021 var->yoffset != fbi->var.yoffset) {
1022 memcpy(&new_var, &fbi->var, sizeof(new_var));
1023 new_var.xoffset = var->xoffset;
1024 new_var.yoffset = var->yoffset;
1025 if (fb_check_var(&new_var, fbi))
1026 ret = -EINVAL;
1027 else {
1028 memcpy(&fbi->var, &new_var, sizeof(new_var));
1029
1030 start = fix->smem_start +
1031 new_var.yoffset * fix->line_length +
Laurent Pincharte6c4d3d2011-06-14 09:24:45 +00001032 new_var.xoffset * fbi->var.bits_per_pixel / 8;
1033 end = start + fbi->var.yres * fix->line_length - 1;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001034 par->dma_start = start;
1035 par->dma_end = end;
1036 }
1037 }
1038
1039 return ret;
1040}
1041
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001042static struct fb_ops da8xx_fb_ops = {
1043 .owner = THIS_MODULE,
1044 .fb_check_var = fb_check_var,
1045 .fb_setcolreg = fb_setcolreg,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001046 .fb_pan_display = da8xx_pan_display,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001047 .fb_ioctl = fb_ioctl,
1048 .fb_fillrect = cfb_fillrect,
1049 .fb_copyarea = cfb_copyarea,
1050 .fb_imageblit = cfb_imageblit,
Chaithrika U S312d9712009-12-15 16:46:39 -08001051 .fb_blank = cfb_blank,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001052};
1053
axel lin1db41e02011-02-22 01:52:42 +00001054static int __devinit fb_probe(struct platform_device *device)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001055{
1056 struct da8xx_lcdc_platform_data *fb_pdata =
1057 device->dev.platform_data;
1058 struct lcd_ctrl_config *lcd_cfg;
1059 struct da8xx_panel *lcdc_info;
1060 struct fb_info *da8xx_fb_info;
1061 struct clk *fb_clk = NULL;
1062 struct da8xx_fb_par *par;
1063 resource_size_t len;
1064 int ret, i;
1065
1066 if (fb_pdata == NULL) {
1067 dev_err(&device->dev, "Can not get platform data\n");
1068 return -ENOENT;
1069 }
1070
1071 lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
1072 if (!lcdc_regs) {
1073 dev_err(&device->dev,
1074 "Can not get memory resource for LCD controller\n");
1075 return -ENOENT;
1076 }
1077
1078 len = resource_size(lcdc_regs);
1079
1080 lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
1081 if (!lcdc_regs)
1082 return -EBUSY;
1083
1084 da8xx_fb_reg_base = (resource_size_t)ioremap(lcdc_regs->start, len);
1085 if (!da8xx_fb_reg_base) {
1086 ret = -EBUSY;
1087 goto err_request_mem;
1088 }
1089
1090 fb_clk = clk_get(&device->dev, NULL);
1091 if (IS_ERR(fb_clk)) {
1092 dev_err(&device->dev, "Can not get device clock\n");
1093 ret = -ENODEV;
1094 goto err_ioremap;
1095 }
1096 ret = clk_enable(fb_clk);
1097 if (ret)
1098 goto err_clk_put;
1099
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301100 /* Determine LCD IP Version */
1101 switch (lcdc_read(LCD_PID_REG)) {
1102 case 0x4C100102:
1103 lcd_revision = LCD_VERSION_1;
1104 break;
1105 case 0x4F200800:
1106 lcd_revision = LCD_VERSION_2;
1107 break;
1108 default:
1109 dev_warn(&device->dev, "Unknown PID Reg value 0x%x, "
1110 "defaulting to LCD revision 1\n",
1111 lcdc_read(LCD_PID_REG));
1112 lcd_revision = LCD_VERSION_1;
1113 break;
1114 }
1115
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001116 for (i = 0, lcdc_info = known_lcd_panels;
1117 i < ARRAY_SIZE(known_lcd_panels);
1118 i++, lcdc_info++) {
1119 if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
1120 break;
1121 }
1122
1123 if (i == ARRAY_SIZE(known_lcd_panels)) {
1124 dev_err(&device->dev, "GLCD: No valid panel found\n");
Roel Kluindd04a6b2009-11-17 14:06:15 -08001125 ret = -ENODEV;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001126 goto err_clk_disable;
1127 } else
1128 dev_info(&device->dev, "GLCD: Found %s panel\n",
1129 fb_pdata->type);
1130
1131 lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
1132
1133 da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
1134 &device->dev);
1135 if (!da8xx_fb_info) {
1136 dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
1137 ret = -ENOMEM;
1138 goto err_clk_disable;
1139 }
1140
1141 par = da8xx_fb_info->par;
Chaithrika U S8097b172009-12-15 16:46:29 -08001142 par->lcdc_clk = fb_clk;
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +05301143#ifdef CONFIG_CPU_FREQ
1144 par->lcd_fck_rate = clk_get_rate(fb_clk);
1145#endif
Chaithrika U S8097b172009-12-15 16:46:29 -08001146 par->pxl_clk = lcdc_info->pxl_clk;
Chaithrika U S36113802009-12-15 16:46:38 -08001147 if (fb_pdata->panel_power_ctrl) {
1148 par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
1149 par->panel_power_ctrl(1);
1150 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001151
1152 if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
1153 dev_err(&device->dev, "lcd_init failed\n");
1154 ret = -EFAULT;
1155 goto err_release_fb;
1156 }
1157
1158 /* allocate frame buffer */
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001159 par->vram_size = lcdc_info->width * lcdc_info->height * lcd_cfg->bpp;
1160 par->vram_size = PAGE_ALIGN(par->vram_size/8);
1161 par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001162
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001163 par->vram_virt = dma_alloc_coherent(NULL,
1164 par->vram_size,
1165 (resource_size_t *) &par->vram_phys,
1166 GFP_KERNEL | GFP_DMA);
1167 if (!par->vram_virt) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001168 dev_err(&device->dev,
1169 "GLCD: kmalloc for frame buffer failed\n");
1170 ret = -EINVAL;
1171 goto err_release_fb;
1172 }
1173
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001174 da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
1175 da8xx_fb_fix.smem_start = par->vram_phys;
1176 da8xx_fb_fix.smem_len = par->vram_size;
1177 da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001178
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001179 par->dma_start = par->vram_phys;
1180 par->dma_end = par->dma_start + lcdc_info->height *
1181 da8xx_fb_fix.line_length - 1;
1182
1183 /* allocate palette buffer */
1184 par->v_palette_base = dma_alloc_coherent(NULL,
1185 PALETTE_SIZE,
1186 (resource_size_t *)
1187 &par->p_palette_base,
1188 GFP_KERNEL | GFP_DMA);
1189 if (!par->v_palette_base) {
1190 dev_err(&device->dev,
1191 "GLCD: kmalloc for palette buffer failed\n");
1192 ret = -EINVAL;
1193 goto err_release_fb_mem;
1194 }
1195 memset(par->v_palette_base, 0, PALETTE_SIZE);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001196
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001197 par->irq = platform_get_irq(device, 0);
1198 if (par->irq < 0) {
1199 ret = -ENOENT;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001200 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001201 }
1202
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001203 /* Initialize par */
1204 da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
1205
1206 da8xx_fb_var.xres = lcdc_info->width;
1207 da8xx_fb_var.xres_virtual = lcdc_info->width;
1208
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001209 da8xx_fb_var.yres = lcdc_info->height;
1210 da8xx_fb_var.yres_virtual = lcdc_info->height * LCD_NUM_BUFFERS;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001211
1212 da8xx_fb_var.grayscale =
1213 lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
1214 da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
1215
1216 da8xx_fb_var.hsync_len = lcdc_info->hsw;
1217 da8xx_fb_var.vsync_len = lcdc_info->vsw;
1218
1219 /* Initialize fbinfo */
1220 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
1221 da8xx_fb_info->fix = da8xx_fb_fix;
1222 da8xx_fb_info->var = da8xx_fb_var;
1223 da8xx_fb_info->fbops = &da8xx_fb_ops;
1224 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -08001225 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
1226 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001227
1228 ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
1229 if (ret)
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001230 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001231 da8xx_fb_info->cmap.len = par->palette_sz;
1232
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001233 /* initialize var_screeninfo */
1234 da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
1235 fb_set_var(da8xx_fb_info, &da8xx_fb_var);
1236
1237 dev_set_drvdata(&device->dev, da8xx_fb_info);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001238
1239 /* initialize the vsync wait queue */
1240 init_waitqueue_head(&par->vsync_wait);
1241 par->vsync_timeout = HZ / 5;
1242
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001243 /* Register the Frame Buffer */
1244 if (register_framebuffer(da8xx_fb_info) < 0) {
1245 dev_err(&device->dev,
1246 "GLCD: Frame Buffer Registration Failed!\n");
1247 ret = -EINVAL;
1248 goto err_dealloc_cmap;
1249 }
1250
Chaithrika U Se04e5482009-12-15 16:46:29 -08001251#ifdef CONFIG_CPU_FREQ
1252 ret = lcd_da8xx_cpufreq_register(par);
1253 if (ret) {
1254 dev_err(&device->dev, "failed to register cpufreq\n");
1255 goto err_cpu_freq;
1256 }
1257#endif
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001258
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301259 if (lcd_revision == LCD_VERSION_1)
1260 lcdc_irq_handler = lcdc_irq_handler_rev01;
1261 else
1262 lcdc_irq_handler = lcdc_irq_handler_rev02;
1263
1264 ret = request_irq(par->irq, lcdc_irq_handler, 0,
1265 DRIVER_NAME, par);
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001266 if (ret)
1267 goto irq_freq;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001268 return 0;
1269
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001270irq_freq:
Chaithrika U Se04e5482009-12-15 16:46:29 -08001271#ifdef CONFIG_CPU_FREQ
axel lin360c2022011-01-20 03:50:51 +00001272 lcd_da8xx_cpufreq_deregister(par);
1273#endif
Chaithrika U Se04e5482009-12-15 16:46:29 -08001274err_cpu_freq:
1275 unregister_framebuffer(da8xx_fb_info);
Chaithrika U Se04e5482009-12-15 16:46:29 -08001276
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001277err_dealloc_cmap:
1278 fb_dealloc_cmap(&da8xx_fb_info->cmap);
1279
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001280err_release_pl_mem:
1281 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
1282 par->p_palette_base);
1283
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001284err_release_fb_mem:
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001285 dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001286
1287err_release_fb:
1288 framebuffer_release(da8xx_fb_info);
1289
1290err_clk_disable:
1291 clk_disable(fb_clk);
1292
1293err_clk_put:
1294 clk_put(fb_clk);
1295
1296err_ioremap:
1297 iounmap((void __iomem *)da8xx_fb_reg_base);
1298
1299err_request_mem:
1300 release_mem_region(lcdc_regs->start, len);
1301
1302 return ret;
1303}
1304
1305#ifdef CONFIG_PM
1306static int fb_suspend(struct platform_device *dev, pm_message_t state)
1307{
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001308 struct fb_info *info = platform_get_drvdata(dev);
1309 struct da8xx_fb_par *par = info->par;
1310
Torben Hohnac751ef2011-01-25 15:07:35 -08001311 console_lock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001312 if (par->panel_power_ctrl)
1313 par->panel_power_ctrl(0);
1314
1315 fb_set_suspend(info, 1);
1316 lcd_disable_raster();
1317 clk_disable(par->lcdc_clk);
Torben Hohnac751ef2011-01-25 15:07:35 -08001318 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001319
1320 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001321}
1322static int fb_resume(struct platform_device *dev)
1323{
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001324 struct fb_info *info = platform_get_drvdata(dev);
1325 struct da8xx_fb_par *par = info->par;
1326
Torben Hohnac751ef2011-01-25 15:07:35 -08001327 console_lock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001328 if (par->panel_power_ctrl)
1329 par->panel_power_ctrl(1);
1330
1331 clk_enable(par->lcdc_clk);
1332 lcd_enable_raster();
1333 fb_set_suspend(info, 0);
Torben Hohnac751ef2011-01-25 15:07:35 -08001334 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001335
1336 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001337}
1338#else
1339#define fb_suspend NULL
1340#define fb_resume NULL
1341#endif
1342
1343static struct platform_driver da8xx_fb_driver = {
1344 .probe = fb_probe,
axel lin1db41e02011-02-22 01:52:42 +00001345 .remove = __devexit_p(fb_remove),
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001346 .suspend = fb_suspend,
1347 .resume = fb_resume,
1348 .driver = {
1349 .name = DRIVER_NAME,
1350 .owner = THIS_MODULE,
1351 },
1352};
1353
1354static int __init da8xx_fb_init(void)
1355{
1356 return platform_driver_register(&da8xx_fb_driver);
1357}
1358
1359static void __exit da8xx_fb_cleanup(void)
1360{
1361 platform_driver_unregister(&da8xx_fb_driver);
1362}
1363
1364module_init(da8xx_fb_init);
1365module_exit(da8xx_fb_cleanup);
1366
1367MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
1368MODULE_AUTHOR("Texas Instruments");
1369MODULE_LICENSE("GPL");