fuze+: implement lcd flip and invert
[maemo-rb.git] / firmware / target / arm / imx233 / sansa-fuzeplus / lcd-fuzeplus.c
blob6ede0d3f2913edad49a28a35f0ac0176868527dd
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"
32 #include "button.h"
33 #include "font.h"
34 #include "action.h"
36 #ifdef HAVE_LCD_ENABLE
37 static bool lcd_on;
38 #endif
39 static unsigned lcd_yuv_options = 0;
40 static int lcd_dcp_channel = -1;
42 static enum lcd_kind_t
44 LCD_KIND_7783 = 0x7783,
45 LCD_KIND_9325 = 0x9325,
46 LCD_KIND_OTHER = 0,
47 } lcd_kind = LCD_KIND_OTHER;
49 static void setup_parameters(void)
51 imx233_lcdif_reset();
52 imx233_lcdif_set_lcd_databus_width(HW_LCDIF_CTRL__LCD_DATABUS_WIDTH_18_BIT);
53 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
54 imx233_lcdif_set_timings(1, 2, 2, 2);
57 static void setup_lcd_pins(bool use_lcdif)
59 /* WARNING
60 * the B1P22 and B1P24 pins are used by the tuner i2c! Do NOT drive
61 * them as lcd_dotclk and lcd_hsync or it will break the tuner! */
62 imx233_pinctrl_acquire_pin(1, 18, "lcd reset");
63 imx233_pinctrl_acquire_pin(1, 19, "lcd rs");
64 imx233_pinctrl_acquire_pin(1, 20, "lcd wr");
65 imx233_pinctrl_acquire_pin(1, 21, "lcd cs");
66 imx233_pinctrl_acquire_pin(1, 23, "lcd enable");
67 imx233_pinctrl_acquire_pin(1, 25, "lcd vsync");
68 imx233_pinctrl_acquire_pin_mask(1, 0x3ffff, "lcd data");
69 if(use_lcdif)
71 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
72 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
73 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
74 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
75 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
76 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
77 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
78 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
79 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
81 else
83 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} */
84 imx233_enable_gpio_output_mask(1, 0x2bfffff, false); /* lcd_{d{0-17},reset,rs,wr,cs,enable,vsync} */
85 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
86 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
87 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
88 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
89 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
90 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
91 imx233_set_pin_function(1, 25, PINCTRL_FUNCTION_GPIO); /* lcd_vsync */
95 static void setup_lcd_pins_i80(bool i80)
97 if(i80)
99 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_12mA); /* lcd_rs */
100 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_12mA); /* lcd_wr */
101 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_12mA); /* lcd_cs */
102 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_12mA); /* lcd_enable */
103 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
104 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_GPIO); /* lcd_wr */
105 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_GPIO); /* lcd_cs */
106 imx233_set_pin_function(1, 23, PINCTRL_FUNCTION_GPIO); /* lcd_enable */
107 /* lcd_{rs,wr,cs,enable} */
108 imx233_enable_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
109 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
111 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
112 __REG_SET(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as GPIO */
113 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_GPIO); /* lcd_d16 */
114 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_GPIO); /* lcd_d17 */
115 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_GPIO); /* lcd_reset */
116 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_GPIO); /* lcd_rs */
118 else
120 imx233_set_gpio_output_mask(1, (1 << 19) | (1 << 20) | (1 << 21) | (1 << 23), true);
121 imx233_set_pin_drive_strength(1, 19, PINCTRL_DRIVE_4mA); /* lcd_rs */
122 imx233_set_pin_drive_strength(1, 20, PINCTRL_DRIVE_4mA); /* lcd_wr */
123 imx233_set_pin_drive_strength(1, 21, PINCTRL_DRIVE_4mA); /* lcd_cs */
124 imx233_set_pin_drive_strength(1, 23, PINCTRL_DRIVE_4mA); /* lcd_enable */
125 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
126 imx233_set_pin_function(1, 20, PINCTRL_FUNCTION_MAIN); /* lcd_wr */
127 imx233_set_pin_function(1, 21, PINCTRL_FUNCTION_MAIN); /* lcd_cs */
128 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
129 __REG_CLR(HW_PINCTRL_MUXSEL(2)) = 0xffffffff; /* lcd_d{0-15} as lcd_d{0-15} */
130 imx233_set_pin_function(1, 16, PINCTRL_FUNCTION_MAIN); /* lcd_d16 */
131 imx233_set_pin_function(1, 17, PINCTRL_FUNCTION_MAIN); /* lcd_d17 */
132 imx233_set_pin_function(1, 18, PINCTRL_FUNCTION_MAIN); /* lcd_reset */
133 imx233_set_pin_function(1, 19, PINCTRL_FUNCTION_MAIN); /* lcd_rs */
137 static void common_lcd_enable(bool enable)
139 imx233_lcdif_enable(enable);
140 setup_lcd_pins(enable); /* use GPIO pins when disable */
143 static void setup_lcdif(void)
145 setup_parameters();
146 common_lcd_enable(true);
147 imx233_lcdif_enable_bus_master(true);
150 static inline uint32_t encode_16_to_18(uint32_t a)
152 return ((a & 0xff) << 1) | (((a >> 8) & 0xff) << 10);
155 static inline uint32_t decode_18_to_16(uint32_t a)
157 return ((a >> 1) & 0xff) | ((a >> 2) & 0xff00);
160 static void setup_lcdif_clock(void)
162 /* the LCD seems to work at 24Mhz, so use the xtal clock with no divider */
163 imx233_clkctrl_enable_clock(CLK_PIX, false);
164 imx233_clkctrl_set_clock_divisor(CLK_PIX, 1);
165 imx233_clkctrl_set_bypass_pll(CLK_PIX, true); /* use XTAL */
166 imx233_clkctrl_enable_clock(CLK_PIX, true);
169 static uint32_t i80_read_register(uint32_t data_out)
171 imx233_lcdif_wait_ready();
172 /* lcd_enable is mapped to the RD pin of the controller */
173 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
174 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
175 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
176 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
177 imx233_enable_gpio_output_mask(1, 0x3ffff, true); /* lcd_d{0-17} */
178 udelay(2);
179 imx233_set_gpio_output(1, 19, false); /* lcd_rs */
180 udelay(1);
181 imx233_set_gpio_output(1, 21, false); /* lcd_cs */
182 udelay(1);
183 imx233_set_gpio_output(1, 20, false); /* lcd_wr */
184 udelay(1);
185 imx233_set_gpio_output_mask(1, data_out & 0x3ffff, true); /* lcd_d{0-17} */
186 udelay(1);
187 imx233_set_gpio_output(1, 20, true); /* lcd_wr */
188 udelay(3);
189 imx233_enable_gpio_output_mask(1, 0x3ffff, false); /* lcd_d{0-17} */
190 udelay(2);
191 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
192 udelay(1);
193 imx233_set_gpio_output(1, 19, true); /* lcd_rs */
194 udelay(1);
195 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
196 udelay(3);
197 imx233_set_gpio_output(1, 23, false); /* lcd_enable */
198 udelay(2);
199 uint32_t data_in = imx233_get_gpio_input_mask(1, 0x3ffff); /* lcd_d{0-17} */
200 udelay(1);
201 imx233_set_gpio_output(1, 23, true); /* lcd_enable */
202 udelay(1);
203 imx233_set_gpio_output(1, 21, true); /* lcd_cs */
204 udelay(1);
205 return data_in;
208 static void lcd_write_reg(uint32_t reg, uint32_t data)
210 uint32_t old_reg = reg;
211 imx233_lcdif_wait_ready();
212 /* get back to 18-bit word length */
213 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_18_BIT);
214 reg = encode_16_to_18(reg);
215 data = encode_16_to_18(data);
217 imx233_lcdif_pio_send(false, 2, &reg);
218 if(old_reg != 0x22)
219 imx233_lcdif_pio_send(true, 2, &data);
222 static uint32_t lcd_read_reg(uint32_t reg)
224 setup_lcd_pins_i80(true);
225 uint32_t data_in = i80_read_register(encode_16_to_18(reg));
226 setup_lcd_pins_i80(false);
227 lcd_write_reg(0x22, 0);
228 return decode_18_to_16(data_in);
231 #define REG_MDELAY 0xffffffff
232 struct lcd_sequence_entry_t
234 uint32_t reg, data;
237 static void lcd_send_sequence(struct lcd_sequence_entry_t *seq, unsigned count)
239 for(;count-- > 0; seq++)
241 if(seq->reg == REG_MDELAY)
242 mdelay(seq->data);
243 else
244 lcd_write_reg(seq->reg, seq->data);
248 #define _begin_seq() static struct lcd_sequence_entry_t __seq[] = {
249 #define _mdelay(a) {REG_MDELAY, a},
250 #define _lcd_write_reg(a, b) {a, b},
251 #define _end_seq() }; lcd_send_sequence(__seq, sizeof(__seq) / sizeof(__seq[0]));
253 static void lcd_init_seq_7783(void)
255 _begin_seq()
256 _mdelay(200)
257 _lcd_write_reg(1, 0x100)
258 _lcd_write_reg(2, 0x700)
259 _lcd_write_reg(3, 0x1030)
260 _lcd_write_reg(7, 0x121)
261 _lcd_write_reg(8, 0x302)
262 _lcd_write_reg(9, 0x200)
263 _lcd_write_reg(0xa, 0)
264 _lcd_write_reg(0x10, 0x790)
265 _lcd_write_reg(0x11, 5)
266 _lcd_write_reg(0x12, 0)
267 _lcd_write_reg(0x13, 0)
268 _mdelay(100)
269 _lcd_write_reg(0x10, 0x12b0)
270 _mdelay(100)
271 _lcd_write_reg(0x11, 7)
272 _mdelay(100)
273 _lcd_write_reg(0x12, 0x89)
274 _lcd_write_reg(0x13, 0x1d00)
275 _lcd_write_reg(0x29, 0x2f)
276 _mdelay(50)
277 _lcd_write_reg(0x30, 0)
278 _lcd_write_reg(0x31, 0x505)
279 _lcd_write_reg(0x32, 0x205)
280 _lcd_write_reg(0x35, 0x206)
281 _lcd_write_reg(0x36, 0x408)
282 _lcd_write_reg(0x37, 0)
283 _lcd_write_reg(0x38, 0x504)
284 _lcd_write_reg(0x39, 0x206)
285 _lcd_write_reg(0x3c, 0x206)
286 _lcd_write_reg(0x3d, 0x408)
287 _lcd_write_reg(0x50, 0) /* left X ? */
288 _lcd_write_reg(0x51, 0xef) /* right X ? */
289 _lcd_write_reg(0x52, 0) /* top Y ? */
290 _lcd_write_reg(0x53, 0x13f) /* bottom Y ? */
291 _lcd_write_reg(0x20, 0) /* left X ? */
292 _lcd_write_reg(0x21, 0) /* top Y ? */
293 _lcd_write_reg(0x60, 0xa700)
294 _lcd_write_reg(0x61, 1)
295 _lcd_write_reg(0x90, 0x33)
296 _lcd_write_reg(0x2b, 0xa)
297 _lcd_write_reg(9, 0)
298 _lcd_write_reg(7, 0x133)
299 _mdelay(50)
300 _lcd_write_reg(0x22, 0)
301 _end_seq()
304 static void lcd_init_seq_9325(void)
306 _begin_seq()
307 _lcd_write_reg(0xe5, 0x78f0)
308 _lcd_write_reg(0xe3, 0x3008)
309 _lcd_write_reg(0xe7, 0x12)
310 _lcd_write_reg(0xef, 0x1231)
311 _lcd_write_reg(0, 1)
312 _lcd_write_reg(1, 0x100)
313 _lcd_write_reg(2, 0x700)
314 _lcd_write_reg(3, 0x1030)
315 _lcd_write_reg(4, 0)
316 _lcd_write_reg(8, 0x207)
317 _lcd_write_reg(9, 0)
318 _lcd_write_reg(0xa, 0)
319 _lcd_write_reg(0xc, 0)
320 _lcd_write_reg(0xd, 0)
321 _lcd_write_reg(0xf, 0)
322 _lcd_write_reg(0x10, 0)
323 _lcd_write_reg(0x11, 7)
324 _lcd_write_reg(0x12, 0)
325 _lcd_write_reg(0x13, 0)
326 _mdelay(20)
327 _lcd_write_reg(0x10, 0x1290)
328 _lcd_write_reg(0x11, 7)
329 _mdelay(50)
330 _lcd_write_reg(0x12, 0x19)
331 _mdelay(50)
332 _lcd_write_reg(0x13, 0x1700)
333 _lcd_write_reg(0x29, 0x14)
334 _mdelay(50)
335 _lcd_write_reg(0x20, 0)
336 _lcd_write_reg(0x21, 0)
337 _lcd_write_reg(0x30, 0x504)
338 _lcd_write_reg(0x31, 7)
339 _lcd_write_reg(0x32, 6)
340 _lcd_write_reg(0x35, 0x106)
341 _lcd_write_reg(0x36, 0x202)
342 _lcd_write_reg(0x37, 0x504)
343 _lcd_write_reg(0x38, 0x500)
344 _lcd_write_reg(0x39, 0x706)
345 _lcd_write_reg(0x3c, 0x204)
346 _lcd_write_reg(0x3d, 0x202)
347 _lcd_write_reg(0x50, 0)
348 _lcd_write_reg(0x51, 0xef)
349 _lcd_write_reg(0x52, 0)
350 _lcd_write_reg(0x53, 0x13f)
351 _lcd_write_reg(0x60, 0xa700)
352 _lcd_write_reg(0x61, 1)
353 _lcd_write_reg(0x6a, 0)
354 _lcd_write_reg(0x2b, 0xd)
355 _mdelay(50)
356 _lcd_write_reg(0x90, 0x11)
357 _lcd_write_reg(0x92, 0x600)
358 _lcd_write_reg(0x93, 3)
359 _lcd_write_reg(0x95, 0x110)
360 _lcd_write_reg(0x97, 0)
361 _lcd_write_reg(0x98, 0)
362 _lcd_write_reg(7, 0x173)
363 _lcd_write_reg(0x22, 0)
364 _end_seq()
367 void lcd_init_device(void)
369 lcd_dcp_channel = imx233_dcp_acquire_channel(TIMEOUT_NOBLOCK);
370 if(lcd_dcp_channel < 0)
371 panicf("imx233_framebuffer_init: imx233_dcp_acquire_channel failed!");
372 setup_lcdif();
373 setup_lcdif_clock();
375 for(int i = 0; i < 10; i++)
377 lcd_kind = lcd_read_reg(0);
378 mdelay(5);
379 if(lcd_kind == LCD_KIND_7783 || lcd_kind == LCD_KIND_9325)
380 break;
382 // reset device
383 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
384 mdelay(50);
385 __REG_CLR(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
386 mdelay(10);
387 __REG_SET(HW_LCDIF_CTRL1) = HW_LCDIF_CTRL1__RESET;
389 switch(lcd_kind)
391 case LCD_KIND_7783: lcd_init_seq_7783(); break;
392 case LCD_KIND_9325: lcd_init_seq_9325(); break;
393 default:
394 lcd_kind = LCD_KIND_7783;
395 lcd_init_seq_7783(); break;
398 #ifdef HAVE_LCD_ENABLE
399 lcd_on = true;
400 #endif
403 #ifdef HAVE_LCD_ENABLE
404 bool lcd_active(void)
406 return lcd_on;
409 static void lcd_enable_7783(bool enable)
411 if(!enable)
413 _begin_seq()
414 _lcd_write_reg(7, 0x131)
415 _mdelay(50)
416 _lcd_write_reg(7, 0x20)
417 _mdelay(50)
418 _lcd_write_reg(0x10, 0x82)
419 _mdelay(50)
420 _end_seq()
422 else
424 _begin_seq()
425 _lcd_write_reg(0x11, 5)
426 _lcd_write_reg(0x10, 0x12b0)
427 _mdelay(50)
428 _lcd_write_reg(7, 0x11)
429 _mdelay(50)
430 _lcd_write_reg(0x12, 0x89)
431 _mdelay(50)
432 _lcd_write_reg(0x13, 0x1d00)
433 _mdelay(50)
434 _lcd_write_reg(0x29, 0x2f)
435 _mdelay(50)
436 _lcd_write_reg(0x2b, 0xa)
437 _lcd_write_reg(7, 0x133)
438 _mdelay(50)
439 _lcd_write_reg(0x22, 0)
440 _end_seq()
444 static void lcd_enable_9325(bool enable)
446 if(!enable)
448 _begin_seq()
449 _lcd_write_reg(7, 0x131)
450 _mdelay(10)
451 _lcd_write_reg(7, 0x130)
452 _mdelay(10)
453 _lcd_write_reg(7, 0)
454 _lcd_write_reg(0x10, 0x80)
455 _lcd_write_reg(0x11, 0)
456 _lcd_write_reg(0x12, 0)
457 _lcd_write_reg(0x13, 0)
458 _mdelay(200)
459 _lcd_write_reg(0x10, 0x82)
460 _end_seq()
462 else
464 _begin_seq()
465 _lcd_write_reg(0x10, 0x80)
466 _lcd_write_reg(0x11, 0)
467 _lcd_write_reg(0x12, 0)
468 _lcd_write_reg(0x13, 0)
469 _lcd_write_reg(7, 1)
470 _mdelay(200)
471 _lcd_write_reg(0x10, 0x1290)
472 _lcd_write_reg(0x11, 7)
473 _mdelay(50)
474 _lcd_write_reg(0x12, 0x19)
475 _mdelay(50)
476 _lcd_write_reg(0x13, 0x1700)
477 _lcd_write_reg(0x29, 0x10)
478 _mdelay(50)
479 _lcd_write_reg(7, 0x133)
480 _lcd_write_reg(0x22, 0)
481 _end_seq()
485 void lcd_enable(bool enable)
487 if(lcd_on == enable)
488 return;
490 lcd_on = enable;
492 if(enable)
493 common_lcd_enable(true);
494 switch(lcd_kind)
496 case LCD_KIND_7783: lcd_enable_7783(enable); break;
497 case LCD_KIND_9325: lcd_enable_9325(enable); break;
498 default: lcd_enable_7783(enable); break;
500 if(!enable)
501 common_lcd_enable(false);
503 #endif
505 #ifdef HAVE_LCD_INVERT
506 void lcd_set_invert_display(bool yesno)
508 /* same for both kinds */
509 lcd_write_reg(0x61, yesno ? 0 : 1);
511 #endif
513 #ifdef HAVE_LCD_FLIP
514 void lcd_set_flip(bool yesno)
516 /* same for both kinds */
517 lcd_write_reg(3, yesno ? 0x1000 : 0x1030);
519 #endif
521 void lcd_update(void)
523 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
526 void lcd_update_rect(int x, int y, int w, int h)
528 #ifdef HAVE_LCD_ENABLE
529 if(!lcd_on)
530 return;
531 #endif
532 /* make sure the rectangle is included in the screen */
533 x = MIN(x, LCD_WIDTH);
534 y = MIN(y, LCD_HEIGHT);
535 w = MIN(w, LCD_WIDTH - x);
536 h = MIN(h, LCD_HEIGHT - y);
538 imx233_lcdif_wait_ready();
539 lcd_write_reg(0x50, x);
540 lcd_write_reg(0x51, x + w - 1);
541 lcd_write_reg(0x52, y);
542 lcd_write_reg(0x53, y + h - 1);
543 lcd_write_reg(0x20, x);
544 lcd_write_reg(0x21, y);
545 lcd_write_reg(0x22, 0);
546 imx233_lcdif_wait_ready();
547 imx233_lcdif_set_word_length(HW_LCDIF_CTRL__WORD_LENGTH_16_BIT);
548 imx233_lcdif_set_byte_packing_format(0xf); /* two pixels per 32-bit word */
549 imx233_lcdif_set_data_format(false, false, false); /* RGB565, don't care, don't care */
550 /* there are two cases here:
551 * - either width = LCD_WIDTH and we can directly memcopy a part of lcd_framebuffer to FRAME
552 * and send it
553 * - either width != LCD_WIDTH and we have to build a contiguous copy of the rectangular area
554 * into FRAME before sending it (which is slower and doesn't use the hardware)
555 * In all cases, FRAME just acts as a temporary buffer.
556 * NOTE It's more interesting to do a copy to FRAME in all cases since in system mode
557 * the clock runs at 24MHz which provides barely 10MB/s bandwidth compared to >100MB/s
558 * for memcopy operations
560 if(w == LCD_WIDTH)
562 memcpy((void *)FRAME, FBADDR(x,y), w * h * sizeof(fb_data));
564 else
566 for(int i = 0; i < h; i++)
567 memcpy((fb_data *)FRAME + i * w, FBADDR(x,y + i), w * sizeof(fb_data));
569 /* WARNING The LCDIF has a limitation on the vertical count ! In 16-bit packed mode
570 * (which we used, ie 16-bit per pixel, 2 pixels per 32-bit words), the v_count
571 * field must be a multiple of 2. Furthermore, it seems the lcd controller doesn't
572 * really like when both w and h are even, probably because the writes to the GRAM
573 * are done on several words and the controller requires dummy writes.
574 * The workaround is to always make sure that we send a number of pixels which is
575 * a multiple of 4 so that both the lcdif and the controller are happy. If any
576 * of w or h is odd, we will send a copy of the first pixels as dummy writes. We will
577 * send at most 3 bytes. We then send (w * h + 3) / 4 x 4 bytes.
579 if(w % 2 == 1 || h % 2 == 1)
581 /* copy three pixel after the last one */
582 for(int i = 0; i < 3; i++)
583 *((fb_data *)FRAME + w * h + i) = *((fb_data *)FRAME + i);
584 /* WARNING we need to update w and h to reflect the pixel count BUT it
585 * has no relation to w * h (it can even be 2 * prime). Hopefully, w <= 240 and
586 * h <= 320 so w * h <= 76800 and (w * h + 3) / 4 <= 38400 which fits into
587 * a 16-bit integer (horizontal count). */
588 h = (w * h + 3) / 4;
589 w = 4;
591 imx233_lcdif_dma_send((void *)FRAME_PHYS_ADDR, w, h);
594 void lcd_yuv_set_options(unsigned options)
596 lcd_yuv_options = options;
599 #define YFAC (74)
600 #define RVFAC (101)
601 #define GUFAC (-24)
602 #define GVFAC (-51)
603 #define BUFAC (128)
605 static inline int clamp(int val, int min, int max)
607 if (val < min)
608 val = min;
609 else if (val > max)
610 val = max;
611 return val;
614 void lcd_blit_yuv(unsigned char * const src[3],
615 int src_x, int src_y, int stride,
616 int x, int y, int width, int height)
618 const unsigned char *ysrc, *usrc, *vsrc;
619 int linecounter;
620 fb_data *dst, *row_end;
621 long z;
623 /* width and height must be >= 2 and an even number */
624 width &= ~1;
625 linecounter = height >> 1;
627 #if LCD_WIDTH >= LCD_HEIGHT
628 dst = FBADDR(x,y);
629 row_end = dst + width;
630 #else
631 dst = FBADDR(LCD_WIDTH - y - 1,x);
632 row_end = dst + LCD_WIDTH * width;
633 #endif
635 z = stride * src_y;
636 ysrc = src[0] + z + src_x;
637 usrc = src[1] + (z >> 2) + (src_x >> 1);
638 vsrc = src[2] + (usrc - src[1]);
640 /* stride => amount to jump from end of last row to start of next */
641 stride -= width;
643 /* upsampling, YUV->RGB conversion and reduction to RGB565 in one go */
649 int y, cb, cr, rv, guv, bu, r, g, b;
651 y = YFAC*(*ysrc++ - 16);
652 cb = *usrc++ - 128;
653 cr = *vsrc++ - 128;
655 rv = RVFAC*cr;
656 guv = GUFAC*cb + GVFAC*cr;
657 bu = BUFAC*cb;
659 r = y + rv;
660 g = y + guv;
661 b = y + bu;
663 if ((unsigned)(r | g | b) > 64*256-1)
665 r = clamp(r, 0, 64*256-1);
666 g = clamp(g, 0, 64*256-1);
667 b = clamp(b, 0, 64*256-1);
670 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
672 #if LCD_WIDTH >= LCD_HEIGHT
673 dst++;
674 #else
675 dst += LCD_WIDTH;
676 #endif
678 y = YFAC*(*ysrc++ - 16);
679 r = y + rv;
680 g = y + guv;
681 b = y + bu;
683 if ((unsigned)(r | g | b) > 64*256-1)
685 r = clamp(r, 0, 64*256-1);
686 g = clamp(g, 0, 64*256-1);
687 b = clamp(b, 0, 64*256-1);
690 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
692 #if LCD_WIDTH >= LCD_HEIGHT
693 dst++;
694 #else
695 dst += LCD_WIDTH;
696 #endif
698 while (dst < row_end);
700 ysrc += stride;
701 usrc -= width >> 1;
702 vsrc -= width >> 1;
704 #if LCD_WIDTH >= LCD_HEIGHT
705 row_end += LCD_WIDTH;
706 dst += LCD_WIDTH - width;
707 #else
708 row_end -= 1;
709 dst -= LCD_WIDTH*width + 1;
710 #endif
714 int y, cb, cr, rv, guv, bu, r, g, b;
716 y = YFAC*(*ysrc++ - 16);
717 cb = *usrc++ - 128;
718 cr = *vsrc++ - 128;
720 rv = RVFAC*cr;
721 guv = GUFAC*cb + GVFAC*cr;
722 bu = BUFAC*cb;
724 r = y + rv;
725 g = y + guv;
726 b = y + bu;
728 if ((unsigned)(r | g | b) > 64*256-1)
730 r = clamp(r, 0, 64*256-1);
731 g = clamp(g, 0, 64*256-1);
732 b = clamp(b, 0, 64*256-1);
735 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
737 #if LCD_WIDTH >= LCD_HEIGHT
738 dst++;
739 #else
740 dst += LCD_WIDTH;
741 #endif
743 y = YFAC*(*ysrc++ - 16);
744 r = y + rv;
745 g = y + guv;
746 b = y + bu;
748 if ((unsigned)(r | g | b) > 64*256-1)
750 r = clamp(r, 0, 64*256-1);
751 g = clamp(g, 0, 64*256-1);
752 b = clamp(b, 0, 64*256-1);
755 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
757 #if LCD_WIDTH >= LCD_HEIGHT
758 dst++;
759 #else
760 dst += LCD_WIDTH;
761 #endif
763 while (dst < row_end);
765 ysrc += stride;
766 usrc += stride >> 1;
767 vsrc += stride >> 1;
769 #if LCD_WIDTH >= LCD_HEIGHT
770 row_end += LCD_WIDTH;
771 dst += LCD_WIDTH - width;
772 #else
773 row_end -= 1;
774 dst -= LCD_WIDTH*width + 1;
775 #endif
777 while (--linecounter > 0);
779 #if LCD_WIDTH >= LCD_HEIGHT
780 lcd_update_rect(x, y, width, height);
781 #else
782 lcd_update_rect(LCD_WIDTH - y - height, x, height, width);
783 #endif
786 bool lcd_debug_screen(void)
788 lcd_setfont(FONT_SYSFIXED);
790 while(1)
792 int button = get_action(CONTEXT_STD, HZ / 10);
793 switch(button)
795 case ACTION_STD_NEXT:
796 case ACTION_STD_PREV:
797 case ACTION_STD_OK:
798 case ACTION_STD_MENU:
799 lcd_setfont(FONT_UI);
800 return true;
801 case ACTION_STD_CANCEL:
802 lcd_setfont(FONT_UI);
803 return false;
806 lcd_clear_display();
807 lcd_putsf(0, 0, "lcd kind: %s",
808 lcd_kind == LCD_KIND_7783 ? "st7783" :
809 lcd_kind == LCD_KIND_9325 ? "ili9325" : "unknown");
810 lcd_update();
811 yield();
814 return true;