1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Mark Arigo
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 ****************************************************************************/
27 /* Settings to remember when display is turned off */
33 static bool display_on
;
35 /* Forward declarations */
36 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
37 static void lcd_display_off(void);
40 /* The SA9200 controller closely matches the register defines for the
42 #define R_START_OSC 0x00
43 #define R_DRV_OUTPUT_CONTROL 0x01
44 #define R_INVERSION_CONTROL 0x02
45 #define R_ENTRY_MODE 0x03
46 #define R_DISP_CONTROL 0x07
47 #define R_BLANK_PERIOD_CONTROL 0x08
48 #define R_FRAME_CYCLE_CONTROL 0x0b
49 #define R_EXT_INTERFACE_CONTROL 0x0c
50 #define R_POWER_CONTROL1 0x10
51 #define R_GAMMA_CONTROL1 0x11
52 #define R_POWER_CONTROL2 0x12
53 #define R_POWER_CONTROL3 0x13
54 #define R_POWER_CONTROL4 0x14
55 #define R_RAM_ADDR_SET 0x21
56 #define R_WRITE_DATA_2_GRAM 0x22
57 #define R_RAM_READ_DATA 0x22
58 #define R_GAMMA_FINE_ADJ_POS1 0x30
59 #define R_GAMMA_FINE_ADJ_POS2 0x31
60 #define R_GAMMA_FINE_ADJ_POS3 0x32
61 #define R_GAMMA_GRAD_ADJ_POS 0x33
62 #define R_GAMMA_FINE_ADJ_NEG1 0x34
63 #define R_GAMMA_FINE_ADJ_NEG2 0x35
64 #define R_GAMMA_FINE_ADJ_NEG3 0x36
65 #define R_GAMMA_GRAD_ADJ_NEG 0x37
66 #define R_GAMMA_CONTROL3 0x38
67 #define R_GATE_SCAN_START_POS 0x40
68 #define R_1ST_SCR_DRV_POS 0x42
69 #define R_2ND_SCR_DRV_POS 0x43
70 #define R_HORIZ_RAM_ADDR_POS 0x44
71 #define R_VERT_RAM_ADDR_POS 0x45
72 #define R_OSC_CONTROL 0x61
73 #define R_LOW_POWER_MODE 0x69
74 #define R_PRE_DRIVING_PERIOD 0x70
75 #define R_GATE_OUT_PERIOD_CTRL 0x71
76 #define R_SOFTWARE_RESET 0x72
79 static unsigned lcd_yuv_options SHAREDBSS_ATTR
= 0;
82 static inline void lcd_wait_write(void)
84 while (LCD1_CONTROL
& LCD1_BUSY_MASK
);
88 static inline void lcd_send_pixel(unsigned pixel
)
91 *(volatile uint8_t *)&LCD1_DATA
= pixel
>> 8;
93 *(volatile uint8_t *)&LCD1_DATA
= pixel
;
97 static void lcd_send_data(unsigned data
)
100 LCD1_DATA
= data
>> 8;
102 LCD1_DATA
= data
& 0xff;
105 /* send LCD command */
106 static void lcd_send_command(unsigned cmd
)
114 static void lcd_write_reg(unsigned reg
, unsigned data
)
116 lcd_send_command(reg
);
121 void lcd_init_device(void)
124 /* This is done by the OF bootloader, no need to redo */
125 DEV_INIT1
&= ~0x3000;
126 DEV_INIT1
= DEV_INIT1
;
129 LCD1_CONTROL
= 0x4680;
131 LCD1_CONTROL
= 0x4684;
135 LCD1_CONTROL
&= ~0x200;
136 LCD1_CONTROL
&= ~0x800;
137 LCD1_CONTROL
&= ~0x400;
145 /* Reset the options */
146 lcd_set_invert_display(false);
148 lcd_set_contrast(DEFAULT_CONTRAST_SETTING
);
151 #ifdef HAVE_LCD_SLEEP
152 static void lcd_power_on(void)
156 /** Power ON Sequence **/
158 /* Start Oscillation */
159 lcd_write_reg(R_START_OSC
, 0x0001);
161 sleep(HZ
/20); /* 50ms or more */
163 /* DSTB=0, SAP2-0=001, BT2-0=101, DC2-0=000, AP2-0=001, SLP=0, STB=0 */
164 lcd_write_reg(R_POWER_CONTROL1
, 0x0d04);
166 /* VR1C=0, VRN14-10=10111, VRP14-10=11111 */
167 lcd_write_reg(R_GAMMA_CONTROL1
, 0x171f);
169 /* SVC3-0=0000, VRH5-4=01 */
170 lcd_write_reg(R_POWER_CONTROL2
, 0x0001);
172 /* VCMR=1, PON=0, VRH3-0=1101 */
173 lcd_write_reg(R_POWER_CONTROL3
, 0x080d);
175 /* VDV6-0=0000100, VCOMG=0, VCM6-0=xxxxxxx */
176 lcd_write_reg(R_POWER_CONTROL4
, 0x0400 | contrast
);
178 /* DSTB=0, SAP2-0=010, BT2-0=010, DC2-0=000, AP2-0=010, SLP=0, STB=0 */
179 lcd_write_reg(R_POWER_CONTROL1
, 0x1208);
181 sleep(HZ
/20); /* 50ms or more */
183 /* VCMR=1, PON=1, VRH3-0=1100 */
184 lcd_write_reg(R_POWER_CONTROL3
, 0x081c);
186 sleep(HZ
/20); /* OF bootlaoder uses 200ms, no delay in OF firmware */
188 /* Instructions for other mode settings (in register order). */
190 lcd_write_reg(R_DRV_OUTPUT_CONTROL
, flip
? 0x090c : 0x0a0c);
192 /* FL1-0=10, FDL=0 */
193 lcd_write_reg(R_INVERSION_CONTROL
, 0x0200);
195 /* BGR=1, MDT1-0=00, I/D1-0=11, AM=0 */
196 lcd_write_reg(R_ENTRY_MODE
, 0x1030);
198 /* PT1-0=00, SPT=0, GON=0, DTE=0, CL=0, REV=1, D1-0=01 */
199 lcd_write_reg(R_DISP_CONTROL
, 0x0005);
201 /* FP3-0=0011, BT3-0=1010 */
202 lcd_write_reg(R_BLANK_PERIOD_CONTROL
, 0x030a);
204 /* DIV1-0=00, RTN3-0=0000 */
205 lcd_write_reg(R_FRAME_CYCLE_CONTROL
, 0x0000);
207 /* RM=0, DM1-0=00, RIM1-0=00 */
208 lcd_write_reg(R_EXT_INTERFACE_CONTROL
, 0x0000);
210 /* PKP1=0x0, PKP0=0x0 */
211 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1
, 0x0000);
213 /* PKP3=0x2, PKP2=0x4 */
214 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2
, 0x0204);
216 /* PKP5=0x0, PKP4=0x1 */
217 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3
, 0x0001);
219 /* PRP1=0x6, PRP0=0x0 */
220 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS
, 0x0600);
222 /* PKN1=0x6, PKN0=0x7 */
223 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1
, 0x0607);
225 /* PKN3=0x3, PKN2=0x5 */
226 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2
, 0x0305);
228 /* PKN5=0x7, PKN4=0x7 */
229 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3
, 0x0707);
231 /* PRN1=0x0, PRN0=0x6 */
232 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG
, 0x0006);
234 /* VRN0=0x4, VRP=0x0 */
235 lcd_write_reg(R_GAMMA_CONTROL3
, 0x0400);
238 lcd_write_reg(R_GATE_SCAN_START_POS
, 0x0000);
240 /* SE1=LCD_HEIGHT-1, SS1=0x0 */
241 lcd_write_reg(R_1ST_SCR_DRV_POS
, 0x9f00);
243 /* SE2=0x0, SS2=0x0 */
244 lcd_write_reg(R_2ND_SCR_DRV_POS
, 0x0000);
246 /* HEA=LCD_WIDTH-1, HSA=0x0 */
247 lcd_write_reg(R_HORIZ_RAM_ADDR_POS
, 0x7f00);
249 /* VEA=LCD_HEIGHT-1, VSA=0x0 */
250 lcd_write_reg(R_VERT_RAM_ADDR_POS
, 0x9f00);
252 /* Unknown registers */
253 lcd_write_reg(0x00a8, 0x0125);
254 lcd_write_reg(0x00a9, 0x0014);
255 lcd_write_reg(0x00a7, 0x0022);
260 static void lcd_power_off(void)
262 /* Display must be off first */
268 /** Power OFF sequence **/
270 /* DSTB=0, SAP2-0=000, BT2-0=001, DC2-0=000, AP2-0=000, SLP=0, STB=0 */
271 lcd_write_reg(R_POWER_CONTROL1
, 0x0100);
273 /* VCMR=1, PON=0, VRH3-0=1101 */
274 lcd_write_reg(R_POWER_CONTROL3
, 0x080d);
282 /* Set standby mode */
284 /* PT1-0=00, SPT=0, GON=1, DTE=1, CL=0, REV=1, D1-0=10 */
285 lcd_write_reg(R_DISP_CONTROL
, 0x0036);
287 /* DSTB=0, SAP2-0=000, BT2-0=101, DC2-0=000, AP2-0=000, SLP=0, STB=1 */
288 lcd_write_reg(R_POWER_CONTROL1
, 0x0501);
290 LCD1_CONTROL
&= ~0xffff0001;
294 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
295 static void lcd_display_off(void)
299 /** Display OFF sequence **/
301 /* PT1-0=10, SPT=0, GON=1, DTE=1, CL=0, REV=1, D1-0=10 */
302 lcd_write_reg(R_DISP_CONTROL
, 0x1036);
304 sleep(HZ
/25); /* 2 or more frames */
306 /* PT1-0=10, SPT=0, GON=1, DTE=0, CL=0, REV=1, D1-0=00 */
307 lcd_write_reg(R_DISP_CONTROL
, 0x1034);
309 sleep(HZ
/500); /* 1ms or more */
311 /* PT1-0=10, SPT=0, GON=0, DTE=0, CL=0, REV=1, D1-0=00 */
312 lcd_write_reg(R_DISP_CONTROL
, 0x1004);
314 sleep(HZ
/25); /* 2 or more frames */
318 #if defined(HAVE_LCD_ENABLE)
319 static void lcd_display_on(void)
321 /* Be sure power is on first */
325 /** Display ON Sequence **/
327 /* PT1-0=00, SPT=0, GON=1, DTE=0, CL=0, REV=0, D1-0=01 */
328 lcd_write_reg(R_DISP_CONTROL
, 0x0021);
330 sleep(HZ
/500); /* 1ms or more */
332 /* PT1-0=00, SPT=0, GON=1, DTE=0, CL=0, REV=0, D1-0=11 */
333 lcd_write_reg(R_DISP_CONTROL
, 0x0023);
335 sleep(HZ
/25); /* 2 or more frames */
337 /* PT1-0=10, SPT=0, GON=1, DTE=1, CL=0, REV=x, D1-0=11 */
338 lcd_write_reg(R_DISP_CONTROL
, invert
? 0x1033 : 0x1037);
343 void lcd_enable(bool on
)
345 if (on
== display_on
)
351 /* Probably out of sync and we don't wanna pepper the code with
352 lcd_update() calls for this. */
354 send_event(LCD_EVENT_ACTIVATION
, NULL
);
363 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
364 bool lcd_active(void)
370 /*** hardware configuration ***/
371 int lcd_default_contrast(void)
373 return DEFAULT_CONTRAST_SETTING
;
376 void lcd_set_contrast(int val
)
378 contrast
= val
& 0x7f;
383 /* VDV6-0=0000100, VCOMG=0, VCM6-0=xxxxxxx */
384 lcd_write_reg(R_POWER_CONTROL4
, 0x0400 | contrast
);
387 void lcd_set_invert_display(bool yesno
)
394 /* PT1-0=10, SPT=0, GON=1, DTE=1, CL=0, REV=x, D1-0=11 */
395 lcd_write_reg(R_DISP_CONTROL
, invert
? 0x1033 : 0x1037);
398 /* turn the display upside down (call lcd_update() afterwards) */
399 void lcd_set_flip(bool yesno
)
406 /* DPL=0, EPL=1, SM=0, GS=x, SS=x, NL4-0=01100 */
407 lcd_write_reg(R_DRV_OUTPUT_CONTROL
, flip
? 0x090c : 0x0a0c);
410 void lcd_yuv_set_options(unsigned options
)
412 lcd_yuv_options
= options
;
415 /* Performance function to blit a YUV bitmap directly to the LCD */
416 void lcd_write_yuv420_lines(unsigned char const * const src
[3],
419 void lcd_write_yuv420_lines_odither(unsigned char const * const src
[3],
424 void lcd_blit_yuv(unsigned char * const src
[3],
425 int src_x
, int src_y
, int stride
,
426 int x
, int y
, int width
, int height
)
428 const unsigned char *yuv_src
[3];
429 const unsigned char *ysrc_max
;
438 /* calculate the drawing region */
439 lcd_write_reg(R_VERT_RAM_ADDR_POS
, ((x
+ width
- 1) << 8) | x
);
441 /* convert YUV coordinates to screen coordinates */
442 y
= LCD_WIDTH
- 1 - y
;
444 /* 2px strip: cursor moves left, then down in gram */
445 /* BGR=1, MDT1-0=00, I/D1-0=10, AM=0 */
446 lcd_write_reg(R_ENTRY_MODE
, 0x1020);
448 yuv_src
[0] = src
[0] + src_y
* stride
+ src_x
;
449 yuv_src
[1] = src
[1] + (src_y
* stride
>> 2) + (src_x
>> 1);
450 yuv_src
[2] = src
[2] + (yuv_src
[1] - src
[1]);
451 ysrc_max
= yuv_src
[0] + height
* stride
;
453 /* cache options setting */
454 options
= lcd_yuv_options
;
458 /* max horiz << 8 | start horiz */
459 lcd_write_reg(R_HORIZ_RAM_ADDR_POS
, (y
<< 8) | (y
- 1));
461 /* position cursor (set AD0-AD15) */
462 lcd_write_reg(R_RAM_ADDR_SET
, (x
<< 8) | y
);
465 lcd_send_command(R_WRITE_DATA_2_GRAM
);
467 if (options
& LCD_YUV_DITHER
)
469 lcd_write_yuv420_lines_odither(yuv_src
, width
, stride
,
474 lcd_write_yuv420_lines(yuv_src
, width
, stride
);
477 y
-= 2; /* move strip by "down" 2 px */
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 /* back to normal right, then down cursor in gram */
485 /* BGR=1, MDT1-0=00, I/D1-0=11, AM=0 */
486 lcd_write_reg(R_ENTRY_MODE
, 0x1030);
489 /* Update the display.
490 This must be called after all other LCD functions that change the display. */
491 void lcd_update(void)
493 const fb_data
*addr
, *end
;
499 end
= FBADDR(LCD_WIDTH
,LCD_HEIGHT
- 1);
501 lcd_write_reg(R_HORIZ_RAM_ADDR_POS
, (LCD_WIDTH
- 1) << 8);
502 lcd_write_reg(R_VERT_RAM_ADDR_POS
, (LCD_HEIGHT
- 1) << 8);
503 lcd_write_reg(R_RAM_ADDR_SET
, 0);
504 lcd_send_command(R_WRITE_DATA_2_GRAM
);
508 lcd_send_pixel(*addr
++);
509 lcd_send_pixel(*addr
++);
514 /* Update a fraction of the display. */
515 void lcd_update_rect(int x
, int y
, int width
, int height
)
522 if (x
+ width
> LCD_WIDTH
)
523 width
= LCD_WIDTH
- x
;
527 return; /* Nothing left to do. */
529 if (y
+ height
> LCD_HEIGHT
)
530 height
= LCD_HEIGHT
- y
;
534 return; /* Nothing left to do. */
538 lcd_write_reg(R_HORIZ_RAM_ADDR_POS
, ((x
+ width
- 1) << 8) | x
);
539 lcd_write_reg(R_VERT_RAM_ADDR_POS
, ((y
+ height
- 1) << 8) | y
);
540 lcd_write_reg(R_RAM_ADDR_SET
, (y
<< 8) | x
);
541 lcd_send_command(R_WRITE_DATA_2_GRAM
);
551 lcd_send_pixel(*addr
++);
552 lcd_send_pixel(*addr
++);
559 lcd_send_pixel(*addr
++); /* odd width */
561 addr
+= LCD_WIDTH
- width
;
563 while (--height
> 0);