YUV Dither: r12 saving was removed but stacked parameter load offset wasn't changed...
[kugel-rb.git] / firmware / target / arm / iriver / h10 / lcd-h10_20gb.c
blob596276a9325e18498e83205ed44a4fd038ab351b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Barry Wardell
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "cpu.h"
23 #include "lcd.h"
24 #include "kernel.h"
25 #include "system.h"
27 /** Initialized in lcd_init_device() **/
28 /* Is the power turned on? */
29 static bool power_on;
30 /* Is the display turned on? */
31 static bool display_on;
32 /* Amount of vertical offset. Used for flip offset correction/detection. */
33 static int y_offset;
34 /* Reverse flag. Must be remembered when display is turned off. */
35 static unsigned short disp_control_rev;
36 /* Contrast setting << 8 */
37 static int lcd_contrast;
39 static unsigned lcd_yuv_options SHAREDBSS_ATTR = 0;
41 /* Forward declarations */
42 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
43 static void lcd_display_off(void);
44 #endif
46 /* register defines for the Renesas HD66773R */
47 #define R_START_OSC 0x00
48 #define R_DEVICE_CODE_READ 0x00
49 #define R_DRV_OUTPUT_CONTROL 0x01
50 #define R_DRV_AC_CONTROL 0x02
51 #define R_POWER_CONTROL1 0x03
52 #define R_POWER_CONTROL2 0x04
53 #define R_ENTRY_MODE 0x05
54 #define R_COMPARE_REG 0x06
55 #define R_DISP_CONTROL 0x07
56 #define R_FRAME_CYCLE_CONTROL 0x0b
57 #define R_POWER_CONTROL3 0x0c
58 #define R_POWER_CONTROL4 0x0d
59 #define R_POWER_CONTROL5 0x0e
60 #define R_GATE_SCAN_START_POS 0x0f
61 #define R_VERT_SCROLL_CONTROL 0x11
62 #define R_1ST_SCR_DRV_POS 0x14
63 #define R_2ND_SCR_DRV_POS 0x15
64 #define R_HORIZ_RAM_ADDR_POS 0x16
65 #define R_VERT_RAM_ADDR_POS 0x17
66 #define R_RAM_WRITE_DATA_MASK 0x20
67 #define R_RAM_ADDR_SET 0x21
68 #define R_WRITE_DATA_2_GRAM 0x22
69 #define R_RAM_READ_DATA 0x22
70 #define R_GAMMA_FINE_ADJ_POS1 0x30
71 #define R_GAMMA_FINE_ADJ_POS2 0x31
72 #define R_GAMMA_FINE_ADJ_POS3 0x32
73 #define R_GAMMA_GRAD_ADJ_POS 0x33
74 #define R_GAMMA_FINE_ADJ_NEG1 0x34
75 #define R_GAMMA_FINE_ADJ_NEG2 0x35
76 #define R_GAMMA_FINE_ADJ_NEG3 0x36
77 #define R_GAMMA_GRAD_ADJ_NEG 0x37
78 #define R_GAMMA_AMP_ADJ_POS 0x3a
79 #define R_GAMMA_AMP_ADJ_NEG 0x3b
81 static inline void lcd_wait_write(void)
83 while (LCD2_PORT & LCD2_BUSY_MASK);
86 /* Send command */
87 static inline void lcd_send_cmd(unsigned v)
89 lcd_wait_write();
90 LCD2_PORT = LCD2_CMD_MASK;
91 LCD2_PORT = LCD2_CMD_MASK | v;
94 /* Send 16-bit data */
95 static inline void lcd_send_data(unsigned v)
97 lcd_wait_write();
98 LCD2_PORT = LCD2_DATA_MASK | (v >> 8); /* Send MSB first */
99 LCD2_PORT = LCD2_DATA_MASK | (v & 0xff);
102 /* Send 16-bit data byte-swapped. Only needed until we can use block transfer. */
103 static inline void lcd_send_data_swapped(unsigned v)
105 lcd_wait_write();
106 LCD2_PORT = LCD2_DATA_MASK | (v & 0xff); /* Send LSB first */
107 LCD2_PORT = LCD2_DATA_MASK | (v >> 8);
110 /* Write value to register */
111 static void lcd_write_reg(int reg, int val)
113 lcd_send_cmd(reg);
114 lcd_send_data(val);
117 /*** hardware configuration ***/
119 int lcd_default_contrast(void)
121 return DEFAULT_CONTRAST_SETTING;
124 void lcd_set_contrast(int val)
126 /* Clamp val in range 0-14, 16-30 */
127 if (val < 1)
128 val = 0;
129 else if (val <= 15)
130 --val;
131 else if (val > 30)
132 val = 30;
134 lcd_contrast = val << 8;
136 if (!power_on)
137 return;
139 /* VCOMG=1, VDV4-0=xxxxx, VCM4-0=11000 */
140 lcd_write_reg(R_POWER_CONTROL5, 0x2018 | lcd_contrast);
143 void lcd_set_invert_display(bool yesno)
145 if (yesno == (disp_control_rev == 0x0000))
146 return;
148 disp_control_rev = yesno ? 0x0000 : 0x0004;
150 if (!display_on)
151 return;
153 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=1, REV=x, D1-0=11 */
154 lcd_write_reg(R_DISP_CONTROL, 0x0033 | disp_control_rev);
158 /* turn the display upside down (call lcd_update() afterwards) */
159 void lcd_set_flip(bool yesno)
161 if (yesno == (y_offset != 0))
162 return;
164 /* The LCD controller is 132x160 while the LCD itself is 128x160, so we need
165 * to shift the origin by 4 when we flip the LCD */
166 y_offset = yesno ? 4 : 0;
168 if (!power_on)
169 return;
171 /* SCN4-0=000x0 (G1/G160) */
172 lcd_write_reg(R_GATE_SCAN_START_POS, yesno ? 0x0002 : 0x0000);
173 /* SM=0, GS=x, SS=x, NL4-0=10011 (G1-G160) */
174 lcd_write_reg(R_DRV_OUTPUT_CONTROL, yesno ? 0x0213 : 0x0113);
177 /* LCD init */
178 void lcd_init_device(void)
180 #ifndef BOOTLOADER
181 /* The OF won't boot if this is done in the bootloader - ideally we should
182 tweak the lcd controller speed settings but this will do for now */
183 CLCD_CLOCK_SRC |= 0xc0000000; /* Set LCD interface clock to PLL */
184 #endif
185 power_on = true;
186 display_on = true;
187 y_offset = 0;
188 disp_control_rev = 0x0004;
189 lcd_contrast = DEFAULT_CONTRAST_SETTING << 8;
192 #ifdef HAVE_LCD_SLEEP
193 static void lcd_power_on(void)
195 /* Be sure standby bit is clear. */
196 /* BT2-0=000, DC2-0=000, AP2-0=000, SLP=0, STB=0 */
197 lcd_write_reg(R_POWER_CONTROL1, 0x0000);
199 /** Power ON Sequence **/
200 /* Per datasheet Rev.1.10, Jun.21.2003, p. 99 */
202 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscillation */
203 /* 10ms or more for oscillation circuit to stabilize */
204 sleep(HZ/50);
205 /* Instruction (1) for power setting; VC2-0, VRH3-0, CAD,
206 VRL3-0, VCM4-0, VDV4-0 */
207 /* VC2-0=001 */
208 lcd_write_reg(R_POWER_CONTROL3, 0x0001);
209 /* VRL3-0=0100, PON=0, VRH3-0=0001 */
210 lcd_write_reg(R_POWER_CONTROL4, 0x0401);
211 /* CAD=1 */
212 lcd_write_reg(R_POWER_CONTROL2, 0x8000);
213 /* VCOMG=0, VDV4-0=xxxxx (19), VCM4-0=11000 */
214 lcd_write_reg(R_POWER_CONTROL5, 0x0018 | lcd_contrast);
215 /* Instruction (2) for power setting; BT2-0, DC2-0, AP2-0 */
216 /* BT2-0=000, DC2-0=001, AP2-0=011, SLP=0, STB=0 */
217 lcd_write_reg(R_POWER_CONTROL1, 0x002c);
218 /* Instruction (3) for power setting; VCOMG = "1" */
219 /* VCOMG=1, VDV4-0=xxxxx (19), VCM4-0=11000 */
220 lcd_write_reg(R_POWER_CONTROL5, 0x2018 | lcd_contrast);
222 /* 40ms or more; time for step-up circuits 1,2 to stabilize */
223 sleep(HZ/25);
225 /* Instruction (4) for power setting; PON = "1" */
226 /* VRL3-0=0100, PON=1, VRH3-0=0001 */
227 lcd_write_reg(R_POWER_CONTROL4, 0x0411);
229 /* 40ms or more; time for step-up circuit 4 to stabilize */
230 sleep(HZ/25);
232 /* Instructions for other mode settings (in register order). */
233 /* SM=0, GS=x, SS=0, NL4-0=10011 (G1-G160)*/
234 lcd_write_reg(R_DRV_OUTPUT_CONTROL, y_offset ? 0x0013 : 0x0113); /* different to X5 */
235 /* FLD1-0=01 (1 field), B/C=1, EOR=1 (C-pat), NW5-0=000000 (1 row) */
236 lcd_write_reg(R_DRV_AC_CONTROL, 0x0700);
237 /* DIT=0, BGR=1, HWM=0, I/D1-0=10, AM=1, LG2-0=000 */
238 lcd_write_reg(R_ENTRY_MODE, 0x1028); /* different to X5 */
239 /* CP15-0=0000000000000000 */
240 lcd_write_reg(R_COMPARE_REG, 0x0000);
241 /* NO1-0=01, SDT1-0=00, EQ1-0=00, DIV1-0=00, RTN3-00000 */
242 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x4000);
243 /* SCN4-0=000x0 (G1/G160) */
244 /* lcd_write_reg(R_GATE_SCAN_START_POS, y_offset ? 0x0000 : 0x0002); */
245 /* VL7-0=0x00 */
246 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0x0000);
247 /* SE17-10(End)=0x9f (159), SS17-10(Start)=0x00 */
248 lcd_write_reg(R_1ST_SCR_DRV_POS, 0x9f00);
249 /* SE27-20(End)=0x5c (92), SS27-20(Start)=0x00 */
250 lcd_write_reg(R_2ND_SCR_DRV_POS, 0x5c00);
251 /* HEA7-0=7f, HSA7-0=00 */
252 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, 0x7f00);
253 /* PKP12-10=0x0, PKP02-00=0x0 */
254 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0003);
255 /* PKP32-30=0x4, PKP22-20=0x0 */
256 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0400);
257 /* PKP52-50=0x4, PKP42-40=0x7 */
258 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0407);
259 /* PRP12-10=0x3, PRP02-00=0x5 */
260 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0305);
261 /* PKN12-10=0x0, PKN02-00=0x3 */
262 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0003);
263 /* PKN32-30=0x7, PKN22-20=0x4 */
264 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0704);
265 /* PKN52-50=0x4, PRN42-40=0x7 */
266 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0407);
267 /* PRN12-10=0x5, PRN02-00=0x3 */
268 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0503);
269 /* VRP14-10=0x14, VRP03-00=0x09 */
270 lcd_write_reg(R_GAMMA_AMP_ADJ_POS, 0x1409);
271 /* VRN14-00=0x06, VRN03-00=0x02 */
272 lcd_write_reg(R_GAMMA_AMP_ADJ_NEG, 0x0602);
274 /* 100ms or more; time for step-up circuits to stabilize */
275 sleep(HZ/10);
277 power_on = true;
280 static void lcd_power_off(void)
282 /* Display must be off first */
283 if (display_on)
284 lcd_display_off();
286 power_on = false;
288 /** Power OFF sequence **/
289 /* Per datasheet Rev.1.10, Jun.21.2003, p. 99 */
291 /* Step-up1 halt setting bit */
292 /* BT2-0=110, DC2-0=001, AP2-0=011, SLP=0, STB=0 */
293 lcd_write_reg(R_POWER_CONTROL1, 0x062c);
294 /* Step-up3,4 halt setting bit */
295 /* VRL3-0=0100, PON=0, VRH3-0=0001 */
296 lcd_write_reg(R_POWER_CONTROL4, 0x0401);
297 /* VCOMG=0, VDV4-0=10011, VCM4-0=11000 */
298 lcd_write_reg(R_POWER_CONTROL5, 0x0018 | lcd_contrast);
300 /* Wait 100ms or more */
301 sleep(HZ/10);
303 /* Step-up2,amp halt setting bit */
304 /* BT2-0=000, DC2-0=000, AP2-0=000, SLP=0, STB=0 */
305 lcd_write_reg(R_POWER_CONTROL1, 0x0000);
308 void lcd_sleep(void)
310 if (power_on)
311 lcd_power_off();
313 /* Set standby mode */
314 /* BT2-0=000, DC2-0=000, AP2-0=000, SLP=0, STB=1 */
315 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
317 #endif
319 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
320 static void lcd_display_off(void)
322 display_on = false;
324 /** Display OFF sequence **/
325 /* Per datasheet Rev.1.10, Jun.21.2003, p. 97 */
327 /* EQ1-0=00 already */
329 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=1, REV=x, D1-0=10 */
330 lcd_write_reg(R_DISP_CONTROL, 0x0032 | disp_control_rev);
332 sleep(HZ/25); /* Wait 2 frames or more */
334 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=0, REV=x, D1-0=10 */
335 lcd_write_reg(R_DISP_CONTROL, 0x0022 | disp_control_rev);
337 sleep(HZ/25); /* Wait 2 frames or more */
339 /* PT1-0=00, VLE2-1=00, SPT=0, GON=0, DTE=0, REV=0, D1-0=00 */
340 lcd_write_reg(R_DISP_CONTROL, 0x0000);
342 #endif
344 #if defined(HAVE_LCD_ENABLE)
345 static void lcd_display_on(void)
347 /* Be sure power is on first */
348 if (!power_on)
349 lcd_power_on();
351 /** Display ON Sequence **/
352 /* Per datasheet Rev.1.10, Jun.21.2003, p. 97 */
354 /* PT1-0=00, VLE2-1=00, SPT=0, GON=0, DTE=0, REV=0, D1-0=01 */
355 lcd_write_reg(R_DISP_CONTROL, 0x0001);
357 sleep(HZ/25); /* Wait 2 frames or more */
359 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=0, REV=x, D1-0=01 */
360 lcd_write_reg(R_DISP_CONTROL, 0x0021 | disp_control_rev);
361 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=0, REV=x, D1-0=11 */
362 lcd_write_reg(R_DISP_CONTROL, 0x0023 | disp_control_rev);
364 sleep(HZ/25); /* Wait 2 frames or more */
366 /* PT1-0=00, VLE2-1=00, SPT=0, GON=1, DTE=1, REV=x, D1-0=11 */
367 lcd_write_reg(R_DISP_CONTROL, 0x0033 | disp_control_rev);
369 display_on = true;
372 void lcd_enable(bool on)
374 if (on == display_on)
375 return;
377 if (on)
379 lcd_display_on();
380 /* Probably out of sync and we don't wanna pepper the code with
381 lcd_update() calls for this. */
382 lcd_update();
383 send_event(LCD_EVENT_ACTIVATION, NULL);
385 else
387 lcd_display_off();
390 #endif
392 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
393 bool lcd_active(void)
395 return display_on;
397 #endif
399 /*** update functions ***/
401 void lcd_yuv_set_options(unsigned options)
403 lcd_yuv_options = options;
406 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
407 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
408 int width,
409 int stride);
410 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
411 int width,
412 int stride,
413 int x_screen, /* To align dither pattern */
414 int y_screen);
416 /* Performance function to blit a YUV bitmap directly to the LCD */
417 void lcd_blit_yuv(unsigned char * const src[3],
418 int src_x, int src_y, int stride,
419 int x, int y, int width, int height)
421 const unsigned char *yuv_src[3];
422 const unsigned char *ysrc_max;
423 int y0;
424 int options;
426 if (!display_on)
427 return;
429 width &= ~1;
430 height &= ~1;
432 /* calculate the drawing region */
434 /* The 20GB LCD is actually 128x160 but rotated 90 degrees so the origin
435 * is actually the bottom left and horizontal and vertical are swapped.
436 * Rockbox expects the origin to be the top left so we need to use
437 * 127 - y instead of just y */
439 /* max vert << 8 | start vert */
440 lcd_write_reg(R_VERT_RAM_ADDR_POS, ((x + width - 1) << 8) | x);
442 y0 = LCD_HEIGHT - 1 - y + y_offset;
444 /* DIT=0, BGR=1, HWM=0, I/D1-0=10, AM=0, LG2-0=000 */
445 lcd_write_reg(R_ENTRY_MODE, 0x1020);
447 yuv_src[0] = src[0] + src_y * stride + src_x;
448 yuv_src[1] = src[1] + (src_y * stride >> 2) + (src_x >> 1);
449 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
450 ysrc_max = yuv_src[0] + height * stride;
452 options = lcd_yuv_options;
456 /* max horiz << 8 | start horiz */
457 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (y0 << 8) | (y0 - 1));
459 /* position cursor (set AD0-AD15) */
460 /* start vert << 8 | start horiz */
461 lcd_write_reg(R_RAM_ADDR_SET, (x << 8) | y0);
463 /* start drawing */
464 lcd_send_cmd(R_WRITE_DATA_2_GRAM);
466 if (options & LCD_YUV_DITHER)
468 lcd_write_yuv420_lines_odither(yuv_src, width, stride,
469 x, y);
470 y -= 2;
472 else
474 lcd_write_yuv420_lines(yuv_src, width, stride);
477 y0 -= 2;
478 yuv_src[0] += stride << 1;
479 yuv_src[1] += stride >> 1;
480 yuv_src[2] += stride >> 1;
482 while (yuv_src[0] < ysrc_max);
484 /* DIT=0, BGR=1, HWM=0, I/D1-0=10, AM=1, LG2-0=000 */
485 lcd_write_reg(R_ENTRY_MODE, 0x1028);
489 /* Update a fraction of the display. */
490 void lcd_update_rect(int x0, int y0, int width, int height)
492 int x1, y1;
493 unsigned short *addr;
495 if (!display_on)
496 return;
498 /* calculate the drawing region */
499 y1 = (y0 + height) - 1; /* max vert */
500 x1 = (x0 + width) - 1; /* max horiz */
502 if(x1 >= LCD_WIDTH)
503 x1 = LCD_WIDTH - 1;
504 if (x1 <= 0)
505 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
506 if(y1 >= LCD_HEIGHT)
507 y1 = LCD_HEIGHT-1;
509 /* The 20GB LCD is actually 128x160 but rotated 90 degrees so the origin
510 * is actually the bottom left and horizontal and vertical are swapped.
511 * Rockbox expects the origin to be the top left so we need to use
512 * 127 - y instead of just y */
514 /* max horiz << 8 | start horiz */
515 lcd_send_cmd(R_HORIZ_RAM_ADDR_POS);
516 lcd_send_data( (((LCD_HEIGHT-1)-y0+y_offset) << 8) | ((LCD_HEIGHT-1)-y1+y_offset) );
518 /* max vert << 8 | start vert */
519 lcd_send_cmd(R_VERT_RAM_ADDR_POS);
520 lcd_send_data((x1 << 8) | x0);
522 /* position cursor (set AD0-AD15) */
523 /* start vert << 8 | start horiz */
524 lcd_send_cmd(R_RAM_ADDR_SET);
525 lcd_send_data( (x0 << 8) | ((LCD_HEIGHT-1)-y0+y_offset) );
527 /* start drawing */
528 lcd_send_cmd(R_WRITE_DATA_2_GRAM);
530 addr = (unsigned short*)&lcd_framebuffer[y0][x0];
532 int c, r;
534 /* for each row */
535 for (r = 0; r < height; r++) {
536 /* for each column */
537 for (c = 0; c < width; c++) {
538 /* output 1 pixel */
539 lcd_send_data_swapped(*addr++);
542 addr += LCD_WIDTH - width;
546 /* Update the display.
547 This must be called after all other LCD functions that change the display. */
548 void lcd_update(void)
550 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);