1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
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
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
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
)
96 asm volatile ("nop\n");
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);
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);
128 while ((DBOP_STAT
& (1<<10)) == 0);
131 static void lcd_write_cmd(int cmd
)
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 */
144 DBOP_TIMPOL_23
= 0xa167e06f;
147 void lcd_write_data(const fb_data
* p_bytes
, int count
)
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
);
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
;
163 DBOP_DOUT32
= *data
++;
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 */
175 lcd_write_value16(*(fb_data
*)data
);
178 static void lcd_write_reg(int reg
, int value
)
180 fb_data data
= value
;
183 lcd_write_value16(data
);
186 /*** hardware configuration ***/
188 void lcd_set_contrast(int val
)
193 void lcd_set_invert_display(bool yesno
)
195 r_disp_control_rev
= yesno
? R_DISP_CONTROL_REV
:
196 R_DISP_CONTROL_NORMAL
;
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
);
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);
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);
277 lcd_write_reg(R_POWER_CONTROL1
, 0x0000); /* STB = 0, SLP = 0 */
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);
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);
303 lcd_write_reg(R_POWER_CONTROL2
, 0x0000);
304 lcd_write_reg(R_POWER_CONTROL3
, 0x0013);
308 lcd_write_reg(R_POWER_CONTROL4
, 0x6d0e);
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() */
339 void lcd_init_device(void)
348 GPIOA_PIN(5) = (1<<5);
354 #if defined(HAVE_LCD_ENABLE)
355 void lcd_enable(bool on
)
357 if (display_on
== on
)
363 send_event(LCD_EVENT_ACTIVATION
, NULL
);
368 lcd_write_reg(R_POWER_CONTROL1
, 0x0001);
373 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
374 bool lcd_active(void)
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
));
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],
411 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src
[3],
414 int x_screen
, /* To align dither pattern */
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];
428 /* Sorry, but width and height must be >= 2 or else */
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;
455 while (--height
> 0);
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;
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)
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
)
500 /* nothing to draw? */
501 if ((width
<= 0) || (height
<= 0) || (x
>= LCD_WIDTH
) ||
502 (y
>= LCD_HEIGHT
) || (x
+ width
<= 0) || (y
+ height
<= 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
);
532 while (--height
> 0);