blob: b7865fa7c525fd27fda3fdf2891f86ef83f188c3 [file] [log] [blame]
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
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"
32
Jens Arnoldcb9e3162006-03-17 15:42:06 +000033static bool display_on = false; /* is the display turned on? */
34static bool display_flipped = false;
35static int xoffset = 0; /* needed for flip */
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +000036
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
44
45#define R_DISP_CONTROL1 0x07
46#define R_DISP_CONTROL2 0x08
47#define R_DISP_CONTROL3 0x09
48
49#define R_FRAME_CYCLE_CONTROL 0x0b
50#define R_EXT_DISP_IF_CONTROL 0x0c
51
52#define R_POWER_CONTROL1 0x10
53#define R_POWER_CONTROL2 0x11
54#define R_POWER_CONTROL3 0x12
55#define R_POWER_CONTROL4 0x13
56
57#define R_RAM_ADDR_SET 0x21
58#define R_WRITE_DATA_2_GRAM 0x22
59
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
64
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
69
70#define R_GAMMA_AMP_ADJ_RES_POS 0x38
71#define R_GAMMA_AMP_AVG_ADJ_RES_NEG 0x39
72
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
79
Jens Arnoldc5a309a2006-08-08 13:44:43 +000080#define LCD_CMD (*(volatile unsigned short *)0xf0000000)
81#define LCD_DATA (*(volatile unsigned short *)0xf0000002)
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +000082
83/* called very frequently - inline! */
Jens Arnoldf3857ab2006-02-05 00:24:08 +000084static inline void lcd_write_reg(int reg, int val)
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000085{
Jens Arnoldc5a309a2006-08-08 13:44:43 +000086 LCD_CMD = reg;
87 LCD_DATA = val;
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000088}
89
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +000090/* called very frequently - inline! */
Jens Arnoldf3857ab2006-02-05 00:24:08 +000091static inline void lcd_begin_write_gram(void)
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000092{
Jens Arnoldc5a309a2006-08-08 13:44:43 +000093 LCD_CMD = R_WRITE_DATA_2_GRAM;
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000094}
95
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000096/*** hardware configuration ***/
97
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +000098void lcd_set_contrast(int val)
99{
100 (void)val;
101}
102
103void lcd_set_invert_display(bool yesno)
104{
105 (void)yesno;
106}
107
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000108static void flip_lcd(bool yesno)
109{
110 if (yesno)
111 {
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 */
115 }
116 else
117 {
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 */
121 }
122}
123
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000124/* turn the display upside down (call lcd_update() afterwards) */
125void lcd_set_flip(bool yesno)
126{
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000127 display_flipped = yesno;
128 xoffset = yesno ? 4 : 0;
129
130 if (display_on)
131 flip_lcd(yesno);
132}
133
134static void _display_on(void)
135{
136 /** Sequence according to datasheet, p. 132 **/
137
138 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
139 sleep(1);
140
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);
147
148 /* initialise power supply */
149
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);
154
155 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
156 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
157
158 /* VDV4-0 = 00110b: VcomA = Vreg1OUT * 0.76,
159 * VCM4-0 = 10000b: VcomH = Vreg1OUT * 0.70*/
160 lcd_write_reg(R_POWER_CONTROL4, 0x0610);
161
162 lcd_write_reg(R_POWER_CONTROL1, 0x0044); /* AP2-0 = 100b, DK = 1 */
163 lcd_write_reg(R_POWER_CONTROL3, 0x0018); /* PON = 1 */
164
165 sleep(4); /* Step-up circuit stabilising time */
166
167 /* start power supply */
168
169 lcd_write_reg(R_POWER_CONTROL1, 0x0540); /* BT2-0 = 101b, DK = 0 */
170 lcd_write_reg(R_POWER_CONTROL4, 0x2610); /* VCOMG = 1 */
171
172 /* other settings */
173
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);
179
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);
186
187 flip_lcd(display_flipped);
188
189 lcd_write_reg(R_2ND_SCR_DRV_POS, 0x0000);
190 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0x0000);
191
192 /* 19 clocks,no equalization */
193 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0002);
194
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);
199
200 /* Front porch lines: 8; Back porch lines: 8; */
201 lcd_write_reg(R_DISP_CONTROL2, 0x0808);
202
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);
207
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);
218
219 display_on=true; /* must be done before calling lcd_update() */
220 lcd_update();
221
222 sleep(4); /* op-amp stabilising time */
223
224 /** Sequence according to datasheet, p. 130 **/
225
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);
229
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);
233
234 lcd_write_reg(R_DISP_CONTROL1, 0x0037); /* GON=1, DTE=1, REV=1, D1-0=11 */
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000235}
236
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000237/* LCD init */
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000238void lcd_init_device(void)
239{
240 /* GPO46 is LCD RESET */
241 or_l(0x00004000, &GPIO1_OUT);
242 or_l(0x00004000, &GPIO1_ENABLE);
243 or_l(0x00004000, &GPIO1_FUNCTION);
244
245 /* Reset LCD */
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000246 and_l(~0x00004000, &GPIO1_OUT);
247 sleep(1);
248 or_l(0x00004000, &GPIO1_OUT);
249 sleep(1);
250
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000251 _display_on();
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000252}
253
254void lcd_enable(bool on)
255{
256 if(display_on!=on)
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000257 {
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000258 if(on)
259 {
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000260 _display_on();
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000261 }
262 else
263 {
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000264 /** Off sequence according to datasheet, p. 130 **/
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000265
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000266 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);
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000269
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000270 lcd_write_reg(R_DISP_CONTROL1, 0x0026); /* GON=1, DTE=0, REV=1, D1-0=10 */
271 sleep(2);
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000272
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000273 lcd_write_reg(R_DISP_CONTROL1, 0x0000); /* GON=0, DTE=0, D1-0=00 */
274
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 */
278
279 /* datasheet p. 131 */
280 lcd_write_reg(R_POWER_CONTROL1, 0x0001); /* STB=1: standby mode */
281
Hristo Kovachev9cf52702006-01-09 16:11:19 +0000282 display_on=false;
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000283 }
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000284 }
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000285}
286
287/*** update functions ***/
288
289/* Performance function that works with an external buffer
290 note that by and bheight are in 8-pixel units! */
291void lcd_blit(const fb_data* data, int x, int by, int width,
292 int bheight, int stride)
293{
294 /* TODO: Implement lcd_blit() */
295 (void)data;
296 (void)x;
297 (void)by;
298 (void)width;
299 (void)bheight;
300 (void)stride;
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000301 /*if(display_on)*/
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000302}
303
Jens Arnold9af13c32006-08-23 17:30:51 +0000304/* 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.
308 */
309extern void lcd_write_yuv420_lines(const unsigned char *y,
310 unsigned char *bu, unsigned char *guv, unsigned char *rv, int width);
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000311
Jens Arnold9af13c32006-08-23 17:30:51 +0000312/* 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
315 */
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000316void 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)
319{
Jens Arnold9af13c32006-08-23 17:30:51 +0000320 /* 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;
327
328 if (!display_on)
329 return;
330
331 width &= ~1; /* stay on the safe side */
332 height &= ~1;
333
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);
337
338 lcd_begin_write_gram();
339
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;
344
345 do
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000346 {
Jens Arnold9af13c32006-08-23 17:30:51 +0000347 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;
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000355 }
Jens Arnold9af13c32006-08-23 17:30:51 +0000356 while (ysrc < ysrc_max);
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000357}
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000358
359/* Update the display.
360 This must be called after all other LCD functions that change the display. */
361void lcd_update(void) ICODE_ATTR;
362void lcd_update(void)
363{
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000364 if(display_on){
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000365 /* reset update window */
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000366 lcd_write_reg(R_VERT_RAM_ADDR_POS,((xoffset+219)<<8) | xoffset);
367
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000368 /* Copy display bitmap to hardware */
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000369 lcd_write_reg(R_RAM_ADDR_SET, xoffset << 8);
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000370 lcd_begin_write_gram();
Jens Arnold83aded92006-11-02 20:50:50 +0000371
372 DAR3 = 0xf0000002;
373 SAR3 = (unsigned long)lcd_framebuffer;
374 BCR3 = LCD_WIDTH*LCD_HEIGHT*2;
375 DCR3 = DMA_AA | DMA_BWC(1)
376 | DMA_SINC | DMA_SSIZE(DMA_SIZE_LINE)
377 | DMA_DSIZE(DMA_SIZE_WORD) | DMA_START;
378
379 while (!(DSR3 & 1));
380 DSR3 = 1;
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000381 }
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000382}
383
384/* Update a fraction of the display. */
385void lcd_update_rect(int, int, int, int) ICODE_ATTR;
386void lcd_update_rect(int x, int y, int width, int height)
387{
Jens Arnold83aded92006-11-02 20:50:50 +0000388 unsigned long dma_addr;
389
Jens Arnoldc5a309a2006-08-08 13:44:43 +0000390 if(display_on) {
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000391
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000392 if(x + width > LCD_WIDTH)
393 width = LCD_WIDTH - x;
Jens Arnold83aded92006-11-02 20:50:50 +0000394 if(width <= 0) /* nothing to do */
395 return;
396 if(y + height > LCD_HEIGHT)
397 height = LCD_HEIGHT - y;
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000398
Jens Arnold83aded92006-11-02 20:50:50 +0000399 /* set update window */
Linus Nielsen Feltzing6ddfac02005-12-11 00:30:15 +0000400
Jens Arnoldcb9e3162006-03-17 15:42:06 +0000401 lcd_write_reg(R_VERT_RAM_ADDR_POS,((x+xoffset+width-1) << 8) | (x+xoffset));
402 lcd_write_reg(R_RAM_ADDR_SET, ((x+xoffset) << 8) | y);
Jens Arnold83aded92006-11-02 20:50:50 +0000403 lcd_begin_write_gram();
404
405 DAR3 = 0xf0000002;
406 dma_addr = (unsigned long)&lcd_framebuffer[y][x];
407 width *= 2;
Linus Nielsen Feltzing6ddfac02005-12-11 00:30:15 +0000408
Jens Arnold83aded92006-11-02 20:50:50 +0000409 for (; height > 0; height--)
410 {
411 SAR3 = dma_addr;
412 BCR3 = width;
413 DCR3 = DMA_AA | DMA_BWC(1)
414 | DMA_SINC | DMA_SSIZE(DMA_SIZE_LINE)
415 | DMA_DSIZE(DMA_SIZE_WORD) | DMA_START;
416
417 dma_addr += LCD_WIDTH*2;
418
419 while (!(DSR3 & 1));
420 DSR3 = 1;
421 }
Linus Nielsen Feltzing67f00222005-12-20 23:15:27 +0000422 }
Linus Nielsen Feltzingdbc8c2c2005-11-14 20:41:49 +0000423}