Apply FS#10447 - e200v2 lcd driver cleanup by Michael Chicoine
[maemo-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / lcd-e200v2.c
blob7f02a8b89491a74d064ad3eaed95705af3d37630
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #include "cpu.h"
24 #include "lcd.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "file.h"
30 #include "debug.h"
31 #include "system.h"
32 #include "font.h"
33 #include "bidi.h"
34 #include "clock-target.h"
36 static bool display_on = false; /* is the display turned on? */
37 static bool display_flipped = false;
38 /* we need to write a red pixel for correct button reads
39 * (see lcd_button_support()), but that must not happen while the lcd is
40 * updating so block lcd_button_support the during updates */
41 static volatile bool lcd_busy = false;
43 /* register defines */
44 #define R_START_OSC 0x00
45 #define R_DRV_OUTPUT_CONTROL 0x01
46 #define R_DRV_WAVEFORM_CONTROL 0x02
47 #define R_ENTRY_MODE 0x03
48 #define R_COMPARE_REG1 0x04
49 #define R_COMPARE_REG2 0x05
51 #define R_DISP_CONTROL1 0x07
52 #define R_DISP_CONTROL2 0x08
53 #define R_DISP_CONTROL3 0x09
55 #define R_FRAME_CYCLE_CONTROL 0x0b
56 #define R_EXT_DISP_IF_CONTROL 0x0c
58 #define R_POWER_CONTROL1 0x10
59 #define R_POWER_CONTROL2 0x11
60 #define R_POWER_CONTROL3 0x12
61 #define R_POWER_CONTROL4 0x13
63 #define R_RAM_ADDR_SET 0x21
64 #define R_WRITE_DATA_2_GRAM 0x22
66 #define R_GAMMA_FINE_ADJ_POS1 0x30
67 #define R_GAMMA_FINE_ADJ_POS2 0x31
68 #define R_GAMMA_FINE_ADJ_POS3 0x32
69 #define R_GAMMA_GRAD_ADJ_POS 0x33
71 #define R_GAMMA_FINE_ADJ_NEG1 0x34
72 #define R_GAMMA_FINE_ADJ_NEG2 0x35
73 #define R_GAMMA_FINE_ADJ_NEG3 0x36
74 #define R_GAMMA_GRAD_ADJ_NEG 0x37
76 #define R_GAMMA_AMP_ADJ_RES_POS 0x38
77 #define R_GAMMA_AMP_AVG_ADJ_RES_NEG 0x39
79 #define R_GATE_SCAN_POS 0x40
80 #define R_VERT_SCROLL_CONTROL 0x41
81 #define R_1ST_SCR_DRV_POS 0x42
82 #define R_2ND_SCR_DRV_POS 0x43
83 #define R_HORIZ_RAM_ADDR_POS 0x44
84 #define R_VERT_RAM_ADDR_POS 0x45
86 /* Flip Flag */
87 #define R_ENTRY_MODE_HORZ_NORMAL 0x7030
88 #define R_ENTRY_MODE_HORZ_FLIPPED 0x7000
89 static unsigned short r_entry_mode = R_ENTRY_MODE_HORZ_NORMAL;
90 #define R_ENTRY_MODE_VERT 0x7038
91 #define R_ENTRY_MODE_SOLID_VERT 0x1038
92 #define R_ENTRY_MODE_VIDEO_NORMAL 0x7020
93 #define R_ENTRY_MODE_VIDEO_FLIPPED 0x7010
96 /* Reverse Flag */
97 #define R_DISP_CONTROL_NORMAL 0x0004
98 #define R_DISP_CONTROL_REV 0x0000
99 static unsigned short r_disp_control_rev = R_DISP_CONTROL_NORMAL;
101 /* TODO: Implement this function */
102 static void lcd_delay(int x)
104 /* This is just arbitrary - the OF does something more complex */
105 x *= 1024;
106 while (x--);
109 /* DBOP initialisation, do what OF does */
110 static void ams3525_dbop_init(void)
112 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
114 DBOP_TIMPOL_01 = 0xe167e167;
115 DBOP_TIMPOL_23 = 0xe167006e;
117 /* short count, 16bit write, read-timing =8 */
118 DBOP_CTRL = (1<<18)|(1<<12)|(8<<0);
120 GPIOB_AFSEL = 0xfc;
121 GPIOC_AFSEL = 0xff;
123 DBOP_TIMPOL_23 = 0x6000e;
125 /* short count,write enable, 16bit write, read-timing =8 */
126 DBOP_CTRL = (1<<18)|(1<<16)|(1<<12)|(8<<0);
127 DBOP_TIMPOL_01 = 0x6e167;
128 DBOP_TIMPOL_23 = 0xa167e06f;
130 /* TODO: The OF calls some other functions here, but maybe not important */
134 static void lcd_write_cmd(int cmd)
136 /* Write register */
137 DBOP_CTRL &= ~(1<<14);
139 DBOP_TIMPOL_23 = 0xa167006e;
141 DBOP_DOUT = cmd;
143 /* Wait for fifo to empty */
144 while ((DBOP_STAT & (1<<10)) == 0);
146 /* Fuze OF has this loop and it seems to help us now also */
147 int delay=8;
148 while(delay--);
150 DBOP_TIMPOL_23 = 0xa167e06f;
153 void lcd_write_data(const fb_data* p_bytes, int count)
155 while (count--)
157 DBOP_DOUT = *p_bytes++;
159 /* Wait for fifo to empty */
160 while ((DBOP_STAT & (1<<10)) == 0);
164 static void lcd_write_reg(int reg, int value)
166 fb_data data = value;
168 lcd_write_cmd(reg);
169 lcd_write_data(&data, 1);
172 /*** hardware configuration ***/
174 void lcd_set_contrast(int val)
176 (void)val;
179 void lcd_set_invert_display(bool yesno)
181 r_disp_control_rev = yesno ? R_DISP_CONTROL_REV :
182 R_DISP_CONTROL_NORMAL;
184 if (display_on)
186 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
191 /* turn the display upside down */
192 void lcd_set_flip(bool yesno)
194 display_flipped = yesno;
196 r_entry_mode = yesno ? R_ENTRY_MODE_HORZ_FLIPPED :
197 R_ENTRY_MODE_HORZ_NORMAL;
200 static void lcd_window(int xmin, int ymin, int xmax, int ymax)
202 if (!display_flipped)
204 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
205 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
206 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | xmin);
208 else
210 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
211 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
212 lcd_write_reg(R_VERT_RAM_ADDR_POS,
213 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_HEIGHT-1 - ymax));
214 lcd_write_reg(R_RAM_ADDR_SET,
215 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_WIDTH-1 - xmin));
219 static void _display_on(void)
221 /* Initialisation the display the same way as the original firmware */
223 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
225 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x011b); /* 220 lines, GS=0, SS=1 */
227 /* B/C = 1: n-line inversion form
228 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
229 * frame select signal and an n-line inversion signal.
230 * FLD = 01b: 1 field interlaced scan, external display iface */
231 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
233 /* Address counter updated in horizontal direction; left to right;
234 * vertical increment horizontal increment.
235 * data format for 8bit transfer or spi = 65k (5,6,5) */
236 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
238 /* Replace data on writing to GRAM */
239 lcd_write_reg(R_COMPARE_REG1, 0);
240 lcd_write_reg(R_COMPARE_REG2, 0);
242 /* GON = 0, DTE = 0, D1-0 = 00b */
243 lcd_write_reg(R_DISP_CONTROL1, 0x0000 | r_disp_control_rev);
245 /* Front porch lines: 2; Back porch lines: 2; */
246 lcd_write_reg(R_DISP_CONTROL2, 0x0203);
248 /* Scan cycle = 0 frames */
249 lcd_write_reg(R_DISP_CONTROL3, 0x0000);
251 /* 16 clocks */
252 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0000);
254 /* 18-bit RGB interface (one transfer/pixel)
255 * internal clock operation;
256 * System interface/VSYNC interface */
257 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0000);
260 /* zero everything*/
261 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
263 lcd_delay(10);
265 /* initialise power supply */
267 /* DC12-10 = 000b: Step-up1 = clock/8,
268 * DC02-00 = 000b: Step-up2 = clock/16,
269 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
270 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
272 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
273 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
275 lcd_delay(40);
277 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
279 /* This register is unknown */
280 lcd_write_reg(0x56, 0x80f);
283 lcd_write_reg(R_POWER_CONTROL1, 0x4140);
285 lcd_delay(10);
287 lcd_write_reg(R_POWER_CONTROL2, 0x0000);
288 lcd_write_reg(R_POWER_CONTROL3, 0x0013);
290 lcd_delay(20);
292 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
294 lcd_delay(20);
296 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
298 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0002);
299 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
300 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0182);
301 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0203);
302 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0706);
303 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0006);
304 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0706);
305 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0000);
306 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x030f);
307 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0f08);
310 lcd_write_reg(R_GATE_SCAN_POS, 0);
311 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0);
313 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
314 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
315 lcd_write_reg(R_2ND_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
317 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
319 display_on=true; /* must be done before calling lcd_update() */
320 lcd_update();
323 /* LCD init */
324 void lcd_init_device(void)
326 ams3525_dbop_init();
328 /* Init GPIOs the same as the OF */
330 GPIOA_DIR |= (1<<5);
331 GPIOA_PIN(5) = 0;
333 GPIOA_PIN(4) = 0; /*c80b0040 := 0;*/
335 lcd_delay(1);
337 GPIOA_PIN(5) = (1<<5);
339 lcd_delay(1);
341 _display_on();
344 #if defined(HAVE_LCD_ENABLE)
345 void lcd_enable(bool on)
347 if(display_on!=on)
349 if(on)
351 _display_on();
352 lcd_activation_call_hook();
354 else
356 display_on=false;
357 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
361 #endif
363 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
364 bool lcd_active(void)
366 return display_on;
369 #endif
371 /*** update functions ***/
373 static unsigned lcd_yuv_options = 0;
375 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
376 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
377 int width,
378 int stride);
379 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
380 int width,
381 int stride,
382 int x_screen, /* To align dither pattern */
383 int y_screen);
385 void lcd_yuv_set_options(unsigned options)
387 lcd_yuv_options = options;
390 static void lcd_window_blit(int xmin, int ymin, int xmax, int ymax)
392 if (!display_flipped)
394 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
395 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
396 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
397 lcd_write_reg(R_RAM_ADDR_SET,
398 (ymin << 8) | (LCD_WIDTH-1 - xmin));
400 else
402 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
403 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
404 lcd_write_reg(R_RAM_ADDR_SET, (ymax << 8) | xmin);
408 /* Performance function to blit a YUV bitmap directly to the LCD
409 * src_x, src_y, width and height should be even
410 * x, y, width and height have to be within LCD bounds
412 void lcd_blit_yuv(unsigned char * const src[3],
413 int src_x, int src_y, int stride,
414 int x, int y, int width, int height)
416 unsigned char const * yuv_src[3];
417 off_t z;
419 lcd_busy = true;
421 /* Sorry, but width and height must be >= 2 or else */
422 width &= ~1;
423 height >>= 1;
425 z = stride*src_y;
426 yuv_src[0] = src[0] + z + src_x;
427 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
428 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
430 if (!display_flipped)
432 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_VIDEO_NORMAL);
434 else
436 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_VIDEO_FLIPPED);
439 if (lcd_yuv_options & LCD_YUV_DITHER)
443 lcd_window_blit(y, x, y+1, x+width-1);
445 /* Start write to GRAM */
446 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
448 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
449 yuv_src[0] += stride << 1; /* Skip down two luma lines */
450 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
451 yuv_src[2] += stride >> 1;
452 y+=2;
454 while (--height > 0);
456 else
460 lcd_window_blit(y, x, y+1, x+width-1);
462 /* Start write to GRAM */
463 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
465 lcd_write_yuv420_lines(yuv_src, width, stride);
466 yuv_src[0] += stride << 1; /* Skip down two luma lines */
467 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
468 yuv_src[2] += stride >> 1;
469 y+=2;
471 while (--height > 0);
474 lcd_busy = false;
477 /* Update the display.
478 This must be called after all other LCD functions that change the display. */
479 void lcd_update(void)
481 if (!display_on)
482 return;
484 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
486 lcd_busy = true;
487 /* Set start position and window */
488 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
490 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
492 lcd_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
494 lcd_busy = false;
495 } /* lcd_update */
498 /* Update a fraction of the display. */
499 void lcd_update_rect(int x, int y, int width, int height)
501 const fb_data *ptr;
502 int ymax, xmax;
505 if (!display_on)
506 return;
508 xmax = x + width;
509 if (xmax >= LCD_WIDTH)
510 xmax = LCD_WIDTH - 1; /* Clip right */
511 if (x < 0)
512 x = 0; /* Clip left */
513 if (x >= xmax)
514 return; /* nothing left to do */
516 width = xmax - x + 1; /* Fix width */
518 ymax = y + height;
519 if (ymax >= LCD_HEIGHT)
520 ymax = LCD_HEIGHT - 1; /* Clip bottom */
521 if (y < 0)
522 y = 0; /* Clip top */
523 if (y >= ymax)
524 return; /* nothing left to do */
526 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
527 lcd_busy = true;
528 lcd_window(x, y, xmax, ymax);
529 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
531 ptr = (fb_data*)&lcd_framebuffer[y][x];
535 lcd_write_data(ptr, width);
536 ptr += LCD_WIDTH;
538 while (++y <= ymax);
540 lcd_busy = false;
541 } /* lcd_update_rect */
543 /* writes one red pixel outside the visible area, needed for correct
544 * dbop reads */
545 bool lcd_button_support(void)
547 fb_data data = (0xf<<12);
549 if (lcd_busy)
550 return false;
552 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
553 /* Set start position and window */
554 lcd_window(LCD_WIDTH+1, LCD_HEIGHT+1, LCD_WIDTH+2, LCD_HEIGHT+2);
556 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
558 lcd_write_data(&data, 1);
559 return true;