Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / firmware / target / arm / s3c2440 / lcd-s3c2440.c
blobb9f7d3ef3d7d22d07264d1f744ff01f1bb7d298e
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> /* off_t */
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 /* 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)
47 bool lcd_active(void)
49 return lcd_on;
51 #endif
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)
65 address += 320*240*2;
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) |
77 (LCD_BPPMODE << 1);
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 */
85 LCDCON5 = 0xB01;
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)
94 if(onoff)
96 GPCCON &=~0xFFF000FC;
97 GPCCON |= 0xAAA000A8;
98 GPCUP |= 0xFC0E;
100 GPDCON &=~0xFFF0FFF0;
101 GPDCON |= 0xAAA0AAA0;
102 GPDUP |= 0xFCFC;
104 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
105 LCDCON1 |= LCD_ENVID;
107 else
109 GPCCON &= ~0xFFF000FC;
110 GPCUP &= ~0xFC0E;
112 GPDCON &= ~0xFFF0FFF0;
113 GPDUP &= ~0xFCFC;
115 LCDCON1 &= ~LCD_ENVID; /* Must disable first or bus may freeze */
116 s3c_regclr32(&CLKCON, 0x20); /* disable LCD clock */
120 #ifdef GIGABEAT_F
121 static void reset_LCD(bool reset)
123 GPBCON&=~0xC000;
124 GPBCON|=0x4000;
125 if(reset)
126 GPBDAT|=0x80;
127 else
128 GPBDAT&=~0x80;
130 #endif
133 /****************************************************************************/
134 #ifdef GIGABEAT_F
135 static void LCD_SPI_send(const unsigned char *array, int count)
137 while (count--)
139 while ((SPSTA0&0x01)==0){};
140 SPTDAT0=*array++;
144 static void LCD_SPI_setreg(unsigned char reg, unsigned char value)
146 unsigned char regval[] =
148 0x00,reg,0x01,value
150 LCD_SPI_send(regval, sizeof(regval));
153 static void LCD_SPI_SS(bool select)
155 delay_cycles(0x4FFF);
157 GPBCON&=~0x30000;
158 GPBCON|=0x10000;
160 if(select)
161 GPBDAT|=0x100;
162 else
163 GPBDAT&=~0x100;
166 static void LCD_SPI_start(void)
168 s3c_regset32(&CLKCON, 0x40000); /* enable SPI clock */
169 LCD_SPI_SS(false);
170 SPCON0=0x3E;
171 SPPRE0=24;
173 reset_LCD(true);
174 LCD_SPI_SS(true);
177 static void LCD_SPI_stop(void)
179 LCD_SPI_SS(false);
181 SPCON0 &= ~0x10;
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
190 * sequence.
193 LCD_CTRL_clock(true);
195 LCD_SPI_start();
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 */
221 LCD_SPI_stop();
223 #endif
224 /****************************************************************************/
226 /* LCD init */
227 void lcd_init_device(void)
229 #ifdef BOOTLOADER
230 int i;
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);
244 oldbuf++;
246 #endif
248 /* Set pins up */
249 GPHUP &= 0x600;
250 GPECON |= 0x0A800000;
251 GPEUP |= 0x3800;
252 #ifdef GIGABEAT_F
253 GPBUP |= 0x181;
254 #endif
256 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
258 LCD_CTRL_setup();
259 #ifdef GIGABEAT_F
260 LCD_SPI_init();
261 #else
262 LCD_CTRL_clock(true);
263 #endif
266 #if defined(HAVE_LCD_SLEEP)
267 static void LCD_SPI_powerdown(void)
269 lcd_powered = false;
271 LCD_SPI_start();
272 LCD_SPI_setreg(0x04, 0x00);
273 LCD_SPI_stop();
275 reset_LCD(false); /* This makes a big difference on power */
276 LCD_CTRL_clock(false);
279 void lcd_sleep(void)
281 if (lcd_powered)
283 /* "not powered" implies "disabled" */
284 if (lcd_on)
285 lcd_enable(false);
287 LCD_SPI_powerdown();
290 #endif
292 #if defined(HAVE_LCD_ENABLE)
293 static void LCD_SPI_powerup(void)
295 LCD_CTRL_clock(true);
297 LCD_SPI_start();
298 LCD_SPI_setreg(0x04, 0x01);
299 LCD_SPI_stop();
301 lcd_powered = true;
304 void lcd_enable(bool state)
306 if (state == lcd_on)
307 return;
309 if(state)
311 /* "enabled" implies "powered" */
312 if (!lcd_powered)
314 LCD_SPI_powerup();
315 /* Wait long enough for a frame to be written - yes, it
316 * takes awhile. */
317 sleep(HZ/5);
320 lcd_on = true;
321 lcd_update();
322 send_event(LCD_EVENT_ACTIVATION, NULL);
324 else
326 lcd_on = false;
329 #endif
331 #ifdef GIGABEAT_F
332 void lcd_set_flip(bool yesno) {
333 if (!lcd_on)
334 return;
336 LCD_SPI_start();
337 if(yesno)
339 LCD_SPI_setreg(0x06, 0x02);
341 else
343 LCD_SPI_setreg(0x06, 0x04);
345 LCD_SPI_stop();
348 int lcd_default_contrast(void)
350 return DEFAULT_CONTRAST_SETTING;
353 void lcd_set_contrast(int val) {
354 if (!lcd_on)
355 return;
357 LCD_SPI_start();
358 LCD_SPI_setreg(0x0B, (unsigned char) val);
359 LCD_SPI_stop();
362 void lcd_set_invert_display(bool yesno) {
363 if (!lcd_on)
364 return;
366 LCD_SPI_start();
367 if(yesno)
369 LCD_SPI_setreg(0x27, 0x10);
371 else
373 LCD_SPI_setreg(0x27, 0x00);
375 LCD_SPI_stop();
377 #else
378 void lcd_set_flip(bool yesno)
380 (void)yesno;
381 /* Not implemented */
384 int lcd_default_contrast(void)
386 return DEFAULT_CONTRAST_SETTING;
389 void lcd_set_contrast(int val)
391 (void)val;
392 /* Not implemented */
395 void lcd_set_invert_display(bool yesno)
397 (void)yesno;
398 /* Not implemented */
401 #endif
403 /* Update a fraction of the display. */
404 void lcd_update_rect(int x, int y, int width, int height)
406 fb_data *dst, *src;
408 if (!lcd_on)
409 return;
411 if (x + width > LCD_WIDTH)
412 width = LCD_WIDTH - x; /* Clip right */
413 if (x < 0)
414 width += x, x = 0; /* Clip left */
415 if (width <= 0)
416 return; /* nothing left to do */
418 if (y + height > LCD_HEIGHT)
419 height = LCD_HEIGHT - y; /* Clip bottom */
420 if (y < 0)
421 height += y, y = 0; /* Clip top */
422 if (height <= 0)
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);
436 else
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)
447 if (!lcd_on)
448 return;
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],
462 int width,
463 int stride);
464 extern void lcd_write_yuv420_lines_odither(fb_data *dst,
465 unsigned char const * const src[3],
466 int width,
467 int stride,
468 int x_screen, /* To align dither pattern */
469 int y_screen);
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
478 line */
479 unsigned char const * yuv_src[3];
480 off_t z;
482 if (!lcd_on)
483 return;
485 /* Sorry, but width and height must be >= 2 or else */
486 width &= ~1;
487 height >>= 1;
489 y = LCD_WIDTH - 1 - y;
490 fb_data *dst = (fb_data*)FRAME + x * LCD_WIDTH + y;
492 z = stride*src_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;
505 dst -= 2;
506 y -= 2;
508 while (--height > 0);
510 else
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;
518 dst -= 2;
520 while (--height > 0);