e200v2/Fuze: Correct and simplify clipping clipping code in lcd_update_rect().
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / lcd-e200v2.c
blobf4d1a7cf56c881313c9053b1410726b8c9079e95
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_value16(unsigned short value)
125 DBOP_CTRL &= ~(1<<14|1<<13);
126 lcd_delay(10);
127 DBOP_DOUT16 = value;
128 while ((DBOP_STAT & (1<<10)) == 0);
131 static void lcd_write_cmd(int cmd)
133 /* Write register */
134 DBOP_TIMPOL_23 = 0xa167006e;
135 lcd_write_value16(cmd);
137 /* Wait for fifo to empty */
138 while ((DBOP_STAT & (1<<10)) == 0);
140 /* Fuze OF has this loop and it seems to help us now also */
141 int delay = 8;
142 while(delay--);
144 DBOP_TIMPOL_23 = 0xa167e06f;
147 void lcd_write_data(const fb_data* p_bytes, int count)
149 const long *data;
150 if ((int)p_bytes & 0x3)
151 { /* need to do a single 16bit write beforehand if the address is
152 * not word aligned */
153 lcd_write_value16(*p_bytes);
154 count--;p_bytes++;
156 /* from here, 32bit transfers are save
157 * set it to transfer 4*(outputwidth) units at a time,
158 * if bit 12 is set it only does 2 halfwords though */
159 DBOP_CTRL |= (1<<13|1<<14);
160 data = (long*)p_bytes;
161 while (count > 1)
163 DBOP_DOUT32 = *data++;
164 count -= 2;
166 /* Wait if push fifo is full */
167 while ((DBOP_STAT & (1<<6)) != 0);
169 /* While push fifo is not empty */
170 while ((DBOP_STAT & (1<<10)) == 0);
172 /* due to the 32bit alignment requirement or uneven count,
173 * we possibly need to do a 16bit transfer at the end also */
174 if (count > 0)
175 lcd_write_value16(*(fb_data*)data);
178 static void lcd_write_reg(int reg, int value)
180 fb_data data = value;
182 lcd_write_cmd(reg);
183 lcd_write_value16(data);
186 /*** hardware configuration ***/
188 void lcd_set_contrast(int val)
190 (void)val;
193 void lcd_set_invert_display(bool yesno)
195 r_disp_control_rev = yesno ? R_DISP_CONTROL_REV :
196 R_DISP_CONTROL_NORMAL;
198 if (display_on)
200 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
205 static bool display_flipped = false;
207 /* turn the display upside down */
208 void lcd_set_flip(bool yesno)
210 display_flipped = yesno;
212 r_entry_mode = yesno ? R_ENTRY_MODE_HORZ_FLIPPED :
213 R_ENTRY_MODE_HORZ_NORMAL;
216 static void lcd_window(int xmin, int ymin, int xmax, int ymax)
218 if (!display_flipped)
220 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
221 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
222 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | xmin);
224 else
226 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
227 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
228 lcd_write_reg(R_VERT_RAM_ADDR_POS,
229 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_HEIGHT-1 - ymax));
230 lcd_write_reg(R_RAM_ADDR_SET,
231 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_WIDTH-1 - xmin));
235 static void _display_on(void)
237 /* Initialisation the display the same way as the original firmware */
239 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
241 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x011b); /* 220 lines, GS=0, SS=1 */
243 /* B/C = 1: n-line inversion form
244 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
245 * frame select signal and an n-line inversion signal.
246 * FLD = 01b: 1 field interlaced scan, external display iface */
247 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
249 /* Address counter updated in horizontal direction; left to right;
250 * vertical increment horizontal increment.
251 * data format for 8bit transfer or spi = 65k (5,6,5) */
252 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
254 /* Replace data on writing to GRAM */
255 lcd_write_reg(R_COMPARE_REG1, 0);
256 lcd_write_reg(R_COMPARE_REG2, 0);
258 /* GON = 0, DTE = 0, D1-0 = 00b */
259 lcd_write_reg(R_DISP_CONTROL1, 0x0000 | r_disp_control_rev);
261 /* Front porch lines: 2; Back porch lines: 2; */
262 lcd_write_reg(R_DISP_CONTROL2, 0x0203);
264 /* Scan cycle = 0 frames */
265 lcd_write_reg(R_DISP_CONTROL3, 0x0000);
267 /* 16 clocks */
268 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0000);
270 /* 18-bit RGB interface (one transfer/pixel)
271 * internal clock operation;
272 * System interface/VSYNC interface */
273 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0000);
276 /* zero everything*/
277 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
279 lcd_delay(10);
281 /* initialise power supply */
283 /* DC12-10 = 000b: Step-up1 = clock/8,
284 * DC02-00 = 000b: Step-up2 = clock/16,
285 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
286 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
288 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
289 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
291 lcd_delay(40);
293 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
295 /* This register is unknown */
296 lcd_write_reg(0x56, 0x80f);
299 lcd_write_reg(R_POWER_CONTROL1, 0x4140);
301 lcd_delay(10);
303 lcd_write_reg(R_POWER_CONTROL2, 0x0000);
304 lcd_write_reg(R_POWER_CONTROL3, 0x0013);
306 lcd_delay(20);
308 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
310 lcd_delay(20);
312 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
314 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0002);
315 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
316 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0182);
317 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0203);
318 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0706);
319 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0006);
320 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0706);
321 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0000);
322 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x030f);
323 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0f08);
326 lcd_write_reg(R_GATE_SCAN_POS, 0);
327 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0);
329 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
330 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
331 lcd_write_reg(R_2ND_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
333 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
335 display_on = true; /* must be done before calling lcd_update() */
336 lcd_update();
339 void lcd_init_device(void)
341 as3525_dbop_init();
343 GPIOA_DIR |= (1<<5);
344 GPIOA_PIN(5) = 0;
345 GPIOA_PIN(4) = 0;
347 lcd_delay(1);
348 GPIOA_PIN(5) = (1<<5);
349 lcd_delay(1);
351 _display_on();
354 #if defined(HAVE_LCD_ENABLE)
355 void lcd_enable(bool on)
357 if (display_on == on)
358 return;
360 if(on)
362 _display_on();
363 send_event(LCD_EVENT_ACTIVATION, NULL);
365 else
367 display_on = false;
368 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
371 #endif
373 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
374 bool lcd_active(void)
376 return display_on;
378 #endif
380 /*** update functions ***/
382 static unsigned lcd_yuv_options = 0;
384 void lcd_yuv_set_options(unsigned options)
386 lcd_yuv_options = options;
389 static void lcd_window_blit(int xmin, int ymin, int xmax, int ymax)
391 if (!display_flipped)
393 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
394 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
395 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
396 lcd_write_reg(R_RAM_ADDR_SET,
397 (ymin << 8) | (LCD_WIDTH-1 - xmin));
399 else
401 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
402 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
403 lcd_write_reg(R_RAM_ADDR_SET, (ymax << 8) | xmin);
407 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
408 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
409 int width,
410 int stride);
411 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
412 int width,
413 int stride,
414 int x_screen, /* To align dither pattern */
415 int y_screen);
417 /* Performance function to blit a YUV bitmap directly to the LCD
418 * src_x, src_y, width and height should be even
419 * x, y, width and height have to be within LCD bounds
421 void lcd_blit_yuv(unsigned char * const src[3],
422 int src_x, int src_y, int stride,
423 int x, int y, int width, int height)
425 unsigned char const * yuv_src[3];
426 off_t z;
428 /* Sorry, but width and height must be >= 2 or else */
429 width &= ~1;
430 height >>= 1;
432 z = stride*src_y;
433 yuv_src[0] = src[0] + z + src_x;
434 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
435 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
437 lcd_write_reg(R_ENTRY_MODE,
438 display_flipped ? R_ENTRY_MODE_VIDEO_FLIPPED : R_ENTRY_MODE_VIDEO_NORMAL
441 if (lcd_yuv_options & LCD_YUV_DITHER)
445 lcd_window_blit(y, x, y+1, x+width-1);
447 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
449 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
450 yuv_src[0] += stride << 1; /* Skip down two luma lines */
451 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
452 yuv_src[2] += stride >> 1;
453 y += 2;
455 while (--height > 0);
457 else
461 lcd_window_blit(y, x, y+1, x+width-1);
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);
475 /* Update the display.
476 This must be called after all other LCD functions that change the display. */
477 void lcd_update(void)
479 if (!display_on)
480 return;
482 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
484 /* Set start position and window */
485 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
487 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
489 lcd_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
492 /* Update a fraction of the display. */
493 void lcd_update_rect(int x, int y, int width, int height)
495 const fb_data *ptr;
497 if (!display_on)
498 return;
500 /* nothing to draw? */
501 if ((width <= 0) || (height <= 0) || (x >= LCD_WIDTH) ||
502 (y >= LCD_HEIGHT) || (x + width <= 0) || (y + height <= 0))
503 return;
505 if (x < 0)
506 { /* clip left */
507 width += x;
508 x = 0;
510 if (y < 0)
511 { /* clip top */
512 height += y;
513 y = 0;
515 if (x + width > LCD_WIDTH)
516 width = LCD_WIDTH - x; /* clip right */
517 if (y + height > LCD_HEIGHT)
518 height = LCD_HEIGHT - y; /* clip bottom */
520 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
522 lcd_window(x, y, x+width-1, y+height-1);
523 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
525 ptr = &lcd_framebuffer[y][x];
529 lcd_write_data(ptr, width);
530 ptr += LCD_WIDTH;
532 while (--height > 0);