add the correct input file to makeindex call
[Rockbox.git] / firmware / drivers / lcd-h300.c
blob41d2d115090c9cf24276ef8a53f95e9a578f33f7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include "cpu.h"
22 #include "lcd.h"
23 #include "kernel.h"
24 #include "thread.h"
25 #include <string.h>
26 #include <stdlib.h>
27 #include "file.h"
28 #include "debug.h"
29 #include "system.h"
30 #include "font.h"
31 #include "bidi.h"
33 static bool display_on = false; /* is the display turned on? */
34 static bool display_flipped = false;
35 static int xoffset = 0; /* needed for flip */
37 /* register defines */
38 #define R_START_OSC 0x00
39 #define R_DRV_OUTPUT_CONTROL 0x01
40 #define R_DRV_WAVEFORM_CONTROL 0x02
41 #define R_ENTRY_MODE 0x03
42 #define R_COMPARE_REG1 0x04
43 #define R_COMPARE_REG2 0x05
45 #define R_DISP_CONTROL1 0x07
46 #define R_DISP_CONTROL2 0x08
47 #define R_DISP_CONTROL3 0x09
49 #define R_FRAME_CYCLE_CONTROL 0x0b
50 #define R_EXT_DISP_IF_CONTROL 0x0c
52 #define R_POWER_CONTROL1 0x10
53 #define R_POWER_CONTROL2 0x11
54 #define R_POWER_CONTROL3 0x12
55 #define R_POWER_CONTROL4 0x13
57 #define R_RAM_ADDR_SET 0x21
58 #define R_WRITE_DATA_2_GRAM 0x22
60 #define R_GAMMA_FINE_ADJ_POS1 0x30
61 #define R_GAMMA_FINE_ADJ_POS2 0x31
62 #define R_GAMMA_FINE_ADJ_POS3 0x32
63 #define R_GAMMA_GRAD_ADJ_POS 0x33
65 #define R_GAMMA_FINE_ADJ_NEG1 0x34
66 #define R_GAMMA_FINE_ADJ_NEG2 0x35
67 #define R_GAMMA_FINE_ADJ_NEG3 0x36
68 #define R_GAMMA_GRAD_ADJ_NEG 0x37
70 #define R_GAMMA_AMP_ADJ_RES_POS 0x38
71 #define R_GAMMA_AMP_AVG_ADJ_RES_NEG 0x39
73 #define R_GATE_SCAN_POS 0x40
74 #define R_VERT_SCROLL_CONTROL 0x41
75 #define R_1ST_SCR_DRV_POS 0x42
76 #define R_2ND_SCR_DRV_POS 0x43
77 #define R_HORIZ_RAM_ADDR_POS 0x44
78 #define R_VERT_RAM_ADDR_POS 0x45
80 #define LCD_CMD (*(volatile unsigned short *)0xf0000000)
81 #define LCD_DATA (*(volatile unsigned short *)0xf0000002)
83 /* called very frequently - inline! */
84 static inline void lcd_write_reg(int reg, int val)
86 LCD_CMD = reg;
87 LCD_DATA = val;
90 /* called very frequently - inline! */
91 static inline void lcd_begin_write_gram(void)
93 LCD_CMD = R_WRITE_DATA_2_GRAM;
96 /*** hardware configuration ***/
98 void lcd_set_contrast(int val)
100 (void)val;
103 void lcd_set_invert_display(bool yesno)
105 (void)yesno;
108 static void flip_lcd(bool yesno)
110 if (yesno)
112 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x031b); /* 224 lines, GS=SS=1 */
113 lcd_write_reg(R_GATE_SCAN_POS, 0x0002); /* 16 lines offset */
114 lcd_write_reg(R_1ST_SCR_DRV_POS, 0xdf04); /* 4..223 */
116 else
118 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x001b); /* 224 lines, GS=SS=0 */
119 lcd_write_reg(R_GATE_SCAN_POS, 0x0000);
120 lcd_write_reg(R_1ST_SCR_DRV_POS, 0xdb00); /* 0..219 */
124 /* turn the display upside down (call lcd_update() afterwards) */
125 void lcd_set_flip(bool yesno)
127 display_flipped = yesno;
128 xoffset = yesno ? 4 : 0;
130 if (display_on)
131 flip_lcd(yesno);
134 static void _display_on(void)
136 /** Sequence according to datasheet, p. 132 **/
138 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
139 sleep(1);
141 /* zero everything*/
142 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
143 lcd_write_reg(R_DISP_CONTROL1, 0x0000); /* GON = 0, DTE = 0, D1-0 = 00b */
144 lcd_write_reg(R_POWER_CONTROL3, 0x0000); /* PON = 0 */
145 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
146 sleep(1);
148 /* initialise power supply */
150 /* DC12-10 = 000b: Step-up1 = clock/8,
151 * DC02-00 = 000b: Step-up2 = clock/16,
152 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
153 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
155 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
156 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
158 /* VDV4-0 = 00110b: VcomA = Vreg1OUT * 0.76,
159 * VCM4-0 = 10000b: VcomH = Vreg1OUT * 0.70*/
160 lcd_write_reg(R_POWER_CONTROL4, 0x0610);
162 lcd_write_reg(R_POWER_CONTROL1, 0x0044); /* AP2-0 = 100b, DK = 1 */
163 lcd_write_reg(R_POWER_CONTROL3, 0x0018); /* PON = 1 */
165 sleep(4); /* Step-up circuit stabilising time */
167 /* start power supply */
169 lcd_write_reg(R_POWER_CONTROL1, 0x0540); /* BT2-0 = 101b, DK = 0 */
170 lcd_write_reg(R_POWER_CONTROL4, 0x2610); /* VCOMG = 1 */
172 /* other settings */
174 /* B/C = 1: n-line inversion form
175 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
176 * frame select signal and an n-line inversion signal.
177 * FLD = 01b: 1 field interlaced scan, external display iface */
178 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
180 /* Address counter updated in vertical direction; left to right;
181 * vertical increment horizontal increment.
182 * data format for 8bit transfer or spi = 65k (5,6,5)
183 * Reverse order of RGB to BGR for 18bit data written to GRAM
184 * Replace data on writing to GRAM */
185 lcd_write_reg(R_ENTRY_MODE, 0x7038);
187 flip_lcd(display_flipped);
189 lcd_write_reg(R_2ND_SCR_DRV_POS, 0x0000);
190 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0x0000);
192 /* 19 clocks,no equalization */
193 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0002);
195 /* Transfer mode for RGB interface disabled
196 * internal clock operation;
197 * System interface/VSYNC interface */
198 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0003);
200 /* Front porch lines: 8; Back porch lines: 8; */
201 lcd_write_reg(R_DISP_CONTROL2, 0x0808);
203 /* Scan mode by the gate driver in the non-display area: disabled;
204 * Cycle of scan by the gate driver - set to 31frames(518ms),
205 * disabled by above setting */
206 lcd_write_reg(R_DISP_CONTROL3, 0x003f);
208 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0003);
209 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
210 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0007);
211 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0705);
212 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0007);
213 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0000);
214 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0407);
215 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0507);
216 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x1d09);
217 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0303);
219 display_on=true; /* must be done before calling lcd_update() */
220 lcd_update();
222 sleep(4); /* op-amp stabilising time */
224 /** Sequence according to datasheet, p. 130 **/
226 lcd_write_reg(R_POWER_CONTROL1, 0x4540); /* SAP2-0=100, BT2-0=101, AP2-0=100 */
227 lcd_write_reg(R_DISP_CONTROL1, 0x0005); /* GON=0, DTE=0, REV=1, D1-0=01 */
228 sleep(2);
230 lcd_write_reg(R_DISP_CONTROL1, 0x0025); /* GON=1, DTE=0, REV=1, D1-0=01 */
231 lcd_write_reg(R_DISP_CONTROL1, 0x0027); /* GON=1, DTE=0, REV=1, D1-0=11 */
232 sleep(2);
234 lcd_write_reg(R_DISP_CONTROL1, 0x0037); /* GON=1, DTE=1, REV=1, D1-0=11 */
237 /* LCD init */
238 void lcd_init_device(void)
240 /* GPO46 is LCD RESET */
241 or_l(0x00004000, &GPIO1_OUT);
242 or_l(0x00004000, &GPIO1_ENABLE);
243 or_l(0x00004000, &GPIO1_FUNCTION);
245 /* Reset LCD */
246 and_l(~0x00004000, &GPIO1_OUT);
247 sleep(1);
248 or_l(0x00004000, &GPIO1_OUT);
249 sleep(1);
251 _display_on();
254 void lcd_enable(bool on)
256 if(display_on!=on)
258 if(on)
260 _display_on();
262 else
264 /** Off sequence according to datasheet, p. 130 **/
266 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0002); /* EQ=0, 18 clks/line */
267 lcd_write_reg(R_DISP_CONTROL1, 0x0036); /* GON=1, DTE=1, REV=1, D1-0=10 */
268 sleep(2);
270 lcd_write_reg(R_DISP_CONTROL1, 0x0026); /* GON=1, DTE=0, REV=1, D1-0=10 */
271 sleep(2);
273 lcd_write_reg(R_DISP_CONTROL1, 0x0000); /* GON=0, DTE=0, D1-0=00 */
275 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* SAP2-0=000, AP2-0=000 */
276 lcd_write_reg(R_POWER_CONTROL3, 0x0000); /* PON=0 */
277 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG=0 */
279 /* datasheet p. 131 */
280 lcd_write_reg(R_POWER_CONTROL1, 0x0001); /* STB=1: standby mode */
282 display_on=false;
287 /*** update functions ***/
289 /* Performance function that works with an external buffer
290 note that by and bheight are in 8-pixel units! */
291 void lcd_blit(const fb_data* data, int x, int by, int width,
292 int bheight, int stride)
294 /* TODO: Implement lcd_blit() */
295 (void)data;
296 (void)x;
297 (void)by;
298 (void)width;
299 (void)bheight;
300 (void)stride;
301 /*if(display_on)*/
304 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420.
305 * y should have two lines of Y back to back.
306 * bu and rv should contain the Cb and Cr data for the two lines of Y.
307 * Stores bu, guv and rv in repective buffers for use in second line.
309 extern void lcd_write_yuv420_lines(const unsigned char *y,
310 unsigned char *bu, unsigned char *guv, unsigned char *rv, int width);
312 /* Performance function to blit a YUV bitmap directly to the LCD
313 * src_x, src_y, width and height should be even
314 * x, y, width and height have to be within LCD bounds
316 void lcd_yuv_blit(unsigned char * const src[3],
317 int src_x, int src_y, int stride,
318 int x, int y, int width, int height)
320 /* IRAM Y, Cb/bu, guv and Cb/rv buffers. */
321 unsigned char y_ibuf[LCD_WIDTH*2];
322 unsigned char bu_ibuf[LCD_WIDTH/2];
323 unsigned char guv_ibuf[LCD_WIDTH/2];
324 unsigned char rv_ibuf[LCD_WIDTH/2];
325 const unsigned char *ysrc, *usrc, *vsrc;
326 const unsigned char *ysrc_max;
328 if (!display_on)
329 return;
331 width &= ~1; /* stay on the safe side */
332 height &= ~1;
334 /* Set start position and window */
335 lcd_write_reg(R_VERT_RAM_ADDR_POS,((x+xoffset+width-1) << 8) | (x+xoffset));
336 lcd_write_reg(R_RAM_ADDR_SET, ((x+xoffset) << 8) | y);
338 lcd_begin_write_gram();
340 ysrc = src[0] + src_y * stride + src_x;
341 usrc = src[1] + (src_y * stride >> 2) + (src_x >> 1);
342 vsrc = src[2] + (src_y * stride >> 2) + (src_x >> 1);
343 ysrc_max = ysrc + height * stride;
347 memcpy(y_ibuf, ysrc, width);
348 memcpy(y_ibuf + width, ysrc + stride, width);
349 memcpy(bu_ibuf, usrc, width >> 1);
350 memcpy(rv_ibuf, vsrc, width >> 1);
351 lcd_write_yuv420_lines(y_ibuf, bu_ibuf, guv_ibuf, rv_ibuf, width);
352 ysrc += 2 * stride;
353 usrc += stride >> 1;
354 vsrc += stride >> 1;
356 while (ysrc < ysrc_max);
359 /* Update the display.
360 This must be called after all other LCD functions that change the display. */
361 void lcd_update(void) ICODE_ATTR;
362 void lcd_update(void)
364 if(display_on){
365 /* reset update window */
366 lcd_write_reg(R_VERT_RAM_ADDR_POS,((xoffset+219)<<8) | xoffset);
368 /* Copy display bitmap to hardware */
369 lcd_write_reg(R_RAM_ADDR_SET, xoffset << 8);
370 lcd_begin_write_gram();
371 lcd_write_data((unsigned short *)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
375 /* Update a fraction of the display. */
376 void lcd_update_rect(int, int, int, int) ICODE_ATTR;
377 void lcd_update_rect(int x, int y, int width, int height)
379 if(display_on) {
380 int ymax = y + height - 1;
382 if(x + width > LCD_WIDTH)
383 width = LCD_WIDTH - x;
384 if (width <= 0)
385 return; /* nothing left to do, 0 is harmful to lcd_write_data() */
386 if(ymax >= LCD_HEIGHT)
387 ymax = LCD_HEIGHT-1;
389 /* set update window */
391 lcd_write_reg(R_VERT_RAM_ADDR_POS,((x+xoffset+width-1) << 8) | (x+xoffset));
392 lcd_write_reg(R_RAM_ADDR_SET, ((x+xoffset) << 8) | y);
393 lcd_begin_write_gram();
395 /* Copy specified rectangle bitmap to hardware */
396 for (; y <= ymax; y++)
398 lcd_write_data ((unsigned short *)&lcd_framebuffer[y][x], width);