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 ** This is imported from lcd-16bit.c
42 extern struct viewport
* current_vp
;
44 /* Copies a rectangle from one framebuffer to another. Can be used in
45 single transfer mode with width = num pixels, and height = 1 which
46 allows a full-width rectangle to be copied more efficiently. */
47 extern void lcd_copy_buffer_rect(fb_data
*dst
, const fb_data
*src
,
48 int width
, int height
);
50 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
57 static unsigned int LCDBANK(unsigned int address
)
59 return ((address
>> 22) & 0xff);
62 static unsigned int LCDBASEU(unsigned int address
)
64 return (address
& ((1 << 22)-1)) >> 1;
67 static unsigned int LCDBASEL(unsigned int address
)
70 return (address
& ((1 << 22)-1)) >> 1;
73 static inline void delay_cycles(volatile int delay
)
75 while(delay
>0) delay
--;
78 static void LCD_CTRL_setup(void)
80 LCDCON1
= (LCD_CLKVAL
<< 8) | (LCD_MMODE
<< 7) | (LCD_PNRMODE
<< 5) |
82 LCDCON2
= (LCD_UPPER_MARGIN
<< 24) | ((LCD_HEIGHT
- 1) << 14) |
83 (LCD_LOWER_MARGIN
<< 6) | (LCD_VSYNC_LEN
<< 0);
84 LCDCON3
= (LCD_LEFT_MARGIN
<< 19) | ((LCD_WIDTH
- 1) << 8) |
85 (LCD_RIGHT_MARGIN
<< 0);
86 LCDCON4
= (LCD_HSYNC_LEN
<< 0);
88 /* HWSWP = 1, INVVFRAM = 1, INVVLINE = 1, FRM565 = 1, All others = 0 */
91 LCDSADDR1
= (LCDBANK((unsigned)FRAME
) << 21) | (LCDBASEU((unsigned)FRAME
));
92 LCDSADDR2
= LCDBASEL((unsigned)FRAME
);
93 LCDSADDR3
= 0x000000F0;
96 static void LCD_CTRL_clock(bool onoff
)
100 GPCCON
&=~0xFFF000FC;
101 GPCCON
|= 0xAAA000A8;
104 GPDCON
&=~0xFFF0FFF0;
105 GPDCON
|= 0xAAA0AAA0;
108 s3c_regset32(&CLKCON
, 0x20); /* enable LCD clock */
109 LCDCON1
|= LCD_ENVID
;
113 GPCCON
&= ~0xFFF000FC;
116 GPDCON
&= ~0xFFF0FFF0;
119 LCDCON1
&= ~LCD_ENVID
; /* Must disable first or bus may freeze */
120 s3c_regclr32(&CLKCON
, 0x20); /* disable LCD clock */
125 static void reset_LCD(bool reset
)
137 /****************************************************************************/
139 static void LCD_SPI_send(const unsigned char *array
, int count
)
143 while ((SPSTA0
&0x01)==0){};
148 static void LCD_SPI_setreg(unsigned char reg
, unsigned char value
)
150 unsigned char regval
[] =
154 LCD_SPI_send(regval
, sizeof(regval
));
157 static void LCD_SPI_SS(bool select
)
159 delay_cycles(0x4FFF);
170 static void LCD_SPI_start(void)
172 s3c_regset32(&CLKCON
, 0x40000); /* enable SPI clock */
181 static void LCD_SPI_stop(void)
186 s3c_regclr32(&CLKCON
, 0x40000); /* disable SPI clock */
189 static void LCD_SPI_init(void)
192 * SPI setup - Some of these registers are known; they are documented in
193 * the wiki. Many thanks to Alex Gerchanovsky for discovering this
197 LCD_CTRL_clock(true);
200 LCD_SPI_setreg(0x0F, 0x01);
201 LCD_SPI_setreg(0x09, 0x06);
202 LCD_SPI_setreg(0x16, 0xA6);
203 LCD_SPI_setreg(0x1E, 0x49);
204 LCD_SPI_setreg(0x1F, 0x26);
205 LCD_SPI_setreg(0x0B, 0x2F);
206 LCD_SPI_setreg(0x0C, 0x2B);
207 LCD_SPI_setreg(0x19, 0x5E);
208 LCD_SPI_setreg(0x1A, 0x15);
209 LCD_SPI_setreg(0x1B, 0x15);
210 LCD_SPI_setreg(0x1D, 0x01);
211 LCD_SPI_setreg(0x00, 0x03);
212 LCD_SPI_setreg(0x01, 0x10);
213 LCD_SPI_setreg(0x02, 0x0A);
214 LCD_SPI_setreg(0x06, 0x04); /* Set the orientation */
215 LCD_SPI_setreg(0x08, 0x2E);
216 LCD_SPI_setreg(0x24, 0x12);
217 LCD_SPI_setreg(0x25, 0x3F);
218 LCD_SPI_setreg(0x26, 0x0B);
219 LCD_SPI_setreg(0x27, 0x00);
220 LCD_SPI_setreg(0x28, 0x00);
221 LCD_SPI_setreg(0x29, 0xF6);
222 LCD_SPI_setreg(0x2A, 0x03);
223 LCD_SPI_setreg(0x2B, 0x0A);
224 LCD_SPI_setreg(0x04, 0x01); /* Turn the display on */
228 /****************************************************************************/
231 void lcd_init_device(void)
235 /* When the Rockbox bootloader starts the framebuffer address is changed
236 * but the LCD display should stay the same til an lcd_update() occurs.
237 * This copies the data from the old framebuffer to the new one to make the
238 * change non-visable to the user.
240 unsigned short *buf
= (unsigned short*)(FRAME
);
241 unsigned short *oldbuf
= (unsigned short*)(LCDSADDR1
<<1);
243 /* The Rockbox bootloader is transitioning from RGB555I to RGB565 mode
244 so convert the frambuffer data accordingly */
245 for(i
=0; i
< 320*240; i
++)
247 *(buf
++) = ((*oldbuf
>>1) & 0x1F) | (*oldbuf
& 0xffc0);
254 GPECON
|= 0x0A800000;
260 s3c_regset32(&CLKCON
, 0x20); /* enable LCD clock */
266 LCD_CTRL_clock(true);
270 #if defined(HAVE_LCD_SLEEP)
271 static void LCD_SPI_powerdown(void)
276 LCD_SPI_setreg(0x04, 0x00);
279 reset_LCD(false); /* This makes a big difference on power */
280 LCD_CTRL_clock(false);
287 /* "not powered" implies "disabled" */
296 #if defined(HAVE_LCD_ENABLE)
297 static void LCD_SPI_powerup(void)
299 LCD_CTRL_clock(true);
302 LCD_SPI_setreg(0x04, 0x01);
308 void lcd_enable(bool state
)
315 /* "enabled" implies "powered" */
319 /* Wait long enough for a frame to be written - yes, it
326 send_event(LCD_EVENT_ACTIVATION
, NULL
);
336 void lcd_set_flip(bool yesno
) {
343 LCD_SPI_setreg(0x06, 0x02);
347 LCD_SPI_setreg(0x06, 0x04);
352 int lcd_default_contrast(void)
354 return DEFAULT_CONTRAST_SETTING
;
357 void lcd_set_contrast(int val
) {
362 LCD_SPI_setreg(0x0B, (unsigned char) val
);
366 void lcd_set_invert_display(bool yesno
) {
373 LCD_SPI_setreg(0x27, 0x10);
377 LCD_SPI_setreg(0x27, 0x00);
382 void lcd_set_flip(bool yesno
)
385 /* Not implemented */
388 int lcd_default_contrast(void)
390 return DEFAULT_CONTRAST_SETTING
;
393 void lcd_set_contrast(int val
)
396 /* Not implemented */
399 void lcd_set_invert_display(bool yesno
)
402 /* Not implemented */
407 /* Update a fraction of the display. */
408 void lcd_update_rect(int x
, int y
, int width
, int height
)
415 if (x
+ width
> LCD_WIDTH
)
416 width
= LCD_WIDTH
- x
; /* Clip right */
418 width
+= x
, x
= 0; /* Clip left */
420 return; /* nothing left to do */
422 if (y
+ height
> LCD_HEIGHT
)
423 height
= LCD_HEIGHT
- y
; /* Clip bottom */
425 height
+= y
, y
= 0; /* Clip top */
427 return; /* nothing left to do */
429 /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer
430 * and lcd_framebuffer */
431 dst
= (fb_data
*)FRAME
+ LCD_WIDTH
*y
+ x
;
432 src
= &lcd_framebuffer
[y
][x
];
434 /* Copy part of the Rockbox framebuffer to the second framebuffer */
435 if (width
< LCD_WIDTH
)
437 /* Not full width - do line-by-line */
438 lcd_copy_buffer_rect(dst
, src
, width
, height
);
442 /* Full width - copy as one line */
443 lcd_copy_buffer_rect(dst
, src
, LCD_WIDTH
*height
, 1);
447 /* Update the display.
448 This must be called after all other LCD functions that change the display. */
449 void lcd_update(void)
454 lcd_copy_buffer_rect((fb_data
*)FRAME
, &lcd_framebuffer
[0][0],
455 LCD_WIDTH
*LCD_HEIGHT
, 1);
458 #if defined(TOSHIBA_GIGABEAT_F) || defined(TOSHIBA_GIGABEAT_S)
459 void lcd_bitmap_transparent_part(const fb_data
*src
, int src_x
, int src_y
,
460 int stride
, int x
, int y
, int width
,
466 if (x
+ width
> current_vp
->width
)
467 width
= current_vp
->width
- x
; /* Clip right */
469 if (x
< 0) /* Clip left */
477 return; /* nothing left to do */
479 if (y
+ height
> current_vp
->height
)
480 height
= current_vp
->height
- y
; /* Clip bottom */
482 if (y
< 0) /* Clip top */
490 return; /* nothing left to do */
492 src
+= stride
* src_y
+ src_x
; /* move starting point */
493 dst
= &lcd_framebuffer
[current_vp
->y
+y
][current_vp
->x
+x
];
497 "mov %[w], %[width] \r\n" /* Load width for inner loop */
499 "ldrh %[px], [%[s]], #2 \r\n" /* Load src pixel */
500 "add %[d], %[d], #2 \r\n" /* Uncoditionally increment dst */
501 "cmp %[px], %[fgcolor] \r\n" /* Compare to foreground color */
502 "streqh %[fgpat], [%[d], #-2] \r\n" /* Store foregroud if match */
503 "cmpne %[px], %[transcolor] \r\n" /* Compare to transparent color */
504 "strneh %[px], [%[d], #-2] \r\n" /* Store dst if not transparent */
505 "subs %[w], %[w], #1 \r\n" /* Width counter has run down? */
506 "bgt .nextpixel \r\n" /* More in this row? */
507 "add %[s], %[s], %[sstp], lsl #1 \r\n" /* Skip over to start of next line */
508 "add %[d], %[d], %[dstp], lsl #1 \r\n"
509 "subs %[h], %[h], #1 \r\n" /* Height counter has run down? */
510 "bgt .rowstart \r\n" /* More rows? */
511 : [w
]"=&r"(w
), [h
]"+&r"(height
), [px
]"=&r"(px
),
512 [s
]"+&r"(src
), [d
]"+&r"(dst
)
514 [sstp
]"r"(stride
- width
),
515 [dstp
]"r"(LCD_WIDTH
- width
),
516 [transcolor
]"r"(TRANSPARENT_COLOR
),
517 [fgcolor
]"r"(REPLACEWITHFG_COLOR
),
518 [fgpat
]"r"(current_vp
->fg_pattern
)
523 void lcd_yuv_set_options(unsigned options
)
525 lcd_yuv_options
= options
;
528 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
529 extern void lcd_write_yuv420_lines(fb_data
*dst
,
530 unsigned char const * const src
[3],
533 extern void lcd_write_yuv420_lines_odither(fb_data
*dst
,
534 unsigned char const * const src
[3],
537 int x_screen
, /* To align dither pattern */
539 /* Performance function to blit a YUV bitmap directly to the LCD */
540 /* For the Gigabeat - show it rotated */
541 /* So the LCD_WIDTH is now the height */
542 void lcd_blit_yuv(unsigned char * const src
[3],
543 int src_x
, int src_y
, int stride
,
544 int x
, int y
, int width
, int height
)
546 /* Caches for chroma data so it only need be recaculated every other
548 unsigned char const * yuv_src
[3];
554 /* Sorry, but width and height must be >= 2 or else */
558 y
= LCD_WIDTH
- 1 - y
;
559 fb_data
*dst
= (fb_data
*)FRAME
+ x
* LCD_WIDTH
+ y
;
562 yuv_src
[0] = src
[0] + z
+ src_x
;
563 yuv_src
[1] = src
[1] + (z
>> 2) + (src_x
>> 1);
564 yuv_src
[2] = src
[2] + (yuv_src
[1] - src
[1]);
566 if (lcd_yuv_options
& LCD_YUV_DITHER
)
570 lcd_write_yuv420_lines_odither(dst
, yuv_src
, width
, stride
, y
, x
);
571 yuv_src
[0] += stride
<< 1; /* Skip down two luma lines */
572 yuv_src
[1] += stride
>> 1; /* Skip down one chroma line */
573 yuv_src
[2] += stride
>> 1;
577 while (--height
> 0);
583 lcd_write_yuv420_lines(dst
, yuv_src
, width
, stride
);
584 yuv_src
[0] += stride
<< 1; /* Skip down two luma lines */
585 yuv_src
[1] += stride
>> 1; /* Skip down one chroma line */
586 yuv_src
[2] += stride
>> 1;
589 while (--height
> 0);