Fix yellow. Another function unneeded in the bootloader.
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / lcd-e200v2.c
blob089a5da24cd99ad56d29fcc36e2e6cc4d3fd56d3
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 "file.h"
26 #include "debug.h"
27 #include "system.h"
28 #include "clock-target.h"
29 #include "dbop-as3525.h"
31 /* The controller is unknown, but some registers appear to be the same as the
32 HD66789R */
33 static bool display_on = false; /* is the display turned on? */
35 /* register defines */
36 #define R_START_OSC 0x00
37 #define R_DRV_OUTPUT_CONTROL 0x01
38 #define R_DRV_WAVEFORM_CONTROL 0x02
39 #define R_ENTRY_MODE 0x03
40 #define R_COMPARE_REG1 0x04
41 #define R_COMPARE_REG2 0x05
43 #define R_DISP_CONTROL1 0x07
44 #define R_DISP_CONTROL2 0x08
45 #define R_DISP_CONTROL3 0x09
47 #define R_FRAME_CYCLE_CONTROL 0x0b
48 #define R_EXT_DISP_IF_CONTROL 0x0c
50 #define R_POWER_CONTROL1 0x10
51 #define R_POWER_CONTROL2 0x11
52 #define R_POWER_CONTROL3 0x12
53 #define R_POWER_CONTROL4 0x13
55 #define R_RAM_ADDR_SET 0x21
56 #define R_WRITE_DATA_2_GRAM 0x22
58 #define R_GAMMA_FINE_ADJ_POS1 0x30
59 #define R_GAMMA_FINE_ADJ_POS2 0x31
60 #define R_GAMMA_FINE_ADJ_POS3 0x32
61 #define R_GAMMA_GRAD_ADJ_POS 0x33
63 #define R_GAMMA_FINE_ADJ_NEG1 0x34
64 #define R_GAMMA_FINE_ADJ_NEG2 0x35
65 #define R_GAMMA_FINE_ADJ_NEG3 0x36
66 #define R_GAMMA_GRAD_ADJ_NEG 0x37
68 #define R_GAMMA_AMP_ADJ_RES_POS 0x38
69 #define R_GAMMA_AMP_AVG_ADJ_RES_NEG 0x39
71 #define R_GATE_SCAN_POS 0x40
72 #define R_VERT_SCROLL_CONTROL 0x41
73 #define R_1ST_SCR_DRV_POS 0x42
74 #define R_2ND_SCR_DRV_POS 0x43
75 #define R_HORIZ_RAM_ADDR_POS 0x44
76 #define R_VERT_RAM_ADDR_POS 0x45
78 /* Flip Flag */
79 #define R_ENTRY_MODE_HORZ_NORMAL 0x7030
80 #define R_ENTRY_MODE_HORZ_FLIPPED 0x7000
81 static unsigned short r_entry_mode = R_ENTRY_MODE_HORZ_NORMAL;
82 #define R_ENTRY_MODE_VERT 0x7038
83 #define R_ENTRY_MODE_SOLID_VERT 0x1038
84 #define R_ENTRY_MODE_VIDEO_NORMAL 0x7020
85 #define R_ENTRY_MODE_VIDEO_FLIPPED 0x7010
88 /* Reverse Flag */
89 #define R_DISP_CONTROL_NORMAL 0x0004
90 #define R_DISP_CONTROL_REV 0x0000
91 static unsigned short r_disp_control_rev = R_DISP_CONTROL_NORMAL;
93 static inline void lcd_delay(int x)
95 do {
96 asm volatile ("nop\n");
97 } while (x--);
100 static void as3525_dbop_init(void)
102 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
104 DBOP_TIMPOL_01 = 0xe167e167;
105 DBOP_TIMPOL_23 = 0xe167006e;
107 /* short count: 16 | output data width: 16 | readstrobe line */
108 DBOP_CTRL = (1<<18|1<<12|1<<3);
110 GPIOB_AFSEL = 0xfc;
111 GPIOC_AFSEL = 0xff;
113 DBOP_TIMPOL_23 = 0x6000e;
115 /* short count: 16|enable write|output data width: 16|read strobe line */
116 DBOP_CTRL = (1<<18|1<<16|1<<12|1<<3);
117 DBOP_TIMPOL_01 = 0x6e167;
118 DBOP_TIMPOL_23 = 0xa167e06f;
120 /* TODO: The OF calls some other functions here, but maybe not important */
123 static void lcd_write_cmd(short cmd)
125 /* Write register */
126 DBOP_TIMPOL_23 = 0xa167006e;
127 dbop_write_data(&cmd, 1);
129 /* Wait for fifo to empty */
130 while ((DBOP_STAT & (1<<10)) == 0);
132 /* Fuze OF has this loop and it seems to help us now also */
133 int delay = 8;
134 while(delay--);
136 DBOP_TIMPOL_23 = 0xa167e06f;
140 static void lcd_write_reg(int reg, int value)
142 unsigned short data = value;
144 lcd_write_cmd(reg);
145 dbop_write_data(&data, 1);
148 /*** hardware configuration ***/
150 void lcd_set_contrast(int val)
152 (void)val;
155 void lcd_set_invert_display(bool yesno)
157 r_disp_control_rev = yesno ? R_DISP_CONTROL_REV :
158 R_DISP_CONTROL_NORMAL;
160 if (display_on)
162 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
167 static bool display_flipped = false;
169 /* turn the display upside down */
170 void lcd_set_flip(bool yesno)
172 display_flipped = yesno;
174 r_entry_mode = yesno ? R_ENTRY_MODE_HORZ_FLIPPED :
175 R_ENTRY_MODE_HORZ_NORMAL;
178 static void lcd_window(int xmin, int ymin, int xmax, int ymax)
180 if (!display_flipped)
182 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
183 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
184 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | xmin);
186 else
188 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
189 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
190 lcd_write_reg(R_VERT_RAM_ADDR_POS,
191 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_HEIGHT-1 - ymax));
192 lcd_write_reg(R_RAM_ADDR_SET,
193 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_WIDTH-1 - xmin));
197 static void _display_on(void)
199 /* Initialisation the display the same way as the original firmware */
201 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
203 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x011b); /* 220 lines, GS=0, SS=1 */
205 /* B/C = 1: n-line inversion form
206 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
207 * frame select signal and an n-line inversion signal.
208 * FLD = 01b: 1 field interlaced scan, external display iface */
209 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
211 /* Address counter updated in horizontal direction; left to right;
212 * vertical increment horizontal increment.
213 * data format for 8bit transfer or spi = 65k (5,6,5) */
214 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
216 /* Replace data on writing to GRAM */
217 lcd_write_reg(R_COMPARE_REG1, 0);
218 lcd_write_reg(R_COMPARE_REG2, 0);
220 /* GON = 0, DTE = 0, D1-0 = 00b */
221 lcd_write_reg(R_DISP_CONTROL1, 0x0000 | r_disp_control_rev);
223 /* Front porch lines: 2; Back porch lines: 2; */
224 lcd_write_reg(R_DISP_CONTROL2, 0x0203);
226 /* Scan cycle = 0 frames */
227 lcd_write_reg(R_DISP_CONTROL3, 0x0000);
229 /* 16 clocks */
230 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0000);
232 /* 18-bit RGB interface (one transfer/pixel)
233 * internal clock operation;
234 * System interface/VSYNC interface */
235 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0000);
238 /* zero everything*/
239 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
241 lcd_delay(10);
243 /* initialise power supply */
245 /* DC12-10 = 000b: Step-up1 = clock/8,
246 * DC02-00 = 000b: Step-up2 = clock/16,
247 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
248 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
250 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
251 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
253 lcd_delay(40);
255 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
257 /* This register is unknown */
258 lcd_write_reg(0x56, 0x80f);
261 lcd_write_reg(R_POWER_CONTROL1, 0x4140);
263 lcd_delay(10);
265 lcd_write_reg(R_POWER_CONTROL2, 0x0000);
266 lcd_write_reg(R_POWER_CONTROL3, 0x0013);
268 lcd_delay(20);
270 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
272 lcd_delay(20);
274 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
276 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0002);
277 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
278 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0182);
279 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0203);
280 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0706);
281 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0006);
282 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0706);
283 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0000);
284 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x030f);
285 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0f08);
288 lcd_write_reg(R_GATE_SCAN_POS, 0);
289 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0);
291 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
292 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
293 lcd_write_reg(R_2ND_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
295 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
297 display_on = true; /* must be done before calling lcd_update() */
298 lcd_update();
301 void lcd_init_device(void)
303 as3525_dbop_init();
305 GPIOA_DIR |= (1<<5);
306 GPIOA_PIN(5) = 0;
307 GPIOA_PIN(4) = 0;
309 lcd_delay(1);
310 GPIOA_PIN(5) = (1<<5);
311 lcd_delay(1);
313 _display_on();
316 #if defined(HAVE_LCD_ENABLE)
317 void lcd_enable(bool on)
319 if (display_on == on)
320 return;
322 if(on)
324 _display_on();
325 send_event(LCD_EVENT_ACTIVATION, NULL);
327 else
329 display_on = false;
330 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
333 #endif
335 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
336 bool lcd_active(void)
338 return display_on;
340 #endif
342 /*** update functions ***/
344 static unsigned lcd_yuv_options = 0;
346 void lcd_yuv_set_options(unsigned options)
348 lcd_yuv_options = options;
352 #ifndef BOOTLOADER
353 static void lcd_window_blit(int xmin, int ymin, int xmax, int ymax)
355 if (!display_flipped)
357 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
358 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
359 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
360 lcd_write_reg(R_RAM_ADDR_SET,
361 (ymin << 8) | (LCD_WIDTH-1 - xmin));
363 else
365 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
366 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
367 lcd_write_reg(R_RAM_ADDR_SET, (ymax << 8) | xmin);
371 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
372 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
373 int width,
374 int stride);
375 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
376 int width,
377 int stride,
378 int x_screen, /* To align dither pattern */
379 int y_screen);
381 /* Performance function to blit a YUV bitmap directly to the LCD
382 * src_x, src_y, width and height should be even
383 * x, y, width and height have to be within LCD bounds
385 void lcd_blit_yuv(unsigned char * const src[3],
386 int src_x, int src_y, int stride,
387 int x, int y, int width, int height)
389 unsigned char const * yuv_src[3];
390 off_t z;
392 /* Sorry, but width and height must be >= 2 or else */
393 width &= ~1;
394 height >>= 1;
396 z = stride*src_y;
397 yuv_src[0] = src[0] + z + src_x;
398 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
399 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
401 lcd_write_reg(R_ENTRY_MODE,
402 display_flipped ? R_ENTRY_MODE_VIDEO_FLIPPED : R_ENTRY_MODE_VIDEO_NORMAL
405 if (lcd_yuv_options & LCD_YUV_DITHER)
409 lcd_window_blit(y, x, y+1, x+width-1);
411 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
413 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
414 yuv_src[0] += stride << 1; /* Skip down two luma lines */
415 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
416 yuv_src[2] += stride >> 1;
417 y += 2;
419 while (--height > 0);
421 else
425 lcd_window_blit(y, x, y+1, x+width-1);
427 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
429 lcd_write_yuv420_lines(yuv_src, width, stride);
430 yuv_src[0] += stride << 1; /* Skip down two luma lines */
431 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
432 yuv_src[2] += stride >> 1;
433 y += 2;
435 while (--height > 0);
439 #endif
442 /* Update the display.
443 This must be called after all other LCD functions that change the display. */
444 void lcd_update(void)
446 if (!display_on)
447 return;
449 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
451 /* Set start position and window */
452 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
454 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
456 dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
459 /* Update a fraction of the display. */
460 void lcd_update_rect(int x, int y, int width, int height)
462 const fb_data *ptr;
464 if (!display_on)
465 return;
467 /* nothing to draw? */
468 if ((width <= 0) || (height <= 0) || (x >= LCD_WIDTH) ||
469 (y >= LCD_HEIGHT) || (x + width <= 0) || (y + height <= 0))
470 return;
472 if (x < 0)
473 { /* clip left */
474 width += x;
475 x = 0;
477 if (y < 0)
478 { /* clip top */
479 height += y;
480 y = 0;
482 if (x + width > LCD_WIDTH)
483 width = LCD_WIDTH - x; /* clip right */
484 if (y + height > LCD_HEIGHT)
485 height = LCD_HEIGHT - y; /* clip bottom */
487 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
489 /* we need to make x and width even to enable 32bit transfers */
490 width = (width + (x & 1) + 1) & ~1;
491 x &= ~1;
493 lcd_window(x, y, x+width-1, y+height-1);
494 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
496 ptr = &lcd_framebuffer[y][x];
500 dbop_write_data(ptr, width);
501 ptr += LCD_WIDTH;
503 while (--height > 0);