Commit FS#11034 by Andrew Engelbrecht. Fixes default contrast value on ipod 3G.
[kugel-rb.git] / firmware / target / arm / ipod / lcd-gray.c
blob05ae303952c9ead49eaeb4a77c124a222cce77f5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Rockbox driver for iPod LCDs
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in November 2005
15 * Original file: linux/arch/armnommu/mach-ipod/fb.c
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
27 ****************************************************************************/
28 #include "config.h"
29 #include "cpu.h"
30 #include "lcd.h"
31 #include "kernel.h"
32 #include "system.h"
33 #include "hwcompat.h"
35 /* LCD command codes for HD66753 */
37 #define R_START_OSC 0x00
38 #define R_DRV_OUTPUT_CONTROL 0x01
39 #define R_DRV_WAVEFORM_CONTROL 0x02
40 #define R_POWER_CONTROL 0x03
41 #define R_CONTRAST_CONTROL 0x04
42 #define R_ENTRY_MODE 0x05
43 #define R_ROTATION 0x06
44 #define R_DISPLAY_CONTROL 0x07
45 #define R_CURSOR_CONTROL 0x08
46 #define R_HORIZONTAL_CURSOR_POS 0x0b
47 #define R_VERTICAL_CURSOR_POS 0x0c
48 #define R_1ST_SCR_DRV_POS 0x0d
49 #define R_2ND_SCR_DRV_POS 0x0e
50 #define R_RAM_WRITE_MASK 0x10
51 #define R_RAM_ADDR_SET 0x11
52 #define R_RAM_DATA 0x12
54 #ifdef HAVE_BACKLIGHT_INVERSION
55 /* The backlight makes the LCD appear negative on the 1st/2nd gen */
56 static bool lcd_inverted = false;
57 static bool lcd_backlit = false;
58 #if NUM_CORES > 1
59 /* invert_display() and the lcd_blit_* functions need to be corelocked */
60 static struct corelock cl IBSS_ATTR;
61 #endif
62 static void invert_display(void);
63 #endif
65 #if defined(IPOD_1G2G) || defined(IPOD_3G)
66 #define POWER_REG_H 0x1120 /* 1/7 Bias, 5x step-up @ clk/8 */
67 #else
68 #define POWER_REG_H 0x1200 /* 1/7 Bias, 6x step-up @ clk/32 */
69 #endif
71 #define CONTRAST_REG_H 0x400
73 #if defined(IPOD_1G2G)
74 #define DEFAULT_CONTRAST 45
75 #elif defined(IPOD_3G)
76 #define DEFAULT_CONTRAST 40
77 #elif defined(IPOD_MINI) || defined(IPOD_MINI2G)
78 #define DEFAULT_CONTRAST 42
79 #elif defined(IPOD_4G)
80 #define DEFAULT_CONTRAST 35
81 #endif
83 /* needed for flip */
84 static int addr_offset;
85 #if defined(IPOD_MINI) || defined(IPOD_MINI2G)
86 static int pix_offset;
87 void lcd_write_data_shifted(const fb_data* p_bytes, int count);
88 #endif
90 /* wait for LCD with timeout */
91 static inline void lcd_wait_write(void)
93 while (LCD1_CONTROL & LCD1_BUSY_MASK);
96 /* send LCD command */
97 static void lcd_prepare_cmd(unsigned cmd)
99 lcd_wait_write();
100 #ifdef IPOD_MINI2G
101 LCD1_CMD = cmd | 0x740000;
102 #else
103 LCD1_CMD = 0;
104 lcd_wait_write();
105 LCD1_CMD = cmd;
106 #endif
109 /* send LCD command and data */
110 static void lcd_cmd_and_data(unsigned cmd, unsigned data)
112 lcd_wait_write();
113 #ifdef IPOD_MINI2G
114 LCD1_CMD = cmd | 0x740000;
115 lcd_wait_write();
116 LCD1_CMD = data | 0x760000;
117 #else
118 LCD1_CMD = 0;
119 lcd_wait_write();
120 LCD1_CMD = cmd;
121 lcd_wait_write();
122 LCD1_DATA = data >> 8;
123 lcd_wait_write();
124 LCD1_DATA = data & 0xff;
125 #endif
128 /* LCD init */
129 void lcd_init_device(void)
131 #if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
132 corelock_init(&cl);
133 #endif
134 #ifdef IPOD_MINI2G /* serial LCD hookup */
135 lcd_wait_write();
136 LCD1_CONTROL = 0x01730084; /* fastest setting */
137 #elif defined(IPOD_1G2G) || defined(IPOD_3G)
138 LCD1_CONTROL = (LCD1_CONTROL & 0x0002) | 0x0084;
139 /* fastest setting, keep backlight bit */
140 #else
141 LCD1_CONTROL = 0x0084; /* fastest setting */
142 #endif
144 lcd_cmd_and_data(R_DRV_WAVEFORM_CONTROL, 0x48);
145 /* C waveform, no EOR, 9 lines inversion */
146 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0xc);
147 lcd_cmd_and_data(R_DISPLAY_CONTROL, 0x0019);
148 lcd_set_contrast(DEFAULT_CONTRAST);
149 #ifdef HAVE_BACKLIGHT_INVERSION
150 invert_display();
151 #endif
152 lcd_set_flip(false);
153 lcd_cmd_and_data(R_ENTRY_MODE, 0x0000);
156 /*** hardware configuration ***/
158 int lcd_default_contrast(void)
160 return DEFAULT_CONTRAST;
163 /* Rockbox stores the contrast as 0..63 - we add 64 to it */
164 void lcd_set_contrast(int val)
166 if (val < 0) val = 0;
167 else if (val > 63) val = 63;
169 lcd_cmd_and_data(R_CONTRAST_CONTROL, CONTRAST_REG_H | (val + 64));
172 #ifdef HAVE_BACKLIGHT_INVERSION
173 static void invert_display(void)
175 static bool last_invert = false;
176 bool new_invert = lcd_inverted ^ lcd_backlit;
178 if (new_invert != last_invert)
180 int oldlevel = disable_irq_save();
181 #if NUM_CORES > 1
182 corelock_lock(&cl);
183 lcd_cmd_and_data(R_DISPLAY_CONTROL, new_invert? 0x0027 : 0x0019);
184 corelock_unlock(&cl);
185 #else
186 lcd_cmd_and_data(R_DISPLAY_CONTROL, new_invert? 0x0027 : 0x0019);
187 #endif
188 restore_irq(oldlevel);
189 last_invert = new_invert;
193 void lcd_set_invert_display(bool yesno)
195 lcd_inverted = yesno;
196 invert_display();
199 void lcd_set_backlight_inversion(bool yesno)
201 lcd_backlit = yesno;
202 invert_display();
204 #else
205 void lcd_set_invert_display(bool yesno)
207 lcd_cmd_and_data(R_DISPLAY_CONTROL, yesno ? 0x0027 : 0x0019);
209 #endif
211 /* turn the display upside down (call lcd_update() afterwards) */
212 void lcd_set_flip(bool yesno)
214 #if defined(IPOD_MINI) || defined(IPOD_MINI2G)
215 if (yesno)
216 { /* 168x112, inverse COM order */
217 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x020d);
218 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x8316); /* 22..131 */
219 addr_offset = (22 << 5) | (20 - 4);
220 pix_offset = -2;
222 else
223 { /* 168x112, inverse SEG order */
224 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x010d);
225 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x6d00); /* 0..109 */
226 addr_offset = 20;
227 pix_offset = 0;
229 #else
230 if (yesno)
231 { /* 168x128, inverse SEG & COM order */
232 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x030f);
233 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x8304); /* 4..131 */
234 addr_offset = (4 << 5) | (20 - 1);
236 else
237 { /* 168x128 */
238 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x000f);
239 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x7f00); /* 0..127 */
240 addr_offset = 20;
242 #endif
245 #ifdef HAVE_LCD_ENABLE
246 void lcd_enable(bool on)
248 if (on)
250 lcd_cmd_and_data(R_START_OSC, 1); /* start oscillation */
251 sleep(HZ/10); /* wait 10ms */
252 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H); /*clear standby mode */
253 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0xc);
254 /* enable opamp & booster */
256 else
258 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H);
259 /* switch off opamp & booster */
260 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x1);
261 /* enter standby mode */
264 #endif /* HAVE_LCD_ENABLE */
266 /*** update functions ***/
268 /* Helper function. */
269 void lcd_mono_data(const unsigned char *data, int count);
271 /* Performance function that works with an external buffer
272 note that x, bwidtht and stride are in 8-pixel units! */
273 void lcd_blit_mono(const unsigned char *data, int bx, int y, int bwidth,
274 int height, int stride)
276 #if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
277 corelock_lock(&cl);
278 #endif
279 while (height--)
281 lcd_cmd_and_data(R_RAM_ADDR_SET, (y++ << 5) + addr_offset - bx);
282 lcd_prepare_cmd(R_RAM_DATA);
283 lcd_mono_data(data, bwidth);
284 data += stride;
286 #if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
287 corelock_unlock(&cl);
288 #endif
291 /* Helper function for lcd_grey_phase_blit(). */
292 void lcd_grey_data(unsigned char *values, unsigned char *phases, int count);
294 /* Performance function that works with an external buffer
295 note that bx and bwidth are in 8-pixel units! */
296 void lcd_blit_grey_phase(unsigned char *values, unsigned char *phases,
297 int bx, int y, int bwidth, int height, int stride)
299 #if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
300 corelock_lock(&cl);
301 #endif
302 while (height--)
304 lcd_cmd_and_data(R_RAM_ADDR_SET, (y++ << 5) + addr_offset - bx);
305 lcd_prepare_cmd(R_RAM_DATA);
306 lcd_grey_data(values, phases, bwidth);
307 values += stride;
308 phases += stride;
310 #if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
311 corelock_unlock(&cl);
312 #endif
315 void lcd_update_rect(int x, int y, int width, int height)
317 int xmax, ymax;
319 if (x + width > LCD_WIDTH)
320 width = LCD_WIDTH - x;
321 if (width <= 0)
322 return;
324 ymax = y + height - 1;
325 if (ymax >= LCD_HEIGHT)
326 ymax = LCD_HEIGHT - 1;
328 #if defined(IPOD_MINI) || defined(IPOD_MINI2G)
329 x += pix_offset;
330 #endif
331 /* writing is done in 16-bit units (8 pixels) */
332 xmax = (x + width - 1) >> 3;
333 x >>= 3;
334 width = xmax - x + 1;
336 for (; y <= ymax; y++)
338 lcd_cmd_and_data(R_RAM_ADDR_SET, (y << 5) + addr_offset - x);
339 lcd_prepare_cmd(R_RAM_DATA);
341 #if defined(IPOD_MINI) || defined(IPOD_MINI2G)
342 if (pix_offset == -2)
343 lcd_write_data_shifted(&lcd_framebuffer[y][2*x], width);
344 else
345 #endif
346 lcd_write_data(&lcd_framebuffer[y][2*x], width);
350 /* Update the display. */
351 void lcd_update(void)
353 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
356 #ifdef HAVE_LCD_SHUTDOWN
357 /* LCD powerdown */
358 void lcd_shutdown(void)
360 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x00); /* Turn off op amp power */
361 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x02); /* Put LCD driver in standby */
363 #endif