blob: 70355920b6340617252e010c70f6459696cf6f98 [file] [log] [blame]
Jens Arnold2906c362007-02-25 13:11:02 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Rockbox driver for iPod LCDs
11 *
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in November 2005
14 *
15 * Original file: linux/arch/armnommu/mach-ipod/fb.c
16 *
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
18 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000019 * 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.
Jens Arnold2906c362007-02-25 13:11:02 +000023 *
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
26 *
27 ****************************************************************************/
28#include "config.h"
29#include "cpu.h"
30#include "lcd.h"
31#include "kernel.h"
32#include "system.h"
Jens Arnold97bdfce2007-08-10 08:09:53 +000033#include "hwcompat.h"
Jens Arnold2906c362007-02-25 13:11:02 +000034
Jens Arnold92abed72007-08-08 07:57:34 +000035/* LCD command codes for HD66753 */
36
Jens Arnold2906c362007-02-25 13:11:02 +000037#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
53
Jens Arnold021c0862007-07-27 12:05:54 +000054#ifdef HAVE_BACKLIGHT_INVERSION
55/* The backlight makes the LCD appear negative on the 1st/2nd gen */
56static bool lcd_inverted = false;
57static bool lcd_backlit = false;
Jens Arnold32bd0f82008-04-04 22:13:53 +000058#if NUM_CORES > 1
59/* invert_display() and the lcd_blit_* functions need to be corelocked */
60static struct corelock cl IBSS_ATTR;
61#endif
Jens Arnold41534a92007-07-27 12:40:36 +000062static void invert_display(void);
Jens Arnold021c0862007-07-27 12:05:54 +000063#endif
64
Jens Arnold97bdfce2007-08-10 08:09:53 +000065#if defined(IPOD_1G2G) || defined(IPOD_3G)
Jens Arnoldb073f0c2008-04-02 19:01:16 +000066#define POWER_REG_H 0x1120 /* 1/7 Bias, 5x step-up @ clk/8 */
Jens Arnold97bdfce2007-08-10 08:09:53 +000067#else
Jens Arnoldb073f0c2008-04-02 19:01:16 +000068#define POWER_REG_H 0x1200 /* 1/7 Bias, 6x step-up @ clk/32 */
Jens Arnold97bdfce2007-08-10 08:09:53 +000069#endif
70
Jens Arnold97bdfce2007-08-10 08:09:53 +000071#define CONTRAST_REG_H 0x400
Jens Arnold97bdfce2007-08-10 08:09:53 +000072
Jens Arnold2906c362007-02-25 13:11:02 +000073/* needed for flip */
74static int addr_offset;
75#if defined(IPOD_MINI) || defined(IPOD_MINI2G)
76static int pix_offset;
Jens Arnold41cd44c2008-01-12 00:59:18 +000077void lcd_write_data_shifted(const fb_data* p_bytes, int count);
Jens Arnold2906c362007-02-25 13:11:02 +000078#endif
79
Jens Arnold2906c362007-02-25 13:11:02 +000080/* wait for LCD with timeout */
81static inline void lcd_wait_write(void)
82{
Jens Arnold8aeed2d2007-10-12 00:28:57 +000083 while (LCD1_CONTROL & LCD1_BUSY_MASK);
Jens Arnold2906c362007-02-25 13:11:02 +000084}
85
Jens Arnold2906c362007-02-25 13:11:02 +000086/* send LCD command */
87static void lcd_prepare_cmd(unsigned cmd)
88{
89 lcd_wait_write();
90#ifdef IPOD_MINI2G
Jens Arnold1f0d06f2007-10-07 16:44:55 +000091 LCD1_CMD = cmd | 0x740000;
Jens Arnold2906c362007-02-25 13:11:02 +000092#else
Jens Arnold1f0d06f2007-10-07 16:44:55 +000093 LCD1_CMD = 0;
Jens Arnold2906c362007-02-25 13:11:02 +000094 lcd_wait_write();
Jens Arnold1f0d06f2007-10-07 16:44:55 +000095 LCD1_CMD = cmd;
Jens Arnold2906c362007-02-25 13:11:02 +000096#endif
97}
98
99/* send LCD command and data */
100static void lcd_cmd_and_data(unsigned cmd, unsigned data)
101{
Jens Arnold41cd44c2008-01-12 00:59:18 +0000102 lcd_wait_write();
103#ifdef IPOD_MINI2G
104 LCD1_CMD = cmd | 0x740000;
105 lcd_wait_write();
106 LCD1_CMD = data | 0x760000;
107#else
108 LCD1_CMD = 0;
109 lcd_wait_write();
110 LCD1_CMD = cmd;
111 lcd_wait_write();
112 LCD1_DATA = data >> 8;
113 lcd_wait_write();
114 LCD1_DATA = data & 0xff;
115#endif
Jens Arnold2906c362007-02-25 13:11:02 +0000116}
117
118/* LCD init */
119void lcd_init_device(void)
120{
Jens Arnold32bd0f82008-04-04 22:13:53 +0000121#if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
122 corelock_init(&cl);
123#endif
Jens Arnold6ef2db22007-11-18 12:49:24 +0000124#ifdef IPOD_MINI2G /* serial LCD hookup */
Jens Arnold97bdfce2007-08-10 08:09:53 +0000125 lcd_wait_write();
Jens Arnold6ef2db22007-11-18 12:49:24 +0000126 LCD1_CONTROL = 0x01730084; /* fastest setting */
127#elif defined(IPOD_1G2G) || defined(IPOD_3G)
Jens Arnoldb073f0c2008-04-02 19:01:16 +0000128 LCD1_CONTROL = (LCD1_CONTROL & 0x0002) | 0x0084;
129 /* fastest setting, keep backlight bit */
Jens Arnold6ef2db22007-11-18 12:49:24 +0000130#else
131 LCD1_CONTROL = 0x0084; /* fastest setting */
Jens Arnold97bdfce2007-08-10 08:09:53 +0000132#endif
133
Jens Arnoldb073f0c2008-04-02 19:01:16 +0000134 lcd_cmd_and_data(R_DRV_WAVEFORM_CONTROL, 0x48);
135 /* C waveform, no EOR, 9 lines inversion */
Jens Arnold97bdfce2007-08-10 08:09:53 +0000136 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0xc);
Jens Arnoldab46e462008-05-17 09:55:23 +0000137 lcd_cmd_and_data(R_DISPLAY_CONTROL, 0x0019);
Jens Arnold41534a92007-07-27 12:40:36 +0000138#ifdef HAVE_BACKLIGHT_INVERSION
139 invert_display();
Jens Arnold41534a92007-07-27 12:40:36 +0000140#endif
Jens Arnold2906c362007-02-25 13:11:02 +0000141 lcd_set_flip(false);
142 lcd_cmd_and_data(R_ENTRY_MODE, 0x0000);
Jens Arnold2906c362007-02-25 13:11:02 +0000143}
144
145/*** hardware configuration ***/
146
147int lcd_default_contrast(void)
148{
Jens Arnoldb073f0c2008-04-02 19:01:16 +0000149#if defined(IPOD_1G2G)
150 return 45;
151#elif defined(IPOD_3G)
152 return 55;
153#elif defined(IPOD_MINI) || defined(IPOD_MINI2G)
Jens Arnold2906c362007-02-25 13:11:02 +0000154 return 42;
Jens Arnold97bdfce2007-08-10 08:09:53 +0000155#elif defined(IPOD_4G)
Jens Arnold2906c362007-02-25 13:11:02 +0000156 return 35;
157#endif
158}
159
160/* Rockbox stores the contrast as 0..63 - we add 64 to it */
161void lcd_set_contrast(int val)
162{
163 if (val < 0) val = 0;
164 else if (val > 63) val = 63;
165
Jens Arnold97bdfce2007-08-10 08:09:53 +0000166 lcd_cmd_and_data(R_CONTRAST_CONTROL, CONTRAST_REG_H | (val + 64));
Jens Arnold2906c362007-02-25 13:11:02 +0000167}
168
Jens Arnold021c0862007-07-27 12:05:54 +0000169#ifdef HAVE_BACKLIGHT_INVERSION
170static void invert_display(void)
171{
Jens Arnold83997492008-04-04 17:33:39 +0000172 static bool last_invert = false;
173 bool new_invert = lcd_inverted ^ lcd_backlit;
174
175 if (new_invert != last_invert)
176 {
177 int oldlevel = disable_irq_save();
Jens Arnold32bd0f82008-04-04 22:13:53 +0000178#if NUM_CORES > 1
179 corelock_lock(&cl);
Jens Arnoldab46e462008-05-17 09:55:23 +0000180 lcd_cmd_and_data(R_DISPLAY_CONTROL, new_invert? 0x0027 : 0x0019);
Jens Arnold32bd0f82008-04-04 22:13:53 +0000181 corelock_unlock(&cl);
182#else
Jens Arnoldab46e462008-05-17 09:55:23 +0000183 lcd_cmd_and_data(R_DISPLAY_CONTROL, new_invert? 0x0027 : 0x0019);
Jens Arnold32bd0f82008-04-04 22:13:53 +0000184#endif
Jens Arnold83997492008-04-04 17:33:39 +0000185 restore_irq(oldlevel);
186 last_invert = new_invert;
187 }
Jens Arnold021c0862007-07-27 12:05:54 +0000188}
189
190void lcd_set_invert_display(bool yesno)
191{
192 lcd_inverted = yesno;
193 invert_display();
194}
195
196void lcd_set_backlight_inversion(bool yesno)
197{
198 lcd_backlit = yesno;
199 invert_display();
200}
201#else
Jens Arnold2906c362007-02-25 13:11:02 +0000202void lcd_set_invert_display(bool yesno)
203{
Jens Arnoldab46e462008-05-17 09:55:23 +0000204 lcd_cmd_and_data(R_DISPLAY_CONTROL, yesno ? 0x0027 : 0x0019);
Jens Arnold2906c362007-02-25 13:11:02 +0000205}
Jens Arnold021c0862007-07-27 12:05:54 +0000206#endif
Jens Arnold2906c362007-02-25 13:11:02 +0000207
208/* turn the display upside down (call lcd_update() afterwards) */
209void lcd_set_flip(bool yesno)
210{
211#if defined(IPOD_MINI) || defined(IPOD_MINI2G)
Jens Arnold41cd44c2008-01-12 00:59:18 +0000212 if (yesno)
213 { /* 168x112, inverse COM order */
Jens Arnold2906c362007-02-25 13:11:02 +0000214 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x020d);
215 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x8316); /* 22..131 */
216 addr_offset = (22 << 5) | (20 - 4);
217 pix_offset = -2;
Jens Arnold41cd44c2008-01-12 00:59:18 +0000218 }
219 else
220 { /* 168x112, inverse SEG order */
Jens Arnold2906c362007-02-25 13:11:02 +0000221 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x010d);
222 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x6d00); /* 0..109 */
223 addr_offset = 20;
224 pix_offset = 0;
225 }
226#else
Jens Arnold41cd44c2008-01-12 00:59:18 +0000227 if (yesno)
228 { /* 168x128, inverse SEG & COM order */
Jens Arnold2906c362007-02-25 13:11:02 +0000229 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x030f);
230 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x8304); /* 4..131 */
231 addr_offset = (4 << 5) | (20 - 1);
Jens Arnold41cd44c2008-01-12 00:59:18 +0000232 }
233 else
234 { /* 168x128 */
Jens Arnold2906c362007-02-25 13:11:02 +0000235 lcd_cmd_and_data(R_DRV_OUTPUT_CONTROL, 0x000f);
236 lcd_cmd_and_data(R_1ST_SCR_DRV_POS, 0x7f00); /* 0..127 */
237 addr_offset = 20;
238 }
239#endif
240}
241
Jens Arnold97bdfce2007-08-10 08:09:53 +0000242void lcd_enable(bool on)
243{
244 if (on)
245 {
246 lcd_cmd_and_data(R_START_OSC, 1); /* start oscillation */
247 sleep(HZ/10); /* wait 10ms */
248 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H); /*clear standby mode */
249 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0xc);
250 /* enable opamp & booster */
251 }
252 else
253 {
254 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H);
255 /* switch off opamp & booster */
256 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x1);
257 /* enter standby mode */
258 }
259}
260
Jens Arnold2906c362007-02-25 13:11:02 +0000261/*** update functions ***/
262
Jens Arnold41cd44c2008-01-12 00:59:18 +0000263/* Helper function. */
264void lcd_mono_data(const unsigned char *data, int count);
265
Jens Arnold2906c362007-02-25 13:11:02 +0000266/* Performance function that works with an external buffer
267 note that x, bwidtht and stride are in 8-pixel units! */
Jens Arnold68a21682008-03-24 00:35:53 +0000268void lcd_blit_mono(const unsigned char *data, int bx, int y, int bwidth,
269 int height, int stride)
Jens Arnold2906c362007-02-25 13:11:02 +0000270{
Jens Arnold32bd0f82008-04-04 22:13:53 +0000271#if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
272 corelock_lock(&cl);
273#endif
Jens Arnold41cd44c2008-01-12 00:59:18 +0000274 while (height--)
275 {
Jens Arnold2906c362007-02-25 13:11:02 +0000276 lcd_cmd_and_data(R_RAM_ADDR_SET, (y++ << 5) + addr_offset - bx);
277 lcd_prepare_cmd(R_RAM_DATA);
Jens Arnold41cd44c2008-01-12 00:59:18 +0000278 lcd_mono_data(data, bwidth);
Jens Arnold2906c362007-02-25 13:11:02 +0000279 data += stride;
280 }
Jens Arnold32bd0f82008-04-04 22:13:53 +0000281#if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
282 corelock_unlock(&cl);
283#endif
Jens Arnold2906c362007-02-25 13:11:02 +0000284}
285
Jens Arnold41cd44c2008-01-12 00:59:18 +0000286/* Helper function for lcd_grey_phase_blit(). */
287void lcd_grey_data(unsigned char *values, unsigned char *phases, int count);
288
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000289/* Performance function that works with an external buffer
290 note that bx and bwidth are in 8-pixel units! */
Jens Arnold68a21682008-03-24 00:35:53 +0000291void lcd_blit_grey_phase(unsigned char *values, unsigned char *phases,
Jens Arnold6a56c142008-01-09 23:48:26 +0000292 int bx, int y, int bwidth, int height, int stride)
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000293{
Jens Arnold32bd0f82008-04-04 22:13:53 +0000294#if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
295 corelock_lock(&cl);
296#endif
297 while (height--)
Jens Arnold41cd44c2008-01-12 00:59:18 +0000298 {
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000299 lcd_cmd_and_data(R_RAM_ADDR_SET, (y++ << 5) + addr_offset - bx);
300 lcd_prepare_cmd(R_RAM_DATA);
Jens Arnold41cd44c2008-01-12 00:59:18 +0000301 lcd_grey_data(values, phases, bwidth);
Jens Arnold6a56c142008-01-09 23:48:26 +0000302 values += stride;
303 phases += stride;
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000304 }
Jens Arnold32bd0f82008-04-04 22:13:53 +0000305#if (NUM_CORES > 1) && defined(HAVE_BACKLIGHT_INVERSION)
306 corelock_unlock(&cl);
307#endif
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000308}
309
Jens Arnold2906c362007-02-25 13:11:02 +0000310void lcd_update_rect(int x, int y, int width, int height)
311{
312 int xmax, ymax;
313
314 if (x + width > LCD_WIDTH)
315 width = LCD_WIDTH - x;
316 if (width <= 0)
317 return;
318
319 ymax = y + height - 1;
320 if (ymax >= LCD_HEIGHT)
321 ymax = LCD_HEIGHT - 1;
322
323#if defined(IPOD_MINI) || defined(IPOD_MINI2G)
324 x += pix_offset;
325#endif
326 /* writing is done in 16-bit units (8 pixels) */
327 xmax = (x + width - 1) >> 3;
328 x >>= 3;
329 width = xmax - x + 1;
330
Jens Arnold41cd44c2008-01-12 00:59:18 +0000331 for (; y <= ymax; y++)
332 {
Jens Arnold2906c362007-02-25 13:11:02 +0000333 lcd_cmd_and_data(R_RAM_ADDR_SET, (y << 5) + addr_offset - x);
334 lcd_prepare_cmd(R_RAM_DATA);
335
Jens Arnold2906c362007-02-25 13:11:02 +0000336#if defined(IPOD_MINI) || defined(IPOD_MINI2G)
Jens Arnold41cd44c2008-01-12 00:59:18 +0000337 if (pix_offset == -2)
338 lcd_write_data_shifted(&lcd_framebuffer[y][2*x], width);
339 else
Jens Arnold2906c362007-02-25 13:11:02 +0000340#endif
Jens Arnold41cd44c2008-01-12 00:59:18 +0000341 lcd_write_data(&lcd_framebuffer[y][2*x], width);
Jens Arnold2906c362007-02-25 13:11:02 +0000342 }
343}
344
345/* Update the display. */
346void lcd_update(void)
347{
348 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
349}
Nicolas Pennequinf5483362007-09-29 20:56:07 +0000350
351#ifdef HAVE_LCD_SHUTDOWN
352/* LCD powerdown */
353void lcd_shutdown(void)
354{
Jens Arnoldb073f0c2008-04-02 19:01:16 +0000355 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x00); /* Turn off op amp power */
356 lcd_cmd_and_data(R_POWER_CONTROL, POWER_REG_H | 0x02); /* Put LCD driver in standby */
Nicolas Pennequinf5483362007-09-29 20:56:07 +0000357}
358#endif