Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by Rob Purchase |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include "hwcompat.h" |
| 24 | #include "kernel.h" |
| 25 | #include "lcd.h" |
| 26 | #include "system.h" |
| 27 | #include "cpu.h" |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 28 | |
| 29 | /* GPIO A pins for LCD panel SDI interface */ |
| 30 | |
| 31 | #define LTV250QV_CS (1<<24) |
| 32 | #define LTV250QV_SCL (1<<25) |
| 33 | #define LTV250QV_SDI (1<<26) |
| 34 | |
| 35 | /* LCD Controller registers */ |
| 36 | |
| 37 | #define LCDC_CTRL (*(volatile unsigned long *)0xF0000000) |
| 38 | #define LCDC_CLKDIV (*(volatile unsigned long *)0xF0000008) |
| 39 | #define LCDC_HTIME1 (*(volatile unsigned long *)0xF000000C) |
| 40 | #define LCDC_HTIME2 (*(volatile unsigned long *)0xF0000010) |
| 41 | #define LCDC_VTIME1 (*(volatile unsigned long *)0xF0000014) |
| 42 | #define LCDC_VTIME2 (*(volatile unsigned long *)0xF0000018) |
| 43 | #define LCDC_VTIME3 (*(volatile unsigned long *)0xF000001C) |
| 44 | #define LCDC_VTIME4 (*(volatile unsigned long *)0xF0000020) |
| 45 | #define LCDC_DS (*(volatile unsigned long *)0xF000005C) |
| 46 | #define LCDC_I1CTRL (*(volatile unsigned long *)0xF000008C) |
| 47 | #define LCDC_I1POS (*(volatile unsigned long *)0xF0000090) |
| 48 | #define LCDC_I1SIZE (*(volatile unsigned long *)0xF0000094) |
| 49 | #define LCDC_I1BASE (*(volatile unsigned long *)0xF0000098) |
| 50 | #define LCDC_I1OFF (*(volatile unsigned long *)0xF00000A8) |
| 51 | #define LCDC_I1SCALE (*(volatile unsigned long *)0xF00000AC) |
| 52 | |
| 53 | /* Power and display status */ |
| 54 | static bool display_on = false; /* Is the display turned on? */ |
| 55 | |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 56 | static unsigned lcd_yuv_options = 0; |
| 57 | |
| 58 | /* Framebuffer copy as seen by the hardware */ |
| 59 | fb_data lcd_driver_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH]; |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 60 | |
| 61 | int lcd_default_contrast(void) |
| 62 | { |
| 63 | return 0x1f; |
| 64 | } |
| 65 | |
| 66 | void lcd_set_contrast(int val) |
| 67 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 68 | /* TODO: This won't be implemented until the S6F2002 controller |
| 69 | is better understood (nb: registers 16-23 control gamma). */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 70 | (void)val; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* LTV250QV panel functions */ |
| 75 | |
| 76 | static void ltv250qv_write(unsigned int command) |
| 77 | { |
| 78 | int i; |
| 79 | |
| 80 | GPIOA_CLEAR = LTV250QV_CS; |
| 81 | |
| 82 | for (i = 23; i >= 0; i--) |
| 83 | { |
| 84 | GPIOA_CLEAR = LTV250QV_SCL; |
| 85 | |
| 86 | if ((command>>i) & 1) |
| 87 | GPIOA_SET = LTV250QV_SDI; |
| 88 | else |
| 89 | GPIOA_CLEAR = LTV250QV_SDI; |
| 90 | |
| 91 | GPIOA_SET = LTV250QV_SCL; |
| 92 | } |
| 93 | |
| 94 | GPIOA_SET = LTV250QV_CS; |
| 95 | } |
| 96 | |
| 97 | static void lcd_write_reg(unsigned char reg, unsigned short val) |
| 98 | { |
| 99 | ltv250qv_write(0x740000 | reg); |
| 100 | ltv250qv_write(0x760000 | val); |
| 101 | } |
| 102 | |
| 103 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 104 | /* |
| 105 | TEMP: Rough millisecond delay routine used by the LCD panel init sequence. |
| 106 | PCK_TCT must first have been initialised to 2Mhz by calling clock_init(). |
| 107 | */ |
| 108 | static void sleep_ms(unsigned int ms) |
| 109 | { |
| 110 | /* disable timer */ |
| 111 | TCFG1 = 0; |
| 112 | |
| 113 | /* set Timer1 reference value based on 125kHz tick */ |
| 114 | TREF1 = ms * 125; |
| 115 | |
| 116 | /* single count, zero the counter, divider = 16 [2^(3+1)], enable */ |
| 117 | TCFG1 = (1<<9) | (1<<8) | (3<<4) | 1; |
| 118 | |
| 119 | /* wait until Timer1 ref reached */ |
| 120 | while (!(TIREQ & TF1)) {}; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void lcd_display_on(void) |
| 125 | { |
| 126 | /* power on sequence as per the D2 firmware */ |
| 127 | GPIOA_SET = (1<<16); |
| 128 | |
| 129 | sleep_ms(10); |
| 130 | |
| 131 | lcd_write_reg(1, 0x1D); |
| 132 | lcd_write_reg(2, 0x0); |
| 133 | lcd_write_reg(3, 0x0); |
| 134 | lcd_write_reg(4, 0x0); |
| 135 | lcd_write_reg(5, 0x40A3); |
| 136 | lcd_write_reg(6, 0x0); |
| 137 | lcd_write_reg(7, 0x0); |
| 138 | lcd_write_reg(8, 0x0); |
| 139 | lcd_write_reg(9, 0x0); |
| 140 | lcd_write_reg(10, 0x0); |
| 141 | lcd_write_reg(16, 0x0); |
| 142 | lcd_write_reg(17, 0x0); |
| 143 | lcd_write_reg(18, 0x0); |
| 144 | lcd_write_reg(19, 0x0); |
| 145 | lcd_write_reg(20, 0x0); |
| 146 | lcd_write_reg(21, 0x0); |
| 147 | lcd_write_reg(22, 0x0); |
| 148 | lcd_write_reg(23, 0x0); |
| 149 | lcd_write_reg(24, 0x0); |
| 150 | lcd_write_reg(25, 0x0); |
| 151 | sleep_ms(10); |
| 152 | |
| 153 | lcd_write_reg(9, 0x4055); |
| 154 | lcd_write_reg(10, 0x0); |
| 155 | sleep_ms(40); |
| 156 | |
| 157 | lcd_write_reg(10, 0x2000); |
| 158 | sleep_ms(40); |
| 159 | |
| 160 | lcd_write_reg(1, 0xC01D); |
| 161 | lcd_write_reg(2, 0x204); |
| 162 | lcd_write_reg(3, 0xE100); |
| 163 | lcd_write_reg(4, 0x1000); |
| 164 | lcd_write_reg(5, 0x5033); |
| 165 | lcd_write_reg(6, 0x4); |
| 166 | lcd_write_reg(7, 0x30); |
| 167 | lcd_write_reg(8, 0x41C); |
| 168 | lcd_write_reg(16, 0x207); |
| 169 | lcd_write_reg(17, 0x702); |
| 170 | lcd_write_reg(18, 0xB05); |
| 171 | lcd_write_reg(19, 0xB05); |
| 172 | lcd_write_reg(20, 0x707); |
| 173 | lcd_write_reg(21, 0x507); |
| 174 | lcd_write_reg(22, 0x103); |
| 175 | lcd_write_reg(23, 0x406); |
| 176 | lcd_write_reg(24, 0x2); |
| 177 | lcd_write_reg(25, 0x0); |
| 178 | sleep_ms(60); |
| 179 | |
| 180 | lcd_write_reg(9, 0xA55); |
| 181 | lcd_write_reg(10, 0x111F); |
| 182 | sleep_ms(10); |
| 183 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 184 | /* tell that we're on now */ |
| 185 | display_on = true; |
| 186 | } |
| 187 | |
| 188 | static void lcd_display_off(void) |
| 189 | { |
| 190 | /* block drawing operations and changing of first */ |
| 191 | display_on = false; |
| 192 | |
| 193 | /* LQV shutdown sequence */ |
| 194 | lcd_write_reg(9, 0x55); |
| 195 | lcd_write_reg(10, 0x1417); |
| 196 | lcd_write_reg(5, 0x4003); |
| 197 | sleep_ms(10); |
| 198 | |
| 199 | lcd_write_reg(9, 0x0); |
| 200 | sleep_ms(10); |
| 201 | |
| 202 | /* kill power to LCD panel (unconfirmed) */ |
| 203 | GPIOA_CLEAR = (1<<16); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | void lcd_enable(bool on) |
| 208 | { |
| 209 | if (on == display_on) |
| 210 | return; |
| 211 | |
| 212 | if (on) |
| 213 | { |
Rob Purchase | 91202e1 | 2008-04-04 20:55:58 +0000 | [diff] [blame] | 214 | lcd_display_on(); |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 215 | LCDC_CTRL |= 1; /* controller enable */ |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 216 | lcd_update(); /* Resync display */ |
Michael Sevakis | adf2e4c | 2008-05-28 10:17:16 +0000 | [diff] [blame] | 217 | lcd_call_enable_hook(); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
Rob Purchase | 91202e1 | 2008-04-04 20:55:58 +0000 | [diff] [blame] | 221 | LCDC_CTRL &= ~1; /* controller disable */ |
| 222 | lcd_display_off(); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | bool lcd_enabled(void) |
| 227 | { |
| 228 | return display_on; |
| 229 | } |
| 230 | |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 231 | /* TODO: implement lcd_sleep() and separate out the power on/off functions */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 232 | |
| 233 | void lcd_init_device(void) |
| 234 | { |
| 235 | BCLKCTR |= 4; /* enable LCD bus clock */ |
| 236 | |
| 237 | /* set PCK_LCD to 108Mhz */ |
| 238 | PCLK_LCD &= ~PCK_EN; |
| 239 | PCLK_LCD = PCK_EN | (CKSEL_PLL1<<24) | 1; /* source = PLL1, divided by 2 */ |
| 240 | |
| 241 | /* reset the LCD controller */ |
| 242 | SWRESET |= 4; |
| 243 | SWRESET &= ~4; |
| 244 | |
| 245 | /* set port configuration */ |
| 246 | PORTCFG1 &= ~0xC0000000; |
| 247 | PORTCFG1 &= ~0x3FC0; |
| 248 | PORTCFG2 &= ~0x100; |
| 249 | |
| 250 | /* set physical display size */ |
| 251 | LCDC_DS = (LCD_HEIGHT<<16) | LCD_WIDTH; |
| 252 | |
| 253 | LCDC_HTIME1 = (0x2d<<16) | 0x3bf; |
| 254 | LCDC_HTIME2 = (1<<16) | 1; |
| 255 | LCDC_VTIME1 = LCDC_VTIME3 = (0<<16) | 239; |
| 256 | LCDC_VTIME2 = LCDC_VTIME4 = (1<<16) | 3; |
| 257 | |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 258 | LCDC_I1BASE = (unsigned int)lcd_driver_framebuffer; |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 259 | LCDC_I1SIZE = (LCD_HEIGHT<<16) | LCD_WIDTH; /* image 1 size */ |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 260 | LCDC_I1POS = (0<<16) | 0; /* position */ |
| 261 | LCDC_I1OFF = 0; /* address offset */ |
| 262 | LCDC_I1SCALE = 0; /* scaling */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 263 | LCDC_I1CTRL = 5; /* 565bpp (7 = 888bpp) */ |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 264 | LCDC_CTRL &= ~(1<<28); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 265 | |
| 266 | LCDC_CLKDIV = (LCDC_CLKDIV &~ 0xFF00FF) | (1<<16) | 2; /* and this means? */ |
| 267 | |
| 268 | /* set and clear various flags - not investigated yet */ |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 269 | LCDC_CTRL &~ 0x090006AA; /* clear bits 1,3,5,7,9,10,24,27 */ |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 270 | LCDC_CTRL |= 0x02800144; /* set bits 2,6,8,25,23 */ |
| 271 | LCDC_CTRL = (LCDC_CTRL &~ 0xF0000) | 0x20000; |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 272 | LCDC_CTRL = (LCDC_CTRL &~ 0x700000) | 0x700000; |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 273 | |
| 274 | /* enable LCD controller */ |
| 275 | LCDC_CTRL |= 1; |
Rob Purchase | 562b9de | 2008-03-05 00:21:56 +0000 | [diff] [blame] | 276 | |
| 277 | /* enable LTV250QV panel */ |
| 278 | lcd_display_on(); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
| 282 | /*** Update functions ***/ |
| 283 | |
| 284 | |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 285 | /* Copies a rectangle from one framebuffer to another. Can be used in |
| 286 | single transfer mode with width = num pixels, and height = 1 which |
| 287 | allows a full-width rectangle to be copied more efficiently. */ |
| 288 | extern void lcd_copy_buffer_rect(fb_data *dst, const fb_data *src, |
| 289 | int width, int height); |
| 290 | |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 291 | /* Update the display. |
| 292 | This must be called after all other LCD functions that change the display. */ |
| 293 | void lcd_update(void) ICODE_ATTR; |
| 294 | void lcd_update(void) |
| 295 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 296 | if (!display_on) |
| 297 | return; |
| 298 | |
| 299 | lcd_copy_buffer_rect(&lcd_driver_framebuffer[0][0], |
| 300 | &lcd_framebuffer[0][0], LCD_WIDTH*LCD_HEIGHT, 1); |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Update a fraction of the display. */ |
| 304 | void lcd_update_rect(int, int, int, int) ICODE_ATTR; |
| 305 | void lcd_update_rect(int x, int y, int width, int height) |
| 306 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 307 | fb_data *dst, *src; |
| 308 | |
| 309 | if (!display_on) |
| 310 | return; |
| 311 | |
| 312 | if (x + width > LCD_WIDTH) |
| 313 | width = LCD_WIDTH - x; /* Clip right */ |
| 314 | if (x < 0) |
| 315 | width += x, x = 0; /* Clip left */ |
| 316 | if (width <= 0) |
| 317 | return; /* nothing left to do */ |
| 318 | |
| 319 | if (y + height > LCD_HEIGHT) |
| 320 | height = LCD_HEIGHT - y; /* Clip bottom */ |
| 321 | if (y < 0) |
| 322 | height += y, y = 0; /* Clip top */ |
| 323 | if (height <= 0) |
| 324 | return; /* nothing left to do */ |
| 325 | |
| 326 | /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer |
| 327 | * and lcd_framebuffer */ |
| 328 | dst = &lcd_driver_framebuffer[y][x]; |
| 329 | src = &lcd_framebuffer[y][x]; |
| 330 | |
| 331 | /* Copy part of the Rockbox framebuffer to the second framebuffer */ |
| 332 | if (width < LCD_WIDTH) |
| 333 | { |
| 334 | /* Not full width - do line-by-line */ |
| 335 | lcd_copy_buffer_rect(dst, src, width, height); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | /* Full width - copy as one line */ |
| 340 | lcd_copy_buffer_rect(dst, src, LCD_WIDTH*height, 1); |
| 341 | } |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void lcd_set_flip(bool yesno) |
| 345 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 346 | // TODO |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 347 | (void)yesno; |
| 348 | } |
| 349 | |
| 350 | void lcd_set_invert_display(bool yesno) |
| 351 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 352 | // TODO |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 353 | (void)yesno; |
| 354 | } |
| 355 | |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 356 | void lcd_yuv_set_options(unsigned options) |
| 357 | { |
| 358 | lcd_yuv_options = options; |
| 359 | } |
| 360 | |
| 361 | /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */ |
| 362 | extern void lcd_write_yuv420_lines(fb_data *dst, |
| 363 | unsigned char const * const src[3], |
| 364 | int width, |
| 365 | int stride); |
| 366 | extern void lcd_write_yuv420_lines_odither(fb_data *dst, |
| 367 | unsigned char const * const src[3], |
| 368 | int width, |
| 369 | int stride, |
| 370 | int x_screen, /* To align dither pattern */ |
| 371 | int y_screen); |
| 372 | |
| 373 | /* Performance function to blit a YUV bitmap directly to the LCD */ |
Jens Arnold | 68a2168 | 2008-03-24 00:35:53 +0000 | [diff] [blame] | 374 | void lcd_blit_yuv(unsigned char * const src[3], |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 375 | int src_x, int src_y, int stride, |
| 376 | int x, int y, int width, int height) |
| 377 | { |
Rob Purchase | b8b0337 | 2008-03-23 19:56:11 +0000 | [diff] [blame] | 378 | unsigned char const * yuv_src[3]; |
| 379 | off_t z; |
| 380 | |
| 381 | if (!display_on) |
| 382 | return; |
| 383 | |
| 384 | /* Sorry, but width and height must be >= 2 or else */ |
| 385 | width &= ~1; |
| 386 | height >>= 1; |
| 387 | |
| 388 | y = LCD_WIDTH - 1 - y; |
| 389 | fb_data *dst = &lcd_driver_framebuffer[x][y]; |
| 390 | |
| 391 | z = stride*src_y; |
| 392 | yuv_src[0] = src[0] + z + src_x; |
| 393 | yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1); |
| 394 | yuv_src[2] = src[2] + (yuv_src[1] - src[1]); |
| 395 | |
| 396 | if (lcd_yuv_options & LCD_YUV_DITHER) |
| 397 | { |
| 398 | do |
| 399 | { |
| 400 | lcd_write_yuv420_lines_odither(dst, yuv_src, width, stride, y, x); |
| 401 | yuv_src[0] += stride << 1; /* Skip down two luma lines */ |
| 402 | yuv_src[1] += stride >> 1; /* Skip down one chroma line */ |
| 403 | yuv_src[2] += stride >> 1; |
| 404 | dst -= 2; |
| 405 | y -= 2; |
| 406 | } |
| 407 | while (--height > 0); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | do |
| 412 | { |
| 413 | lcd_write_yuv420_lines(dst, yuv_src, width, stride); |
| 414 | yuv_src[0] += stride << 1; /* Skip down two luma lines */ |
| 415 | yuv_src[1] += stride >> 1; /* Skip down one chroma line */ |
| 416 | yuv_src[2] += stride >> 1; |
| 417 | dst -= 2; |
| 418 | } |
| 419 | while (--height > 0); |
| 420 | } |
Rob Purchase | 47ea030 | 2008-01-14 22:04:48 +0000 | [diff] [blame] | 421 | } |