1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Dave Chapman
12 * LCD driver for the Sansa Fuze - controller unknown
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
30 #include "clock-target.h"
31 #include "dbop-as3525.h"
33 /* The controller is unknown, but some registers appear to be the same as the
35 static bool display_on
= false; /* is the display turned on? */
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
81 #define R_ENTRY_MODE_HORZ_NORMAL 0x7030
82 #define R_ENTRY_MODE_HORZ_FLIPPED 0x7000
83 static 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
87 #define R_ENTRY_MODE_VIDEO_NORMAL 0x7038
88 #define R_ENTRY_MODE_VIDEO_FLIPPED 0x7018
91 #define R_DISP_CONTROL_NORMAL 0x0004
92 #define R_DISP_CONTROL_REV 0x0000
93 static unsigned short r_disp_control_rev
= R_DISP_CONTROL_NORMAL
;
95 static const int xoffset
= 20;
97 static inline void lcd_delay(int x
)
100 asm volatile ("nop\n");
104 static void as3525_dbop_init(void)
106 CGU_DBOP
= (1<<3) | AS3525_DBOP_DIV
;
108 DBOP_TIMPOL_01
= 0xe167e167;
109 DBOP_TIMPOL_23
= 0xe167006e;
111 /* short count: 16 | output data width: 16 | readstrobe line */
112 DBOP_CTRL
= (1<<18|1<<12|1<<3);
117 DBOP_TIMPOL_23
= 0x6000e;
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;
124 /* TODO: The OF calls some other functions here, but maybe not important */
127 static void lcd_write_cmd(short cmd
)
130 DBOP_TIMPOL_23
= 0xa167006e;
131 dbop_write_data(&cmd
, 1);
135 DBOP_TIMPOL_23
= 0xa167e06f;
138 static void lcd_write_reg(int reg
, int value
)
140 int16_t data
= value
;
143 dbop_write_data(&data
, 1);
146 /*** hardware configuration ***/
148 void lcd_set_contrast(int val
)
153 void lcd_set_invert_display(bool yesno
)
155 r_disp_control_rev
= yesno
? R_DISP_CONTROL_REV
:
156 R_DISP_CONTROL_NORMAL
;
160 lcd_write_reg(R_DISP_CONTROL1
, 0x0013 | r_disp_control_rev
);
166 static bool display_flipped
= false;
168 /* turn the display upside down */
169 void lcd_set_flip(bool yesno
)
171 display_flipped
= yesno
;
173 r_entry_mode
= yesno
? R_ENTRY_MODE_HORZ_FLIPPED
:
174 R_ENTRY_MODE_HORZ_NORMAL
;
178 static void _display_on(void)
180 /* Initialise in the same way as the original firmare */
182 lcd_write_reg(R_DISP_CONTROL1
, 0);
183 lcd_write_reg(R_POWER_CONTROL4
, 0);
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);
190 lcd_write_reg(R_POWER_CONTROL4
, 0x60);
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);
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);
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);
219 lcd_write_reg(R_DISP_CONTROL1
, 0x11);
220 lcd_write_reg(R_DISP_CONTROL1
, 0x13 | r_disp_control_rev
);
222 display_on
= true; /* must be done before calling lcd_update() */
226 void lcd_init_device(void)
230 GPIOA_DIR
|= (1<<5|1<<4|1<<3);
232 GPIOA_PIN(3) = (1<<3);
234 GPIOA_PIN(5) = (1<<5);
239 #if defined(HAVE_LCD_ENABLE)
240 void lcd_enable(bool on
)
242 if (display_on
== on
)
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
);
258 lcd_update(); /* Resync display */
259 send_event(LCD_EVENT_ACTIVATION
, NULL
);
265 lcd_write_reg(R_DISP_CONTROL1
, 0x22);
266 lcd_write_reg(R_DISP_CONTROL1
, 0);
267 lcd_write_reg(R_POWER_CONTROL1
, 1);
273 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
274 bool lcd_active(void)
280 /*** update functions ***/
282 /* FIXME : find the datasheet for this RENESAS controller so we identify the
283 * registers used in windowing code (not present in HD66789R) */
285 /* Set horizontal window addresses */
286 static void lcd_window_x(int xmin
, int xmax
)
290 lcd_write_reg(R_HORIZ_RAM_ADDR_POS
+ 2, (xmax
<< 8) | xmin
);
291 lcd_write_reg(R_RAM_ADDR_SET
- 1, xmin
);
294 /* Set vertical window addresses */
295 static void lcd_window_y(int ymin
, int ymax
)
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
);
302 static unsigned lcd_yuv_options
= 0;
304 void lcd_yuv_set_options(unsigned options
)
306 lcd_yuv_options
= options
;
309 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
310 extern void lcd_write_yuv420_lines(unsigned char const * const src
[3],
313 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src
[3],
316 int x_screen
, /* To align dither pattern */
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
323 void 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
)
327 unsigned char const * yuv_src
[3];
330 /* Sorry, but width and height must be >= 2 or else */
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]);
340 lcd_write_reg(R_ENTRY_MODE
,
341 display_flipped
? R_ENTRY_MODE_VIDEO_FLIPPED
: R_ENTRY_MODE_VIDEO_NORMAL
344 lcd_write_reg(R_ENTRY_MODE
, R_ENTRY_MODE_VIDEO_NORMAL
);
347 lcd_window_x(x
, x
+ width
- 1);
349 if (lcd_yuv_options
& LCD_YUV_DITHER
)
353 lcd_window_y(y
, y
+ 1);
355 lcd_write_cmd(R_WRITE_DATA_2_GRAM
);
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;
363 while (--height
> 0);
369 lcd_window_y(y
, y
+ 1);
371 lcd_write_cmd(R_WRITE_DATA_2_GRAM
);
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;
379 while (--height
> 0);
383 /* Update the display.
384 This must be called after all other LCD functions that change the display. */
385 void lcd_update(void)
390 lcd_write_reg(R_ENTRY_MODE
, r_entry_mode
);
392 lcd_window_x(0, LCD_WIDTH
- 1);
393 lcd_window_y(0, LCD_HEIGHT
- 1);
395 lcd_write_cmd(R_WRITE_DATA_2_GRAM
);
397 dbop_write_data((fb_data
*)lcd_framebuffer
, LCD_WIDTH
*LCD_HEIGHT
);
400 /* Update a fraction of the display. */
401 void lcd_update_rect(int x
, int y
, int width
, int height
)
408 /* nothing to draw? */
409 if ((width
<= 0) || (height
<= 0) || (x
>= LCD_WIDTH
) ||
410 (y
>= LCD_HEIGHT
) || (x
+ width
<= 0) || (y
+ height
<= 0))
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 */
428 lcd_write_reg(R_ENTRY_MODE
, r_entry_mode
);
430 /* we need to make x and width even to enable 32bit transfers */
431 width
= (width
+ (x
& 1) + 1) & ~1;
434 lcd_window_x(x
, x
+ width
- 1);
435 lcd_window_y(y
, y
+ height
-1);
437 lcd_write_cmd(R_WRITE_DATA_2_GRAM
);
439 ptr
= &lcd_framebuffer
[y
][x
];
443 dbop_write_data(ptr
, width
);
446 while (--height
> 0);