1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Greg White
11 * Copyright (C) 2009 by Bob Cousins
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <sys/types.h>
30 #include "lcd-target.h"
32 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
34 static bool lcd_on
= true;
35 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
36 static bool lcd_powered
= true;
38 static unsigned lcd_yuv_options
= 0;
40 /* Copies a rectangle from one framebuffer to another. Can be used in
41 single transfer mode with width = num pixels, and height = 1 which
42 allows a full-width rectangle to be copied more efficiently. */
43 extern void lcd_copy_buffer_rect(fb_data
*dst
, const fb_data
*src
,
44 int width
, int height
);
46 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
53 static unsigned int LCDBANK(unsigned int address
)
55 return ((address
>> 22) & 0xff);
58 static unsigned int LCDBASEU(unsigned int address
)
60 return (address
& ((1 << 22)-1)) >> 1;
63 static unsigned int LCDBASEL(unsigned int address
)
66 return (address
& ((1 << 22)-1)) >> 1;
69 static inline void delay_cycles(volatile int delay
)
71 while(delay
>0) delay
--;
74 static void LCD_CTRL_setup(void)
76 LCDCON1
= (LCD_CLKVAL
<< 8) | (LCD_MMODE
<< 7) | (LCD_PNRMODE
<< 5) |
78 LCDCON2
= (LCD_UPPER_MARGIN
<< 24) | ((LCD_HEIGHT
- 1) << 14) |
79 (LCD_LOWER_MARGIN
<< 6) | (LCD_VSYNC_LEN
<< 0);
80 LCDCON3
= (LCD_LEFT_MARGIN
<< 19) | ((LCD_WIDTH
- 1) << 8) |
81 (LCD_RIGHT_MARGIN
<< 0);
82 LCDCON4
= (LCD_HSYNC_LEN
<< 0);
84 /* HWSWP = 1, INVVFRAM = 1, INVVLINE = 1, FRM565 = 1, All others = 0 */
87 LCDSADDR1
= (LCDBANK((unsigned)FRAME
) << 21) | (LCDBASEU((unsigned)FRAME
));
88 LCDSADDR2
= LCDBASEL((unsigned)FRAME
);
89 LCDSADDR3
= 0x000000F0;
92 static void LCD_CTRL_clock(bool onoff
)
100 GPDCON
&=~0xFFF0FFF0;
101 GPDCON
|= 0xAAA0AAA0;
104 s3c_regset32(&CLKCON
, 0x20); /* enable LCD clock */
105 LCDCON1
|= LCD_ENVID
;
109 GPCCON
&= ~0xFFF000FC;
112 GPDCON
&= ~0xFFF0FFF0;
115 LCDCON1
&= ~LCD_ENVID
; /* Must disable first or bus may freeze */
116 s3c_regclr32(&CLKCON
, 0x20); /* disable LCD clock */
121 static void reset_LCD(bool reset
)
133 /****************************************************************************/
135 static void LCD_SPI_send(const unsigned char *array
, int count
)
139 while ((SPSTA0
&0x01)==0){};
144 static void LCD_SPI_setreg(unsigned char reg
, unsigned char value
)
146 unsigned char regval
[] =
150 LCD_SPI_send(regval
, sizeof(regval
));
153 static void LCD_SPI_SS(bool select
)
155 delay_cycles(0x4FFF);
166 static void LCD_SPI_start(void)
168 s3c_regset32(&CLKCON
, 0x40000); /* enable SPI clock */
177 static void LCD_SPI_stop(void)
182 s3c_regclr32(&CLKCON
, 0x40000); /* disable SPI clock */
185 static void LCD_SPI_init(void)
188 * SPI setup - Some of these registers are known; they are documented in
189 * the wiki. Many thanks to Alex Gerchanovsky for discovering this
193 LCD_CTRL_clock(true);
196 LCD_SPI_setreg(0x0F, 0x01);
197 LCD_SPI_setreg(0x09, 0x06);
198 LCD_SPI_setreg(0x16, 0xA6);
199 LCD_SPI_setreg(0x1E, 0x49);
200 LCD_SPI_setreg(0x1F, 0x26);
201 LCD_SPI_setreg(0x0B, 0x2F);
202 LCD_SPI_setreg(0x0C, 0x2B);
203 LCD_SPI_setreg(0x19, 0x5E);
204 LCD_SPI_setreg(0x1A, 0x15);
205 LCD_SPI_setreg(0x1B, 0x15);
206 LCD_SPI_setreg(0x1D, 0x01);
207 LCD_SPI_setreg(0x00, 0x03);
208 LCD_SPI_setreg(0x01, 0x10);
209 LCD_SPI_setreg(0x02, 0x0A);
210 LCD_SPI_setreg(0x06, 0x04); /* Set the orientation */
211 LCD_SPI_setreg(0x08, 0x2E);
212 LCD_SPI_setreg(0x24, 0x12);
213 LCD_SPI_setreg(0x25, 0x3F);
214 LCD_SPI_setreg(0x26, 0x0B);
215 LCD_SPI_setreg(0x27, 0x00);
216 LCD_SPI_setreg(0x28, 0x00);
217 LCD_SPI_setreg(0x29, 0xF6);
218 LCD_SPI_setreg(0x2A, 0x03);
219 LCD_SPI_setreg(0x2B, 0x0A);
220 LCD_SPI_setreg(0x04, 0x01); /* Turn the display on */
224 /****************************************************************************/
227 void lcd_init_device(void)
231 /* When the Rockbox bootloader starts the framebuffer address is changed
232 * but the LCD display should stay the same til an lcd_update() occurs.
233 * This copies the data from the old framebuffer to the new one to make the
234 * change non-visable to the user.
236 unsigned short *buf
= (unsigned short*)(FRAME
);
237 unsigned short *oldbuf
= (unsigned short*)(LCDSADDR1
<<1);
239 /* The Rockbox bootloader is transitioning from RGB555I to RGB565 mode
240 so convert the frambuffer data accordingly */
241 for(i
=0; i
< 320*240; i
++)
243 *(buf
++) = ((*oldbuf
>>1) & 0x1F) | (*oldbuf
& 0xffc0);
250 GPECON
|= 0x0A800000;
256 s3c_regset32(&CLKCON
, 0x20); /* enable LCD clock */
262 LCD_CTRL_clock(true);
266 #if defined(HAVE_LCD_SLEEP)
267 static void LCD_SPI_powerdown(void)
272 LCD_SPI_setreg(0x04, 0x00);
275 reset_LCD(false); /* This makes a big difference on power */
276 LCD_CTRL_clock(false);
283 /* "not powered" implies "disabled" */
292 #if defined(HAVE_LCD_ENABLE)
293 static void LCD_SPI_powerup(void)
295 LCD_CTRL_clock(true);
298 LCD_SPI_setreg(0x04, 0x01);
304 void lcd_enable(bool state
)
311 /* "enabled" implies "powered" */
315 /* Wait long enough for a frame to be written - yes, it
322 send_event(LCD_EVENT_ACTIVATION
, NULL
);
332 void lcd_set_flip(bool yesno
) {
339 LCD_SPI_setreg(0x06, 0x02);
343 LCD_SPI_setreg(0x06, 0x04);
348 int lcd_default_contrast(void)
350 return DEFAULT_CONTRAST_SETTING
;
353 void lcd_set_contrast(int val
) {
358 LCD_SPI_setreg(0x0B, (unsigned char) val
);
362 void lcd_set_invert_display(bool yesno
) {
369 LCD_SPI_setreg(0x27, 0x10);
373 LCD_SPI_setreg(0x27, 0x00);
378 void lcd_set_flip(bool yesno
)
381 /* Not implemented */
384 int lcd_default_contrast(void)
386 return DEFAULT_CONTRAST_SETTING
;
389 void lcd_set_contrast(int val
)
392 /* Not implemented */
395 void lcd_set_invert_display(bool yesno
)
398 /* Not implemented */
403 /* Update a fraction of the display. */
404 void lcd_update_rect(int x
, int y
, int width
, int height
)
411 if (x
+ width
> LCD_WIDTH
)
412 width
= LCD_WIDTH
- x
; /* Clip right */
414 width
+= x
, x
= 0; /* Clip left */
416 return; /* nothing left to do */
418 if (y
+ height
> LCD_HEIGHT
)
419 height
= LCD_HEIGHT
- y
; /* Clip bottom */
421 height
+= y
, y
= 0; /* Clip top */
423 return; /* nothing left to do */
425 /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer
426 * and lcd_framebuffer */
427 dst
= (fb_data
*)FRAME
+ LCD_WIDTH
*y
+ x
;
428 src
= &lcd_framebuffer
[y
][x
];
430 /* Copy part of the Rockbox framebuffer to the second framebuffer */
431 if (width
< LCD_WIDTH
)
433 /* Not full width - do line-by-line */
434 lcd_copy_buffer_rect(dst
, src
, width
, height
);
438 /* Full width - copy as one line */
439 lcd_copy_buffer_rect(dst
, src
, LCD_WIDTH
*height
, 1);
443 /* Update the display.
444 This must be called after all other LCD functions that change the display. */
445 void lcd_update(void)
450 lcd_copy_buffer_rect((fb_data
*)FRAME
, &lcd_framebuffer
[0][0],
451 LCD_WIDTH
*LCD_HEIGHT
, 1);
454 void lcd_yuv_set_options(unsigned options
)
456 lcd_yuv_options
= options
;
459 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
460 extern void lcd_write_yuv420_lines(fb_data
*dst
,
461 unsigned char const * const src
[3],
464 extern void lcd_write_yuv420_lines_odither(fb_data
*dst
,
465 unsigned char const * const src
[3],
468 int x_screen
, /* To align dither pattern */
470 /* Performance function to blit a YUV bitmap directly to the LCD */
471 /* For the Gigabeat - show it rotated */
472 /* So the LCD_WIDTH is now the height */
473 void lcd_blit_yuv(unsigned char * const src
[3],
474 int src_x
, int src_y
, int stride
,
475 int x
, int y
, int width
, int height
)
477 /* Caches for chroma data so it only need be recaculated every other
479 unsigned char const * yuv_src
[3];
485 /* Sorry, but width and height must be >= 2 or else */
489 y
= LCD_WIDTH
- 1 - y
;
490 fb_data
*dst
= (fb_data
*)FRAME
+ x
* LCD_WIDTH
+ y
;
493 yuv_src
[0] = src
[0] + z
+ src_x
;
494 yuv_src
[1] = src
[1] + (z
>> 2) + (src_x
>> 1);
495 yuv_src
[2] = src
[2] + (yuv_src
[1] - src
[1]);
497 if (lcd_yuv_options
& LCD_YUV_DITHER
)
501 lcd_write_yuv420_lines_odither(dst
, yuv_src
, width
, stride
, y
, x
);
502 yuv_src
[0] += stride
<< 1; /* Skip down two luma lines */
503 yuv_src
[1] += stride
>> 1; /* Skip down one chroma line */
504 yuv_src
[2] += stride
>> 1;
508 while (--height
> 0);
514 lcd_write_yuv420_lines(dst
, yuv_src
, width
, stride
);
515 yuv_src
[0] += stride
<< 1; /* Skip down two luma lines */
516 yuv_src
[1] += stride
>> 1; /* Skip down one chroma line */
517 yuv_src
[2] += stride
>> 1;
520 while (--height
> 0);