video: da8xx-fb: fix 24bpp raster configuration
[linux-2.6-block.git] / drivers / video / da8xx-fb.c
CommitLineData
4ed824d9
SR
1/*
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>
9dd44d5d 29#include <linux/pm_runtime.h>
4ed824d9 30#include <linux/interrupt.h>
a481b37a 31#include <linux/wait.h>
4ed824d9 32#include <linux/clk.h>
e04e5483 33#include <linux/cpufreq.h>
1d3c6c7b 34#include <linux/console.h>
deb95c6c 35#include <linux/spinlock.h>
5a0e3ad6 36#include <linux/slab.h>
a0239073 37#include <linux/delay.h>
3b9cc4ea 38#include <linux/lcm.h>
4ed824d9 39#include <video/da8xx-fb.h>
12fa8350 40#include <asm/div64.h>
4ed824d9
SR
41
42#define DRIVER_NAME "da8xx_lcdc"
43
c6daf05b
MP
44#define LCD_VERSION_1 1
45#define LCD_VERSION_2 2
46
4ed824d9 47/* LCD Status Register */
1f9c3e1f 48#define LCD_END_OF_FRAME1 BIT(9)
4ed824d9 49#define LCD_END_OF_FRAME0 BIT(8)
1f9c3e1f 50#define LCD_PL_LOAD_DONE BIT(6)
4ed824d9
SR
51#define LCD_FIFO_UNDERFLOW BIT(5)
52#define LCD_SYNC_LOST BIT(2)
a481b37a 53#define LCD_FRAME_DONE BIT(0)
4ed824d9
SR
54
55/* LCD DMA Control Register */
56#define LCD_DMA_BURST_SIZE(x) ((x) << 4)
57#define LCD_DMA_BURST_1 0x0
58#define LCD_DMA_BURST_2 0x1
59#define LCD_DMA_BURST_4 0x2
60#define LCD_DMA_BURST_8 0x3
61#define LCD_DMA_BURST_16 0x4
c6daf05b
MP
62#define LCD_V1_END_OF_FRAME_INT_ENA BIT(2)
63#define LCD_V2_END_OF_FRAME0_INT_ENA BIT(8)
64#define LCD_V2_END_OF_FRAME1_INT_ENA BIT(9)
4ed824d9
SR
65#define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
66
67/* LCD Control Register */
68#define LCD_CLK_DIVISOR(x) ((x) << 8)
69#define LCD_RASTER_MODE 0x01
70
71/* LCD Raster Control Register */
72#define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
73#define PALETTE_AND_DATA 0x00
74#define PALETTE_ONLY 0x01
1f9c3e1f 75#define DATA_ONLY 0x02
4ed824d9
SR
76
77#define LCD_MONO_8BIT_MODE BIT(9)
78#define LCD_RASTER_ORDER BIT(8)
79#define LCD_TFT_MODE BIT(7)
c6daf05b
MP
80#define LCD_V1_UNDERFLOW_INT_ENA BIT(6)
81#define LCD_V2_UNDERFLOW_INT_ENA BIT(5)
82#define LCD_V1_PL_INT_ENA BIT(4)
83#define LCD_V2_PL_INT_ENA BIT(6)
4ed824d9
SR
84#define LCD_MONOCHROME_MODE BIT(1)
85#define LCD_RASTER_ENABLE BIT(0)
86#define LCD_TFT_ALT_ENABLE BIT(23)
87#define LCD_STN_565_ENABLE BIT(24)
c6daf05b
MP
88#define LCD_V2_DMA_CLK_EN BIT(2)
89#define LCD_V2_LIDD_CLK_EN BIT(1)
90#define LCD_V2_CORE_CLK_EN BIT(0)
91#define LCD_V2_LPP_B10 26
1a2b750c
MP
92#define LCD_V2_TFT_24BPP_MODE BIT(25)
93#define LCD_V2_TFT_24BPP_UNPACK BIT(26)
4ed824d9
SR
94
95/* LCD Raster Timing 2 Register */
96#define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
97#define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
98#define LCD_SYNC_CTRL BIT(25)
99#define LCD_SYNC_EDGE BIT(24)
100#define LCD_INVERT_PIXEL_CLOCK BIT(22)
101#define LCD_INVERT_LINE_CLOCK BIT(21)
102#define LCD_INVERT_FRAME_CLOCK BIT(20)
103
104/* LCD Block */
c6daf05b 105#define LCD_PID_REG 0x0
4ed824d9
SR
106#define LCD_CTRL_REG 0x4
107#define LCD_STAT_REG 0x8
108#define LCD_RASTER_CTRL_REG 0x28
109#define LCD_RASTER_TIMING_0_REG 0x2C
110#define LCD_RASTER_TIMING_1_REG 0x30
111#define LCD_RASTER_TIMING_2_REG 0x34
112#define LCD_DMA_CTRL_REG 0x40
113#define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
114#define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
1f9c3e1f
MA
115#define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
116#define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
117
c6daf05b
MP
118/* Interrupt Registers available only in Version 2 */
119#define LCD_RAW_STAT_REG 0x58
120#define LCD_MASKED_STAT_REG 0x5c
121#define LCD_INT_ENABLE_SET_REG 0x60
122#define LCD_INT_ENABLE_CLR_REG 0x64
123#define LCD_END_OF_INT_IND_REG 0x68
124
125/* Clock registers available only on Version 2 */
126#define LCD_CLK_ENABLE_REG 0x6c
127#define LCD_CLK_RESET_REG 0x70
74a0efde 128#define LCD_CLK_MAIN_RESET BIT(3)
c6daf05b 129
1f9c3e1f 130#define LCD_NUM_BUFFERS 2
4ed824d9
SR
131
132#define WSI_TIMEOUT 50
133#define PALETTE_SIZE 256
4ed824d9 134
34aef6eb 135static void __iomem *da8xx_fb_reg_base;
4ed824d9 136static struct resource *lcdc_regs;
c6daf05b
MP
137static unsigned int lcd_revision;
138static irq_handler_t lcdc_irq_handler;
a481b37a
MP
139static wait_queue_head_t frame_done_wq;
140static int frame_done_flag;
4ed824d9
SR
141
142static inline unsigned int lcdc_read(unsigned int addr)
143{
144 return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
145}
146
147static inline void lcdc_write(unsigned int val, unsigned int addr)
148{
149 __raw_writel(val, da8xx_fb_reg_base + (addr));
150}
151
152struct da8xx_fb_par {
dbe8e48a 153 struct device *dev;
4ed824d9
SR
154 resource_size_t p_palette_base;
155 unsigned char *v_palette_base;
1f9c3e1f
MA
156 dma_addr_t vram_phys;
157 unsigned long vram_size;
158 void *vram_virt;
159 unsigned int dma_start;
160 unsigned int dma_end;
4ed824d9
SR
161 struct clk *lcdc_clk;
162 int irq;
4ed824d9 163 unsigned int palette_sz;
36113804 164 int blank;
1f9c3e1f
MA
165 wait_queue_head_t vsync_wait;
166 int vsync_flag;
167 int vsync_timeout;
deb95c6c
MP
168 spinlock_t lock_for_chan_update;
169
170 /*
171 * LCDC has 2 ping pong DMA channels, channel 0
172 * and channel 1.
173 */
174 unsigned int which_dma_channel_done;
e04e5483
C
175#ifdef CONFIG_CPU_FREQ
176 struct notifier_block freq_transition;
177#endif
44f627ae 178 unsigned int lcd_fck_rate;
36113804 179 void (*panel_power_ctrl)(int);
1a2b750c 180 u32 pseudo_palette[16];
b6dbe8e4
AM
181 struct fb_videomode mode;
182 struct lcd_ctrl_config cfg;
4ed824d9
SR
183};
184
be0f6dbc 185static struct fb_var_screeninfo da8xx_fb_var;
4ed824d9 186
48c68c4f 187static struct fb_fix_screeninfo da8xx_fb_fix = {
4ed824d9
SR
188 .id = "DA8xx FB Drv",
189 .type = FB_TYPE_PACKED_PIXELS,
190 .type_aux = 0,
191 .visual = FB_VISUAL_PSEUDOCOLOR,
1f9c3e1f 192 .xpanstep = 0,
4ed824d9 193 .ypanstep = 1,
1f9c3e1f 194 .ywrapstep = 0,
4ed824d9
SR
195 .accel = FB_ACCEL_NONE
196};
197
f772fabd 198static struct fb_videomode known_lcd_panels[] = {
4ed824d9
SR
199 /* Sharp LCD035Q3DG01 */
200 [0] = {
f772fabd
MP
201 .name = "Sharp_LCD035Q3DG01",
202 .xres = 320,
203 .yres = 240,
a6a799f8 204 .pixclock = KHZ2PICOS(4607),
f772fabd
MP
205 .left_margin = 6,
206 .right_margin = 8,
207 .upper_margin = 2,
208 .lower_margin = 2,
209 .hsync_len = 0,
210 .vsync_len = 0,
3b43ad20
MP
211 .sync = FB_SYNC_CLK_INVERT |
212 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
4ed824d9
SR
213 },
214 /* Sharp LK043T1DG01 */
215 [1] = {
f772fabd
MP
216 .name = "Sharp_LK043T1DG01",
217 .xres = 480,
218 .yres = 272,
a6a799f8 219 .pixclock = KHZ2PICOS(7833),
f772fabd
MP
220 .left_margin = 2,
221 .right_margin = 2,
222 .upper_margin = 2,
223 .lower_margin = 2,
224 .hsync_len = 41,
225 .vsync_len = 10,
3b43ad20 226 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
f772fabd 227 .flag = 0,
4ed824d9 228 },
f413070e
AG
229 [2] = {
230 /* Hitachi SP10Q010 */
f772fabd
MP
231 .name = "SP10Q010",
232 .xres = 320,
233 .yres = 240,
a6a799f8 234 .pixclock = KHZ2PICOS(7833),
f772fabd
MP
235 .left_margin = 10,
236 .right_margin = 10,
237 .upper_margin = 10,
238 .lower_margin = 10,
239 .hsync_len = 10,
240 .vsync_len = 10,
3b43ad20 241 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
f772fabd 242 .flag = 0,
f413070e 243 },
4ed824d9
SR
244};
245
fe8c98f0
DE
246static inline bool da8xx_fb_is_raster_enabled(void)
247{
248 return !!(lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE);
249}
250
36113804
C
251/* Enable the Raster Engine of the LCD Controller */
252static inline void lcd_enable_raster(void)
253{
254 u32 reg;
255
92b4e450
MP
256 /* Put LCDC in reset for several cycles */
257 if (lcd_revision == LCD_VERSION_2)
258 /* Write 1 to reset LCDC */
259 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
260 mdelay(1);
261
74a0efde
MP
262 /* Bring LCDC out of reset */
263 if (lcd_revision == LCD_VERSION_2)
264 lcdc_write(0, LCD_CLK_RESET_REG);
92b4e450 265 mdelay(1);
74a0efde 266
92b4e450 267 /* Above reset sequence doesnot reset register context */
36113804
C
268 reg = lcdc_read(LCD_RASTER_CTRL_REG);
269 if (!(reg & LCD_RASTER_ENABLE))
270 lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
271}
272
4ed824d9 273/* Disable the Raster Engine of the LCD Controller */
26e71645
DE
274static inline void lcd_disable_raster(enum da8xx_frame_complete
275 wait_for_frame_done)
4ed824d9 276{
4ed824d9 277 u32 reg;
a481b37a 278 int ret;
4ed824d9
SR
279
280 reg = lcdc_read(LCD_RASTER_CTRL_REG);
2f93e8f4 281 if (reg & LCD_RASTER_ENABLE)
4ed824d9 282 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
a481b37a
MP
283 else
284 /* return if already disabled */
285 return;
286
26e71645
DE
287 if ((wait_for_frame_done == DA8XX_FRAME_WAIT) &&
288 (lcd_revision == LCD_VERSION_2)) {
a481b37a
MP
289 frame_done_flag = 0;
290 ret = wait_event_interruptible_timeout(frame_done_wq,
291 frame_done_flag != 0,
292 msecs_to_jiffies(50));
293 if (ret == 0)
294 pr_err("LCD Controller timed out\n");
295 }
4ed824d9
SR
296}
297
298static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
299{
1f9c3e1f
MA
300 u32 start;
301 u32 end;
302 u32 reg_ras;
303 u32 reg_dma;
c6daf05b 304 u32 reg_int;
1f9c3e1f
MA
305
306 /* init reg to clear PLM (loading mode) fields */
307 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
308 reg_ras &= ~(3 << 20);
309
310 reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
311
312 if (load_mode == LOAD_DATA) {
313 start = par->dma_start;
314 end = par->dma_end;
315
316 reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
c6daf05b
MP
317 if (lcd_revision == LCD_VERSION_1) {
318 reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
319 } else {
320 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
321 LCD_V2_END_OF_FRAME0_INT_ENA |
a481b37a
MP
322 LCD_V2_END_OF_FRAME1_INT_ENA |
323 LCD_FRAME_DONE;
c6daf05b
MP
324 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
325 }
1f9c3e1f
MA
326 reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
327
328 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
329 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
330 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
331 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
332 } else if (load_mode == LOAD_PALETTE) {
333 start = par->p_palette_base;
334 end = start + par->palette_sz - 1;
335
336 reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
c6daf05b
MP
337
338 if (lcd_revision == LCD_VERSION_1) {
339 reg_ras |= LCD_V1_PL_INT_ENA;
340 } else {
341 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
342 LCD_V2_PL_INT_ENA;
343 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
344 }
1f9c3e1f
MA
345
346 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
347 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
348 }
4ed824d9 349
1f9c3e1f
MA
350 lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
351 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
4ed824d9 352
1f9c3e1f
MA
353 /*
354 * The Raster enable bit must be set after all other control fields are
355 * set.
356 */
357 lcd_enable_raster();
4ed824d9
SR
358}
359
fb8fa943
MP
360/* Configure the Burst Size and fifo threhold of DMA */
361static int lcd_cfg_dma(int burst_size, int fifo_th)
4ed824d9
SR
362{
363 u32 reg;
364
365 reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
366 switch (burst_size) {
367 case 1:
368 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
369 break;
370 case 2:
371 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
372 break;
373 case 4:
374 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
375 break;
376 case 8:
377 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
378 break;
379 case 16:
3b43ad20 380 default:
4ed824d9
SR
381 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
382 break;
4ed824d9 383 }
fb8fa943
MP
384
385 reg |= (fifo_th << 8);
386
2f93e8f4 387 lcdc_write(reg, LCD_DMA_CTRL_REG);
4ed824d9
SR
388
389 return 0;
390}
391
392static void lcd_cfg_ac_bias(int period, int transitions_per_int)
393{
394 u32 reg;
395
396 /* Set the AC Bias Period and Number of Transisitons per Interrupt */
397 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
398 reg |= LCD_AC_BIAS_FREQUENCY(period) |
399 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
400 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
401}
402
403static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
404 int front_porch)
405{
406 u32 reg;
407
408 reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
409 reg |= ((back_porch & 0xff) << 24)
410 | ((front_porch & 0xff) << 16)
411 | ((pulse_width & 0x3f) << 10);
412 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
413}
414
415static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
416 int front_porch)
417{
418 u32 reg;
419
420 reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
421 reg |= ((back_porch & 0xff) << 24)
422 | ((front_porch & 0xff) << 16)
423 | ((pulse_width & 0x3f) << 10);
424 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
425}
426
3b43ad20
MP
427static int lcd_cfg_display(const struct lcd_ctrl_config *cfg,
428 struct fb_videomode *panel)
4ed824d9
SR
429{
430 u32 reg;
c6daf05b 431 u32 reg_int;
4ed824d9
SR
432
433 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
434 LCD_MONO_8BIT_MODE |
435 LCD_MONOCHROME_MODE);
436
3b43ad20 437 switch (cfg->panel_shade) {
4ed824d9
SR
438 case MONOCHROME:
439 reg |= LCD_MONOCHROME_MODE;
440 if (cfg->mono_8bit_mode)
441 reg |= LCD_MONO_8BIT_MODE;
442 break;
443 case COLOR_ACTIVE:
444 reg |= LCD_TFT_MODE;
445 if (cfg->tft_alt_mode)
446 reg |= LCD_TFT_ALT_ENABLE;
447 break;
448
449 case COLOR_PASSIVE:
3b43ad20
MP
450 /* AC bias applicable only for Pasive panels */
451 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
452 if (cfg->bpp == 12 && cfg->stn_565_mode)
4ed824d9
SR
453 reg |= LCD_STN_565_ENABLE;
454 break;
455
456 default:
457 return -EINVAL;
458 }
459
460 /* enable additional interrupts here */
c6daf05b
MP
461 if (lcd_revision == LCD_VERSION_1) {
462 reg |= LCD_V1_UNDERFLOW_INT_ENA;
463 } else {
464 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
465 LCD_V2_UNDERFLOW_INT_ENA;
466 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
467 }
4ed824d9
SR
468
469 lcdc_write(reg, LCD_RASTER_CTRL_REG);
470
471 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
472
3b43ad20 473 reg |= LCD_SYNC_CTRL;
4ed824d9
SR
474
475 if (cfg->sync_edge)
476 reg |= LCD_SYNC_EDGE;
477 else
478 reg &= ~LCD_SYNC_EDGE;
479
3b43ad20 480 if (panel->sync & FB_SYNC_HOR_HIGH_ACT)
4ed824d9
SR
481 reg |= LCD_INVERT_LINE_CLOCK;
482 else
483 reg &= ~LCD_INVERT_LINE_CLOCK;
484
3b43ad20 485 if (panel->sync & FB_SYNC_VERT_HIGH_ACT)
4ed824d9
SR
486 reg |= LCD_INVERT_FRAME_CLOCK;
487 else
488 reg &= ~LCD_INVERT_FRAME_CLOCK;
489
490 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
491
492 return 0;
493}
494
495static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
496 u32 bpp, u32 raster_order)
497{
1f9c3e1f 498 u32 reg;
4ed824d9 499
1a2b750c
MP
500 if (bpp > 16 && lcd_revision == LCD_VERSION_1)
501 return -EINVAL;
502
4ed824d9
SR
503 /* Set the Panel Width */
504 /* Pixels per line = (PPL + 1)*16 */
4d740801
MP
505 if (lcd_revision == LCD_VERSION_1) {
506 /*
507 * 0x3F in bits 4..9 gives max horizontal resolution = 1024
508 * pixels.
509 */
510 width &= 0x3f0;
511 } else {
512 /*
513 * 0x7F in bits 4..10 gives max horizontal resolution = 2048
514 * pixels.
515 */
516 width &= 0x7f0;
517 }
518
4ed824d9
SR
519 reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
520 reg &= 0xfffffc00;
4d740801
MP
521 if (lcd_revision == LCD_VERSION_1) {
522 reg |= ((width >> 4) - 1) << 4;
523 } else {
524 width = (width >> 4) - 1;
525 reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
526 }
4ed824d9
SR
527 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
528
529 /* Set the Panel Height */
4d740801 530 /* Set bits 9:0 of Lines Per Pixel */
4ed824d9
SR
531 reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
532 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
533 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
534
4d740801
MP
535 /* Set bit 10 of Lines Per Pixel */
536 if (lcd_revision == LCD_VERSION_2) {
537 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
538 reg |= ((height - 1) & 0x400) << 16;
539 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
540 }
541
4ed824d9
SR
542 /* Set the Raster Order of the Frame Buffer */
543 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
544 if (raster_order)
545 reg |= LCD_RASTER_ORDER;
1a2b750c
MP
546
547 par->palette_sz = 16 * 2;
4ed824d9
SR
548
549 switch (bpp) {
550 case 1:
551 case 2:
552 case 4:
553 case 16:
1a2b750c
MP
554 break;
555 case 24:
556 reg |= LCD_V2_TFT_24BPP_MODE;
fa8a00cc 557 break;
1a2b750c 558 case 32:
fa8a00cc 559 reg |= LCD_V2_TFT_24BPP_MODE;
1a2b750c 560 reg |= LCD_V2_TFT_24BPP_UNPACK;
4ed824d9 561 break;
4ed824d9
SR
562 case 8:
563 par->palette_sz = 256 * 2;
564 break;
565
566 default:
567 return -EINVAL;
568 }
569
1a2b750c
MP
570 lcdc_write(reg, LCD_RASTER_CTRL_REG);
571
4ed824d9
SR
572 return 0;
573}
574
1a2b750c 575#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
4ed824d9
SR
576static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
577 unsigned blue, unsigned transp,
578 struct fb_info *info)
579{
580 struct da8xx_fb_par *par = info->par;
1f9c3e1f 581 unsigned short *palette = (unsigned short *) par->v_palette_base;
4ed824d9 582 u_short pal;
1f9c3e1f 583 int update_hw = 0;
4ed824d9
SR
584
585 if (regno > 255)
586 return 1;
587
588 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
589 return 1;
590
1a2b750c
MP
591 if (info->var.bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
592 return -EINVAL;
f413070e 593
1a2b750c
MP
594 switch (info->fix.visual) {
595 case FB_VISUAL_TRUECOLOR:
596 red = CNVT_TOHW(red, info->var.red.length);
597 green = CNVT_TOHW(green, info->var.green.length);
598 blue = CNVT_TOHW(blue, info->var.blue.length);
599 break;
600 case FB_VISUAL_PSEUDOCOLOR:
601 switch (info->var.bits_per_pixel) {
602 case 4:
603 if (regno > 15)
604 return -EINVAL;
605
606 if (info->var.grayscale) {
607 pal = regno;
608 } else {
609 red >>= 4;
610 green >>= 8;
611 blue >>= 12;
612
613 pal = red & 0x0f00;
614 pal |= green & 0x00f0;
615 pal |= blue & 0x000f;
616 }
617 if (regno == 0)
618 pal |= 0x2000;
619 palette[regno] = pal;
620 break;
621
622 case 8:
f413070e
AG
623 red >>= 4;
624 green >>= 8;
625 blue >>= 12;
626
627 pal = (red & 0x0f00);
628 pal |= (green & 0x00f0);
629 pal |= (blue & 0x000f);
4ed824d9 630
1a2b750c
MP
631 if (palette[regno] != pal) {
632 update_hw = 1;
633 palette[regno] = pal;
634 }
635 break;
1f9c3e1f 636 }
1a2b750c
MP
637 break;
638 }
4ed824d9 639
1a2b750c
MP
640 /* Truecolor has hardware independent palette */
641 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
642 u32 v;
4ed824d9 643
1a2b750c
MP
644 if (regno > 15)
645 return -EINVAL;
4ed824d9 646
1a2b750c
MP
647 v = (red << info->var.red.offset) |
648 (green << info->var.green.offset) |
649 (blue << info->var.blue.offset);
4ed824d9 650
1a2b750c
MP
651 switch (info->var.bits_per_pixel) {
652 case 16:
653 ((u16 *) (info->pseudo_palette))[regno] = v;
654 break;
655 case 24:
656 case 32:
657 ((u32 *) (info->pseudo_palette))[regno] = v;
658 break;
659 }
1f9c3e1f
MA
660 if (palette[0] != 0x4000) {
661 update_hw = 1;
662 palette[0] = 0x4000;
663 }
4ed824d9
SR
664 }
665
1f9c3e1f
MA
666 /* Update the palette in the h/w as needed. */
667 if (update_hw)
668 lcd_blit(LOAD_PALETTE, par);
669
4ed824d9
SR
670 return 0;
671}
1a2b750c 672#undef CNVT_TOHW
4ed824d9 673
39c87d45 674static void da8xx_fb_lcd_reset(void)
4ed824d9 675{
4ed824d9
SR
676 /* DMA has to be disabled */
677 lcdc_write(0, LCD_DMA_CTRL_REG);
678 lcdc_write(0, LCD_RASTER_CTRL_REG);
c6daf05b 679
74a0efde 680 if (lcd_revision == LCD_VERSION_2) {
c6daf05b 681 lcdc_write(0, LCD_INT_ENABLE_SET_REG);
74a0efde
MP
682 /* Write 1 to reset */
683 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
684 lcdc_write(0, LCD_CLK_RESET_REG);
685 }
4ed824d9
SR
686}
687
a6a799f8
DE
688static inline unsigned da8xx_fb_calc_clk_divider(struct da8xx_fb_par *par,
689 unsigned pixclock)
8097b174 690{
a6a799f8
DE
691 return par->lcd_fck_rate / (PICOS2KHZ(pixclock) * 1000);
692}
8097b174 693
404fdfe7
AM
694static inline unsigned da8xx_fb_round_clk(struct da8xx_fb_par *par,
695 unsigned pixclock)
696{
697 unsigned div;
698
699 div = da8xx_fb_calc_clk_divider(par, pixclock);
700 return KHZ2PICOS(par->lcd_fck_rate / (1000 * div));
701}
702
a6a799f8
DE
703static inline void da8xx_fb_config_clk_divider(unsigned div)
704{
8097b174
C
705 /* Configure the LCD clock divisor. */
706 lcdc_write(LCD_CLK_DIVISOR(div) |
707 (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
c6daf05b
MP
708
709 if (lcd_revision == LCD_VERSION_2)
710 lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
711 LCD_V2_CORE_CLK_EN, LCD_CLK_ENABLE_REG);
a6a799f8
DE
712}
713
714static inline void da8xx_fb_calc_config_clk_divider(struct da8xx_fb_par *par,
715 struct fb_videomode *mode)
716{
717 unsigned div = da8xx_fb_calc_clk_divider(par, mode->pixclock);
c6daf05b 718
a6a799f8 719 da8xx_fb_config_clk_divider(div);
8097b174
C
720}
721
4ed824d9 722static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
f772fabd 723 struct fb_videomode *panel)
4ed824d9
SR
724{
725 u32 bpp;
726 int ret = 0;
727
a6a799f8 728 da8xx_fb_calc_config_clk_divider(par, panel);
4ed824d9 729
f772fabd 730 if (panel->sync & FB_SYNC_CLK_INVERT)
2f93e8f4
SR
731 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
732 LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
733 else
734 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
735 ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
736
fb8fa943
MP
737 /* Configure the DMA burst size and fifo threshold. */
738 ret = lcd_cfg_dma(cfg->dma_burst_sz, cfg->fifo_th);
4ed824d9
SR
739 if (ret < 0)
740 return ret;
741
4ed824d9 742 /* Configure the vertical and horizontal sync properties. */
f772fabd
MP
743 lcd_cfg_vertical_sync(panel->lower_margin, panel->vsync_len,
744 panel->upper_margin);
745 lcd_cfg_horizontal_sync(panel->right_margin, panel->hsync_len,
746 panel->left_margin);
4ed824d9
SR
747
748 /* Configure for disply */
3b43ad20 749 ret = lcd_cfg_display(cfg, panel);
4ed824d9
SR
750 if (ret < 0)
751 return ret;
752
3b43ad20 753 bpp = cfg->bpp;
4ed824d9 754
4ed824d9
SR
755 if (bpp == 12)
756 bpp = 16;
f772fabd
MP
757 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->xres,
758 (unsigned int)panel->yres, bpp,
4ed824d9
SR
759 cfg->raster_order);
760 if (ret < 0)
761 return ret;
762
763 /* Configure FDD */
764 lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
765 (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
766
767 return 0;
768}
769
c6daf05b
MP
770/* IRQ handler for version 2 of LCDC */
771static irqreturn_t lcdc_irq_handler_rev02(int irq, void *arg)
772{
773 struct da8xx_fb_par *par = arg;
774 u32 stat = lcdc_read(LCD_MASKED_STAT_REG);
c6daf05b
MP
775
776 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
26e71645 777 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
c6daf05b
MP
778 lcdc_write(stat, LCD_MASKED_STAT_REG);
779 lcd_enable_raster();
780 } else if (stat & LCD_PL_LOAD_DONE) {
781 /*
782 * Must disable raster before changing state of any control bit.
783 * And also must be disabled before clearing the PL loading
784 * interrupt via the following write to the status register. If
785 * this is done after then one gets multiple PL done interrupts.
786 */
26e71645 787 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
c6daf05b
MP
788
789 lcdc_write(stat, LCD_MASKED_STAT_REG);
790
8a81dccd
MP
791 /* Disable PL completion interrupt */
792 lcdc_write(LCD_V2_PL_INT_ENA, LCD_INT_ENABLE_CLR_REG);
c6daf05b
MP
793
794 /* Setup and start data loading mode */
795 lcd_blit(LOAD_DATA, par);
796 } else {
797 lcdc_write(stat, LCD_MASKED_STAT_REG);
798
799 if (stat & LCD_END_OF_FRAME0) {
deb95c6c 800 par->which_dma_channel_done = 0;
c6daf05b
MP
801 lcdc_write(par->dma_start,
802 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
803 lcdc_write(par->dma_end,
804 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
805 par->vsync_flag = 1;
806 wake_up_interruptible(&par->vsync_wait);
807 }
808
809 if (stat & LCD_END_OF_FRAME1) {
deb95c6c 810 par->which_dma_channel_done = 1;
c6daf05b
MP
811 lcdc_write(par->dma_start,
812 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
813 lcdc_write(par->dma_end,
814 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
815 par->vsync_flag = 1;
816 wake_up_interruptible(&par->vsync_wait);
817 }
a481b37a
MP
818
819 /* Set only when controller is disabled and at the end of
820 * active frame
821 */
822 if (stat & BIT(0)) {
823 frame_done_flag = 1;
824 wake_up_interruptible(&frame_done_wq);
825 }
c6daf05b
MP
826 }
827
828 lcdc_write(0, LCD_END_OF_INT_IND_REG);
829 return IRQ_HANDLED;
830}
831
832/* IRQ handler for version 1 LCDC */
833static irqreturn_t lcdc_irq_handler_rev01(int irq, void *arg)
4ed824d9 834{
1f9c3e1f 835 struct da8xx_fb_par *par = arg;
4ed824d9 836 u32 stat = lcdc_read(LCD_STAT_REG);
1f9c3e1f 837 u32 reg_ras;
4ed824d9
SR
838
839 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
26e71645 840 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
4ed824d9 841 lcdc_write(stat, LCD_STAT_REG);
36113804 842 lcd_enable_raster();
1f9c3e1f
MA
843 } else if (stat & LCD_PL_LOAD_DONE) {
844 /*
845 * Must disable raster before changing state of any control bit.
846 * And also must be disabled before clearing the PL loading
847 * interrupt via the following write to the status register. If
848 * this is done after then one gets multiple PL done interrupts.
849 */
26e71645 850 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
1f9c3e1f 851
4ed824d9
SR
852 lcdc_write(stat, LCD_STAT_REG);
853
1f9c3e1f
MA
854 /* Disable PL completion inerrupt */
855 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
c6daf05b 856 reg_ras &= ~LCD_V1_PL_INT_ENA;
1f9c3e1f
MA
857 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
858
859 /* Setup and start data loading mode */
860 lcd_blit(LOAD_DATA, par);
861 } else {
862 lcdc_write(stat, LCD_STAT_REG);
863
864 if (stat & LCD_END_OF_FRAME0) {
deb95c6c 865 par->which_dma_channel_done = 0;
1f9c3e1f
MA
866 lcdc_write(par->dma_start,
867 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
868 lcdc_write(par->dma_end,
869 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
870 par->vsync_flag = 1;
871 wake_up_interruptible(&par->vsync_wait);
872 }
873
874 if (stat & LCD_END_OF_FRAME1) {
deb95c6c 875 par->which_dma_channel_done = 1;
1f9c3e1f
MA
876 lcdc_write(par->dma_start,
877 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
878 lcdc_write(par->dma_end,
879 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
880 par->vsync_flag = 1;
881 wake_up_interruptible(&par->vsync_wait);
882 }
883 }
884
4ed824d9
SR
885 return IRQ_HANDLED;
886}
887
888static int fb_check_var(struct fb_var_screeninfo *var,
889 struct fb_info *info)
890{
891 int err = 0;
87dac71d
AM
892 struct da8xx_fb_par *par = info->par;
893 int bpp = var->bits_per_pixel >> 3;
894 unsigned long line_size = var->xres_virtual * bpp;
4ed824d9 895
1a2b750c
MP
896 if (var->bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
897 return -EINVAL;
898
4ed824d9
SR
899 switch (var->bits_per_pixel) {
900 case 1:
901 case 8:
902 var->red.offset = 0;
903 var->red.length = 8;
904 var->green.offset = 0;
905 var->green.length = 8;
906 var->blue.offset = 0;
907 var->blue.length = 8;
908 var->transp.offset = 0;
909 var->transp.length = 0;
f413070e 910 var->nonstd = 0;
4ed824d9
SR
911 break;
912 case 4:
913 var->red.offset = 0;
914 var->red.length = 4;
915 var->green.offset = 0;
916 var->green.length = 4;
917 var->blue.offset = 0;
918 var->blue.length = 4;
919 var->transp.offset = 0;
920 var->transp.length = 0;
f413070e 921 var->nonstd = FB_NONSTD_REV_PIX_IN_B;
4ed824d9
SR
922 break;
923 case 16: /* RGB 565 */
3510b8f7 924 var->red.offset = 11;
4ed824d9
SR
925 var->red.length = 5;
926 var->green.offset = 5;
927 var->green.length = 6;
3510b8f7 928 var->blue.offset = 0;
4ed824d9
SR
929 var->blue.length = 5;
930 var->transp.offset = 0;
931 var->transp.length = 0;
f413070e 932 var->nonstd = 0;
4ed824d9 933 break;
1a2b750c
MP
934 case 24:
935 var->red.offset = 16;
936 var->red.length = 8;
937 var->green.offset = 8;
938 var->green.length = 8;
939 var->blue.offset = 0;
940 var->blue.length = 8;
941 var->nonstd = 0;
942 break;
943 case 32:
944 var->transp.offset = 24;
945 var->transp.length = 8;
946 var->red.offset = 16;
947 var->red.length = 8;
948 var->green.offset = 8;
949 var->green.length = 8;
950 var->blue.offset = 0;
951 var->blue.length = 8;
952 var->nonstd = 0;
953 break;
4ed824d9
SR
954 default:
955 err = -EINVAL;
956 }
957
958 var->red.msb_right = 0;
959 var->green.msb_right = 0;
960 var->blue.msb_right = 0;
961 var->transp.msb_right = 0;
87dac71d
AM
962
963 if (line_size * var->yres_virtual > par->vram_size)
964 var->yres_virtual = par->vram_size / line_size;
965
966 if (var->yres > var->yres_virtual)
967 var->yres = var->yres_virtual;
968
969 if (var->xres > var->xres_virtual)
970 var->xres = var->xres_virtual;
971
972 if (var->xres + var->xoffset > var->xres_virtual)
973 var->xoffset = var->xres_virtual - var->xres;
974 if (var->yres + var->yoffset > var->yres_virtual)
975 var->yoffset = var->yres_virtual - var->yres;
976
404fdfe7
AM
977 var->pixclock = da8xx_fb_round_clk(par, var->pixclock);
978
4ed824d9
SR
979 return err;
980}
981
e04e5483
C
982#ifdef CONFIG_CPU_FREQ
983static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
984 unsigned long val, void *data)
985{
986 struct da8xx_fb_par *par;
e04e5483
C
987
988 par = container_of(nb, struct da8xx_fb_par, freq_transition);
f820917a
MP
989 if (val == CPUFREQ_POSTCHANGE) {
990 if (par->lcd_fck_rate != clk_get_rate(par->lcdc_clk)) {
991 par->lcd_fck_rate = clk_get_rate(par->lcdc_clk);
26e71645 992 lcd_disable_raster(DA8XX_FRAME_WAIT);
a6a799f8 993 da8xx_fb_calc_config_clk_divider(par, &par->mode);
67900814
MP
994 if (par->blank == FB_BLANK_UNBLANK)
995 lcd_enable_raster();
f820917a 996 }
e04e5483
C
997 }
998
999 return 0;
1000}
1001
1002static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
1003{
1004 par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
1005
1006 return cpufreq_register_notifier(&par->freq_transition,
1007 CPUFREQ_TRANSITION_NOTIFIER);
1008}
1009
1010static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
1011{
1012 cpufreq_unregister_notifier(&par->freq_transition,
1013 CPUFREQ_TRANSITION_NOTIFIER);
1014}
1015#endif
1016
48c68c4f 1017static int fb_remove(struct platform_device *dev)
4ed824d9
SR
1018{
1019 struct fb_info *info = dev_get_drvdata(&dev->dev);
4ed824d9
SR
1020
1021 if (info) {
1022 struct da8xx_fb_par *par = info->par;
1023
e04e5483
C
1024#ifdef CONFIG_CPU_FREQ
1025 lcd_da8xx_cpufreq_deregister(par);
1026#endif
36113804
C
1027 if (par->panel_power_ctrl)
1028 par->panel_power_ctrl(0);
1029
26e71645 1030 lcd_disable_raster(DA8XX_FRAME_WAIT);
4ed824d9
SR
1031 lcdc_write(0, LCD_RASTER_CTRL_REG);
1032
1033 /* disable DMA */
1034 lcdc_write(0, LCD_DMA_CTRL_REG);
1035
1036 unregister_framebuffer(info);
1037 fb_dealloc_cmap(&info->cmap);
1f9c3e1f
MA
1038 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
1039 par->p_palette_base);
1040 dma_free_coherent(NULL, par->vram_size, par->vram_virt,
1041 par->vram_phys);
4ed824d9 1042 free_irq(par->irq, par);
9dd44d5d
MP
1043 pm_runtime_put_sync(&dev->dev);
1044 pm_runtime_disable(&dev->dev);
4ed824d9 1045 framebuffer_release(info);
34aef6eb 1046 iounmap(da8xx_fb_reg_base);
4ed824d9
SR
1047 release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
1048
1049 }
2f93e8f4 1050 return 0;
4ed824d9
SR
1051}
1052
1f9c3e1f
MA
1053/*
1054 * Function to wait for vertical sync which for this LCD peripheral
1055 * translates into waiting for the current raster frame to complete.
1056 */
1057static int fb_wait_for_vsync(struct fb_info *info)
1058{
1059 struct da8xx_fb_par *par = info->par;
1060 int ret;
1061
1062 /*
1063 * Set flag to 0 and wait for isr to set to 1. It would seem there is a
25985edc 1064 * race condition here where the ISR could have occurred just before or
1f9c3e1f
MA
1065 * just after this set. But since we are just coarsely waiting for
1066 * a frame to complete then that's OK. i.e. if the frame completed
1067 * just before this code executed then we have to wait another full
1068 * frame time but there is no way to avoid such a situation. On the
1069 * other hand if the frame completed just after then we don't need
1070 * to wait long at all. Either way we are guaranteed to return to the
1071 * user immediately after a frame completion which is all that is
1072 * required.
1073 */
1074 par->vsync_flag = 0;
1075 ret = wait_event_interruptible_timeout(par->vsync_wait,
1076 par->vsync_flag != 0,
1077 par->vsync_timeout);
1078 if (ret < 0)
1079 return ret;
1080 if (ret == 0)
1081 return -ETIMEDOUT;
1082
1083 return 0;
1084}
1085
4ed824d9
SR
1086static int fb_ioctl(struct fb_info *info, unsigned int cmd,
1087 unsigned long arg)
1088{
1089 struct lcd_sync_arg sync_arg;
1090
1091 switch (cmd) {
1092 case FBIOGET_CONTRAST:
1093 case FBIOPUT_CONTRAST:
1094 case FBIGET_BRIGHTNESS:
1095 case FBIPUT_BRIGHTNESS:
1096 case FBIGET_COLOR:
1097 case FBIPUT_COLOR:
2f93e8f4 1098 return -ENOTTY;
4ed824d9
SR
1099 case FBIPUT_HSYNC:
1100 if (copy_from_user(&sync_arg, (char *)arg,
1101 sizeof(struct lcd_sync_arg)))
2f93e8f4 1102 return -EFAULT;
4ed824d9
SR
1103 lcd_cfg_horizontal_sync(sync_arg.back_porch,
1104 sync_arg.pulse_width,
1105 sync_arg.front_porch);
1106 break;
1107 case FBIPUT_VSYNC:
1108 if (copy_from_user(&sync_arg, (char *)arg,
1109 sizeof(struct lcd_sync_arg)))
2f93e8f4 1110 return -EFAULT;
4ed824d9
SR
1111 lcd_cfg_vertical_sync(sync_arg.back_porch,
1112 sync_arg.pulse_width,
1113 sync_arg.front_porch);
1114 break;
1f9c3e1f
MA
1115 case FBIO_WAITFORVSYNC:
1116 return fb_wait_for_vsync(info);
4ed824d9
SR
1117 default:
1118 return -EINVAL;
1119 }
1120 return 0;
1121}
1122
312d9715
C
1123static int cfb_blank(int blank, struct fb_info *info)
1124{
1125 struct da8xx_fb_par *par = info->par;
1126 int ret = 0;
1127
1128 if (par->blank == blank)
1129 return 0;
1130
1131 par->blank = blank;
1132 switch (blank) {
1133 case FB_BLANK_UNBLANK:
f7c848b6
MP
1134 lcd_enable_raster();
1135
312d9715
C
1136 if (par->panel_power_ctrl)
1137 par->panel_power_ctrl(1);
312d9715 1138 break;
99a647d1
YY
1139 case FB_BLANK_NORMAL:
1140 case FB_BLANK_VSYNC_SUSPEND:
1141 case FB_BLANK_HSYNC_SUSPEND:
312d9715
C
1142 case FB_BLANK_POWERDOWN:
1143 if (par->panel_power_ctrl)
1144 par->panel_power_ctrl(0);
1145
26e71645 1146 lcd_disable_raster(DA8XX_FRAME_WAIT);
312d9715
C
1147 break;
1148 default:
1149 ret = -EINVAL;
1150 }
1151
1152 return ret;
1153}
1154
1f9c3e1f
MA
1155/*
1156 * Set new x,y offsets in the virtual display for the visible area and switch
1157 * to the new mode.
1158 */
1159static int da8xx_pan_display(struct fb_var_screeninfo *var,
1160 struct fb_info *fbi)
1161{
1162 int ret = 0;
1163 struct fb_var_screeninfo new_var;
1164 struct da8xx_fb_par *par = fbi->par;
1165 struct fb_fix_screeninfo *fix = &fbi->fix;
1166 unsigned int end;
1167 unsigned int start;
deb95c6c 1168 unsigned long irq_flags;
1f9c3e1f
MA
1169
1170 if (var->xoffset != fbi->var.xoffset ||
1171 var->yoffset != fbi->var.yoffset) {
1172 memcpy(&new_var, &fbi->var, sizeof(new_var));
1173 new_var.xoffset = var->xoffset;
1174 new_var.yoffset = var->yoffset;
1175 if (fb_check_var(&new_var, fbi))
1176 ret = -EINVAL;
1177 else {
1178 memcpy(&fbi->var, &new_var, sizeof(new_var));
1179
1180 start = fix->smem_start +
1181 new_var.yoffset * fix->line_length +
e6c4d3d4
LP
1182 new_var.xoffset * fbi->var.bits_per_pixel / 8;
1183 end = start + fbi->var.yres * fix->line_length - 1;
1f9c3e1f
MA
1184 par->dma_start = start;
1185 par->dma_end = end;
deb95c6c
MP
1186 spin_lock_irqsave(&par->lock_for_chan_update,
1187 irq_flags);
1188 if (par->which_dma_channel_done == 0) {
1189 lcdc_write(par->dma_start,
1190 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1191 lcdc_write(par->dma_end,
1192 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1193 } else if (par->which_dma_channel_done == 1) {
1194 lcdc_write(par->dma_start,
1195 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1196 lcdc_write(par->dma_end,
1197 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1198 }
1199 spin_unlock_irqrestore(&par->lock_for_chan_update,
1200 irq_flags);
1f9c3e1f
MA
1201 }
1202 }
1203
1204 return ret;
1205}
1206
fe8c98f0
DE
1207static int da8xxfb_set_par(struct fb_info *info)
1208{
1209 struct da8xx_fb_par *par = info->par;
1210 int ret;
1211 bool raster = da8xx_fb_is_raster_enabled();
1212
1213 if (raster)
26e71645 1214 lcd_disable_raster(DA8XX_FRAME_WAIT);
fe8c98f0
DE
1215
1216 fb_var_to_videomode(&par->mode, &info->var);
1217
1218 par->cfg.bpp = info->var.bits_per_pixel;
1219
1220 info->fix.visual = (par->cfg.bpp <= 8) ?
1221 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1222 info->fix.line_length = (par->mode.xres * par->cfg.bpp) / 8;
1223
1224 ret = lcd_init(par, &par->cfg, &par->mode);
1225 if (ret < 0) {
1226 dev_err(par->dev, "lcd init failed\n");
1227 return ret;
1228 }
1229
1230 par->dma_start = info->fix.smem_start +
1231 info->var.yoffset * info->fix.line_length +
1232 info->var.xoffset * info->var.bits_per_pixel / 8;
1233 par->dma_end = par->dma_start +
1234 info->var.yres * info->fix.line_length - 1;
1235
1236 lcdc_write(par->dma_start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1237 lcdc_write(par->dma_end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1238 lcdc_write(par->dma_start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1239 lcdc_write(par->dma_end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1240
1241 if (raster)
1242 lcd_enable_raster();
1243
1244 return 0;
1245}
1246
4ed824d9
SR
1247static struct fb_ops da8xx_fb_ops = {
1248 .owner = THIS_MODULE,
1249 .fb_check_var = fb_check_var,
fe8c98f0 1250 .fb_set_par = da8xxfb_set_par,
4ed824d9 1251 .fb_setcolreg = fb_setcolreg,
1f9c3e1f 1252 .fb_pan_display = da8xx_pan_display,
4ed824d9
SR
1253 .fb_ioctl = fb_ioctl,
1254 .fb_fillrect = cfb_fillrect,
1255 .fb_copyarea = cfb_copyarea,
1256 .fb_imageblit = cfb_imageblit,
312d9715 1257 .fb_blank = cfb_blank,
4ed824d9
SR
1258};
1259
48c68c4f 1260static int fb_probe(struct platform_device *device)
4ed824d9
SR
1261{
1262 struct da8xx_lcdc_platform_data *fb_pdata =
1263 device->dev.platform_data;
1264 struct lcd_ctrl_config *lcd_cfg;
f772fabd 1265 struct fb_videomode *lcdc_info;
4ed824d9
SR
1266 struct fb_info *da8xx_fb_info;
1267 struct clk *fb_clk = NULL;
1268 struct da8xx_fb_par *par;
1269 resource_size_t len;
1270 int ret, i;
3b9cc4ea 1271 unsigned long ulcm;
4ed824d9
SR
1272
1273 if (fb_pdata == NULL) {
1274 dev_err(&device->dev, "Can not get platform data\n");
1275 return -ENOENT;
1276 }
1277
1278 lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
1279 if (!lcdc_regs) {
1280 dev_err(&device->dev,
1281 "Can not get memory resource for LCD controller\n");
1282 return -ENOENT;
1283 }
1284
1285 len = resource_size(lcdc_regs);
1286
1287 lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
1288 if (!lcdc_regs)
1289 return -EBUSY;
1290
34aef6eb 1291 da8xx_fb_reg_base = ioremap(lcdc_regs->start, len);
4ed824d9
SR
1292 if (!da8xx_fb_reg_base) {
1293 ret = -EBUSY;
1294 goto err_request_mem;
1295 }
1296
81cec3c7 1297 fb_clk = clk_get(&device->dev, "fck");
4ed824d9
SR
1298 if (IS_ERR(fb_clk)) {
1299 dev_err(&device->dev, "Can not get device clock\n");
1300 ret = -ENODEV;
1301 goto err_ioremap;
1302 }
9dd44d5d
MP
1303
1304 pm_runtime_enable(&device->dev);
1305 pm_runtime_get_sync(&device->dev);
4ed824d9 1306
c6daf05b
MP
1307 /* Determine LCD IP Version */
1308 switch (lcdc_read(LCD_PID_REG)) {
1309 case 0x4C100102:
1310 lcd_revision = LCD_VERSION_1;
1311 break;
1312 case 0x4F200800:
8f22e8ea 1313 case 0x4F201000:
c6daf05b
MP
1314 lcd_revision = LCD_VERSION_2;
1315 break;
1316 default:
1317 dev_warn(&device->dev, "Unknown PID Reg value 0x%x, "
1318 "defaulting to LCD revision 1\n",
1319 lcdc_read(LCD_PID_REG));
1320 lcd_revision = LCD_VERSION_1;
1321 break;
1322 }
1323
4ed824d9
SR
1324 for (i = 0, lcdc_info = known_lcd_panels;
1325 i < ARRAY_SIZE(known_lcd_panels);
1326 i++, lcdc_info++) {
1327 if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
1328 break;
1329 }
1330
1331 if (i == ARRAY_SIZE(known_lcd_panels)) {
1332 dev_err(&device->dev, "GLCD: No valid panel found\n");
dd04a6b3 1333 ret = -ENODEV;
9dd44d5d 1334 goto err_pm_runtime_disable;
4ed824d9
SR
1335 } else
1336 dev_info(&device->dev, "GLCD: Found %s panel\n",
1337 fb_pdata->type);
1338
1339 lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
1340
1341 da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
1342 &device->dev);
1343 if (!da8xx_fb_info) {
1344 dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
1345 ret = -ENOMEM;
9dd44d5d 1346 goto err_pm_runtime_disable;
4ed824d9
SR
1347 }
1348
1349 par = da8xx_fb_info->par;
dbe8e48a 1350 par->dev = &device->dev;
8097b174 1351 par->lcdc_clk = fb_clk;
f820917a 1352 par->lcd_fck_rate = clk_get_rate(fb_clk);
36113804
C
1353 if (fb_pdata->panel_power_ctrl) {
1354 par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
1355 par->panel_power_ctrl(1);
1356 }
4ed824d9 1357
b866458b 1358 fb_videomode_to_var(&da8xx_fb_var, lcdc_info);
b6dbe8e4 1359 par->cfg = *lcd_cfg;
b866458b 1360
fe8c98f0 1361 da8xx_fb_lcd_reset();
4ed824d9
SR
1362
1363 /* allocate frame buffer */
f772fabd
MP
1364 par->vram_size = lcdc_info->xres * lcdc_info->yres * lcd_cfg->bpp;
1365 ulcm = lcm((lcdc_info->xres * lcd_cfg->bpp)/8, PAGE_SIZE);
3b9cc4ea 1366 par->vram_size = roundup(par->vram_size/8, ulcm);
1f9c3e1f
MA
1367 par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
1368
1369 par->vram_virt = dma_alloc_coherent(NULL,
1370 par->vram_size,
1371 (resource_size_t *) &par->vram_phys,
1372 GFP_KERNEL | GFP_DMA);
1373 if (!par->vram_virt) {
4ed824d9
SR
1374 dev_err(&device->dev,
1375 "GLCD: kmalloc for frame buffer failed\n");
1376 ret = -EINVAL;
1377 goto err_release_fb;
1378 }
1379
1f9c3e1f
MA
1380 da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
1381 da8xx_fb_fix.smem_start = par->vram_phys;
1382 da8xx_fb_fix.smem_len = par->vram_size;
f772fabd 1383 da8xx_fb_fix.line_length = (lcdc_info->xres * lcd_cfg->bpp) / 8;
1f9c3e1f
MA
1384
1385 par->dma_start = par->vram_phys;
f772fabd 1386 par->dma_end = par->dma_start + lcdc_info->yres *
1f9c3e1f
MA
1387 da8xx_fb_fix.line_length - 1;
1388
1389 /* allocate palette buffer */
1390 par->v_palette_base = dma_alloc_coherent(NULL,
1391 PALETTE_SIZE,
1392 (resource_size_t *)
1393 &par->p_palette_base,
1394 GFP_KERNEL | GFP_DMA);
1395 if (!par->v_palette_base) {
1396 dev_err(&device->dev,
1397 "GLCD: kmalloc for palette buffer failed\n");
1398 ret = -EINVAL;
1399 goto err_release_fb_mem;
1400 }
1401 memset(par->v_palette_base, 0, PALETTE_SIZE);
4ed824d9 1402
4ed824d9
SR
1403 par->irq = platform_get_irq(device, 0);
1404 if (par->irq < 0) {
1405 ret = -ENOENT;
1f9c3e1f 1406 goto err_release_pl_mem;
4ed824d9
SR
1407 }
1408
4ed824d9 1409 da8xx_fb_var.grayscale =
3b43ad20 1410 lcd_cfg->panel_shade == MONOCHROME ? 1 : 0;
4ed824d9 1411 da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
4ed824d9
SR
1412
1413 /* Initialize fbinfo */
1414 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
1415 da8xx_fb_info->fix = da8xx_fb_fix;
1416 da8xx_fb_info->var = da8xx_fb_var;
1417 da8xx_fb_info->fbops = &da8xx_fb_ops;
1418 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
3510b8f7
SR
1419 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
1420 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
4ed824d9
SR
1421
1422 ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
1423 if (ret)
93c176f3 1424 goto err_release_pl_mem;
4ed824d9
SR
1425 da8xx_fb_info->cmap.len = par->palette_sz;
1426
4ed824d9
SR
1427 /* initialize var_screeninfo */
1428 da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
1429 fb_set_var(da8xx_fb_info, &da8xx_fb_var);
1430
1431 dev_set_drvdata(&device->dev, da8xx_fb_info);
1f9c3e1f
MA
1432
1433 /* initialize the vsync wait queue */
1434 init_waitqueue_head(&par->vsync_wait);
1435 par->vsync_timeout = HZ / 5;
deb95c6c
MP
1436 par->which_dma_channel_done = -1;
1437 spin_lock_init(&par->lock_for_chan_update);
1f9c3e1f 1438
4ed824d9
SR
1439 /* Register the Frame Buffer */
1440 if (register_framebuffer(da8xx_fb_info) < 0) {
1441 dev_err(&device->dev,
1442 "GLCD: Frame Buffer Registration Failed!\n");
1443 ret = -EINVAL;
1444 goto err_dealloc_cmap;
1445 }
1446
e04e5483
C
1447#ifdef CONFIG_CPU_FREQ
1448 ret = lcd_da8xx_cpufreq_register(par);
1449 if (ret) {
1450 dev_err(&device->dev, "failed to register cpufreq\n");
1451 goto err_cpu_freq;
1452 }
1453#endif
93c176f3 1454
c6daf05b
MP
1455 if (lcd_revision == LCD_VERSION_1)
1456 lcdc_irq_handler = lcdc_irq_handler_rev01;
a481b37a
MP
1457 else {
1458 init_waitqueue_head(&frame_done_wq);
c6daf05b 1459 lcdc_irq_handler = lcdc_irq_handler_rev02;
a481b37a 1460 }
c6daf05b
MP
1461
1462 ret = request_irq(par->irq, lcdc_irq_handler, 0,
1463 DRIVER_NAME, par);
93c176f3
CA
1464 if (ret)
1465 goto irq_freq;
4ed824d9
SR
1466 return 0;
1467
93c176f3 1468irq_freq:
e04e5483 1469#ifdef CONFIG_CPU_FREQ
360c202b 1470 lcd_da8xx_cpufreq_deregister(par);
e04e5483 1471err_cpu_freq:
3a84409c 1472#endif
e04e5483 1473 unregister_framebuffer(da8xx_fb_info);
e04e5483 1474
4ed824d9
SR
1475err_dealloc_cmap:
1476 fb_dealloc_cmap(&da8xx_fb_info->cmap);
1477
1f9c3e1f
MA
1478err_release_pl_mem:
1479 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
1480 par->p_palette_base);
1481
4ed824d9 1482err_release_fb_mem:
1f9c3e1f 1483 dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
4ed824d9
SR
1484
1485err_release_fb:
1486 framebuffer_release(da8xx_fb_info);
1487
9dd44d5d
MP
1488err_pm_runtime_disable:
1489 pm_runtime_put_sync(&device->dev);
1490 pm_runtime_disable(&device->dev);
4ed824d9
SR
1491
1492err_ioremap:
34aef6eb 1493 iounmap(da8xx_fb_reg_base);
4ed824d9
SR
1494
1495err_request_mem:
1496 release_mem_region(lcdc_regs->start, len);
1497
1498 return ret;
1499}
1500
1501#ifdef CONFIG_PM
7a93cbbb
MP
1502struct lcdc_context {
1503 u32 clk_enable;
1504 u32 ctrl;
1505 u32 dma_ctrl;
1506 u32 raster_timing_0;
1507 u32 raster_timing_1;
1508 u32 raster_timing_2;
1509 u32 int_enable_set;
1510 u32 dma_frm_buf_base_addr_0;
1511 u32 dma_frm_buf_ceiling_addr_0;
1512 u32 dma_frm_buf_base_addr_1;
1513 u32 dma_frm_buf_ceiling_addr_1;
1514 u32 raster_ctrl;
1515} reg_context;
1516
1517static void lcd_context_save(void)
1518{
1519 if (lcd_revision == LCD_VERSION_2) {
1520 reg_context.clk_enable = lcdc_read(LCD_CLK_ENABLE_REG);
1521 reg_context.int_enable_set = lcdc_read(LCD_INT_ENABLE_SET_REG);
1522 }
1523
1524 reg_context.ctrl = lcdc_read(LCD_CTRL_REG);
1525 reg_context.dma_ctrl = lcdc_read(LCD_DMA_CTRL_REG);
1526 reg_context.raster_timing_0 = lcdc_read(LCD_RASTER_TIMING_0_REG);
1527 reg_context.raster_timing_1 = lcdc_read(LCD_RASTER_TIMING_1_REG);
1528 reg_context.raster_timing_2 = lcdc_read(LCD_RASTER_TIMING_2_REG);
1529 reg_context.dma_frm_buf_base_addr_0 =
1530 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1531 reg_context.dma_frm_buf_ceiling_addr_0 =
1532 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1533 reg_context.dma_frm_buf_base_addr_1 =
1534 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1535 reg_context.dma_frm_buf_ceiling_addr_1 =
1536 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1537 reg_context.raster_ctrl = lcdc_read(LCD_RASTER_CTRL_REG);
1538 return;
1539}
1540
1541static void lcd_context_restore(void)
1542{
1543 if (lcd_revision == LCD_VERSION_2) {
1544 lcdc_write(reg_context.clk_enable, LCD_CLK_ENABLE_REG);
1545 lcdc_write(reg_context.int_enable_set, LCD_INT_ENABLE_SET_REG);
1546 }
1547
1548 lcdc_write(reg_context.ctrl, LCD_CTRL_REG);
1549 lcdc_write(reg_context.dma_ctrl, LCD_DMA_CTRL_REG);
1550 lcdc_write(reg_context.raster_timing_0, LCD_RASTER_TIMING_0_REG);
1551 lcdc_write(reg_context.raster_timing_1, LCD_RASTER_TIMING_1_REG);
1552 lcdc_write(reg_context.raster_timing_2, LCD_RASTER_TIMING_2_REG);
1553 lcdc_write(reg_context.dma_frm_buf_base_addr_0,
1554 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1555 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_0,
1556 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1557 lcdc_write(reg_context.dma_frm_buf_base_addr_1,
1558 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1559 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_1,
1560 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1561 lcdc_write(reg_context.raster_ctrl, LCD_RASTER_CTRL_REG);
1562 return;
1563}
1564
4ed824d9
SR
1565static int fb_suspend(struct platform_device *dev, pm_message_t state)
1566{
1d3c6c7b
C
1567 struct fb_info *info = platform_get_drvdata(dev);
1568 struct da8xx_fb_par *par = info->par;
1569
ac751efa 1570 console_lock();
1d3c6c7b
C
1571 if (par->panel_power_ctrl)
1572 par->panel_power_ctrl(0);
1573
1574 fb_set_suspend(info, 1);
26e71645 1575 lcd_disable_raster(DA8XX_FRAME_WAIT);
7a93cbbb 1576 lcd_context_save();
9dd44d5d 1577 pm_runtime_put_sync(&dev->dev);
ac751efa 1578 console_unlock();
1d3c6c7b
C
1579
1580 return 0;
4ed824d9
SR
1581}
1582static int fb_resume(struct platform_device *dev)
1583{
1d3c6c7b
C
1584 struct fb_info *info = platform_get_drvdata(dev);
1585 struct da8xx_fb_par *par = info->par;
1586
ac751efa 1587 console_lock();
9dd44d5d 1588 pm_runtime_get_sync(&dev->dev);
7a93cbbb 1589 lcd_context_restore();
67900814
MP
1590 if (par->blank == FB_BLANK_UNBLANK) {
1591 lcd_enable_raster();
f7c848b6 1592
67900814
MP
1593 if (par->panel_power_ctrl)
1594 par->panel_power_ctrl(1);
1595 }
1d3c6c7b 1596
1d3c6c7b 1597 fb_set_suspend(info, 0);
ac751efa 1598 console_unlock();
1d3c6c7b
C
1599
1600 return 0;
4ed824d9
SR
1601}
1602#else
1603#define fb_suspend NULL
1604#define fb_resume NULL
1605#endif
1606
1607static struct platform_driver da8xx_fb_driver = {
1608 .probe = fb_probe,
48c68c4f 1609 .remove = fb_remove,
4ed824d9
SR
1610 .suspend = fb_suspend,
1611 .resume = fb_resume,
1612 .driver = {
1613 .name = DRIVER_NAME,
1614 .owner = THIS_MODULE,
1615 },
1616};
1617
1618static int __init da8xx_fb_init(void)
1619{
1620 return platform_driver_register(&da8xx_fb_driver);
1621}
1622
1623static void __exit da8xx_fb_cleanup(void)
1624{
1625 platform_driver_unregister(&da8xx_fb_driver);
1626}
1627
1628module_init(da8xx_fb_init);
1629module_exit(da8xx_fb_cleanup);
1630
1631MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
1632MODULE_AUTHOR("Texas Instruments");
1633MODULE_LICENSE("GPL");