Simplify lcd invert
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / lcd-e200v2.c
blob438020fb685fee6c90a4c361b71ed35cadcccc16
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 #define R_ENTRY_MODE_HORZ_NORMAL 0x7030
79 #define R_ENTRY_MODE_VERT 0x7038
80 #define R_ENTRY_MODE_SOLID_VERT 0x1038
82 static inline void lcd_delay(int x)
84 do {
85 asm volatile ("nop\n");
86 } while (x--);
89 static void as3525_dbop_init(void)
91 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
93 DBOP_TIMPOL_01 = 0xe167e167;
94 DBOP_TIMPOL_23 = 0xe167006e;
96 /* short count: 16 | output data width: 16 | readstrobe line */
97 DBOP_CTRL = (1<<18|1<<12|1<<3);
99 GPIOB_AFSEL = 0xfc;
100 GPIOC_AFSEL = 0xff;
102 DBOP_TIMPOL_23 = 0x6000e;
104 /* short count: 16|enable write|output data width: 16|read strobe line */
105 DBOP_CTRL = (1<<18|1<<16|1<<12|1<<3);
106 DBOP_TIMPOL_01 = 0x6e167;
107 DBOP_TIMPOL_23 = 0xa167e06f;
109 /* TODO: The OF calls some other functions here, but maybe not important */
112 static void lcd_write_cmd(short cmd)
114 /* Write register */
115 DBOP_TIMPOL_23 = 0xa167006e;
116 dbop_write_data(&cmd, 1);
118 /* Wait for fifo to empty */
119 while ((DBOP_STAT & (1<<10)) == 0);
121 /* Fuze OF has this loop and it seems to help us now also */
122 int delay = 8;
123 while(delay--);
125 DBOP_TIMPOL_23 = 0xa167e06f;
129 static void lcd_write_reg(int reg, int value)
131 unsigned short data = value;
133 lcd_write_cmd(reg);
134 dbop_write_data(&data, 1);
137 /*** hardware configuration ***/
139 void lcd_set_contrast(int val)
141 (void)val;
144 void lcd_set_invert_display(bool yesno)
146 if (display_on)
148 lcd_write_reg(R_DISP_CONTROL1, yesno ? 0x33 : 0x37);
153 /* turn the display upside down */
154 void lcd_set_flip(bool yesno)
156 lcd_write_reg(R_DRV_OUTPUT_CONTROL, yesno ? 0x21b : 0x11b);
159 static void lcd_window(int xmin, int ymin, int xmax, int ymax)
161 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
162 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
163 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | xmin);
166 static void _display_on(void)
168 /* Initialisation the display the same way as the original firmware */
170 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
172 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x011b); /* 220 lines, GS=0, SS=1 */
174 /* B/C = 1: n-line inversion form
175 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
176 * frame select signal and an n-line inversion signal.
177 * FLD = 01b: 1 field interlaced scan, external display iface */
178 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
180 /* Address counter updated in horizontal direction; left to right;
181 * vertical increment horizontal increment.
182 * data format for 8bit transfer or spi = 65k (5,6,5) */
183 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ_NORMAL);
185 /* Replace data on writing to GRAM */
186 lcd_write_reg(R_COMPARE_REG1, 0);
187 lcd_write_reg(R_COMPARE_REG2, 0);
189 /* GON = 0, DTE = 0, D1-0 = 00b */
190 lcd_write_reg(R_DISP_CONTROL1, 0x0000);
192 /* Front porch lines: 2; Back porch lines: 2; */
193 lcd_write_reg(R_DISP_CONTROL2, 0x0203);
195 /* Scan cycle = 0 frames */
196 lcd_write_reg(R_DISP_CONTROL3, 0x0000);
198 /* 16 clocks */
199 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0000);
201 /* 18-bit RGB interface (one transfer/pixel)
202 * internal clock operation;
203 * System interface/VSYNC interface */
204 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0000);
207 /* zero everything*/
208 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
210 lcd_delay(10);
212 /* initialise power supply */
214 /* DC12-10 = 000b: Step-up1 = clock/8,
215 * DC02-00 = 000b: Step-up2 = clock/16,
216 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
217 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
219 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
220 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
222 lcd_delay(40);
224 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
226 /* This register is unknown */
227 lcd_write_reg(0x56, 0x80f);
230 lcd_write_reg(R_POWER_CONTROL1, 0x4140);
232 lcd_delay(10);
234 lcd_write_reg(R_POWER_CONTROL2, 0x0000);
235 lcd_write_reg(R_POWER_CONTROL3, 0x0013);
237 lcd_delay(20);
239 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
241 lcd_delay(20);
243 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
245 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0002);
246 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
247 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0182);
248 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0203);
249 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0706);
250 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0006);
251 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0706);
252 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0000);
253 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x030f);
254 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0f08);
257 lcd_write_reg(R_GATE_SCAN_POS, 0);
258 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0);
260 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
261 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
262 lcd_write_reg(R_2ND_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
264 lcd_write_reg(R_DISP_CONTROL1, 0x0037);
266 display_on = true; /* must be done before calling lcd_update() */
267 lcd_update();
270 void lcd_init_device(void)
272 as3525_dbop_init();
274 GPIOA_DIR |= (1<<5);
275 GPIOA_PIN(5) = 0;
276 GPIOA_PIN(4) = 0;
278 lcd_delay(1);
279 GPIOA_PIN(5) = (1<<5);
280 lcd_delay(1);
282 _display_on();
285 #if defined(HAVE_LCD_ENABLE)
286 void lcd_enable(bool on)
288 if (display_on == on)
289 return;
291 if(on)
293 _display_on();
294 send_event(LCD_EVENT_ACTIVATION, NULL);
296 else
298 display_on = false;
299 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
302 #endif
304 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
305 bool lcd_active(void)
307 return display_on;
309 #endif
311 /*** update functions ***/
313 static unsigned lcd_yuv_options = 0;
315 void lcd_yuv_set_options(unsigned options)
317 lcd_yuv_options = options;
321 #ifndef BOOTLOADER
322 static void lcd_window_blit(int xmin, int ymin, int xmax, int ymax)
324 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
325 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
326 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
327 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | (LCD_WIDTH-1 - xmin));
330 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
331 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
332 int width,
333 int stride);
334 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
335 int width,
336 int stride,
337 int x_screen, /* To align dither pattern */
338 int y_screen);
340 /* Performance function to blit a YUV bitmap directly to the LCD
341 * src_x, src_y, width and height should be even
342 * x, y, width and height have to be within LCD bounds
344 void lcd_blit_yuv(unsigned char * const src[3],
345 int src_x, int src_y, int stride,
346 int x, int y, int width, int height)
348 unsigned char const * yuv_src[3];
349 off_t z;
351 /* Sorry, but width and height must be >= 2 or else */
352 width &= ~1;
353 height >>= 1;
355 z = stride*src_y;
356 yuv_src[0] = src[0] + z + src_x;
357 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
358 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
360 if (lcd_yuv_options & LCD_YUV_DITHER)
364 lcd_window_blit(y, x, y+1, x+width-1);
366 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
368 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
369 yuv_src[0] += stride << 1; /* Skip down two luma lines */
370 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
371 yuv_src[2] += stride >> 1;
372 y += 2;
374 while (--height > 0);
376 else
380 lcd_window_blit(y, x, y+1, x+width-1);
382 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
384 lcd_write_yuv420_lines(yuv_src, width, stride);
385 yuv_src[0] += stride << 1; /* Skip down two luma lines */
386 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
387 yuv_src[2] += stride >> 1;
388 y += 2;
390 while (--height > 0);
394 #endif
397 /* Update the display.
398 This must be called after all other LCD functions that change the display. */
399 void lcd_update(void)
401 if (!display_on)
402 return;
404 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ_NORMAL);
406 /* Set start position and window */
407 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
409 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
411 dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
414 /* Update a fraction of the display. */
415 void lcd_update_rect(int x, int y, int width, int height)
417 const fb_data *ptr;
419 if (!display_on)
420 return;
422 /* nothing to draw? */
423 if ((width <= 0) || (height <= 0) || (x >= LCD_WIDTH) ||
424 (y >= LCD_HEIGHT) || (x + width <= 0) || (y + height <= 0))
425 return;
427 if (x < 0)
428 { /* clip left */
429 width += x;
430 x = 0;
432 if (y < 0)
433 { /* clip top */
434 height += y;
435 y = 0;
437 if (x + width > LCD_WIDTH)
438 width = LCD_WIDTH - x; /* clip right */
439 if (y + height > LCD_HEIGHT)
440 height = LCD_HEIGHT - y; /* clip bottom */
442 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ_NORMAL);
444 /* we need to make x and width even to enable 32bit transfers */
445 width = (width + (x & 1) + 1) & ~1;
446 x &= ~1;
448 lcd_window(x, y, x+width-1, y+height-1);
449 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
451 ptr = &lcd_framebuffer[y][x];
455 dbop_write_data(ptr, width);
456 ptr += LCD_WIDTH;
458 while (--height > 0);