summaryrefslogtreecommitdiff
path: root/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c')
-rw-r--r--firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c447
1 files changed, 447 insertions, 0 deletions
diff --git a/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c b/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
new file mode 100644
index 0000000000..bdf1c704e0
--- /dev/null
+++ b/firmware/target/arm/as3525/sansa-fuzev2/lcd-fuzev2.c
@@ -0,0 +1,447 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 by Dave Chapman
11 *
12 * LCD driver for the Sansa Fuze - controller unknown
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23#include "config.h"
24
25#include "cpu.h"
26#include "lcd.h"
27#include "file.h"
28#include "debug.h"
29#include "system.h"
30#include "clock-target.h"
31#include "dbop-as3525.h"
32
33/* The controller is unknown, but some registers appear to be the same as the
34 HD66789R */
35static bool display_on = false; /* is the display turned on? */
36
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
80/* Flip Flag */
81#define R_ENTRY_MODE_HORZ_NORMAL 0x7030
82#define R_ENTRY_MODE_HORZ_FLIPPED 0x7000
83static unsigned short r_entry_mode = R_ENTRY_MODE_HORZ_NORMAL;
84#define R_ENTRY_MODE_VERT 0x7038
85#define R_ENTRY_MODE_SOLID_VERT 0x1038
86/* FIXME */
87#define R_ENTRY_MODE_VIDEO_NORMAL 0x7038
88#define R_ENTRY_MODE_VIDEO_FLIPPED 0x7018
89
90/* Reverse Flag */
91#define R_DISP_CONTROL_NORMAL 0x0004
92#define R_DISP_CONTROL_REV 0x0000
93static unsigned short r_disp_control_rev = R_DISP_CONTROL_NORMAL;
94
95static const int xoffset = 20;
96
97static inline void lcd_delay(int x)
98{
99 do {
100 asm volatile ("nop\n");
101 } while (x--);
102}
103
104static void as3525_dbop_init(void)
105{
106 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
107
108 DBOP_TIMPOL_01 = 0xe167e167;
109 DBOP_TIMPOL_23 = 0xe167006e;
110
111 /* short count: 16 | output data width: 16 | readstrobe line */
112 DBOP_CTRL = (1<<18|1<<12|1<<3);
113
114 GPIOB_AFSEL = 0xfc;
115 GPIOC_AFSEL = 0xff;
116
117 DBOP_TIMPOL_23 = 0x6000e;
118
119 /* short count: 16|enable write|output data width: 16|read strobe line */
120 DBOP_CTRL = (1<<18|1<<16|1<<12|1<<3);
121 DBOP_TIMPOL_01 = 0x6e167;
122 DBOP_TIMPOL_23 = 0xa167e06f;
123
124 /* TODO: The OF calls some other functions here, but maybe not important */
125}
126
127static void lcd_write_cmd(short cmd)
128{
129 /* Write register */
130 DBOP_TIMPOL_23 = 0xa167006e;
131 dbop_write_data(&cmd, 1);
132
133 lcd_delay(4);
134
135 DBOP_TIMPOL_23 = 0xa167e06f;
136}
137
138static void lcd_write_reg(int reg, int value)
139{
140 int16_t data = value;
141
142 lcd_write_cmd(reg);
143 dbop_write_data(&data, 1);
144}
145
146/*** hardware configuration ***/
147
148void lcd_set_contrast(int val)
149{
150 (void)val;
151}
152
153void lcd_set_invert_display(bool yesno)
154{
155 r_disp_control_rev = yesno ? R_DISP_CONTROL_REV :
156 R_DISP_CONTROL_NORMAL;
157
158 if (display_on)
159 {
160 lcd_write_reg(R_DISP_CONTROL1, 0x0013 | r_disp_control_rev);
161 }
162
163}
164
165#ifdef HAVE_LCD_FLIP
166static bool display_flipped = false;
167
168/* turn the display upside down */
169void lcd_set_flip(bool yesno)
170{
171 display_flipped = yesno;
172
173 r_entry_mode = yesno ? R_ENTRY_MODE_HORZ_FLIPPED :
174 R_ENTRY_MODE_HORZ_NORMAL;
175}
176#endif
177
178static void _display_on(void)
179{
180 /* Initialise in the same way as the original firmare */
181
182 lcd_write_reg(R_DISP_CONTROL1, 0);
183 lcd_write_reg(R_POWER_CONTROL4, 0);
184
185 lcd_write_reg(R_POWER_CONTROL2, 0x3704);
186 lcd_write_reg(0x14, 0x1a1b);
187 lcd_write_reg(R_POWER_CONTROL1, 0x3860);
188 lcd_write_reg(R_POWER_CONTROL4, 0x40);
189
190 lcd_write_reg(R_POWER_CONTROL4, 0x60);
191
192 lcd_write_reg(R_POWER_CONTROL4, 0x70);
193 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 277);
194 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, (7<<8));
195 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
196 lcd_write_reg(R_DISP_CONTROL2, 0x01);
197 lcd_write_reg(R_FRAME_CYCLE_CONTROL, (1<<10));
198 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0);
199
200 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x40);
201 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0687);
202 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0306);
203 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x104);
204 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0585);
205 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 255+66);
206 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0687+128);
207 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 259);
208 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0);
209 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0);
210
211 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_WIDTH - 1));
212 lcd_write_reg(R_2ND_SCR_DRV_POS, 0);
213 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (LCD_WIDTH - 1));
214 lcd_write_reg(R_VERT_RAM_ADDR_POS, 0);
215 lcd_write_reg(0x46, (((LCD_WIDTH - 1) + xoffset) << 8) | xoffset);
216 lcd_write_reg(0x47, (LCD_HEIGHT - 1));
217 lcd_write_reg(0x48, 0x0);
218
219 lcd_write_reg(R_DISP_CONTROL1, 0x11);
220 lcd_write_reg(R_DISP_CONTROL1, 0x13 | r_disp_control_rev);
221
222 display_on = true; /* must be done before calling lcd_update() */
223 lcd_update();
224}
225
226void lcd_init_device(void)
227{
228 as3525_dbop_init();
229
230 GPIOA_DIR |= (1<<5|1<<4|1<<3);
231 GPIOA_PIN(5) = 0;
232 GPIOA_PIN(3) = (1<<3);
233 GPIOA_PIN(4) = 0;
234 GPIOA_PIN(5) = (1<<5);
235
236 _display_on();
237}
238
239#if defined(HAVE_LCD_ENABLE)
240void lcd_enable(bool on)
241{
242 if (display_on == on)
243 return;
244
245 if(on)
246 {
247 lcd_write_reg(R_START_OSC, 1);
248 lcd_write_reg(R_POWER_CONTROL1, 0);
249 lcd_write_reg(R_POWER_CONTROL2, 0x3704);
250 lcd_write_reg(0x14, 0x1a1b);
251 lcd_write_reg(R_POWER_CONTROL1, 0x3860);
252 lcd_write_reg(R_POWER_CONTROL4, 0x40);
253 lcd_write_reg(R_POWER_CONTROL4, 0x60);
254 lcd_write_reg(R_POWER_CONTROL4, 112);
255 lcd_write_reg(R_DISP_CONTROL1, 0x11);
256 lcd_write_reg(R_DISP_CONTROL1, 0x13 | r_disp_control_rev);
257 display_on = true;
258 lcd_update(); /* Resync display */
259 send_event(LCD_EVENT_ACTIVATION, NULL);
260 sleep(0);
261
262 }
263 else
264 {
265 lcd_write_reg(R_DISP_CONTROL1, 0x22);
266 lcd_write_reg(R_DISP_CONTROL1, 0);
267 lcd_write_reg(R_POWER_CONTROL1, 1);
268 display_on = false;
269 }
270}
271#endif
272
273#if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
274bool lcd_active(void)
275{
276 return display_on;
277}
278#endif
279
280/*** update functions ***/
281
282/* FIXME : find the datasheet for this RENESAS controller so we identify the
283 * registers used in windowing code (not present in HD66789R) */
284
285/* Set horizontal window addresses */
286static void lcd_window_x(int xmin, int xmax)
287{
288 xmin += xoffset;
289 xmax += xoffset;
290 lcd_write_reg(R_HORIZ_RAM_ADDR_POS + 2, (xmax << 8) | xmin);
291 lcd_write_reg(R_RAM_ADDR_SET - 1, xmin);
292}
293
294/* Set vertical window addresses */
295static void lcd_window_y(int ymin, int ymax)
296{
297 lcd_write_reg(R_VERT_RAM_ADDR_POS + 2, ymax);
298 lcd_write_reg(R_VERT_RAM_ADDR_POS + 3, ymin);
299 lcd_write_reg(R_RAM_ADDR_SET, ymin);
300}
301
302static unsigned lcd_yuv_options = 0;
303
304void lcd_yuv_set_options(unsigned options)
305{
306 lcd_yuv_options = options;
307}
308
309/* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
310extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
311 int width,
312 int stride);
313extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
314 int width,
315 int stride,
316 int x_screen, /* To align dither pattern */
317 int y_screen);
318
319/* Performance function to blit a YUV bitmap directly to the LCD
320 * src_x, src_y, width and height should be even
321 * x, y, width and height have to be within LCD bounds
322 */
323void lcd_blit_yuv(unsigned char * const src[3],
324 int src_x, int src_y, int stride,
325 int x, int y, int width, int height)
326{
327 unsigned char const * yuv_src[3];
328 off_t z;
329
330 /* Sorry, but width and height must be >= 2 or else */
331 width &= ~1;
332 height >>= 1;
333
334 z = stride*src_y;
335 yuv_src[0] = src[0] + z + src_x;
336 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
337 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
338
339#ifdef HAVE_LCD_FLIP
340 lcd_write_reg(R_ENTRY_MODE,
341 display_flipped ? R_ENTRY_MODE_VIDEO_FLIPPED : R_ENTRY_MODE_VIDEO_NORMAL
342 );
343#else
344 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_VIDEO_NORMAL);
345#endif
346
347 lcd_window_x(x, x + width - 1);
348
349 if (lcd_yuv_options & LCD_YUV_DITHER)
350 {
351 do
352 {
353 lcd_window_y(y, y + 1);
354
355 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
356
357 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
358 yuv_src[0] += stride << 1; /* Skip down two luma lines */
359 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
360 yuv_src[2] += stride >> 1;
361 y += 2;
362 }
363 while (--height > 0);
364 }
365 else
366 {
367 do
368 {
369 lcd_window_y(y, y + 1);
370
371 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
372
373 lcd_write_yuv420_lines(yuv_src, width, stride);
374 yuv_src[0] += stride << 1; /* Skip down two luma lines */
375 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
376 yuv_src[2] += stride >> 1;
377 y += 2;
378 }
379 while (--height > 0);
380 }
381}
382
383/* Update the display.
384 This must be called after all other LCD functions that change the display. */
385void lcd_update(void)
386{
387 if (!display_on)
388 return;
389
390 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
391
392 lcd_window_x(0, LCD_WIDTH - 1);
393 lcd_window_y(0, LCD_HEIGHT - 1);
394
395 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
396
397 dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
398}
399
400/* Update a fraction of the display. */
401void lcd_update_rect(int x, int y, int width, int height)
402{
403 const fb_data *ptr;
404
405 if (!display_on)
406 return;
407
408 /* nothing to draw? */
409 if ((width <= 0) || (height <= 0) || (x >= LCD_WIDTH) ||
410 (y >= LCD_HEIGHT) || (x + width <= 0) || (y + height <= 0))
411 return;
412
413 if (x < 0)
414 { /* clip left */
415 width += x;
416 x = 0;
417 }
418 if (y < 0)
419 { /* clip top */
420 height += y;
421 y = 0;
422 }
423 if (x + width > LCD_WIDTH)
424 width = LCD_WIDTH - x; /* clip right */
425 if (y + height > LCD_HEIGHT)
426 height = LCD_HEIGHT - y; /* clip bottom */
427
428 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
429
430 /* we need to make x and width even to enable 32bit transfers */
431 width = (width + (x & 1) + 1) & ~1;
432 x &= ~1;
433
434 lcd_window_x(x, x + width - 1);
435 lcd_window_y(y, y + height -1);
436
437 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
438
439 ptr = &lcd_framebuffer[y][x];
440
441 do
442 {
443 dbop_write_data(ptr, width);
444 ptr += LCD_WIDTH;
445 }
446 while (--height > 0);
447}