lcd drivers: Convert lcd_[remote_]framebuffer to a pointer
[maemo-rb.git] / firmware / target / arm / imx233 / sansa-fuzeplus / lcd-fuzeplus.c
blobb25fff8c3b97c36bcfbe1782d6b9bf19f4db475e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2011 by Amaury Pouly
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> /* off_t */
22 #include <string.h>
23 #include "cpu.h"
24 #include "system.h"
25 #include "backlight-target.h"
26 #include "lcd.h"
27 #include "lcdif-imx233.h"
28 #include "clkctrl-imx233.h"
29 #include "pinctrl-imx233.h"
30 #include "dcp-imx233.h"
31 #include "logf.h"
33 #ifdef HAVE_LCD_ENABLE
34 bool lcd_on; /* framebuffer-imx233.c */
35 #endif
36 static unsigned lcd_yuv_options = 0;
37 static int lcd_dcp_channel = -1;
39 static enum lcd_kind_t
41 LCD_KIND_7783 = 0x7783,
42 LCD_KIND_9325 = 0x9325,
43 LCD_KIND_OTHER = 0,
44 } lcd_kind = LCD_KIND_OTHER;
46 static void setup_parameters(void)
48 imx233_lcdif_reset();
49 imx233_lcdif_set_lcd_databus_width(HW_LCDIF_CTRL__LCD_DATABUS_WIDTH_18_BIT);
50 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
51 imx233_lcdif_set_timings(1, 2, 2, 2);
54 static void setup_lcd_pins(bool use_lcdif)
56 if(use_lcdif)
58 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
59 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
60 imx233_set_pin_function(1, 22, PINCTRL_FUNCTION_GPIO); /* lcd_dotclk */
61 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
62 imx233_set_pin_function(1, 24, PINCTRL_FUNCTION_GPIO); /* lcd_hsync */
63 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
64 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
65 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
66 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
67 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
68 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
70 else
72 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
73 imx233_enable_gpio_output_mask(1, 0x3ffffff, false); /* lcd_{d{0-17},reset,rs,wr,cs,dotclk,enable,hsync,vsync} */
74 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
75 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
76 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
77 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
78 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
79 imx233_set_pin_function(1, 22, PINCTRL_FUNCTION_GPIO); /* lcd_dotclk */
80 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
81 imx233_set_pin_function(1, 24, PINCTRL_FUNCTION_GPIO); /* lcd_hsync */
82 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
86 static void setup_lcd_pins_i80(bool i80)
88 if(i80)
90 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_12mA); /* lcd_rs */
91 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_12mA); /* lcd_wr */
92 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_12mA); /* lcd_cs */
93 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_12mA); /* lcd_enable */
94 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
95 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
96 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
97 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
98 /* lcd_{rs,wr,cs,enable} */
99 imx233_enable_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
100 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
102 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
103 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as GPIO */
104 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
105 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
106 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_GPIO); /* lcd_reset */
107 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
109 else
111 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
112 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_4mA); /* lcd_rs */
113 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_4mA); /* lcd_wr */
114 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_4mA); /* lcd_cs */
115 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_4mA); /* lcd_enable */
116 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
117 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
118 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
119 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
120 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as lcd_d{0-15} */
121 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
122 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
123 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
124 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
128 static void common_lcd_enable(bool enable)
130 imx233_lcdif_enable(enable);
131 setup_lcd_pins(enable); /* use GPIO pins when disable */
134 static void setup_lcdif(void)
136 setup_parameters();
137 common_lcd_enable(true);
138 imx233_lcdif_enable_bus_master(true);
141 static inline uint32_t encode_16_to_18(uint32_t a)
143 return ((a & 0xff) << 1) | (((a >> 8) & 0xff) << 10);
146 static inline uint32_t decode_18_to_16(uint32_t a)
148 return ((a >> 1) & 0xff) | ((a >> 2) & 0xff00);
151 static void setup_lcdif_clock(void)
153 /* the LCD seems to work at 24Mhz, so use the xtal clock with no divider */
154 imx233_enable_clock(CLK_PIX, false);
155 imx233_set_clock_divisor(CLK_PIX, 1);
156 imx233_set_bypass_pll(CLK_PIX, true); /* use XTAL */
157 imx233_enable_clock(CLK_PIX, true);
160 static uint32_t i80_read_register(uint32_t data_out)
162 imx233_lcdif_wait_ready();
163 /* lcd_enable is mapped to the RD pin of the controller */
164 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
165 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
166 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
167 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
168 imx233_enable_gpio_output_mask(1, 0x3ffff, true); /* lcd_d{0-17} */
169 udelay(2);
170 imx233_set_gpio_output(1, 19, false); /* lcd_rs */
171 udelay(1);
172 imx233_set_gpio_output(1, 21, false); /* lcd_cs */
173 udelay(1);
174 imx233_set_gpio_output(1, 20, false); /* lcd_wr */
175 udelay(1);
176 imx233_set_gpio_output_mask(1, data_out & 0x3ffff, true); /* lcd_d{0-17} */
177 udelay(1);
178 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
179 udelay(3);
180 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
181 udelay(2);
182 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
183 udelay(1);
184 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
185 udelay(1);
186 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
187 udelay(3);
188 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
189 udelay(2);
190 uint32_t data_in = imx233_get_gpio_input_mask(1, 0x3ffff); /* lcd_d{0-17} */
191 udelay(1);
192 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
193 udelay(1);
194 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
195 udelay(1);
196 return data_in;
199 static void lcd_write_reg(uint32_t reg, uint32_t data)
201 uint32_t old_reg = reg;
202 /* get back to 18-bit word length */
203 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
204 reg = encode_16_to_18(reg);
205 data = encode_16_to_18(data);
207 imx233_lcdif_pio_send(false, 2, &reg);
208 if(old_reg != 0x22)
209 imx233_lcdif_pio_send(true, 2, &data);
212 static uint32_t lcd_read_reg(uint32_t reg)
214 setup_lcd_pins_i80(true);
215 uint32_t data_in = i80_read_register(encode_16_to_18(reg));
216 setup_lcd_pins_i80(false);
217 lcd_write_reg(0x22, 0);
218 return decode_18_to_16(data_in);
221 #define REG_MDELAY 0xffffffff
222 struct lcd_sequence_entry_t
224 uint32_t reg, data;
227 static void lcd_send_sequence(struct lcd_sequence_entry_t *seq, unsigned count)
229 for(;count-- > 0; seq++)
231 if(seq->reg == REG_MDELAY)
232 mdelay(seq->data);
233 else
234 lcd_write_reg(seq->reg, seq->data);
238 #define _begin_seq() static struct lcd_sequence_entry_t __seq[] = {
239 #define _mdelay(a) {REG_MDELAY, a},
240 #define _lcd_write_reg(a, b) {a, b},
241 #define _end_seq() }; lcd_send_sequence(__seq, sizeof(__seq) / sizeof(__seq[0]));
243 static void lcd_init_seq_7783(void)
245 _begin_seq()
246 _mdelay(200)
247 _lcd_write_reg(1, 0x100)
248 _lcd_write_reg(2, 0x700)
249 _lcd_write_reg(3, 0x1030)
250 _lcd_write_reg(7, 0x121)
251 _lcd_write_reg(8, 0x302)
252 _lcd_write_reg(9, 0x200)
253 _lcd_write_reg(0xa, 0)
254 _lcd_write_reg(0x10, 0x790)
255 _lcd_write_reg(0x11, 5)
256 _lcd_write_reg(0x12, 0)
257 _lcd_write_reg(0x13, 0)
258 _mdelay(100)
259 _lcd_write_reg(0x10, 0x12b0)
260 _mdelay(100)
261 _lcd_write_reg(0x11, 7)
262 _mdelay(100)
263 _lcd_write_reg(0x12, 0x89)
264 _lcd_write_reg(0x13, 0x1d00)
265 _lcd_write_reg(0x29, 0x2f)
266 _mdelay(50)
267 _lcd_write_reg(0x30, 0)
268 _lcd_write_reg(0x31, 0x505)
269 _lcd_write_reg(0x32, 0x205)
270 _lcd_write_reg(0x35, 0x206)
271 _lcd_write_reg(0x36, 0x408)
272 _lcd_write_reg(0x37, 0)
273 _lcd_write_reg(0x38, 0x504)
274 _lcd_write_reg(0x39, 0x206)
275 _lcd_write_reg(0x3c, 0x206)
276 _lcd_write_reg(0x3d, 0x408)
277 _lcd_write_reg(0x50, 0) /* left X ? */
278 _lcd_write_reg(0x51, 0xef) /* right X ? */
279 _lcd_write_reg(0x52, 0) /* top Y ? */
280 _lcd_write_reg(0x53, 0x13f) /* bottom Y ? */
281 _lcd_write_reg(0x20, 0) /* left X ? */
282 _lcd_write_reg(0x21, 0) /* top Y ? */
283 _lcd_write_reg(0x60, 0xa700)
284 _lcd_write_reg(0x61, 1)
285 _lcd_write_reg(0x90, 0x33)
286 _lcd_write_reg(0x2b, 0xa)
287 _lcd_write_reg(9, 0)
288 _lcd_write_reg(7, 0x133)
289 _mdelay(50)
290 _lcd_write_reg(0x22, 0)
291 _end_seq()
294 static void lcd_init_seq_9325(void)
296 _begin_seq()
297 _lcd_write_reg(0xe5, 0x78f0)
298 _lcd_write_reg(0xe3, 0x3008)
299 _lcd_write_reg(0xe7, 0x12)
300 _lcd_write_reg(0xef, 0x1231)
301 _lcd_write_reg(0, 1)
302 _lcd_write_reg(1, 0x100)
303 _lcd_write_reg(2, 0x700)
304 _lcd_write_reg(3, 0x1030)
305 _lcd_write_reg(4, 0)
306 _lcd_write_reg(8, 0x207)
307 _lcd_write_reg(9, 0)
308 _lcd_write_reg(0xa, 0)
309 _lcd_write_reg(0xc, 0)
310 _lcd_write_reg(0xd, 0)
311 _lcd_write_reg(0xf, 0)
312 _lcd_write_reg(0x10, 0)
313 _lcd_write_reg(0x11, 7)
314 _lcd_write_reg(0x12, 0)
315 _lcd_write_reg(0x13, 0)
316 _mdelay(20)
317 _lcd_write_reg(0x10, 0x1290)
318 _lcd_write_reg(0x11, 7)
319 _mdelay(50)
320 _lcd_write_reg(0x12, 0x19)
321 _mdelay(50)
322 _lcd_write_reg(0x13, 0x1700)
323 _lcd_write_reg(0x29, 0x14)
324 _mdelay(50)
325 _lcd_write_reg(0x20, 0)
326 _lcd_write_reg(0x21, 0)
327 _lcd_write_reg(0x30, 0x504)
328 _lcd_write_reg(0x31, 7)
329 _lcd_write_reg(0x32, 6)
330 _lcd_write_reg(0x35, 0x106)
331 _lcd_write_reg(0x36, 0x202)
332 _lcd_write_reg(0x37, 0x504)
333 _lcd_write_reg(0x38, 0x500)
334 _lcd_write_reg(0x39, 0x706)
335 _lcd_write_reg(0x3c, 0x204)
336 _lcd_write_reg(0x3d, 0x202)
337 _lcd_write_reg(0x50, 0)
338 _lcd_write_reg(0x51, 0xef)
339 _lcd_write_reg(0x52, 0)
340 _lcd_write_reg(0x53, 0x13f)
341 _lcd_write_reg(0x60, 0xa700)
342 _lcd_write_reg(0x61, 1)
343 _lcd_write_reg(0x6a, 0)
344 _lcd_write_reg(0x2b, 0xd)
345 _mdelay(50)
346 _lcd_write_reg(0x90, 0x11)
347 _lcd_write_reg(0x92, 0x600)
348 _lcd_write_reg(0x93, 3)
349 _lcd_write_reg(0x95, 0x110)
350 _lcd_write_reg(0x97, 0)
351 _lcd_write_reg(0x98, 0)
352 _lcd_write_reg(7, 0x173)
353 _lcd_write_reg(0x22, 0)
354 _end_seq()
357 void lcd_init_device(void)
359 lcd_dcp_channel = imx233_dcp_acquire_channel(TIMEOUT_NOBLOCK);
360 if(lcd_dcp_channel < 0)
361 panicf("imx233_framebuffer_init: imx233_dcp_acquire_channel failed!");
362 setup_lcdif();
363 setup_lcdif_clock();
365 for(int i = 0; i < 10; i++)
367 lcd_kind = lcd_read_reg(0);
368 mdelay(5);
369 if(lcd_kind == LCD_KIND_7783 || lcd_kind == LCD_KIND_9325)
370 break;
372 // reset device
373 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
374 mdelay(50);
375 __REG_CLR(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
376 mdelay(10);
377 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
379 switch(lcd_kind)
381 case LCD_KIND_7783: lcd_init_seq_7783(); break;
382 case LCD_KIND_9325: lcd_init_seq_9325(); break;
383 default:
384 lcd_kind = LCD_KIND_7783;
385 lcd_init_seq_7783(); break;
388 #ifdef HAVE_LCD_ENABLE
389 lcd_on = true;
390 #endif
393 #ifdef HAVE_LCD_ENABLE
394 bool lcd_active(void)
396 return lcd_on;
399 static void lcd_enable_7783(bool enable)
401 if(!enable)
403 _begin_seq()
404 _lcd_write_reg(7, 0x131)
405 _mdelay(50)
406 _lcd_write_reg(7, 0x20)
407 _mdelay(50)
408 _lcd_write_reg(0x10, 0x82)
409 _mdelay(50)
410 _end_seq()
412 else
414 _begin_seq()
415 _lcd_write_reg(0x11, 5)
416 _lcd_write_reg(0x10, 0x12b0)
417 _mdelay(50)
418 _lcd_write_reg(7, 0x11)
419 _mdelay(50)
420 _lcd_write_reg(0x12, 0x89)
421 _mdelay(50)
422 _lcd_write_reg(0x13, 0x1d00)
423 _mdelay(50)
424 _lcd_write_reg(0x29, 0x2f)
425 _mdelay(50)
426 _lcd_write_reg(0x2b, 0xa)
427 _lcd_write_reg(7, 0x133)
428 _mdelay(50)
429 _lcd_write_reg(0x22, 0)
430 _end_seq()
434 static void lcd_enable_9325(bool enable)
436 if(!enable)
438 _begin_seq()
439 _lcd_write_reg(7, 0x131)
440 _mdelay(10)
441 _lcd_write_reg(7, 0x130)
442 _mdelay(10)
443 _lcd_write_reg(7, 0)
444 _lcd_write_reg(0x10, 0x80)
445 _lcd_write_reg(0x11, 0)
446 _lcd_write_reg(0x12, 0)
447 _lcd_write_reg(0x13, 0)
448 _mdelay(200)
449 _lcd_write_reg(0x10, 0x82)
450 _end_seq()
452 else
454 _begin_seq()
455 _lcd_write_reg(0x10, 0x80)
456 _lcd_write_reg(0x11, 0)
457 _lcd_write_reg(0x12, 0)
458 _lcd_write_reg(0x13, 0)
459 _lcd_write_reg(7, 1)
460 _mdelay(200)
461 _lcd_write_reg(0x10, 0x1290)
462 _lcd_write_reg(0x11, 7)
463 _mdelay(50)
464 _lcd_write_reg(0x12, 0x19)
465 _mdelay(50)
466 _lcd_write_reg(0x13, 0x1700)
467 _lcd_write_reg(0x29, 0x10)
468 _mdelay(50)
469 _lcd_write_reg(7, 0x133)
470 _lcd_write_reg(0x22, 0)
471 _end_seq()
475 void lcd_enable(bool enable)
477 if(lcd_on == enable)
478 return;
480 lcd_on = enable;
482 if(enable)
483 common_lcd_enable(true);
484 switch(lcd_kind)
486 case LCD_KIND_7783: lcd_enable_7783(enable); break;
487 case LCD_KIND_9325: lcd_enable_9325(enable); break;
488 default: lcd_enable_7783(enable); break;
490 if(!enable)
491 common_lcd_enable(false);
493 #endif
495 void lcd_update(void)
497 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
500 void lcd_update_rect(int x, int y, int w, int h)
502 #ifdef HAVE_LCD_ENABLE
503 if(!lcd_on)
504 return;
505 #endif
506 /* make sure the rectangle is included in the screen */
507 x = MIN(x, LCD_WIDTH);
508 y = MIN(y, LCD_HEIGHT);
509 w = MIN(w, LCD_WIDTH - x);
510 h = MIN(h, LCD_HEIGHT - y);
512 imx233_lcdif_wait_ready();
513 lcd_write_reg(0x50, x);
514 lcd_write_reg(0x51, x + w - 1);
515 lcd_write_reg(0x52, y);
516 lcd_write_reg(0x53, y + h - 1);
517 lcd_write_reg(0x20, x);
518 lcd_write_reg(0x21, y);
519 lcd_write_reg(0x22, 0);
520 imx233_lcdif_wait_ready();
521 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_16_BIT);
522 imx233_lcdif_set_byte_packing_format(0xf); /* two pixels per 32-bit word */
523 imx233_lcdif_set_data_format(false, false, false); /* RGB565, don't care, don't care */
524 /* there are two cases here:
525 * - either width = LCD_WIDTH and we can directly memcopy a part of lcd_framebuffer to FRAME
526 * and send it
527 * - either width != LCD_WIDTH and we have to build a contiguous copy of the rectangular area
528 * into FRAME before sending it (which is slower and doesn't use the hardware)
529 * In all cases, FRAME just acts as a temporary buffer.
530 * NOTE It's more interesting to do a copy to FRAME in all cases since in system mode
531 * the clock runs at 24MHz which provides barely 10MB/s bandwidth compared to >100MB/s
532 * for memcopy operations
534 if(w == LCD_WIDTH)
536 memcpy((void *)FRAME, FBADDR(x,y), w * h * sizeof(fb_data));
538 else
540 for(int i = 0; i < h; i++)
541 memcpy((fb_data *)FRAME + i * w, FBADDR(x,y + i), w * sizeof(fb_data));
543 /* WARNING The LCDIF has a limitation on the vertical count ! In 16-bit packed mode
544 * (which we used, ie 16-bit per pixel, 2 pixels per 32-bit words), the v_count
545 * field must be a multiple of 2. Furthermore, it seems the lcd controller doesn't
546 * really like when both w and h are even, probably because the writes to the GRAM
547 * are done on several words and the controller requires dummy writes.
548 * The workaround is to always make sure that we send a number of pixels which is
549 * a multiple of 4 so that both the lcdif and the controller are happy. If any
550 * of w or h is odd, we will send a copy of the first pixels as dummy writes. We will
551 * send at most 3 bytes. We then send (w * h + 3) / 4 x 4 bytes.
553 if(w % 2 == 1 || h % 2 == 1)
555 /* copy three pixel after the last one */
556 for(int i = 0; i < 3; i++)
557 *((fb_data *)FRAME + w * h + i) = *((fb_data *)FRAME + i);
558 /* WARNING we need to update w and h to reflect the pixel count BUT it
559 * has no relation to w * h (it can even be 2 * prime). Hopefully, w <= 240 and
560 * h <= 320 so w * h <= 76800 and (w * h + 3) / 4 <= 38400 which fits into
561 * a 16-bit integer (horizontal count). */
562 h = (w * h + 3) / 4;
563 w = 4;
565 imx233_lcdif_dma_send((void *)FRAME_PHYS_ADDR, w, h);
568 void lcd_yuv_set_options(unsigned options)
570 lcd_yuv_options = options;
573 #define YFAC (74)
574 #define RVFAC (101)
575 #define GUFAC (-24)
576 #define GVFAC (-51)
577 #define BUFAC (128)
579 static inline int clamp(int val, int min, int max)
581 if (val < min)
582 val = min;
583 else if (val > max)
584 val = max;
585 return val;
588 void lcd_blit_yuv(unsigned char * const src[3],
589 int src_x, int src_y, int stride,
590 int x, int y, int width, int height)
592 const unsigned char *ysrc, *usrc, *vsrc;
593 int linecounter;
594 fb_data *dst, *row_end;
595 long z;
597 /* width and height must be >= 2 and an even number */
598 width &= ~1;
599 linecounter = height >> 1;
601 #if LCD_WIDTH >= LCD_HEIGHT
602 dst = FBADDR(x,y);
603 row_end = dst + width;
604 #else
605 dst = FBADDR(LCD_WIDTH - y - 1,x);
606 row_end = dst + LCD_WIDTH * width;
607 #endif
609 z = stride * src_y;
610 ysrc = src[0] + z + src_x;
611 usrc = src[1] + (z >> 2) + (src_x >> 1);
612 vsrc = src[2] + (usrc - src[1]);
614 /* stride => amount to jump from end of last row to start of next */
615 stride -= width;
617 /* upsampling, YUV->RGB conversion and reduction to RGB565 in one go */
623 int y, cb, cr, rv, guv, bu, r, g, b;
625 y = YFAC*(*ysrc++ - 16);
626 cb = *usrc++ - 128;
627 cr = *vsrc++ - 128;
629 rv = RVFAC*cr;
630 guv = GUFAC*cb + GVFAC*cr;
631 bu = BUFAC*cb;
633 r = y + rv;
634 g = y + guv;
635 b = y + bu;
637 if ((unsigned)(r | g | b) > 64*256-1)
639 r = clamp(r, 0, 64*256-1);
640 g = clamp(g, 0, 64*256-1);
641 b = clamp(b, 0, 64*256-1);
644 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
646 #if LCD_WIDTH >= LCD_HEIGHT
647 dst++;
648 #else
649 dst += LCD_WIDTH;
650 #endif
652 y = YFAC*(*ysrc++ - 16);
653 r = y + rv;
654 g = y + guv;
655 b = y + bu;
657 if ((unsigned)(r | g | b) > 64*256-1)
659 r = clamp(r, 0, 64*256-1);
660 g = clamp(g, 0, 64*256-1);
661 b = clamp(b, 0, 64*256-1);
664 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
666 #if LCD_WIDTH >= LCD_HEIGHT
667 dst++;
668 #else
669 dst += LCD_WIDTH;
670 #endif
672 while (dst < row_end);
674 ysrc += stride;
675 usrc -= width >> 1;
676 vsrc -= width >> 1;
678 #if LCD_WIDTH >= LCD_HEIGHT
679 row_end += LCD_WIDTH;
680 dst += LCD_WIDTH - width;
681 #else
682 row_end -= 1;
683 dst -= LCD_WIDTH*width + 1;
684 #endif
688 int y, cb, cr, rv, guv, bu, r, g, b;
690 y = YFAC*(*ysrc++ - 16);
691 cb = *usrc++ - 128;
692 cr = *vsrc++ - 128;
694 rv = RVFAC*cr;
695 guv = GUFAC*cb + GVFAC*cr;
696 bu = BUFAC*cb;
698 r = y + rv;
699 g = y + guv;
700 b = y + bu;
702 if ((unsigned)(r | g | b) > 64*256-1)
704 r = clamp(r, 0, 64*256-1);
705 g = clamp(g, 0, 64*256-1);
706 b = clamp(b, 0, 64*256-1);
709 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
711 #if LCD_WIDTH >= LCD_HEIGHT
712 dst++;
713 #else
714 dst += LCD_WIDTH;
715 #endif
717 y = YFAC*(*ysrc++ - 16);
718 r = y + rv;
719 g = y + guv;
720 b = y + bu;
722 if ((unsigned)(r | g | b) > 64*256-1)
724 r = clamp(r, 0, 64*256-1);
725 g = clamp(g, 0, 64*256-1);
726 b = clamp(b, 0, 64*256-1);
729 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
731 #if LCD_WIDTH >= LCD_HEIGHT
732 dst++;
733 #else
734 dst += LCD_WIDTH;
735 #endif
737 while (dst < row_end);
739 ysrc += stride;
740 usrc += stride >> 1;
741 vsrc += stride >> 1;
743 #if LCD_WIDTH >= LCD_HEIGHT
744 row_end += LCD_WIDTH;
745 dst += LCD_WIDTH - width;
746 #else
747 row_end -= 1;
748 dst -= LCD_WIDTH*width + 1;
749 #endif
751 while (--linecounter > 0);
753 #if LCD_WIDTH >= LCD_HEIGHT
754 lcd_update_rect(x, y, width, height);
755 #else
756 lcd_update_rect(LCD_WIDTH - y - height, x, height, width);
757 #endif