Gigabeat S: Wrong thing messing with IPU_CONF. No fiddling is needed just yet.
[maemo-rb.git] / firmware / target / arm / imx31 / gigabeat-s / lcd-gigabeat-s.c
blobd567de8dbb420ff5dec26ab8483af1e870f715fb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Will Robertson
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>
22 #include "inttypes.h"
24 #include "config.h"
25 #include "system.h"
26 #include "cpu.h"
27 #include "spi-imx31.h"
28 #include "mc13783.h"
29 #include "string.h"
30 #include "lcd.h"
31 #include "kernel.h"
32 #include "lcd-target.h"
33 #include "backlight-target.h"
35 #define MAIN_LCD_IDMAC_CHANNEL 14
36 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
38 /* Copies a rectangle from one framebuffer to another. Can be used in
39 single transfer mode with width = num pixels, and height = 1 which
40 allows a full-width rectangle to be copied more efficiently. */
41 extern void lcd_copy_buffer_rect(fb_data *dst, const fb_data *src,
42 int width, int height);
44 static bool lcd_on = true;
45 static bool lcd_powered = true;
46 static unsigned lcd_yuv_options = 0;
48 #if 0
49 /* Initialization data from OF bootloader. Identical to Gigabeat F/X. */
50 static const unsigned char lcd_init_data[50] =
52 /* Reg Val */
53 0x0f, 0x01,
54 0x09, 0x06,
55 0x16, 0xa6,
56 0x1e, 0x49,
57 0x1f, 0x26,
58 0x0b, 0x2f, /* Set contrast 0-63 */
59 0x0c, 0x2b,
60 0x19, 0x5e,
61 0x1a, 0x15,
62 0x1b, 0x15,
63 0x1d, 0x01,
64 0x00, 0x03,
65 0x01, 0x10,
66 0x02, 0x0a,
67 0x06, 0x04, /* Set the orientation 2=upside down, 4=normal */
68 0x08, 0x2e,
69 0x24, 0x12,
70 0x25, 0x3f,
71 0x26, 0x0b,
72 0x27, 0x00,
73 0x28, 0x00,
74 0x29, 0xf6,
75 0x2a, 0x03,
76 0x2b, 0x0a,
77 0x04, 0x01, /* Display ON */
79 #endif
81 static const struct spi_node lcd_spi_node =
83 /* Original firmware settings for LCD panel commication */
84 CSPI3_NUM, /* CSPI module 3 */
85 CSPI_CONREG_CHIP_SELECT_SS1 | /* Chip select 1 */
86 CSPI_CONREG_DRCTL_DONT_CARE | /* Don't care about CSPI_RDY */
87 CSPI_CONREG_DATA_RATE_DIV_16 | /* Clock = IPG_CLK/16 = 4,125,000Hz. */
88 CSPI_BITCOUNT(32-1) | /* All 32 bits are to be transferred */
89 CSPI_CONREG_SSPOL | /* SS active high */
90 CSPI_CONREG_PHA | /* Phase 1 operation */
91 CSPI_CONREG_POL | /* Active low polarity */
92 CSPI_CONREG_MODE, /* Master mode */
93 0, /* SPI clock - no wait states */
96 static void lcd_write_reg(unsigned reg, unsigned val)
98 /* packet: |00|rr|01|vv| */
99 uint32_t packet = ((reg & 0xff) << 16) | 0x0100 | (val & 0xff);
101 struct spi_transfer_desc xfer;
103 xfer.node = &lcd_spi_node;
104 xfer.txbuf = &packet;
105 xfer.rxbuf = NULL;
106 xfer.count = 1;
107 xfer.callback = NULL;
108 xfer.next = NULL;
110 if (spi_transfer(&xfer))
112 /* Just busy wait; the interface is not used very much */
113 while (!spi_transfer_complete(&xfer));
117 static void lcd_enable_interface(bool enable)
119 if (enable)
121 spi_enable_module(&lcd_spi_node);
123 else
125 spi_disable_module(&lcd_spi_node);
129 static void lcd_set_power(bool powered)
131 if (powered)
133 lcd_powered = false;
134 lcd_write_reg(0x04, 0x00);
135 lcd_enable_interface(false);
136 imx31_regclr32(&GPIO3_DR, (1 << 12));
137 mc13783_clear(MC13783_REGULATOR_MODE1, MC13783_VCAMEN);
139 else
141 mc13783_set(MC13783_REGULATOR_MODE1, MC13783_VCAMEN);
142 imx31_regset32(&GPIO3_DR, (1 << 12));
143 lcd_enable_interface(true);
144 lcd_write_reg(0x04, 0x01);
145 lcd_powered = true;
149 /* LCD init */
150 void lcd_init_device(void)
152 /* Move the framebuffer */
153 #ifdef BOOTLOADER
154 /* Only do this once to avoid flicker */
155 memset(FRAME, 0x00, FRAME_SIZE);
156 #endif
157 IPU_IPU_IMA_ADDR = ((0x1 << 16) | (MAIN_LCD_IDMAC_CHANNEL << 4)) + (1 << 3);
158 IPU_IPU_IMA_DATA = FRAME_PHYS_ADDR;
160 lcd_enable_interface(true);
161 #ifdef HAVE_LCD_CONTRAST
162 lcd_set_contrast(DEFAULT_CONTRAST_SETTING);
163 #endif
164 #ifdef HAVE_LCD_INVERT
165 lcd_set_invert_display(false);
166 #endif
167 #ifdef HAVE_LCD_FLIP
168 lcd_set_flip(false);
169 #endif
172 /* Update a fraction of the display. */
173 void lcd_update_rect(int x, int y, int width, int height)
175 fb_data *dst, *src;
177 if (!lcd_on)
178 return;
180 if (x + width > LCD_WIDTH)
181 width = LCD_WIDTH - x; /* Clip right */
182 if (x < 0)
183 width += x, x = 0; /* Clip left */
184 if (width <= 0)
185 return; /* nothing left to do */
187 if (y + height > LCD_HEIGHT)
188 height = LCD_HEIGHT - y; /* Clip bottom */
189 if (y < 0)
190 height += y, y = 0; /* Clip top */
191 if (height <= 0)
192 return; /* nothing left to do */
194 /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer
195 * and lcd_framebuffer */
196 dst = (fb_data *)FRAME + LCD_WIDTH*y + x;
197 src = &lcd_framebuffer[y][x];
199 /* Copy part of the Rockbox framebuffer to the second framebuffer */
200 if (width < LCD_WIDTH)
202 /* Not full width - do line-by-line */
203 lcd_copy_buffer_rect(dst, src, width, height);
205 else
207 /* Full width - copy as one line */
208 lcd_copy_buffer_rect(dst, src, LCD_WIDTH*height, 1);
212 void lcd_sleep(void)
214 if (!lcd_powered)
215 return;
217 IPU_IDMAC_CHA_EN &= ~(1ul << MAIN_LCD_IDMAC_CHANNEL);
218 lcd_enable(false);
219 lcd_set_power(false);
220 _backlight_lcd_sleep();
223 void lcd_enable(bool state)
225 if (state == lcd_on)
226 return;
228 if (state)
230 if (!lcd_powered)
231 lcd_set_power(true);
232 IPU_IDMAC_CHA_EN |= 1ul << MAIN_LCD_IDMAC_CHANNEL;
233 sleep(HZ/50);
234 lcd_on = true;
235 lcd_update();
236 send_event(LCD_EVENT_ACTIVATION, NULL);
238 else
240 lcd_on = false;
244 bool lcd_active(void)
246 return lcd_on;
249 /* Update the display.
250 This must be called after all other LCD functions that change the display. */
251 void lcd_update(void)
253 if (!lcd_on)
254 return;
256 lcd_copy_buffer_rect((fb_data *)FRAME, &lcd_framebuffer[0][0],
257 LCD_WIDTH*LCD_HEIGHT, 1);
260 void lcd_yuv_set_options(unsigned options)
262 lcd_yuv_options = options;
265 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
266 extern void lcd_write_yuv420_lines(fb_data *dst,
267 unsigned char const * const src[3],
268 int width,
269 int stride);
270 extern void lcd_write_yuv420_lines_odither(fb_data *dst,
271 unsigned char const * const src[3],
272 int width,
273 int stride,
274 int x_screen, /* To align dither pattern */
275 int y_screen);
276 /* Performance function to blit a YUV bitmap directly to the LCD */
277 /* For the Gigabeat - show it rotated */
278 /* So the LCD_WIDTH is now the height */
279 void lcd_blit_yuv(unsigned char * const src[3],
280 int src_x, int src_y, int stride,
281 int x, int y, int width, int height)
283 /* Caches for chroma data so it only need be recaculated every other
284 line */
285 unsigned char const * yuv_src[3];
286 off_t z;
288 if (!lcd_on)
289 return;
291 /* Sorry, but width and height must be >= 2 or else */
292 width &= ~1;
293 height >>= 1;
295 y = LCD_WIDTH - 1 - y;
296 fb_data *dst = (fb_data*)FRAME + x * LCD_WIDTH + y;
298 z = stride*src_y;
299 yuv_src[0] = src[0] + z + src_x;
300 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
301 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
303 if (lcd_yuv_options & LCD_YUV_DITHER)
307 lcd_write_yuv420_lines_odither(dst, yuv_src, width, stride, y, x);
308 yuv_src[0] += stride << 1; /* Skip down two luma lines */
309 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
310 yuv_src[2] += stride >> 1;
311 dst -= 2;
312 y -= 2;
314 while (--height > 0);
316 else
320 lcd_write_yuv420_lines(dst, yuv_src, width, stride);
321 yuv_src[0] += stride << 1; /* Skip down two luma lines */
322 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
323 yuv_src[2] += stride >> 1;
324 dst -= 2;
326 while (--height > 0);
330 #ifdef HAVE_LCD_CONTRAST
331 void lcd_set_contrast(int val)
333 if (!lcd_on)
334 return;
336 lcd_write_reg(0x0b, val);
339 int lcd_default_contrast(void)
341 return DEFAULT_CONTRAST_SETTING;
343 #endif /* HAVE_LCD_CONTRAST */
345 #ifdef HAVE_LCD_INVERT
346 void lcd_set_invert_display(bool yesno)
348 if (!lcd_on)
349 return;
351 lcd_write_reg(0x27, yesno ? 0x10 : 00);
353 #endif /* HAVE_LCD_INVERT */
355 #ifdef HAVE_LCD_FLIP
356 void lcd_set_flip(bool yesno)
358 if (!lcd_on)
359 return;
361 lcd_write_reg(0x06, yesno ? 0x02 : 0x04);
363 #endif /* HAVE_LCD_FLIP */