Convert lcd_activation callbacks to use the event system to allow for multiple parall...
[kugel-rb.git] / firmware / target / arm / s3c2440 / lcd-s3c2440.c
blobd702b5713a87d6f4d6650d9a0578cd53a52bdec1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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>
24 #include "config.h"
25 #include "system.h"
26 #include "cpu.h"
27 #include "string.h"
28 #include "lcd.h"
29 #include "kernel.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;
37 #endif
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)
51 bool lcd_active(void)
53 return lcd_on;
55 #endif
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)
69 address += 320*240*2;
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) |
81 (LCD_BPPMODE << 1);
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 */
89 LCDCON5 = 0xB01;
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)
98 if(onoff)
100 GPCCON &=~0xFFF000FC;
101 GPCCON |= 0xAAA000A8;
102 GPCUP |= 0xFC0E;
104 GPDCON &=~0xFFF0FFF0;
105 GPDCON |= 0xAAA0AAA0;
106 GPDUP |= 0xFCFC;
108 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
109 LCDCON1 |= LCD_ENVID;
111 else
113 GPCCON &= ~0xFFF000FC;
114 GPCUP &= ~0xFC0E;
116 GPDCON &= ~0xFFF0FFF0;
117 GPDUP &= ~0xFCFC;
119 LCDCON1 &= ~LCD_ENVID; /* Must disable first or bus may freeze */
120 s3c_regclr32(&CLKCON, 0x20); /* disable LCD clock */
124 #ifdef GIGABEAT_F
125 static void reset_LCD(bool reset)
127 GPBCON&=~0xC000;
128 GPBCON|=0x4000;
129 if(reset)
130 GPBDAT|=0x80;
131 else
132 GPBDAT&=~0x80;
134 #endif
137 /****************************************************************************/
138 #ifdef GIGABEAT_F
139 static void LCD_SPI_send(const unsigned char *array, int count)
141 while (count--)
143 while ((SPSTA0&0x01)==0){};
144 SPTDAT0=*array++;
148 static void LCD_SPI_setreg(unsigned char reg, unsigned char value)
150 unsigned char regval[] =
152 0x00,reg,0x01,value
154 LCD_SPI_send(regval, sizeof(regval));
157 static void LCD_SPI_SS(bool select)
159 delay_cycles(0x4FFF);
161 GPBCON&=~0x30000;
162 GPBCON|=0x10000;
164 if(select)
165 GPBDAT|=0x100;
166 else
167 GPBDAT&=~0x100;
170 static void LCD_SPI_start(void)
172 s3c_regset32(&CLKCON, 0x40000); /* enable SPI clock */
173 LCD_SPI_SS(false);
174 SPCON0=0x3E;
175 SPPRE0=24;
177 reset_LCD(true);
178 LCD_SPI_SS(true);
181 static void LCD_SPI_stop(void)
183 LCD_SPI_SS(false);
185 SPCON0 &= ~0x10;
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
194 * sequence.
197 LCD_CTRL_clock(true);
199 LCD_SPI_start();
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 */
225 LCD_SPI_stop();
227 #endif
228 /****************************************************************************/
230 /* LCD init */
231 void lcd_init_device(void)
233 #ifdef BOOTLOADER
234 int i;
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);
248 oldbuf++;
250 #endif
252 /* Set pins up */
253 GPHUP &= 0x600;
254 GPECON |= 0x0A800000;
255 GPEUP |= 0x3800;
256 #ifdef GIGABEAT_F
257 GPBUP |= 0x181;
258 #endif
260 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
262 LCD_CTRL_setup();
263 #ifdef GIGABEAT_F
264 LCD_SPI_init();
265 #else
266 LCD_CTRL_clock(true);
267 #endif
270 #if defined(HAVE_LCD_SLEEP)
271 static void LCD_SPI_powerdown(void)
273 lcd_powered = false;
275 LCD_SPI_start();
276 LCD_SPI_setreg(0x04, 0x00);
277 LCD_SPI_stop();
279 reset_LCD(false); /* This makes a big difference on power */
280 LCD_CTRL_clock(false);
283 void lcd_sleep(void)
285 if (lcd_powered)
287 /* "not powered" implies "disabled" */
288 if (lcd_on)
289 lcd_enable(false);
291 LCD_SPI_powerdown();
294 #endif
296 #if defined(HAVE_LCD_ENABLE)
297 static void LCD_SPI_powerup(void)
299 LCD_CTRL_clock(true);
301 LCD_SPI_start();
302 LCD_SPI_setreg(0x04, 0x01);
303 LCD_SPI_stop();
305 lcd_powered = true;
308 void lcd_enable(bool state)
310 if (state == lcd_on)
311 return;
313 if(state)
315 /* "enabled" implies "powered" */
316 if (!lcd_powered)
318 LCD_SPI_powerup();
319 /* Wait long enough for a frame to be written - yes, it
320 * takes awhile. */
321 sleep(HZ/5);
324 lcd_on = true;
325 lcd_update();
326 send_event(LCD_EVENT_ACTIVATION, NULL);
328 else
330 lcd_on = false;
333 #endif
335 #ifdef GIGABEAT_F
336 void lcd_set_flip(bool yesno) {
337 if (!lcd_on)
338 return;
340 LCD_SPI_start();
341 if(yesno)
343 LCD_SPI_setreg(0x06, 0x02);
345 else
347 LCD_SPI_setreg(0x06, 0x04);
349 LCD_SPI_stop();
352 int lcd_default_contrast(void)
354 return DEFAULT_CONTRAST_SETTING;
357 void lcd_set_contrast(int val) {
358 if (!lcd_on)
359 return;
361 LCD_SPI_start();
362 LCD_SPI_setreg(0x0B, (unsigned char) val);
363 LCD_SPI_stop();
366 void lcd_set_invert_display(bool yesno) {
367 if (!lcd_on)
368 return;
370 LCD_SPI_start();
371 if(yesno)
373 LCD_SPI_setreg(0x27, 0x10);
375 else
377 LCD_SPI_setreg(0x27, 0x00);
379 LCD_SPI_stop();
381 #else
382 void lcd_set_flip(bool yesno)
384 (void)yesno;
385 /* Not implemented */
388 int lcd_default_contrast(void)
390 return DEFAULT_CONTRAST_SETTING;
393 void lcd_set_contrast(int val)
395 (void)val;
396 /* Not implemented */
399 void lcd_set_invert_display(bool yesno)
401 (void)yesno;
402 /* Not implemented */
405 #endif
407 /* Update a fraction of the display. */
408 void lcd_update_rect(int x, int y, int width, int height)
410 fb_data *dst, *src;
412 if (!lcd_on)
413 return;
415 if (x + width > LCD_WIDTH)
416 width = LCD_WIDTH - x; /* Clip right */
417 if (x < 0)
418 width += x, x = 0; /* Clip left */
419 if (width <= 0)
420 return; /* nothing left to do */
422 if (y + height > LCD_HEIGHT)
423 height = LCD_HEIGHT - y; /* Clip bottom */
424 if (y < 0)
425 height += y, y = 0; /* Clip top */
426 if (height <= 0)
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);
440 else
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)
451 if (!lcd_on)
452 return;
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,
461 int height)
463 int w, px;
464 fb_data *dst;
466 if (x + width > current_vp->width)
467 width = current_vp->width - x; /* Clip right */
469 if (x < 0) /* Clip left */
471 width += x;
472 src_x -= x;
473 x = 0;
476 if (width <= 0)
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 */
484 height += y;
485 src_y -= y;
486 y = 0;
489 if (height <= 0)
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];
495 asm volatile (
496 ".rowstart: \r\n"
497 "mov %[w], %[width] \r\n" /* Load width for inner loop */
498 ".nextpixel: \r\n"
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)
513 : [width]"r"(width),
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)
521 #endif
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],
531 int width,
532 int stride);
533 extern void lcd_write_yuv420_lines_odither(fb_data *dst,
534 unsigned char const * const src[3],
535 int width,
536 int stride,
537 int x_screen, /* To align dither pattern */
538 int y_screen);
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
547 line */
548 unsigned char const * yuv_src[3];
549 off_t z;
551 if (!lcd_on)
552 return;
554 /* Sorry, but width and height must be >= 2 or else */
555 width &= ~1;
556 height >>= 1;
558 y = LCD_WIDTH - 1 - y;
559 fb_data *dst = (fb_data*)FRAME + x * LCD_WIDTH + y;
561 z = stride*src_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;
574 dst -= 2;
575 y -= 2;
577 while (--height > 0);
579 else
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;
587 dst -= 2;
589 while (--height > 0);