udelay between command and data write seems to get rid of the display glitches on...
[kugel-rb.git] / firmware / target / arm / lcd-c200_c200v2.c
blobf82f8a809e1d769cf7bd6444306c44c588b6c76d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 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 ****************************************************************************/
21 #include <sys/types.h>
23 #include "config.h"
24 #include "cpu.h"
25 #include "lcd.h"
26 #include "kernel.h"
27 #include "system.h"
28 #ifdef SANSA_C200V2
29 #include "dbop-as3525.h"
30 #endif
32 /* Display status */
33 #if MEMORYSIZE > 2
34 static unsigned lcd_yuv_options SHAREDBSS_ATTR = 0;
35 #endif
36 static bool is_lcd_enabled = true;
38 /* LCD command set for Samsung S6B33B2 */
40 #define R_NOP 0x00
41 #define R_OSCILLATION_MODE 0x02
42 #define R_DRIVER_OUTPUT_MODE 0x10
43 #define R_DCDC_SET 0x20
44 #define R_BIAS_SET 0x22
45 #define R_DCDC_CLOCK_DIV 0x24
46 #define R_DCDC_AMP_ONOFF 0x26
47 #define R_TEMP_COMPENSATION 0x28
48 #define R_CONTRAST_CONTROL1 0x2a
49 #define R_CONTRAST_CONTROL2 0x2b
50 #define R_STANDBY_OFF 0x2c
51 #define R_STANDBY_ON 0x2d
52 #define R_DDRAM_BURST_OFF 0x2e
53 #define R_DDRAM_BURST_ON 0x2f
54 #define R_ADDRESSING_MODE 0x30
55 #define R_ROW_VECTOR_MODE 0x32
56 #define R_N_LINE_INVERSION 0x34
57 #define R_FRAME_FREQ_CONTROL 0x36
58 #define R_RED_PALETTE 0x38
59 #define R_GREEN_PALETTE 0x3a
60 #define R_BLUE_PALETTE 0x3c
61 #define R_ENTRY_MODE 0x40
62 #define R_X_ADDR_AREA 0x42
63 #define R_Y_ADDR_AREA 0x43
64 #define R_RAM_SKIP_AREA 0x45
65 #define R_DISPLAY_OFF 0x50
66 #define R_DISPLAY_ON 0x51
67 #define R_SPEC_DISPLAY_PATTERN 0x53
68 #define R_PARTIAL_DISPLAY_MODE 0x55
69 #define R_PARTIAL_START_LINE 0x56
70 #define R_PARTIAL_END_LINE 0x57
71 #define R_AREA_SCROLL_MODE 0x59
72 #define R_SCROLL_START_LINE 0x5a
73 #define R_DATA_FORMAT_SELECT 0x60
75 #if defined(SANSA_C200)
76 /* wait for LCD */
77 static inline void lcd_wait_write(void)
79 while (LCD1_CONTROL & LCD1_BUSY_MASK);
82 /* send LCD data */
83 static void lcd_send_pixel(const fb_data data)
85 lcd_wait_write();
86 LCD1_DATA = data >> 8;
87 lcd_wait_write();
88 LCD1_DATA = data & 0xff;
91 inline void lcd_write_data(const fb_data *data, int width)
93 do {
94 lcd_send_pixel(*data++);
95 } while(--width);
98 /* send LCD command */
99 static void lcd_send_command(unsigned char cmd, unsigned char arg)
101 lcd_wait_write();
102 LCD1_CMD = cmd;
103 /* if the argument is 0, we send a NOP (= 0) command */
104 lcd_wait_write();
105 LCD1_CMD = arg;
108 static inline void c200v1_lcd_init(void)
110 /* This is from the c200 of bootloader beginning at offset 0xbbf4 */
111 outl(inl(0x70000010) & ~0xfc000000, 0x70000010);
112 outl(inl(0x70000010), 0x70000010);
114 DEV_INIT2 &= ~0x400;
115 udelay(10000);
117 LCD1_CONTROL &= ~0x4;
118 udelay(15);
120 LCD1_CONTROL |= 0x4;
121 udelay(10);
123 LCD1_CONTROL = 0x0084; /* bits (9,10) = 00 -> fastest setting */
124 udelay(10000);
127 #elif defined(SANSA_C200V2)
129 /* send LCD data */
130 void lcd_write_data(const fb_data *data, int width)
132 do {
133 DBOP_DOUT = *data << 8 | *data >> 8;
134 data++;
136 /* Wait if push fifo is full */
137 while ((DBOP_STAT & (1<<6)) != 0);
138 } while(--width);
140 /* While push fifo is not empty */
141 while ((DBOP_STAT & (1<<10)) == 0);
144 /* send LCD command */
145 static void lcd_send_command(unsigned char cmd, unsigned char val)
147 DBOP_TIMPOL_23 = 0xa167006e;
149 DBOP_DOUT = cmd | val << 8;
151 while ((DBOP_STAT & (1<<10)) == 0);
153 DBOP_TIMPOL_23 = 0xa167e06f;
156 static inline void as3525_dbop_init(void)
158 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
160 DBOP_TIMPOL_01 = 0xe167e167;
161 DBOP_TIMPOL_23 = 0xe167006e;
162 DBOP_CTRL = 0x40008;
164 GPIOB_AFSEL = 0xc;
165 GPIOC_AFSEL = 0xff;
167 DBOP_TIMPOL_23 = 0x6006e;
168 DBOP_CTRL = 0x52008;
169 DBOP_TIMPOL_01 = 0x6e167;
170 DBOP_TIMPOL_23 = 0xa167e06f;
172 udelay(20000);
175 #endif
177 static void lcd_reset(void)
179 #if defined(SANSA_C200V2)
180 /* reset lcd */
181 GPIOB_DIR |= (1<<6);
182 GPIOB_PIN(6) = 0; /* pull reset low */
183 udelay(20000);
184 GPIOB_PIN(6) = 1<<6; /* release reset */
185 udelay(20000);
186 #endif
187 lcd_send_command(R_STANDBY_OFF, 0);
188 udelay(20000);
190 lcd_send_command(R_OSCILLATION_MODE, 0x01);
191 udelay(20000);
193 lcd_send_command(R_DCDC_AMP_ONOFF, 0x01);
194 udelay(20000);
196 lcd_send_command(R_DCDC_AMP_ONOFF, 0x09);
197 udelay(20000);
199 lcd_send_command(R_DCDC_AMP_ONOFF, 0x0b);
200 udelay(20000);
202 lcd_send_command(R_DCDC_AMP_ONOFF, 0x0f);
203 udelay(20000);
205 lcd_send_command(R_DRIVER_OUTPUT_MODE, 0x07);
207 lcd_send_command(R_DCDC_SET, 0x03);
209 lcd_send_command(R_DCDC_CLOCK_DIV, 0x03);
211 lcd_send_command(R_TEMP_COMPENSATION, 0x01);
213 lcd_send_command(R_CONTRAST_CONTROL1, 0x55);
215 lcd_send_command(R_ADDRESSING_MODE, 0x10);
217 lcd_send_command(R_ROW_VECTOR_MODE, 0x0e);
219 lcd_send_command(R_N_LINE_INVERSION, 0x0d);
221 lcd_send_command(R_FRAME_FREQ_CONTROL, 0);
223 lcd_send_command(R_ENTRY_MODE, 0x82);
225 /* vertical dimensions */
226 lcd_send_command(R_Y_ADDR_AREA, 0x1a); /* y1 + 0x1a */
227 lcd_send_command(LCD_HEIGHT - 1 + 0x1a, 0); /* y2 + 0x1a */
229 /* horizontal dimensions */
230 lcd_send_command(R_X_ADDR_AREA, 0); /* x1 */
231 lcd_send_command(LCD_WIDTH - 1, 0); /* x2 */
233 udelay(100000);
235 lcd_send_command(R_DISPLAY_ON, 0);
238 /* LCD init */
239 void lcd_init_device(void)
241 #if defined(SANSA_C200)
242 c200v1_lcd_init();
243 #elif defined(SANSA_C200V2)
244 as3525_dbop_init();
245 #endif
246 lcd_reset();
249 /*** hardware configuration ***/
250 int lcd_default_contrast(void)
252 return DEFAULT_CONTRAST_SETTING;
255 void lcd_set_contrast(int val)
257 lcd_send_command(R_CONTRAST_CONTROL1, val);
260 void lcd_set_invert_display(bool yesno)
262 #ifdef HAVE_LCD_INVERT
263 lcd_send_command(R_SPEC_DISPLAY_PATTERN, yesno ? 1 : 0);
264 #else
265 (void)yesno;
266 #endif
269 #if defined(HAVE_LCD_ENABLE)
270 void lcd_enable(bool yesno)
272 if (yesno == is_lcd_enabled)
273 return;
275 if ((is_lcd_enabled = yesno))
277 lcd_send_command(R_STANDBY_OFF, 0);
278 lcd_send_command(R_DISPLAY_ON, 0);
279 send_event(LCD_EVENT_ACTIVATION, NULL);
281 else
283 lcd_send_command(R_STANDBY_ON, 0);
286 #endif
288 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
289 bool lcd_active(void)
291 return is_lcd_enabled;
293 #endif
296 /* turn the display upside down (call lcd_update() afterwards) */
297 void lcd_set_flip(bool yesno)
299 lcd_send_command(R_DRIVER_OUTPUT_MODE, yesno ? 0x02 : 0x07);
302 /*** update functions ***/
304 #if MEMORYSIZE > 2 /* not for C200V2 */
305 void lcd_yuv_set_options(unsigned options)
307 lcd_yuv_options = options;
310 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
311 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
312 int width,
313 int stride);
314 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
315 int width,
316 int stride,
317 int x_screen, /* To align dither pattern */
318 int y_screen);
319 /* Performance function to blit a YUV bitmap directly to the LCD */
320 void lcd_blit_yuv(unsigned char * const src[3],
321 int src_x, int src_y, int stride,
322 int x, int y, int width, int height)
324 unsigned char const * yuv_src[3];
325 off_t z;
327 /* Sorry, but width and height must be >= 2 or else */
328 width &= ~1;
329 height >>= 1;
331 y += 0x1a;
333 z = stride*src_y;
334 yuv_src[0] = src[0] + z + src_x;
335 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
336 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
338 lcd_send_command(R_ENTRY_MODE, 0x80);
340 lcd_send_command(R_X_ADDR_AREA, x);
341 lcd_send_command(x + width - 1, 0);
343 if (lcd_yuv_options & LCD_YUV_DITHER)
347 lcd_send_command(R_Y_ADDR_AREA, y);
348 lcd_send_command(y + 1, 0);
350 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
352 yuv_src[0] += stride << 1; /* Skip down two luma lines */
353 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
354 yuv_src[2] += stride >> 1;
355 y += 2;
357 while (--height > 0);
359 else
363 lcd_send_command(R_Y_ADDR_AREA, y);
364 lcd_send_command(y + 1, 0);
366 lcd_write_yuv420_lines(yuv_src, width, stride);
368 yuv_src[0] += stride << 1; /* Skip down two luma lines */
369 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
370 yuv_src[2] += stride >> 1;
371 y += 2;
373 while (--height > 0);
376 #endif /* MEMORYSIZE > 2 */
378 /* Update the display.
379 This must be called after all other LCD functions that change the display. */
380 void lcd_update(void)
382 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
385 /* Update a fraction of the display. */
386 void lcd_update_rect(int x, int y, int width, int height)
388 const fb_data *addr;
390 if (x + width >= LCD_WIDTH)
391 width = LCD_WIDTH - x;
392 if (y + height >= LCD_HEIGHT)
393 height = LCD_HEIGHT - y;
395 if ((width <= 0) || (height <= 0))
396 return; /* Nothing left to do. */
398 addr = &lcd_framebuffer[y][x];
400 if (width <= 1) {
401 /* The X end address must be larger than the X start address, so we
402 * switch to vertical mode for single column updates and set the
403 * window width to 2 */
404 lcd_send_command(R_ENTRY_MODE, 0x80);
405 lcd_send_command(R_X_ADDR_AREA, x);
406 lcd_send_command(x + 1, 0);
407 } else {
408 lcd_send_command(R_ENTRY_MODE, 0x82);
409 lcd_send_command(R_X_ADDR_AREA, x);
410 lcd_send_command(x + width - 1, 0);
413 lcd_send_command(R_Y_ADDR_AREA, y + 0x1a);
414 lcd_send_command(y + height - 1 + 0x1a, 0);
416 #ifdef SANSA_C200V2
417 /* somehow there are glitches without this delay */
418 udelay(1);
419 #endif
421 do {
422 lcd_write_data(addr, width);
423 addr += LCD_WIDTH;
424 } while (--height > 0);